为什么 H1 标签被排除在 NuxtJS 的内容模块的目录列表之外?
Why are H1 tags excluded from NuxtJS's Content module's TOC list?
我正在导入大量降价内容,这些内容都使用了大量的 H1 (#) 标签。在创建 TOC 组件时,我注意到 H1 标签被排除在 @Nuxt/Content 方便提供的 TOC 数组之外。
这让我很头疼,我宁愿不写一个脚本来更改数百个MD文件来将每个标题修改得更深一层,尽管这是一种选择。
我尝试过的事情:
mounted() {
this.observer = new IntersectionObserver(entries => {
entries.forEach(entry => {
const id = entry.target.getAttribute('id');
if (entry.isIntersecting) {
this.currentlyActiveToc = id;
}
});
}, this.observerOptions);
// Including h1 explicitly to the function
document.querySelectorAll('.nuxt-content h1[id], .nuxt-content h2[id], .nuxt-content h3[id]').forEach((section) => {
this.observer.observe(section);
});
},
修改 content/parsers/markdown/index.js
generateToc
函数以在 const depth
中包含 h1
generateToc (body) {
const { tocTags } = this.options
const children = this.flattenNode(body, 2)
return children.filter(node => tocTags.includes(node.tag)).map((node) => {
const id = node.props.id
const depth = ({
h1: 2,
h2: 3,
h3: 4,
h4: 5,
b5: 6
})[node.tag]
const text = this.flattenNodeText(node)
return {
id,
depth,
text
}
})
}
在 Nuxt/Vue 中,文档 object 仍然没有注册要包含在目录中的 h1 标签。有没有人有解决方法或如何包含它们的想法?
最后 -- 使用 H1 / #
标签来分隔 markdown 中的主要部分是否被认为不是好的做法?
提前致谢!
看错地方了。
我通过更改 content/lib/utils.js
文件成功地将 H1 标签添加到目录列表中。
const { tocDepth } = markdown
const tocTags = []
if (tocDepth < 1) {
logger.info(`content.markdown.tocDepth is set as ${tocDepth}. Table of contents of markdown files will be empty.`)
return tocTags
}
if (tocDepth > 6) {
logger.info(`content.markdown.tocDepth is set as ${tocDepth}. Table of contents of markdown files will include all the headings.`)
}
// CHANGE LINE BELOW FROM i=2 to i=1
for (let i = 1; i <= tocDepth; i++) {
tocTags.push(`h${i}`)
}
return tocTags
}
在网上找不到与此问题相关的任何内容,希望这对某人有所帮助!
我正在导入大量降价内容,这些内容都使用了大量的 H1 (#) 标签。在创建 TOC 组件时,我注意到 H1 标签被排除在 @Nuxt/Content 方便提供的 TOC 数组之外。
这让我很头疼,我宁愿不写一个脚本来更改数百个MD文件来将每个标题修改得更深一层,尽管这是一种选择。
我尝试过的事情:
mounted() {
this.observer = new IntersectionObserver(entries => {
entries.forEach(entry => {
const id = entry.target.getAttribute('id');
if (entry.isIntersecting) {
this.currentlyActiveToc = id;
}
});
}, this.observerOptions);
// Including h1 explicitly to the function
document.querySelectorAll('.nuxt-content h1[id], .nuxt-content h2[id], .nuxt-content h3[id]').forEach((section) => {
this.observer.observe(section);
});
},
修改 content/parsers/markdown/index.js
generateToc
函数以在 const depth
generateToc (body) {
const { tocTags } = this.options
const children = this.flattenNode(body, 2)
return children.filter(node => tocTags.includes(node.tag)).map((node) => {
const id = node.props.id
const depth = ({
h1: 2,
h2: 3,
h3: 4,
h4: 5,
b5: 6
})[node.tag]
const text = this.flattenNodeText(node)
return {
id,
depth,
text
}
})
}
在 Nuxt/Vue 中,文档 object 仍然没有注册要包含在目录中的 h1 标签。有没有人有解决方法或如何包含它们的想法?
最后 -- 使用 H1 / #
标签来分隔 markdown 中的主要部分是否被认为不是好的做法?
提前致谢!
看错地方了。
我通过更改 content/lib/utils.js
文件成功地将 H1 标签添加到目录列表中。
const { tocDepth } = markdown
const tocTags = []
if (tocDepth < 1) {
logger.info(`content.markdown.tocDepth is set as ${tocDepth}. Table of contents of markdown files will be empty.`)
return tocTags
}
if (tocDepth > 6) {
logger.info(`content.markdown.tocDepth is set as ${tocDepth}. Table of contents of markdown files will include all the headings.`)
}
// CHANGE LINE BELOW FROM i=2 to i=1
for (let i = 1; i <= tocDepth; i++) {
tocTags.push(`h${i}`)
}
return tocTags
}
在网上找不到与此问题相关的任何内容,希望这对某人有所帮助!