js: markdown-it: Markdown解析器

白色玫瑰 程序猿

时间: 2023-07-11 阅读: 1 字数:3064

{}
js: markdown-it: Markdown解析器 文档: github: https://github.com/markdown-it/markdown-it github: https://github.com/markdown-it/markdown-it-for-inline doc: ...

目录

js: markdown-it: Markdown解析器

文档:

github: <a href="https://github.com/markdown-it/markdown-it">https://github.com/markdown-it/markdown-it</a> github: <a href="https://github.com/markdown-it/markdown-it-for-inline">https://github.com/markdown-it/markdown-it-for-inline</a> doc: <a href="https://github.com/markdown-it/markdown-it/blob/master/docs/architecture.md#renderer">https://github.com/markdown-it/markdown-it/blob/master/docs/architecture.md#renderer</a>

安装依赖

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

代码示例

1、默认default

linkify: false


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

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

输出

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

2、自动识别url

linkify: true


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

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

输出

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

3、自动识别url 并设置新窗口打开

linkify: true && target=_blank

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>

原文地址:https://blog.csdn.net/mouday/article/details/116497662?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168906542916800213021592%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=168906542916800213021592&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-29-116497662-null-null.142^v88^control_2,239^v2^insert_chatgpt&utm_term=markdown

本文章网址:https://www.sjxi.cn/detil/5bb62dc14b674a95a0d7b41d6681d28c

打赏作者

本站为非盈利网站,如果您喜欢这篇文章,欢迎支持我们继续运营!

最新评论
当前未登陆哦
登陆后才可评论哦

湘ICP备2021009447号

×

(穷逼博主)在线接单

QQ: 1164453243

邮箱: abcdsjx@126.com

前端项目代做
前后端分离
Python 爬虫脚本
Java 后台开发
各种脚本编写
服务器搭建
个人博客搭建
Web 应用开发
Chrome 插件编写
Bug 修复