Skip to content

Commit b468bcd

Browse files
authored
refactor: simplify auto header insertion logic (#2578)
1 parent c9301b0 commit b468bcd

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

docs/configuration.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ window.$docsify = {
7272
- Type: `Boolean`
7373
- Default: `false`
7474

75-
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).
75+
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.
76+
77+
For more details, see [#78](https://github.com/docsifyjs/docsify/issues/78).
7678

7779
```js
7880
window.$docsify = {

src/core/render/index.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -342,11 +342,15 @@ export function Render(Base) {
342342

343343
if (autoHeader && activeEl) {
344344
const main = dom.getNode('#main');
345-
const firstNode = main.children[0];
346-
if (firstNode && firstNode.tagName !== 'H1') {
347-
const h1 = this.compiler.header(activeEl.innerText, 1);
348-
const wrapper = dom.create('div', h1);
349-
dom.before(main, wrapper.children[0]);
345+
const hasH1 = main.querySelector('h1');
346+
347+
if (!hasH1) {
348+
const h1HTML = this.compiler.header(activeEl.innerText, 1);
349+
const h1Node = dom.create('div', h1HTML).firstElementChild;
350+
351+
if (h1Node) {
352+
dom.before(main, h1Node);
353+
}
350354
}
351355
}
352356
}

0 commit comments

Comments
 (0)