欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript實(shí)現(xiàn)簡(jiǎn)單的Markdown語法解析器

 更新時(shí)間:2023年03月22日 09:22:24   作者:TANKING  
Markdown 是一種輕量級(jí)標(biāo)記語言, 它允許人們使用易讀易寫的純文本格式編寫文檔,然后轉(zhuǎn)換成有效的 XHTML(或者HTML)文檔。本文將利用JavaScript實(shí)現(xiàn)簡(jiǎn)單的Markdown語法解析器,感興趣的可以了解一下

什么是markdown

Markdown 是一種輕量級(jí)標(biāo)記語言,創(chuàng)始人為約翰·格魯伯(John Gruber)。 它允許人們使用易讀易寫的純文本格式編寫文檔,然后轉(zhuǎn)換成有效的 XHTML(或者HTML)文檔。這種語言吸收了很多在電子郵件中已有的純文本標(biāo)記的特性。

由于 Markdown 的輕量化、易讀易寫特性,并且對(duì)于圖片,圖表、數(shù)學(xué)式都有支持,許多網(wǎng)站都廣泛使用 Markdown 來撰寫幫助文檔或是用于論壇上發(fā)表消息。 如 GitHub、Reddit、Diaspora、Stack ExchangeOpenStreetMap 、SourceForge、簡(jiǎn)書等,甚至還能被使用來撰寫電子書?,F(xiàn)在我們所看的 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實(shí)現(xiàn)一個(gè)簡(jiǎn)單的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">這里會(huì)實(shí)時(shí)顯示markdown語法的解析結(jié)果</div>
</div>

<script type="text/javascript">

// 解析markdown語法為html
function markdownToHTML(markdownContent) {

  // 處理標(biāo)題
  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" rel="external nofollow" >$1</a>');
  markdownContent = markdownContent.replace(/!\[(.*?)\]\((.*?)\)/gm, '<img src="$2" alt="$1">');

  // 處理行內(nèi)代碼和代碼塊
  markdownContent = markdownContent.replace(/`(.*?)`/gm, '<code>$1</code>');
  markdownContent = markdownContent.replace(/```([\s\S]*?)```/gm, '<pre>$1</pre>');

  // 處理換行
  markdownContent = markdownContent.replace(/\n/g, "<br>");

  return markdownContent;
}

// 實(shí)時(shí)解析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>

實(shí)現(xiàn)原理

實(shí)現(xiàn)起來非常簡(jiǎn)單,就是通過正則替換預(yù)定的字符來實(shí)現(xiàn)HTML的輸出。

demo

到此這篇關(guān)于JavaScript實(shí)現(xiàn)簡(jiǎn)單的Markdown語法解析器的文章就介紹到這了,更多相關(guān)JavaScript Markdown語法解析器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論