频道
bg

Unified

coding一月 30, 20221mins
Frontend

Unified生态专门处理不同格式内容的转换与处理,主要流程是

  1. 通过Parser将源文件的内容转换的AST
  2. 不同的AST之间进行转换
  3. 对AST进行修改
  4. 将AST进行Stringify转换成目标格式的内容

tsx

| ........................ process ........................... |
| .......... parse ... | ... run ... | ... stringify ..........|
+--------+ +----------+
Input ->- | Parser | ->- Syntax Tree ->- | Compiler | ->- Output
+--------+ | +----------+
X
|
+--------------+
| Transformers |
+--------------+

ASTH1

  • esast — JavaScript
  • hast — HTML
  • mdast — markdown
  • nlcst — natural language
  • xast — XML

APIH1

[unified.parse()](https://github.com/unifiedjs/unified#processorparsefile) 只执行Parse过程 unified.run() 执行Parse以及Tranform过程

[unified.process()](https://github.com/unifiedjs/unified#processorprocessfile-done) 会执行完整个流程

模块H1

remarkH2

处理markdown格式

rehypeH2

处理html格式

示例H1

将markdown转换成html,并进行一些处理后输出HTML

bash

const file = await unified()
.use(remarkParse)
.use(remarkRehype)
.use(rehypeDocument, {title: '👋🌍'})
.use(rehypeFormat)
.use(rehypeStringify)
.process('# Hello world!'
---
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>👋🌍</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h1>Hello world!</h1>
</body>
</html>

评论


新的评论

匹配您的Gravatar头像

Joen Yu

@2022 JoenYu, all rights reserved. Made with love.