我正在使用remark为一个包含超文本标记语言标签的Markdown文档获取一个AST。当我运行以下命令时:
const remark = require('remark')
const result = remark.parse('<h1>First</h1>')
console.log(JSON.stringify(result, null, 2))我得到一个包含level-1标题的AST:
{
"type": "root",
"children": [
{
"type": "heading",
"depth": 1,
"children": [
{
"type": "text",
"value": "Title",
"position": {
"start": {
"line": 1,
"column": 3,
"offset": 2
},
"end": {
"line": 1,
"column": 8,
"offset": 7
}
}
}
],
"position": {
"start": {
"line": 1,
"column": 1,
"offset": 0
},
"end": {
"line": 1,
"column": 8,
"offset": 7
}
}
},
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "body",
"position": {
"start": {
"line": 2,
"column": 1,
"offset": 8
},
"end": {
"line": 2,
"column": 5,
"offset": 12
}
}
}
],
"position": {
"start": {
"line": 2,
"column": 1,
"offset": 8
},
"end": {
"line": 2,
"column": 5,
"offset": 12
}
}
}
],
"position": {
"start": {
"line": 1,
"column": 1,
"offset": 0
},
"end": {
"line": 2,
"column": 5,
"offset": 12
}
}
}但是如果我使用显式的h1标记:
const remark = require('remark')
const result = remark.parse('<h1>Title</h1>\nbody') # <- note change
console.log(JSON.stringify(result, null, 2))我得到了一个html类型的节点,其中包含标记的文本及其内容:
{
"type": "root",
"children": [
{
"type": "html",
"value": "<h1>Title</h1>\nbody",
"position": {
"start": {
"line": 1,
"column": 1,
"offset": 0
},
"end": {
"line": 2,
"column": 5,
"offset": 19
}
}
}
],
"position": {
"start": {
"line": 1,
"column": 1,
"offset": 0
},
"end": {
"line": 2,
"column": 5,
"offset": 19
}
}
}我希望在第二种情况下获得与第一种情况相同的AST,即我希望remark解析HTML.我预计它会默认这样做,因为Markdown允许包含HTML;如果解析器配置选项启用了这一点,我就无法找到它。指针将非常受欢迎。
发布于 2021-03-09 06:33:53
也许你想要使用的是rehype-raw插件。它允许您解析markdown中嵌入的HTML。查看相关的讨论here。
https://stackoverflow.com/questions/64573043
复制相似问题