From a09047dad09e701fa5f4d117190b7e0bcf2e14c6 Mon Sep 17 00:00:00 2001 From: Luffy <52o@qq52o.cn> Date: Thu, 24 Jul 2025 13:41:12 +0800 Subject: [PATCH 1/5] fix: unwrap top-level

tags wrapping elements in sidebar list items --- src/core/event/index.js | 4 ++-- src/core/fetch/index.js | 4 +--- src/core/render/compiler.js | 5 +++-- src/core/render/utils.js | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 7 deletions(-) diff --git a/src/core/event/index.js b/src/core/event/index.js index 9e3be2dd6..837f9e48e 100644 --- a/src/core/event/index.js +++ b/src/core/event/index.js @@ -244,9 +244,9 @@ export function Events(Base) { dom.on(sidebarElm, 'click', ({ target }) => { const linkElm = target.closest('a'); const linkParent = linkElm?.closest('li'); - const subSidebar = linkParent?.querySelector('.app-sub-sidebar'); + const hasSubSidebar = linkParent?.querySelector('.app-sub-sidebar'); - if (subSidebar) { + if (hasSubSidebar) { dom.toggleClass(linkParent, 'collapse'); } }); diff --git a/src/core/fetch/index.js b/src/core/fetch/index.js index 52b2ab1dd..ea921ff46 100644 --- a/src/core/fetch/index.js +++ b/src/core/fetch/index.js @@ -1,4 +1,4 @@ -/* eslint-disable no-unused-vars */ + import { getParentPath, stringifyQuery } from '../router/util.js'; import { noop, isExternal } from '../util/core.js'; import { get } from '../util/ajax.js'; @@ -262,8 +262,6 @@ export function Fetch(Base) { } initFetch() { - const { loadSidebar } = this.config; - this.$fetch(_ => this.callHook('ready')); } }; diff --git a/src/core/render/compiler.js b/src/core/render/compiler.js index 124be9b7f..d7f3368cf 100644 --- a/src/core/render/compiler.js +++ b/src/core/render/compiler.js @@ -5,7 +5,7 @@ import { tree as treeTpl } from './tpl.js'; import { genTree } from './gen-tree.js'; import { slugify } from './slugify.js'; import { emojify } from './emojify.js'; -import { getAndRemoveConfig } from './utils.js'; +import { getAndRemoveConfig, maybeUnwrapTopLevelPWithA } from './utils.js'; import { imageCompiler } from './compiler/image.js'; import { headingCompiler } from './compiler/heading.js'; import { highlightCodeCompiler } from './compiler/code.js'; @@ -193,7 +193,8 @@ export class Compiler { // compile sidebar from _sidebar.md if (text) { - return this.compile(text); + html = this.compile(text); + return maybeUnwrapTopLevelPWithA(html); } // compile sidebar from content's headings toc for (let i = 0; i < toc.length; i++) { diff --git a/src/core/render/utils.js b/src/core/render/utils.js index 66796de1e..af725ef79 100644 --- a/src/core/render/utils.js +++ b/src/core/render/utils.js @@ -88,3 +88,40 @@ export function getAndRemoveDocsifyIgnoreConfig(content = '') { return { content, ignoreAllSubs, ignoreSubHeading }; } + +/** + * Unwraps

tags that directly wrap a single tag inside

  • elements at the top level. + * + * This function processes an HTML string and removes

    wrappers inside

  • elements + * when the

    contains exactly one element and no other content. + * + * It does NOT process nested structures. + * + * @see https://github.com/markedjs/marked/issues/1461 + * @param {string} html - The HTML string to transform. + * @returns {string} - The transformed HTML with

    removed from

  • structures. + */ +export function maybeUnwrapTopLevelPWithA(html) { + // Only unwrap if the pattern
  • { + const first = li.firstElementChild; + if ( + first?.tagName === 'P' && + first.children.length === 1 && + first.firstElementChild.tagName === 'A' && + first.childNodes.length === 1 + ) { + // Replace the

    with the inside it + li.replaceChild(first.firstElementChild, first); + } + }); + + return template.innerHTML; +} From 67429c81568cdcd451a4487f831ade00da3a00dd Mon Sep 17 00:00:00 2001 From: Luffy <52o@qq52o.cn> Date: Thu, 24 Jul 2025 13:53:29 +0800 Subject: [PATCH 2/5] fix: remove unnecessary blank line at the beginning of index.js --- src/core/fetch/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/core/fetch/index.js b/src/core/fetch/index.js index ea921ff46..be4e21409 100644 --- a/src/core/fetch/index.js +++ b/src/core/fetch/index.js @@ -1,4 +1,3 @@ - import { getParentPath, stringifyQuery } from '../router/util.js'; import { noop, isExternal } from '../util/core.js'; import { get } from '../util/ajax.js'; From fc64a24302070206cd05ced0a062707850b557a0 Mon Sep 17 00:00:00 2001 From: Luffy <52o@qq52o.cn> Date: Thu, 24 Jul 2025 14:02:40 +0800 Subject: [PATCH 3/5] test: update snapshot --- test/integration/__snapshots__/docs.test.js.snap | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/__snapshots__/docs.test.js.snap b/test/integration/__snapshots__/docs.test.js.snap index 4a084fed4..44fa009f8 100644 --- a/test/integration/__snapshots__/docs.test.js.snap +++ b/test/integration/__snapshots__/docs.test.js.snap @@ -25,6 +25,6 @@ exports[`Docs Site sidebar renders and is unchanged 1`] = `

    + " `; From 4cca3373e2d1fc697da2ff5d2cc1a02164a6c8d7 Mon Sep 17 00:00:00 2001 From: sy-records <52o@qq52o.cn> Date: Mon, 28 Jul 2025 19:22:14 +0800 Subject: [PATCH 4/5] fix: remove maybeUnwrapTopLevelPWithA --- docs/_sidebar.md | 4 ++-- src/core/render/compiler.js | 5 ++--- src/core/render/utils.js | 37 ------------------------------------- 3 files changed, 4 insertions(+), 42 deletions(-) diff --git a/docs/_sidebar.md b/docs/_sidebar.md index 37b6f32cf..86ad48f6b 100644 --- a/docs/_sidebar.md +++ b/docs/_sidebar.md @@ -27,5 +27,5 @@ - [Embed Files](embed-files.md) - [UI Kit](ui-kit.md) -- [Awesome docsify](awesome.md) -- [Changelog](changelog.md) +* [Awesome docsify](awesome.md) +* [Changelog](changelog.md) diff --git a/src/core/render/compiler.js b/src/core/render/compiler.js index d7f3368cf..124be9b7f 100644 --- a/src/core/render/compiler.js +++ b/src/core/render/compiler.js @@ -5,7 +5,7 @@ import { tree as treeTpl } from './tpl.js'; import { genTree } from './gen-tree.js'; import { slugify } from './slugify.js'; import { emojify } from './emojify.js'; -import { getAndRemoveConfig, maybeUnwrapTopLevelPWithA } from './utils.js'; +import { getAndRemoveConfig } from './utils.js'; import { imageCompiler } from './compiler/image.js'; import { headingCompiler } from './compiler/heading.js'; import { highlightCodeCompiler } from './compiler/code.js'; @@ -193,8 +193,7 @@ export class Compiler { // compile sidebar from _sidebar.md if (text) { - html = this.compile(text); - return maybeUnwrapTopLevelPWithA(html); + return this.compile(text); } // compile sidebar from content's headings toc for (let i = 0; i < toc.length; i++) { diff --git a/src/core/render/utils.js b/src/core/render/utils.js index af725ef79..66796de1e 100644 --- a/src/core/render/utils.js +++ b/src/core/render/utils.js @@ -88,40 +88,3 @@ export function getAndRemoveDocsifyIgnoreConfig(content = '') { return { content, ignoreAllSubs, ignoreSubHeading }; } - -/** - * Unwraps

    tags that directly wrap a single tag inside

  • elements at the top level. - * - * This function processes an HTML string and removes

    wrappers inside

  • elements - * when the

    contains exactly one element and no other content. - * - * It does NOT process nested structures. - * - * @see https://github.com/markedjs/marked/issues/1461 - * @param {string} html - The HTML string to transform. - * @returns {string} - The transformed HTML with

    removed from

  • structures. - */ -export function maybeUnwrapTopLevelPWithA(html) { - // Only unwrap if the pattern
  • { - const first = li.firstElementChild; - if ( - first?.tagName === 'P' && - first.children.length === 1 && - first.firstElementChild.tagName === 'A' && - first.childNodes.length === 1 - ) { - // Replace the

    with the inside it - li.replaceChild(first.firstElementChild, first); - } - }); - - return template.innerHTML; -} From 28ce8a6b539b9d1b92bdc1fb2cea7fe9809d7edd Mon Sep 17 00:00:00 2001 From: sy-records <52o@qq52o.cn> Date: Mon, 28 Jul 2025 19:26:25 +0800 Subject: [PATCH 5/5] chore: update snapshot --- test/integration/__snapshots__/docs.test.js.snap | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/__snapshots__/docs.test.js.snap b/test/integration/__snapshots__/docs.test.js.snap index 44fa009f8..ce0dc1978 100644 --- a/test/integration/__snapshots__/docs.test.js.snap +++ b/test/integration/__snapshots__/docs.test.js.snap @@ -25,6 +25,6 @@ exports[`Docs Site sidebar renders and is unchanged 1`] = `

    + " `;