remark:转换 Markdown 的神器

工具75 次阅读9 分钟

remark 是一个使用插件转换 Markdown 的工具。它可以检查和修改您的标记,可以在服务器、客户端、CLI、Deno 等上使用 remark。

另外,remark 还是一个插件生态系统,它使用 markdown 作为结构化数据,特别是 AST(抽象语法树)。AST 使程序可以轻松处理 markdown,我们称这些程序为插件。remark 生态系统包含许多现有插件,当然也可以自己制作插件。

image.png

guthub:https://github.com/remarkjs/remark

1、remark主要功能点

  • 100% 符合 CommonMark 和 GFM 或 MDX(通过插件)
  • **AST**抽象语法树(AST)使检查和修改内容变得更加容易
  • **popular**世界上最流行的 Markdown 解析器
  • **插件**150 多个可选择的插件

2、remark如何使用

将markdown 转换为html一个示例,比如通过如下代码可以将一段 markdown 转换为

markdown: # Hello, \*Mercury\*!

html: <h1>Hello, <em>Mercury</em>!</h1>

示例代码:

import rehypeStringify from 'rehype-stringify'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import {unified} from 'unified'

const file = await unified()
  .use(remarkParse)
  .use(remarkRehype)
  .use(rehypeStringify)
  .process('# Hello, *Mercury*!')

console.log(String(file)) // => '<h1>Hello, <em>Mercury</em>!</h1>'

3、remark插件系统

  • [remark-parse](https://github.com/remarkjs/remark/tree/main/packages/remark-parse)— 插件将 markdown 作为输入并将其转换为语法树 (mdast)
  • [remark-stringify](https://github.com/remarkjs/remark/blob/main/packages/remark-stringify)— 插件获取语法树(mdast)并将其转换为 markdown 作为输出
  • [remark](https://github.com/remarkjs/remark/tree/main/packages/remark)unified、remark-parse、remark-stringify在输入和输出为 markdown 时很有用
  • [remark-cli](https://github.com/remarkjs/remark/tree/main/packages/remark-cli)— CLI 用于remark检查和格式化脚本中的 markdown
  • [remark-gfm](https://github.com/remarkjs/remark-gfm)— 添加对 GFM(GitHub 风格 markdown)的支持
  • [remark-lint](https://github.com/remarkjs/remark-lint)— 检查 markdown 并警告不一致之处
  • **[remark-toc](https://github.com/remarkjs/remark-toc)**— 生成目录
  • **[remark-collapse](https://github.com/Rokt33r/remark-collapse)**** – 使某些部分可折叠**
  • **[remark-breaks](https://github.com/remarkjs/remark-breaks)**— 支持硬中断而不需要空格或转义符。
  • [remark-rehype](https://github.com/remarkjs/remark-rehype)— 将 markdown 转换为 HTML
  • [remark-directive](https://github.com/remarkjs/remark-directive)— 支持指令
  • [remark-frontmatter](https://github.com/remarkjs/remark-frontmatter)— 支持前置内容(YAML、TOML 等)
  • [remark-math](https://github.com/remarkjs/remark-math)— 支持数学
  • **[remark-html](https://github.com/remarkjs/remark-html)**—将 markdown 编译为 HTML 的统一(remark )插件。当您想将 markdown 转换为 HTML 时,此插件很有用。它是 的快捷方式 **.use(remarkRehype).use(rehypeSanitize).use(rehypeStringify)**

您可以从现有的 150 多个插件中进行选择。以下是查找插件的三种好方法:

  • [awesome-remark](https://github.com/remarkjs/awesome-remark)— 精选最棒的项目
  • [官方插件列表](https://github.com/remarkjs/remark/blob/main/doc/plugins.md#list-of-plugins)— 所有插件的列表
  • [remark-plugin主题](https://github.com/topics/remark-plugin)— GitHub 上的任何带remark-plugin标签的 repo

其中[remark-breaks](https://github.com/remarkjs/remark-breaks) 是比较好用的一个插件,经常我们 markdown 如下两个段落,使用了这个插件后,转换的 html 能够保留两个段落。

Mars is
the fourth planet

转换后

<p>Mars is<br>
the fourth planet</p>

如果不使用的话,这两行文字转换后得到一行文字。

4、remark 样式模板

remark-templates:https://github.com/3mdeb/remark-templates

继续阅读

基于全文检索与主题相似度