From 96c7803e63fbfdb5c5dd2b2285928fffdaeed577 Mon Sep 17 00:00:00 2001 From: Amauri Dias Date: Sat, 9 Nov 2024 19:51:36 -0300 Subject: [PATCH] fix: Support hash parsing of URLs (fixes Docusaurus 3 compat) (#216) Co-authored-by: Christian Flach --- .../src/server/parse.test.js | 38 +++++++++++++++++++ .../src/server/parse.ts | 5 ++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/packages/docusaurus-search-local/src/server/parse.test.js b/packages/docusaurus-search-local/src/server/parse.test.js index d59939f..4296787 100644 --- a/packages/docusaurus-search-local/src/server/parse.test.js +++ b/packages/docusaurus-search-local/src/server/parse.test.js @@ -310,5 +310,43 @@ describe("parser", () => { ], }); }); + + it("parses hashes correctly from URLs", () => { + const html = ` + TITLE + +
+

a

+

b

+

c

+
+ + + `; + expect(html2text(html, "docs")).toEqual({ + docSidebarParentCategories: [], + pageTitle: "a", + sections: [ + { + title: "a", + hash: "#first#second", + content: "", + tags: [], + }, + { + title: "b", + hash: "#first#second", + content: "", + tags: [], + }, + { + title: "c", + hash: "#first#second", + content: "", + tags: [], + }, + ], + }); + }); }); }); diff --git a/packages/docusaurus-search-local/src/server/parse.ts b/packages/docusaurus-search-local/src/server/parse.ts index d13a7fb..e9a4a75 100644 --- a/packages/docusaurus-search-local/src/server/parse.ts +++ b/packages/docusaurus-search-local/src/server/parse.ts @@ -124,8 +124,9 @@ export function html2text( // # .not("a[aria-hidden=true], a.hash-link") .text(); - const hash = $(heading).find("a.hash-link").attr("href") || ""; - + const linkHash = $(heading).find("a.hash-link").attr("href") || ""; + const [, ...hashParts] = linkHash.split("#"); + const hash = hashParts.length ? `#${hashParts.join("#")}` : ""; let $sectionElements; if ($(heading).parents(".markdown").length === 0) { // $(heading) is the page title