将 markdown (md) 嵌入到 HTML 中

Embed markdown (md) into HTML

提问人:Jacob Birkett 提问时间:10/21/2015 最后编辑:pnutsJacob Birkett 更新时间:7/14/2023 访问量:25264

问:

我需要帮助在 HTML 索引文件中嵌入 Markdown 或 *.md 文件。我发现我可以在 markdown 中嵌入 HTML,但反之亦然。这将有助于提高我的编辑速度,因为 Markdown 格式非常易于使用(因为我现在正在使用它),而且我不必更改网站其余部分的格式。我知道这样做是为了嵌入另一个带有 HTML 文件的 .我还可以使用 javascript 在页面加载时解释 md 格式。提前致谢。<iframe src="path/to/html>html-name</iframe>

JavaScript的 [HTML全文] 嵌入 降价

评论

3赞 OneCricketeer 10/21/2015
你试过搜索吗?
4赞 Jacob Birkett 10/21/2015
答案是肯定的!如果我已经找到了问题的答案,为什么还要发布问题?
0赞 OneCricketeer 10/21/2015
只是好奇,因为该链接似乎可以满足您的要求,除非您的意思是实际将 Markdown 文件注入 HTML
0赞 Jacob Birkett 10/21/2015
实际上这就是我的意思,但该链接也有效!我可以嵌入另一个带有 markdown 的 HTML 文档!谢谢
0赞 Jacob Birkett 10/21/2015
等。。。不。刚刚尝试了我现有站点中的代码...不工作。只是一个白屏,我认为主题是冲突的。:/

答:

17赞 Jacob Birkett 3/26/2017 #1

这是我早已忘记的解决方案:

Tagdown Logo

忘记了我问过这个问题并且没有得到答案,我创建了自己的解决方案,作为 Chris Jeffrey 的 .marked.js

我称之为.tagdown.js

这里是:http://spikespaz.com/tagdownjs/

以防万一该链接或我的域过期:https://spikespaz.github.io/tagdownjs/

Github:https://github.com/spikespaz/tagdownjs

这允许将 markdown 直接添加到站点中,在带有类 的标记集中。请参阅网站上的示例。里面没有主题系统,只有降价解析器。markdown

更新

项目 TagdownJS 已从 Github 中删除。它的代码看起来非常简单,以至于它不值得拥有自己的存储库。

在它找到新家之前,只需找到 Christopher Jeffery 的Marked.js,然后使用以下代码即可。

document.body.style.display = "none"; // Hide the page until it's finished rendering.

document.createElement("markdown");
var md_tags = document.getElementsByTagName("markdown"); // Returns array of all markdown tags.

for (var i = 0; i < md_tags.length; i++) { // Iterate through all the tags, and generate the HTML.
    var md_text = md_tags[i].textContent.replace(/^[^\S\n]+/mg, ""); // I love regex, so shoot me.

    var md_div = document.createElement("div"); // Make a new div to replace the fake tag.
    md_div.id = "content";
    md_div.innerHTML = marked(md_text);

    md_tags[i].parentNode.appendChild(md_div); // Add remove the old raw markdown.
    md_tags[i].parentNode.removeChild(md_tags[i]);
}

document.body.style.display = ""; // Show the rendered page.
3赞 LinconFive 12/3/2020 #2

基本上,您需要将 MD 格式解释为 HTML。Javascript 是一种选择。

下面举个例子。(虽然是 Windows,独立于操作系统)

假设一个文件夹 mytest 看起来像这样,

D:\mytest>dir
 Volume in drive D is Data
 Volume Serial Number is ABCD-EFGH

 Directory of D:\mytest

12/03/2020  10:10 AM    <DIR>          .
12/03/2020  10:10 AM    <DIR>          ..
12/03/2020  10:09 AM             7,973 example-image.jpg
12/03/2020  10:12 AM             4,619 md_html.html
12/03/2020  10:00 AM             2,299 md_html.min.js
               3 File(s)         14,891 bytes
               2 Dir(s)  778,204,147,712 bytes free

D:\mytest>

这是html内容,

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <link rel="stylesheet" href="https://cdn.bootcss.com/highlight.js/9.12.0/styles/atom-one-light.min.css" />
  <link rel="stylesheet" href="https://cdn.bootcss.com/github-markdown-css/2.8.0/github-markdown.min.css" />
  <title>Marked In HTML</title>
</head>
<body>
  <template type="markdown">
    Try Marked In HTML !
    ====
  </template>
</body>
<script src="https://cdn.bootcss.com/marked/0.3.6/marked.min.js" charset="utf-8"></script>
<script src="https://cdn.bootcss.com/highlight.js/9.12.0/highlight.min.js" charset="utf-8"></script>
<script src="https://cdn.bootcss.com/highlight.js/9.12.0/languages/javascript.min.js" charset="utf-8"></script>
<script src="md_html.min.js" charset="utf-8"></script>
<script type="text/javascript">
  markedInHtml.init()
</script>
</html>

js

!function(n){function t(r){if(e[r])return e[r].exports;var i=e[r]={i:r,l:!1,exports:{}};return n[r].call(i.exports,i,i.exports,t),i.l=!0,i.exports}var e={};t.m=n,t.c=e,t.i=function(n){return n},t.d=function(n,e,r){t.o(n,e)||Object.defineProperty(n,e,{configurable:!1,enumerable:!0,get:r})},t.n=function(n){var e=n&&n.__esModule?function(){return n.default}:function(){return n};return t.d(e,"a",e),e},t.o=function(n,t){return Object.prototype.hasOwnProperty.call(n,t)},t.p="",t(t.s=1)}([function(n,t,e){"use strict";function r(n){return n&&n.__esModule?n:{default:n}}function i(n){if(Array.isArray(n)){for(var t=0,e=Array(n.length);t<n.length;t++)e[t]=n[t];return e}return Array.from(n)}function a(n,t){if(!(n instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0}),t.MarkedInHtml=void 0;var o=function(){function n(n,t){for(var e=0;e<t.length;e++){var r=t[e];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(n,r.key,r)}}return function(t,e,r){return e&&n(t.prototype,e),r&&n(t,r),t}}(),u=e(3),l=r(u),s=e(2),c=r(s);t.MarkedInHtml=function(){function n(){a(this,n),l.default.setOptions(this.options||{gfm:!0,tables:!0,breaks:!1,pedantic:!1,sanitize:!1,smartLists:!0,smartypants:!1,highlight:function(n,t,e){return c.default.highlightAuto(n).value}})}return o(n,[{key:"init",value:function(){var n=this;document.querySelectorAll('template[type="markdown"]').forEach(function(t){var e=document.createElement("div");e.innerHTML=n.parse(t),e.id=t.id,e.classList.add(["markdown-body"].concat(i(Array.from(t.classList)))),e.dataset.markdown=n.intelligentProcessingIndent(t),t.parentElement.replaceChild(e,t)})}},{key:"parse",value:function(n){return(0,l.default)(this.intelligentProcessingIndent(n))}},{key:"intelligentProcessingIndent",value:function(n){var t=n.innerHTML.split("\n");t.length&&/^\s*$/.test(t[0])&&t.shift(),t.length&&/^\s*$/.test(t[t.length-1])&&t.pop();var e=Math.min.apply(Math,i(t.map(function(n){return n.length?n.match(/^\s*/)[0].length:1/0})));return t.map(function(n){return n.substring(e)}).join("\n")}}]),n}()},function(n,t,e){"use strict";var r=e(0);window&&(window.markedInHtml=new r.MarkedInHtml)},function(n,t){n.exports=hljs},function(n,t){n.exports=marked}]);

jpgexample-image.jpg

打开html后,您应该能够转换

Try Marked In HTML !
    ====

result.png

可以尝试替换模板,有些效果可能无法呈现。例如,使用 John Gabriele 的快速降价示例等式显示得不好。

有人建议🎅使用片段,但我未能完成这项工作❄️,解析不成功。🥺

3赞 zhlicen 11/22/2021 #3

https://github.com/zhlicen/md.htmzeromd.js 只需提供 md.htm 文件和 md 文件,然后直接通过 url 访问:

/md.htm?src=README.md

现场演示:https://b.0-0.plus/blog/md.htm?src=https://raw.githubusercontent.com/microsoft/vscode/main/README.md

评论

2赞 Jmaurier 11/23/2021
虽然此链接可以回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。- 来自评论
3赞 Ajay 7/14/2023 #4

“md-block.js”似乎适用于基本的东西:

<script type="module" src="https://md-block.verou.me/md-block.js"></script>

然后将您的 MD 包裹在md-block

下面是一个简单的例子:

 <body>
        <h1>Normal HTML Heading</h1>
    
        <md-block>
            # Markdown Heading
            **Something in BOLD**
            ~~strikthrough~~
        </md-block>
    <script type="module" src="https://md-block.verou.me/md-block.js"></script>
</body>