Skip to content

Commit

Permalink
fix: Correctly extract page titles when not specified in frontmatter, c…
Browse files Browse the repository at this point in the history
…loses #146

Co-authored-by: Joe Kleinschmidt <joe@Joes-MacBook-Pro-2.local>
Co-authored-by: Christian Flach <cmfcmf.flach@gmail.com>
Fixes #146
  • Loading branch information
kleinschmidtj authored Dec 22, 2022
1 parent f169f9a commit 1f33151
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 3 deletions.
76 changes: 76 additions & 0 deletions packages/docusaurus-search-local/src/server/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions packages/docusaurus-search-local/src/server/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions packages/example-docs/docs/d-s-l-test-no-title-h1-pre-text.md
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions packages/example-docs/docs/d-s-l-test-no-title-h1.md
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions packages/example-docs/docs/d-s-l-test-no-title-h2.md
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion packages/example-docs/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down

0 comments on commit 1f33151

Please sign in to comment.