简介
@iktakahiro/markdown-it-br
是一款基于 markdown-it
的插件,用于支持在 markdown 中使用 <br>
标签。
安装
使用 npm 进行安装:
npm install @iktakahiro/markdown-it-br --save
使用
基本用法
首先,需要引入 markdown-it
和 @iktakahiro/markdown-it-br
:
const markdownIt = require('markdown-it'); const markdownItBr = require('@iktakahiro/markdown-it-br');
然后,需要将 @iktakahiro/markdown-it-br
插件加入到 markdown-it
中:
const md = markdownIt().use(markdownItBr);
接下来可以开始使用 md
,例如:
const result = md.render('hello<br>world'); console.log(result); // 输出:hello<br>\nworld
在上述代码中,<br>
标签会被渲染成 <br>
。
内联标签
除了 <br>
标签之外,还可以使用 \\
在行末表示换行,例如:
const result = md.render('hello\\\\\nworld'); console.log(result); // 输出:hello<br>\nworld
在上述代码中,\\\\\n
会被渲染成 <br>
。
块级标签
默认情况下, <br>
标签会被渲染成内联标签。如果需要将其作为块级标签使用,可以使用 !br
语法:
const result = md.render('hello\n!br\nworld'); console.log(result); // 输出:<p>hello</p>\n<br>\n<p>world</p>
在上述代码中,!br
会被渲染成 <br>
。
示例代码
-- -------------------- ---- ------- ----- ---------- - ----------------------- ----- ------------ - -------------------------------------- ----- -- - ------------------------------- ----- ------- - ---------------------------- --------------------- -- ------------------- ----- ------- - ------------------------------ --------------------- -- ------------------- ----- ------- - ------------------------------- --------------------- -- -----------------------------------
指导意义
- 掌握如何使用基于
markdown-it
的插件。 - 熟练掌握在 markdown 中插入
<br>
标签的格式。 - 了解如何使用插件中提供的高级用法,例如将
<br>
渲染为块级标签。
来源:JavaScript中文网 ,转载请注明来源 https://www.javascriptcn.com/post/6005671081e8991b448e34fe