From b3159b14b8d8dce0ddd93e1e8c0753f13112b3d7 Mon Sep 17 00:00:00 2001 From: Luffy <52o@qq52o.cn> Date: Thu, 24 Jul 2025 18:04:40 +0800 Subject: [PATCH 1/2] refactor: simplify auto header insertion logic - Use querySelector instead of getElementsByTagName for clarity - Avoid redundant DOM wrapping by directly accessing firstElementChild - Add null check to prevent potential DOM insertion errors --- src/core/render/index.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/core/render/index.js b/src/core/render/index.js index 3ee35513b..85b6e7200 100644 --- a/src/core/render/index.js +++ b/src/core/render/index.js @@ -342,11 +342,15 @@ export function Render(Base) { if (autoHeader && activeEl) { const main = dom.getNode('#main'); - const firstNode = main.children[0]; - if (firstNode && firstNode.tagName !== 'H1') { - const h1 = this.compiler.header(activeEl.innerText, 1); - const wrapper = dom.create('div', h1); - dom.before(main, wrapper.children[0]); + const hasH1 = main.querySelector('h1'); + + if (!hasH1) { + const h1HTML = this.compiler.header(activeEl.innerText, 1); + const h1Node = dom.create('div', h1HTML).firstElementChild; + + if (h1Node) { + dom.before(main, h1Node); + } } } } From f969a655048d09eff740e27f4412184dcb62849f Mon Sep 17 00:00:00 2001 From: Luffy <52o@qq52o.cn> Date: Wed, 30 Jul 2025 11:00:53 +0800 Subject: [PATCH 2/2] docs: update autoHeader --- docs/configuration.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/configuration.md b/docs/configuration.md index f1fbce04a..bb5df138c 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -72,7 +72,9 @@ window.$docsify = { - Type: `Boolean` - Default: `false` -If `loadSidebar` and `autoHeader` are both enabled, for each link in `_sidebar.md`, prepend a header to the page before converting it to HTML. See [#78](https://github.com/docsifyjs/docsify/issues/78). +If `loadSidebar` and `autoHeader` are both enabled, for each link in `_sidebar.md`, prepend a header to the page before converting it to HTML — but only if the page does not already contain an H1 heading. + +For more details, see [#78](https://github.com/docsifyjs/docsify/issues/78). ```js window.$docsify = {