将markdown文件从http目录加载到javascript字符串中

时间:2019-05-04 16:13:52

标签: javascript html latex markdown

我需要一些JavaScript代码才能从我的http目录中读取“降价文件”并将其放入javascript字符串中。我将如何修改此代码来做到这一点?

android:launchMode="singleTop"

1 个答案:

答案 0 :(得分:1)

在不使用http服务器的情况下在本地加载文本文件的唯一方法是使用HTML5 API通过文件对话框加载文件,在文件对话框中,用户选择要读取的markdown文件:

<!DOCTYPE html>
<html>
  <head>
    <title>Render "Markdown file with Tex-Dollar" in browser</title>

    <!-- node.js packages required: -->
    <!--     npm install jquery -->
    <!--     npm install showdown -->
    <!--     npm install mathjax -->

    <script type="text/javascript" src="./node_modules/showdown/dist/jquery.js"></script>

    <script type="text/javascript" src="./node_modules/showdown/dist/showdown.js"></script>

    <script type="text/x-mathjax-config">
        MathJax.Hub.Config({
            tex2jax: {inlineMath: [["$","$"],["\\(","\\)"]]}
    });
    </script>

    <script type="text/javascript" 
        src="./node_modules/mathjax/MathJax.js?config=TeX-AMS_HTML-full"></script>


    <script type="text/javascript">
    var reader; 

    function checkFileAPI() {
        if (window.File && window.FileReader && window.FileList && window.Blob) {
            reader = new FileReader();
            return true; 
        } else {
            alert('The File APIs are not fully supported by your browser. Fallback required.');
            return false;
        }
    }

    function readText(filePath) {
        var output = ""; //placeholder for text output
        if(filePath.files && filePath.files[0]) {           
            reader.onload = function (e) {
                output = e.target.result;
                displayContents(output);
            };//end onload()
            reader.readAsText(filePath.files[0]);
        }//end if html5 filelist support
        else if(ActiveXObject && filePath) { //fallback to IE 6-8 support via ActiveX
            try {
                reader = new ActiveXObject("Scripting.FileSystemObject");
                var file = reader.OpenTextFile(filePath, 1); //ActiveX File Object
                output = file.ReadAll(); //text contents of file
                file.Close(); //close file "input stream"
                displayContents(output);
            } catch (e) {
                if (e.number == -2146827859) {
                    alert('Unable to access local files due to browser security settings. ' + 
                     'To overcome this, go to Tools->Internet Options->Security->Custom Level. ' + 
                     'Find the setting for "Initialize and script ActiveX controls not marked as safe" and change it to "Enable" or "Prompt"'); 
                }
            }       
        }
        else { //this is where you could fallback to Java Applet, Flash or similar
            return false;
        }       
        return true;
    }   

    function displayContents(txt) {
        converter = new showdown.Converter();
        html = converter.makeHtml(txt);
        var el = document.getElementById('main'); 
        el.innerHTML = html; //display output in DOM

        MathJax.Hub.Queue(["Typeset",MathJax.Hub, "main"]);

    }   
</script>
</head>

<body onload="checkFileAPI();">
    <div id="container">    
        <input type="file" onchange='readText(this)' />
        <br/>
        <hr/>   

        <h3>Contents of the Text file:</h3>

        <div id="main">
            ...
        </div>
    </div>
</body>

</html>

从markdown加载时,mathjax渲染有点片状...如果有人知道如何修复它。让我知道。谢谢。