JavaScript实现一个简单的Markdown语法解析器

什么是markdown

Markdown 是一种轻量级标记语言,创始人为约翰·格鲁伯(John Gruber)。 它允许人们使用易读易写的纯文本格式编写文档,然后转换成有效的 XHTML(或者HTML)文档。这种语言吸收了很多在电子邮件中已有的纯文本标记的特性。

由于 Markdown 的轻量化、易读易写特性,并且对于图片,图表、数学式都有支持,许多网站都广泛使用 Markdown 来撰写帮助文档或是用于论坛上发表消息。 如 GitHubRedditDiasporaStack ExchangeOpenStreetMapSourceForge、简书等,甚至还能被使用来撰写电子书。现在我们所看的 segmentfault 的编辑器也是支持markdown语法的!

上代码

</!DOCTYPE html>
<html>
<head>
 <title></title>
 <meta charset="utf-8">
 <script src="https://cdn.staticfile.org/jquery/3.6.1/jquery.min.js"></script>
 <style>
 *{
 padding: 0;
 margin: 0;
 font-family: system-ui,-apple-system,BlinkMacSystemFont,Helvetica Neue,PingFang SC,Hiragino Sans GB,Microsoft YaHei UI,Microsoft YaHei,Arial,sans-serif;
 }
 #app{
 width: 810px;
 height: 400px;
 margin: 30px auto 0;
 padding: 20px 20px;
 background: #00965e;
 }
 #app .md-editor{
 width: 400px;
 height: 400px;
 float: left;
 }
 #app .md-content{
 width: 100%;
 height: 400px;
 outline: none;
 resize: none;
 padding: 10px 10px;
 font-size: 17px;
 border: none;
 background: #eee;
 }
 #app .md-html{
 width: 400px;
 height: 400px;
 float: right;
 background: #eee;
 }
 #app code{
 color: #666;
 padding: 2px 5px;
 background: #fff;
 border-radius: 5px;
 font-size: 14px;
 }
 </style>
</head>
<body>
<h3 style="text-align: center;margin-top: 100px;">JavaScript实现一个简单的MarkDown语法解析器</h3>
<div id="app">
 
 <div class="md-editor">
 <form>
 <textarea name="md-content" class="md-content" placeholder="在这里使用markdown语法编写"></textarea>
 </form>
 </div>
 <div class="md-html">这里会实时显示markdown语法的解析结果</div>
</div>
<script type="text/javascript">
// 解析markdown语法为html
function markdownToHTML(markdownContent) {
 // 处理标题
 markdownContent = markdownContent.replace(/^#\s(.*)$/gm, '<h1>$1</h1>');
 markdownContent = markdownContent.replace(/^##\s(.*)$/gm, '<h2>$1</h2>');
 markdownContent = markdownContent.replace(/^###\s(.*)$/gm, '<h3>$1</h3>');
 markdownContent = markdownContent.replace(/^####\s(.*)$/gm, '<h4>$1</h4>');
 markdownContent = markdownContent.replace(/^#####\s(.*)$/gm, '<h5>$1</h5>');
 markdownContent = markdownContent.replace(/^######\s(.*)$/gm, '<h6>$1</h6>');
 // 处理加粗、斜体、删除线
 markdownContent = markdownContent.replace(/\*\*(.*)\*\*/gm, '<strong>$1</strong>');
 markdownContent = markdownContent.replace(/__(.*)__/gm, '<strong>$1</strong>');
 markdownContent = markdownContent.replace(/\*(.*)\*/gm, '<em>$1</em>');
 markdownContent = markdownContent.replace(/_(.*)_/gm, '<em>$1</em>');
 markdownContent = markdownContent.replace(/~~(.*)~~/gm, '<del>$1</del>');
 // 处理链接和图片
 markdownContent = markdownContent.replace(/\[(.*?)\]\((.*?)\)/gm, '<a href="$2">$1</a>');
 markdownContent = markdownContent.replace(/!\[(.*?)\]\((.*?)\)/gm, '<img src="$2" alt="$1">');
 // 处理行内代码和代码块
 markdownContent = markdownContent.replace(/`(.*?)`/gm, '<code>$1</code>');
 markdownContent = markdownContent.replace(/```([\s\S]*?)```/gm, '<pre>$1</pre>');
 // 处理换行
 markdownContent = markdownContent.replace(/\n/g, "<br>");
 return markdownContent;
}
// 实时解析markdown语法
$("#app .md-editor .md-content").bind("input propertychange",function(event){
 let md_content = $('#app .md-editor .md-content').val();
 $('#app .md-html').html(markdownToHTML(md_content));
});
</script>
</body>
</html>

实现原理

实现起来非常简单,就是通过正则替换预定的字符来实现HTML的输出。

demo

作者

TANKING

作者:TANKING原文地址:https://segmentfault.com/a/1190000043566043

%s 个评论

要回复文章请先登录注册