当前位置: 首页 > 工具软件 > Showdown.js > 使用案例 >

showdown.js markdown解析成html并高亮

宗政才俊
2023-12-01
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.2.0/styles/default.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/showdown/1.9.1/showdown.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.2.0/highlight.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>


<body>
    <div id="markdown-content"></div>
    <script>
      window.onload = function() {
        var markdownContent = document.getElementById('markdown-content');
        var markdownConverter = new showdown.Converter();
        var markdownText = '```javascript\nfunction add(x, y) {\n  return x + y;\n}\n```';
        var html = markdownConverter.makeHtml(markdownText);
        markdownContent.innerHTML = html;
        $('pre code').each(function(i, block) {
          hljs.highlightBlock(block);
        });
      };
    </script>
</body>

 类似资料: