Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add header fragment support for plugin-docusaurus-v3 #755

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/plugin-docusaurus-v3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"dependencies": {
"@docusaurus/plugin-content-docs": "^3.2.0",
"@docusaurus/theme-common": "^3.2.0",
"@docusaurus/utils": "^3.0.1",
"@orama/highlight": "^0.1.5",
"@orama/orama": "workspace:*",
"@orama/plugin-analytics": "workspace:*",
Expand Down
16 changes: 13 additions & 3 deletions packages/plugin-docusaurus-v3/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { JSDOM } from "jsdom"
import MarkdownIt from "markdown-it"
import matter from "gray-matter"
import { createSnapshot, deployIndex, fetchEndpointConfig } from "./utils"
import { parseMarkdownHeadingId, writeMarkdownHeadingId } from "@docusaurus/utils"

enum DeployType {
SNAPSHOT_ONLY = "snapshot-only",
Expand Down Expand Up @@ -201,12 +202,13 @@ async function generateDocs({
const { title, permalink, source } = data
const fileContent = readFileSync(source.replace("@site", siteDir), "utf-8")
const contentWithoutFrontMatter = matter(fileContent).content
const contentWithIds = writeMarkdownHeadingId(contentWithoutFrontMatter)

return parseHTMLContent({
originalTitle: title,
version,
category,
html: new MarkdownIt().render(contentWithoutFrontMatter),
html: new MarkdownIt().render(contentWithIds),
path: permalink
})
}
Expand Down Expand Up @@ -249,8 +251,12 @@ function parseHTMLContent({
})
}
headers.forEach((header) => {
const sectionTitle = header.textContent?.trim()
const headerText = header.textContent?.trim() ?? ""
const headerTag = header.tagName.toLowerCase()

// Use parseMarkdownHeadingId to extract clean title and section ID
const { text: sectionTitle, id: sectionId } = parseMarkdownHeadingId(headerText);

let sectionContent = ""

let sibling = header.nextElementSibling
Expand All @@ -266,13 +272,17 @@ function parseHTMLContent({
content: sectionContent,
version,
category,
path
path: headerTag === 'h1' ? path : `${removeTrailingSlash(path)}#${sectionId}`
})
})

return sections
}

function removeTrailingSlash(str: string): string {
return str.endsWith('/') ? str.slice(0, -1) : str;
}

function indexPath(outDir: string, version: string) {
return resolve(outDir, "orama-search-index-@VERSION@.json.gz".replace("@VERSION@", version))
}
Expand Down
25 changes: 14 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading