diff --git a/services/www/astro.config.ts b/services/www/astro.config.ts index ba0d0ad2ae8..99b93d84ce6 100644 --- a/services/www/astro.config.ts +++ b/services/www/astro.config.ts @@ -3,6 +3,7 @@ import mdx from "@astrojs/mdx" import { unified } from "@astrojs/markdown-remark" import { defineConfig } from "astro/config" import remarkDocsLinks from "./src/docs/remark-links" +import rehypeTableScroll from "./src/docs/rehype-table-scroll" const base = "/v2/" @@ -13,7 +14,7 @@ export default defineConfig({ adapter: cloudflare({ imageService: "passthrough" }), integrations: [mdx()], markdown: { - processor: unified({ remarkPlugins: [[remarkDocsLinks, { base }]] }), + processor: unified({ remarkPlugins: [[remarkDocsLinks, { base }]], rehypePlugins: [rehypeTableScroll] }), shikiConfig: { themes: { light: "github-light", diff --git a/services/www/src/docs/components/DocsHeader.astro b/services/www/src/docs/components/DocsHeader.astro index 99d790d35aa..8881d19a33a 100644 --- a/services/www/src/docs/components/DocsHeader.astro +++ b/services/www/src/docs/components/DocsHeader.astro @@ -48,7 +48,7 @@ const searchEnabled = import.meta.env.PROD >
diff --git a/services/www/src/docs/rehype-table-scroll.ts b/services/www/src/docs/rehype-table-scroll.ts new file mode 100644 index 00000000000..bdfc501983e --- /dev/null +++ b/services/www/src/docs/rehype-table-scroll.ts @@ -0,0 +1,41 @@ +interface HastNode { + type: string + tagName?: string + properties?: Record + attributes?: { name?: string; value?: unknown }[] + children?: HastNode[] +} + +// Wide Markdown tables would otherwise widen the whole page on small screens. +export default function rehypeTableScroll() { + return (tree: HastNode) => { + const visit = (node: HastNode) => { + if (isScroller(node)) return + node.children = node.children?.map((child) => + child.type === "element" && child.tagName === "table" + ? { + type: "element", + tagName: "div", + properties: { className: ["docs-table-scroll"], tabIndex: 0 }, + children: [child], + } + : child, + ) + node.children?.forEach(visit) + } + + visit(tree) + } +} + +// Some pages already wrap tables by hand in an MDX `
`. +function isScroller(node: HastNode) { + const className = node.properties?.className + if (Array.isArray(className) && className.includes("docs-table-scroll")) return true + return !!node.attributes?.some( + (attribute) => + (attribute.name === "class" || attribute.name === "className") && + typeof attribute.value === "string" && + attribute.value.split(/\s+/).includes("docs-table-scroll"), + ) +}