gpt4 book ai didi

javascript - 如何配置 remark.js 来解析 Markdown 中嵌入的 HTML?

转载 作者:行者123 更新时间:2023-12-05 00:38:25 35 4
gpt4 key购买 nike

我正在使用 remark获取包含 HTML 标签的 Markdown 文档的 AST。当我运行这个:

const remark = require('remark')
const result = remark.parse('<h1>First</h1>')
console.log(JSON.stringify(result, null, 2))
我得到一个包含 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;如果这是由解析器配置选项启用的,我找不到它。指针将非常受欢迎。

最佳答案

也许你想要使用的是 rehype-raw插入。它允许您在 Markdown 中解析嵌入的 HTML。查看相关讨论here .

关于javascript - 如何配置 remark.js 来解析 Markdown 中嵌入的 HTML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64573043/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com