diff --git a/astro.config.mjs b/astro.config.mjs index 7490e417d..28feda494 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -9,6 +9,7 @@ import rehypeSlug from "rehype-slug"; import remarkDirective from "remark-directive"; import remarkMath from "remark-math"; import { customAsidePlugin } from "./src/lib/aside/customAsidePlugin"; +import { apiDocsOnly } from "./src/lib/markdown/apiDocsOnly"; import { normalizeMath } from "./src/lib/markdown/normalizeMath"; import { mermaid } from "./src/utils/mermaid"; import { redirects } from "./src/utils/redirects"; @@ -18,9 +19,9 @@ export default defineConfig({ markdown: { remarkPlugins: [remarkMath, normalizeMath, remarkDirective, mermaid, customAsidePlugin], rehypePlugins: [ - rehypeSlug, + apiDocsOnly(rehypeSlug), [ - rehypeAutolinkHeadings, + apiDocsOnly(rehypeAutolinkHeadings), { behavior: "append", properties: { diff --git a/src/layouts/docs-layout.astro b/src/layouts/docs-layout.astro index 86da527ea..a9ef487ae 100644 --- a/src/layouts/docs-layout.astro +++ b/src/layouts/docs-layout.astro @@ -19,6 +19,8 @@ const { title, description, image } = Astro.props; const pages = await getCollection("Docs"); let nav = generateDocsNav(pages); + +const isApiDocs = Astro.url.pathname.startsWith("/docs/api-documentation/"); --- @@ -53,11 +55,17 @@ let nav = generateDocsNav(pages); - - + `} + + ) +} - + // On initial page load with a hash, the browser's native scroll-to-anchor + // sometimes lands before late layout (font loading, hydration of fixed + // panels) settles — leaving the target behind the sticky header. Re-run the + // scroll after \`load\` and on any hashchange. + function scrollToHash() { + const hash = window.location.hash; + if (!hash || hash.length < 2) return; + let id; + try { + id = decodeURIComponent(hash.slice(1)); + } catch (e) { + id = hash.slice(1); + } + const el = document.getElementById(id); + if (el) el.scrollIntoView({ block: "start" }); + } + if (document.readyState === "complete") { + scrollToHash(); + } else { + window.addEventListener("load", scrollToHash, { once: true }); + } + window.addEventListener("hashchange", scrollToHash); + `} + + ) +} diff --git a/src/lib/markdown/apiDocsOnly.ts b/src/lib/markdown/apiDocsOnly.ts new file mode 100644 index 000000000..fdd6187dc --- /dev/null +++ b/src/lib/markdown/apiDocsOnly.ts @@ -0,0 +1,16 @@ +import type { Plugin } from "unified"; + +const API_DOCS_PATH_FRAGMENT = "/content/Docs/api-documentation/"; + +export function apiDocsOnly

>(plugin: P): P { + const wrapped = function (this: unknown, ...args: any[]) { + const transformer = (plugin as any).apply(this, args); + if (typeof transformer !== "function") return transformer; + return function (tree: any, file: any) { + const path = String(file?.history?.[0] ?? file?.path ?? "").replace(/\\/g, "/"); + if (!path.includes(API_DOCS_PATH_FRAGMENT)) return; + return transformer(tree, file); + }; + }; + return wrapped as unknown as P; +}