Skip to content

Commit

Permalink
fix: Support hash parsing of URLs (fixes Docusaurus 3 compat) (#216)
Browse files Browse the repository at this point in the history
Co-authored-by: Christian Flach <[email protected]>
  • Loading branch information
fxamauri and cmfcmf authored Nov 9, 2024
1 parent c353c5b commit 96c7803
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
38 changes: 38 additions & 0 deletions packages/docusaurus-search-local/src/server/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,5 +310,43 @@ describe("parser", () => {
],
});
});

it("parses hashes correctly from URLs", () => {
const html = `<html>
<head><title>TITLE</title></head>
<body>
<article>
<h1>a<a href="lala.com#first#second" class="hash-link"></a></h1>
<h1>b<a href="#first#second" class="hash-link"></a></h1>
<h1>c<a href="/foo#first#second" class="hash-link"></a></h1>
</article>
</body>
</html>
`;
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: [],
},
],
});
});
});
});
5 changes: 3 additions & 2 deletions packages/docusaurus-search-local/src/server/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,9 @@ export function html2text(
// <a class="hash-link" href="#first-header" title="Direct link to heading">#</a>
.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
Expand Down

0 comments on commit 96c7803

Please sign in to comment.