{"id":228,"date":"2025-12-05T20:14:59","date_gmt":"2025-12-05T12:14:59","guid":{"rendered":"https:\/\/caoxunyi.cn\/?p=228"},"modified":"2025-12-05T20:14:59","modified_gmt":"2025-12-05T12:14:59","slug":"%e6%8b%92%e7%bb%9d%e8%87%83%e8%82%bf%ef%bc%81%e5%8e%9f%e7%94%9f-js-%e6%89%8b%e6%90%93%e4%b8%80%e4%b8%aa%e8%bd%bb%e9%87%8f%e7%ba%a7-markdown-%e6%b8%b2%e6%9f%93%e5%99%a8","status":"publish","type":"post","link":"https:\/\/caoxunyi.cn\/index.php\/228\/","title":{"rendered":"\u62d2\u7edd\u81c3\u80bf\uff01\u539f\u751f JS \u624b\u6413\u4e00\u4e2a\u8f7b\u91cf\u7ea7 Markdown \u6e32\u67d3\u5668"},"content":{"rendered":"<p>\u5728\u73b0\u4ee3\u524d\u7aef\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u4e60\u60ef\u4e86\u7528 React\u3001Vue \u914d\u5408\u5404\u79cd\u590d\u6742\u7684\u63d2\u4ef6\u6765\u6e32\u67d3\u5185\u5bb9\u3002\u4f46\u6709\u65f6\uff0c\u6211\u4eec\u53ea\u662f\u60f3\u5728\u4e00\u4e2a\u7b80\u5355\u7684\u9759\u6001\u7f51\u9875\u4e0a\u5c55\u793a\u4e00\u7bc7 README \u6216\u6280\u672f\u6587\u6863\uff0c\u5f15\u5165\u4e00\u5957\u91cd\u578b\u6846\u67b6\u663e\u5f97\u6709\u4e9b\u201c\u6740\u9e21\u7528\u725b\u5200\u201d\u3002<\/p>\n<p>\u4eca\u5929\uff0c\u6211\u4eec\u5c31\u56de\u5f52\u672c\u6e90\uff0c\u5229\u7528\u6d4f\u89c8\u5668\u539f\u751f\u7684 <strong>Fetch API<\/strong> \u914d\u5408\u4e09\u4e2a\u7ecf\u5178\u7684\u5de5\u5177\u5e93\uff0c<strong>\u624b\u6413<\/strong>\u4e00\u4e2a\u5b89\u5168\u3001\u6f02\u4eae\u4e14\u652f\u6301\u4ee3\u7801\u9ad8\u4eae\u7684 Markdown \u6e32\u67d3\u5668\u3002<\/p>\n<h2>\u6838\u5fc3\u67b6\u6784\uff1a\u6e32\u67d3\u6d41\u6c34\u7ebf<\/h2>\n<p>\u8981\u5728\u7f51\u9875\u4e0a\u628a Markdown \u53d8\u6210 HTML\uff0c\u5176\u5b9e\u5c31\u662f\u5efa\u7acb\u4e00\u6761\u6570\u636e\u5904\u7406\u7684\u201c\u6d41\u6c34\u7ebf\u201d\u3002\u6574\u4e2a\u8fc7\u7a0b\u53ef\u4ee5\u5206\u4e3a\u56db\u4e2a\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li><strong>\u53d6\u8d27 (Fetch)<\/strong>\uff1a\u901a\u8fc7\u7f51\u7edc\u8bf7\u6c42\u83b7\u53d6 <code>.md<\/code> \u6587\u4ef6\u7684\u7eaf\u6587\u672c\u5185\u5bb9\u3002<\/li>\n<li><strong>\u7ffb\u8bd1 (Parse)<\/strong>\uff1a\u5c06 Markdown \u8bed\u6cd5\u8f6c\u6362\u6210 HTML \u5b57\u7b26\u4e32\uff08\u4f7f\u7528 <code>marked<\/code>\uff09\u3002<\/li>\n<li><strong>\u5b89\u68c0 (Sanitize)<\/strong>\uff1a\u6e05\u6d17 HTML\uff0c\u79fb\u9664\u6076\u610f\u811a\u672c\u9632\u6b62 XSS \u653b\u51fb\uff08\u4f7f\u7528 <code>DOMPurify<\/code>\uff09\u3002<\/li>\n<li><strong>\u88c5\u4fee (Highlight & Style)<\/strong>\uff1a\u7ed9\u4ee3\u7801\u5757\u4e0a\u8272\uff0c\u5e76\u5e94\u7528\u6392\u7248\u6837\u5f0f\uff08\u4f7f\u7528 <code>highlight.js<\/code> \u548c GitHub CSS\uff09\u3002<\/li>\n<\/ol>\n<h2>1. \u51c6\u5907\u5de5\u5177 (The Stack)<\/h2>\n<p>\u6211\u4eec\u8981\u7528\u5230\u4e09\u4e2a\u201c\u524d\u7aef\u8001\u5b57\u53f7\u201d\u5e93\uff0c\u76f4\u63a5\u901a\u8fc7 CDN \u5f15\u5165\uff0c\u65e0\u9700 npm \u5b89\u88c5\uff0c\u65e0\u9700\u6253\u5305\u3002<\/p>\n<ul>\n<li><strong>Marked<\/strong>: \u8d1f\u8d23\u6838\u5fc3\u7684 Markdown \u89e3\u6790\uff0c\u901f\u5ea6\u5feb\uff0c\u4f53\u79ef\u5c0f\u3002<\/li>\n<li><strong>DOMPurify<\/strong>: \u8d1f\u8d23\u5b89\u5168\u3002<strong>\uff08\u6781\u5176\u91cd\u8981\uff09<\/strong> \u56e0\u4e3a Markdown \u5141\u8bb8\u5d4c\u5165 HTML\uff0c\u5982\u679c\u4e0d\u6e05\u6d17\uff0c\u9ed1\u5ba2\u53ef\u4ee5\u76f4\u63a5\u5199\u5165 `\n<script>` &#25915;&#20987;&#20320;&#30340;&#29992;&#25143;&#12290;<\/script><\/li>\n<li><strong>Highlight.js<\/strong>: \u8d1f\u8d23\u81ea\u52a8\u68c0\u6d4b\u4ee3\u7801\u8bed\u8a00\u5e76\u9ad8\u4eae\u3002<\/li>\n<\/ul>\n<h2>2. \u6838\u5fc3\u4ee3\u7801\u5b9e\u73b0<\/h2>\n<h3>\u7b2c\u4e00\u6b65\uff1a\u5f15\u5165\u8d44\u6e90<\/h3>\n<p>\u9664\u4e86 JS \u5e93\uff0c\u4e3a\u4e86\u8ba9\u6392\u7248\u597d\u770b\uff0c\u6211\u4eec\u76f4\u63a5\u5f15\u5165 <strong>GitHub Markdown CSS<\/strong>\uff0c\u8ba9\u4f60\u7684\u6587\u7ae0\u770b\u8d77\u6765\u50cf\u5728 GitHub \u4e0a\u4e00\u6837\u4e13\u4e1a\u3002<\/p>\n<p>HTML<\/p>\n<pre><code>&lt;link rel=&quot;stylesheet&quot; href=&quot;https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/github-markdown-css\/5.5.0\/github-markdown-light.min.css&quot;&gt;\n&lt;link rel=&quot;stylesheet&quot; href=&quot;https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/highlight.js\/11.9.0\/styles\/github.min.css&quot;&gt;\n\n&lt;script src=&quot;https:\/\/cdn.jsdelivr.net\/npm\/marked\/marked.min.js&quot;&gt;&lt;\/script&gt;\n&lt;script src=&quot;https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/dompurify\/3.0.8\/purify.min.js&quot;&gt;&lt;\/script&gt;\n&lt;script src=&quot;https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/highlight.js\/11.9.0\/highlight.min.js&quot;&gt;&lt;\/script&gt;<\/code><\/pre>\n<h3>\u7b2c\u4e8c\u6b65\uff1a\u7f16\u5199\u6e32\u67d3\u903b\u8f91<\/h3>\n<p>\u8fd9\u662f\u6574\u4e2a\u5f15\u64ce\u7684\u5fc3\u810f\u3002\u6211\u4eec\u901a\u8fc7 <code>fetch<\/code> \u8bfb\u53d6\u672c\u5730\u6216\u8fdc\u7a0b\u7684\u6587\u4ef6\uff0c\u7136\u540e\u4f9d\u6b21\u8c03\u7528\u5e93\u51fd\u6570\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<p>JavaScript<\/p>\n<pre><code>const contentDiv = document.getElementById('content');\nconst mdUrl = '.\/post.md'; \/\/ \u4f60\u7684\u6587\u4ef6\u8def\u5f84\n\nfetch(mdUrl)\n    .then(response =&gt; {\n        if (!response.ok) throw new Error(&quot;\u6587\u4ef6\u52a0\u8f7d\u5931\u8d25 QAQ&quot;);\n        return response.text(); \/\/ \u62ff\u5230\u7eaf\u6587\u672c\n    })\n    .then(text =&gt; {\n        \/\/ 1. \u7ffb\u8bd1\uff1aMarkdown -&gt; HTML\n        const dirtyHtml = marked.parse(text);\n\n        \/\/ 2. \u5b89\u68c0\uff1a\u6e05\u6d17\u810f\u4ee3\u7801 (\u9632 XSS)\n        const cleanHtml = DOMPurify.sanitize(dirtyHtml);\n\n        \/\/ 3. \u4e0a\u67b6\uff1a\u63d2\u5165\u9875\u9762\n        contentDiv.innerHTML = cleanHtml;\n\n        \/\/ 4. \u88c5\u4fee\uff1a\u4ee3\u7801\u9ad8\u4eae\n        contentDiv.querySelectorAll('pre code').forEach((block) =&gt; {\n            hljs.highlightElement(block);\n        });\n    })\n    .catch(err =&gt; {\n        console.error(err);\n        contentDiv.innerHTML = `&lt;p style=&quot;color:red&quot;&gt;\u52a0\u8f7d\u51fa\u9519\u4e86: ${err.message}&lt;\/p&gt;`;\n    });<\/code><\/pre>\n<hr \/>\n<h2>3. \u5e38\u89c1\u5751\u70b9\uff1aCORS \u8de8\u57df<\/h2>\n<p>\u5f88\u591a\u65b0\u624b\u5728\u5199\u8fd9\u6bb5\u4ee3\u7801\u65f6\uff0c\u4f1a\u76f4\u63a5\u53cc\u51fb <code>index.html<\/code> \u6253\u5f00\uff0c\u7136\u540e\u53d1\u73b0\u63a7\u5236\u53f0\u62a5\u9519\uff1a<\/p>\n<blockquote>\n<p><em>Access to fetch at 'file:\/\/...' from origin 'null' has been blocked by CORS policy.<\/em><\/p>\n<\/blockquote>\n<p>\u539f\u56e0\uff1a\u51fa\u4e8e\u5b89\u5168\u7b56\u7565\uff0c\u6d4f\u89c8\u5668\u7981\u6b62\u7f51\u9875\u76f4\u63a5\u901a\u8fc7 file:\/\/ \u534f\u8bae\u8bfb\u53d6\u786c\u76d8\u6587\u4ef6\u3002<\/p>\n<p>\u89e3\u51b3\uff1a\u4f60\u5fc5\u987b\u542f\u52a8\u4e00\u4e2a\u672c\u5730 HTTP \u670d\u52a1\u5668\u3002<\/p>\n<ul>\n<li><strong>VS Code \u7528\u6237<\/strong>\uff1a\u5b89\u88c5 \"Live Server\" \u63d2\u4ef6\uff0c\u53f3\u952e\u70b9\u51fb HTML \u6587\u4ef6\u9009\u62e9 \"Open with Live Server\"\u3002<\/li>\n<li><strong>Python \u7528\u6237<\/strong>\uff1a\u7ec8\u7aef\u8fd0\u884c <code>python -m http.server<\/code>\u3002<\/li>\n<\/ul>\n<hr \/>\n<h2>\u5b8c\u6574\u6f14\u793a\u4ee3\u7801<\/h2>\n<p>\u4f60\u53ef\u4ee5\u65b0\u5efa\u4e00\u4e2a <code>index.html<\/code>\uff0c\u540c\u7ea7\u76ee\u5f55\u4e0b\u653e\u4e00\u4e2a <code>post.md<\/code>\uff0c\u7136\u540e\u628a\u4e0b\u9762\u7684\u4ee3\u7801\u590d\u5236\u8fdb\u53bb\u4f53\u9a8c\u3002<\/p>\n<p>HTML<\/p>\n<pre><code>&lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;zh-CN&quot;&gt;\n&lt;head&gt;\n    &lt;meta charset=&quot;UTF-8&quot;&gt;\n    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;\n\n&lt;title&gt;\u539f\u751f Markdown \u6e32\u67d3\u5668 Demo&lt;\/title&gt;\n\n    &lt;link rel=&quot;stylesheet&quot; href=&quot;https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/github-markdown-css\/5.5.0\/github-markdown-light.min.css&quot;&gt;\n    &lt;link rel=&quot;stylesheet&quot; href=&quot;https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/highlight.js\/11.9.0\/styles\/github.min.css&quot;&gt;\n\n&lt;style&gt;\n        body {\n            background-color: #f6f8fa;\n            padding: 20px;\n            font-family: -apple-system, sans-serif;\n        }\n\n        \/* \u6a21\u62df\u4e00\u5f20 A4 \u7eb8\u7684\u9605\u8bfb\u4f53\u9a8c *\/\n        .page-container {\n            max-width: 850px;\n            margin: 0 auto;\n            background: white;\n            border: 1px solid #d0d7de;\n            border-radius: 6px;\n            padding: 40px;\n            box-shadow: 0 2px 10px rgba(0,0,0,0.05);\n        }\n\n        \/* \u5fc5\u987b\u52a0\u4e0a\u8fd9\u4e2a\u7c7b\u540d\uff0cgithub-markdown-css \u624d\u4f1a\u751f\u6548 *\/\n        .markdown-body {\n            box-sizing: border-box;\n            min-width: 200px;\n            max-width: 100%;\n        }\n\n        .loading { text-align: center; color: #666; margin-top: 50px; }\n    &lt;\/style&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n\n    &lt;div class=&quot;page-container&quot;&gt;\n        &lt;div id=&quot;content&quot; class=&quot;markdown-body&quot;&gt;\n            &lt;div class=&quot;loading&quot;&gt;\u6b63\u5728\u52a0\u8f7d\u6587\u7ae0... (&ang;\u30fb&omega;&lt; )\u2312\u2605&lt;\/div&gt;\n        &lt;\/div&gt;\n    &lt;\/div&gt;\n\n    &lt;script src=&quot;https:\/\/cdn.jsdelivr.net\/npm\/marked\/marked.min.js&quot;&gt;&lt;\/script&gt;\n    &lt;script src=&quot;https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/dompurify\/3.0.8\/purify.min.js&quot;&gt;&lt;\/script&gt;\n    &lt;script src=&quot;https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/highlight.js\/11.9.0\/highlight.min.js&quot;&gt;&lt;\/script&gt;\n\n&lt;script&gt;\n        \/\/ \u914d\u7f6e\uff1a\u4f60\u7684 Markdown \u6587\u4ef6\u8def\u5f84\n        const FILE_PATH = '.\/post.md'; \n\n        const contentDiv = document.getElementById('content');\n\n        \/\/ \u5f00\u59cb\u52a0\u8f7d\n        fetch(FILE_PATH)\n            .then(response =&gt; {\n                if (!response.ok) throw new Error(`\u72b6\u6001\u7801 ${response.status}`);\n                return response.text();\n            })\n            .then(text =&gt; {\n                \/\/ 1. Parse: \u8f6c\u6362\n                const rawHtml = marked.parse(text);\n\n                \/\/ 2. Sanitize: \u6e05\u6d17 (\u5b89\u5168\u7b2c\u4e00!)\n                const safeHtml = DOMPurify.sanitize(rawHtml);\n\n                \/\/ 3. Render: \u6ce8\u5165\n                contentDiv.innerHTML = safeHtml;\n\n                \/\/ 4. Highlight: \u9ad8\u4eae\u4ee3\u7801\u5757\n                contentDiv.querySelectorAll('pre code').forEach((block) =&gt; {\n                    hljs.highlightElement(block);\n                });\n            })\n            .catch(error =&gt; {\n                console.error(error);\n                contentDiv.innerHTML = `\n                    &lt;div style=&quot;text-align:center; color:#cf222e;&quot;&gt;\n\n&lt;h3&gt;\u52a0\u8f7d\u5931\u8d25&lt;\/h3&gt;\n\n&lt;p&gt;${error.message}&lt;\/p&gt;\n\n&lt;small&gt;\u63d0\u793a\uff1a\u8bf7\u786e\u4fdd\u4f7f\u7528\u4e86 Live Server \u6216\u672c\u5730\u670d\u52a1\u5668\u8fd0\u884c&lt;\/small&gt;\n                    &lt;\/div&gt;\n                `;\n            });\n    &lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u770b\uff0c\u6211\u4eec\u6ca1\u6709\u4f7f\u7528\u4efb\u4f55\u590d\u6742\u7684\u6784\u5efa\u5de5\u5177\uff08Webpack\/Vite\uff09\uff0c\u4ec5\u7528\u4e86\u4e0d\u5230 50 \u884c\u4ee3\u7801\uff0c\u5c31\u5b9e\u73b0\u4e86\u4e00\u4e2a\u529f\u80fd\u5b8c\u5907\u7684 Markdown \u9605\u8bfb\u5668\u3002<\/p>\n<p>\u8fd9\u79cd\u201c\u624b\u6413\u201d\u7684\u65b9\u5f0f\u975e\u5e38\u9002\u5408\u4e2a\u4eba\u535a\u5ba2\u3001\u7b80\u5355\u7684\u6587\u6863\u7ad9\u70b9\uff0c\u6216\u8005\u901a\u8fc7\u8fd9\u4e00\u8fc7\u7a0b\u53bb\u6df1\u5165\u7406\u89e3\u6d4f\u89c8\u5668\u5982\u4f55\u5904\u7406\u6587\u672c\u548c DOM\u3002<\/p>\n<p>Happy Coding! (\u2220\u30fb\u03c9< )\u2312\u2605<\/p>","protected":false},"excerpt":{"rendered":"<p>\u5728\u73b0\u4ee3\u524d\u7aef\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u4e60\u60ef\u4e86\u7528 React\u3001Vue \u914d\u5408\u5404\u79cd\u590d\u6742\u7684\u63d2\u4ef6\u6765\u6e32\u67d3\u5185\u5bb9\u3002\u4f46\u6709\u65f6\uff0c\u6211\u4eec\u53ea\u662f\u60f3\u5728\u4e00\u4e2a\u7b80\u5355\u7684\u9759\u6001\u7f51\u9875\u4e0a\u5c55\u793a\u4e00\u7bc7 &#8230;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-228","post","type-post","status-publish","format-standard","hentry","category-learn"],"_links":{"self":[{"href":"https:\/\/caoxunyi.cn\/index.php\/wp-json\/wp\/v2\/posts\/228","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/caoxunyi.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/caoxunyi.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/caoxunyi.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/caoxunyi.cn\/index.php\/wp-json\/wp\/v2\/comments?post=228"}],"version-history":[{"count":1,"href":"https:\/\/caoxunyi.cn\/index.php\/wp-json\/wp\/v2\/posts\/228\/revisions"}],"predecessor-version":[{"id":229,"href":"https:\/\/caoxunyi.cn\/index.php\/wp-json\/wp\/v2\/posts\/228\/revisions\/229"}],"wp:attachment":[{"href":"https:\/\/caoxunyi.cn\/index.php\/wp-json\/wp\/v2\/media?parent=228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/caoxunyi.cn\/index.php\/wp-json\/wp\/v2\/categories?post=228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/caoxunyi.cn\/index.php\/wp-json\/wp\/v2\/tags?post=228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}