Skip to content

Commit 8f52f9e

Browse files
fix(seo): address CR — detect h1 anywhere in mdx tree
Copilot CR: `tree.children.some(...)` 只看顶级子节点,h1 嵌套在 blockquote / list 里(markdown 允许 \`> # title\`)会漏判。 改用 visit 整树扫描检测 h1。代价是多一次 traversal(约几十微秒每文档), 确保所有位置的 h1 都被识别并触发整树 demote。
1 parent 5410caa commit 8f52f9e

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

source.config.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,12 @@ function remarkNormalizeCodeLang() {
5858
*/
5959
function remarkShiftHeadingIfH1() {
6060
return (tree: Root) => {
61-
const hasH1 = tree.children.some(
62-
(n) => n.type === "heading" && n.depth === 1,
63-
);
61+
// 用 visit 遍历整树检测 h1:h1 可能嵌套在 blockquote / list 里
62+
// (markdown 语法允许 `> # title`),只看 tree.children 顶级会漏掉
63+
let hasH1 = false;
64+
visit(tree, "heading", (node) => {
65+
if (node.depth === 1) hasH1 = true;
66+
});
6467
if (!hasH1) return;
6568
visit(tree, "heading", (node) => {
6669
if (node.depth < 6) node.depth += 1;

0 commit comments

Comments
 (0)