diff --git a/packages/docusaurus-search-local/src/server/parse.test.js b/packages/docusaurus-search-local/src/server/parse.test.js index 06ed4d7..27681ea 100644 --- a/packages/docusaurus-search-local/src/server/parse.test.js +++ b/packages/docusaurus-search-local/src/server/parse.test.js @@ -116,6 +116,82 @@ describe("parser", () => { }); }); + it("parses page without title in frontmatter and with h1 in Markdown", async () => { + const htmlPath = path.join( + __dirname, + "..", + "..", + "..", + "example-docs/build/docs/next/d-s-l-test-no-title-h1/index.html" + ); + const html = await readFileAsync(htmlPath, "utf-8"); + expect(html2text(html, "docs")).toEqual({ + docSidebarParentCategories: ["Docusaurus"], + pageTitle: "Page title from markdown", + sections: [ + { + content: "bla bla", + hash: "", + title: "Page title from markdown", + tags: [], + }, + ], + }); + }); + + it("parses page without title in frontmatter and with h2 in Markdown", async () => { + const htmlPath = path.join( + __dirname, + "..", + "..", + "..", + "example-docs/build/docs/next/d-s-l-test-no-title-h2/index.html" + ); + const html = await readFileAsync(htmlPath, "utf-8"); + expect(html2text(html, "docs")).toEqual({ + docSidebarParentCategories: ["Docusaurus"], + pageTitle: "d-s-l-test-no-title-h2", + sections: [ + { content: "", hash: "", title: "d-s-l-test-no-title-h2", tags: [] }, + { + content: "bla bla", + hash: "#page-title-from-markdown", + title: "Page title from markdown", + tags: [], + }, + ], + }); + }); + + it("parses page without title in frontmatter and with h1 in Markdown but with pre text", async () => { + const htmlPath = path.join( + __dirname, + "..", + "..", + "..", + "example-docs/build/docs/next/d-s-l-test-no-title-h1-pre-text/index.html" + ); + const html = await readFileAsync(htmlPath, "utf-8"); + expect(html2text(html, "docs")).toEqual({ + docSidebarParentCategories: ["Docusaurus"], + pageTitle: "d-s-l-test-no-title-h1-pre-text", + sections: [ + { + content: "some pre text before the actual page title", + hash: "", + title: "d-s-l-test-no-title-h1-pre-text", + tags: [], + }, + { + content: "bla bla", + hash: "", + title: "Page title from markdown", + tags: [], + }, + ], + }); + }); + it("parses nested sidebar categories", async () => { const htmlPath = path.join( __dirname, diff --git a/packages/docusaurus-search-local/src/server/parse.ts b/packages/docusaurus-search-local/src/server/parse.ts index b872584..1ccb6b0 100644 --- a/packages/docusaurus-search-local/src/server/parse.ts +++ b/packages/docusaurus-search-local/src/server/parse.ts @@ -97,8 +97,7 @@ export function html2text( if (type === "docs" || type === "blog") { const HEADINGS = "h1, h2, h3"; - const pageTitle = $("article header h1").first().text(); - + const pageTitle = $("article h1").first().text(); const sections: Array<{ title: string; hash: string; diff --git a/packages/example-docs/docs/d-s-l-test-no-title-h1-pre-text.md b/packages/example-docs/docs/d-s-l-test-no-title-h1-pre-text.md new file mode 100644 index 0000000..5bd1b51 --- /dev/null +++ b/packages/example-docs/docs/d-s-l-test-no-title-h1-pre-text.md @@ -0,0 +1,11 @@ +--- +id: d-s-l-test-no-title-h1-pre-text +# title: DOC TITLE # Deliberately no title here +sidebar_label: SIDEBAR LABEL +--- + +some pre text before the actual page title + +# Page title from markdown + +bla bla \ No newline at end of file diff --git a/packages/example-docs/docs/d-s-l-test-no-title-h1.md b/packages/example-docs/docs/d-s-l-test-no-title-h1.md new file mode 100644 index 0000000..a3df11d --- /dev/null +++ b/packages/example-docs/docs/d-s-l-test-no-title-h1.md @@ -0,0 +1,9 @@ +--- +id: d-s-l-test-no-title-h1 +# title: DOC TITLE # Deliberately no title here +sidebar_label: SIDEBAR LABEL +--- + +# Page title from markdown + +bla bla \ No newline at end of file diff --git a/packages/example-docs/docs/d-s-l-test-no-title-h2.md b/packages/example-docs/docs/d-s-l-test-no-title-h2.md new file mode 100644 index 0000000..6e6cdd0 --- /dev/null +++ b/packages/example-docs/docs/d-s-l-test-no-title-h2.md @@ -0,0 +1,9 @@ +--- +id: d-s-l-test-no-title-h2 +# title: DOC TITLE # Deliberately no title here +sidebar_label: SIDEBAR LABEL +--- + +## Page title from markdown + +bla bla \ No newline at end of file diff --git a/packages/example-docs/sidebars.js b/packages/example-docs/sidebars.js index daa6dd7..87f3221 100644 --- a/packages/example-docs/sidebars.js +++ b/packages/example-docs/sidebars.js @@ -11,7 +11,7 @@ module.exports = { someSidebar: { - Docusaurus: ['d-s-l-test', 'd-s-l-test2', 'd-s-l-test3', 'translated'], + Docusaurus: ['d-s-l-test', 'd-s-l-test2', 'd-s-l-test3', 'd-s-l-test-no-title-h1-pre-text', 'd-s-l-test-no-title-h1', 'd-s-l-test-no-title-h2', 'translated'], SidebarParent: [ { SidebarChild: ['nested_sidebar_doc']