js: markdown-it: Markdown解析器

js: markdown-it: Markdown解析器html

文檔:git

安裝依賴

npm i -S markdown-it markdown-it-for-inline

代碼示例

一、默認defaultgithub

linkify: falsenpm

var md = require("markdown-it")()

const text = "http://www.baidu.com";
var result = md.render(text);
console.log(result);

輸出markdown

<p>http://www.baidu.com</p>

二、自動識別urlide

linkify: trueui

var md = require("markdown-it")({ linkify: true })

const text = "http://www.baidu.com";
var result = md.render(text);
console.log(result);

輸出url

<p><a href="http://www.baidu.com">http://www.baidu.com</a></p>

三、自動識別url 並設置新窗口打開spa

linkify: true && target=_blankcode

var iterator = require("markdown-it-for-inline");

var md = require("markdown-it")({ linkify: true }).use(
  iterator,
  "url_new_win",
  "link_open",
  function(tokens, idx) {
    var aIndex = tokens[idx].attrIndex("target");

    if (aIndex < 0) {
      tokens[idx].attrPush(["target", "_blank"]);
    } else {
      tokens[idx].attrs[aIndex][1] = "_blank";
    }
  }
);

const text = "http://www.baidu.com";
var result = md.render(text);
console.log(result);

輸出

<p><a href="http://www.baidu.com" target="_blank">http://www.baidu.com</a></p>
相關文章
相關標籤/搜索