diff --git a/apps/docs/app/guides/database/extensions/wrappers/[[...slug]]/page.tsx b/apps/docs/app/guides/database/extensions/wrappers/[[...slug]]/page.tsx index 16a6e7ce50e8d..5e2f9537d34db 100644 --- a/apps/docs/app/guides/database/extensions/wrappers/[[...slug]]/page.tsx +++ b/apps/docs/app/guides/database/extensions/wrappers/[[...slug]]/page.tsx @@ -1,5 +1,7 @@ import matter from 'gray-matter' -import { type Heading } from 'mdast' +import { type Heading, type Node } from 'mdast' +import { gfm } from 'micromark-extension-gfm' +import { gfmFromMarkdown, gfmToMarkdown } from 'mdast-util-gfm' import { fromMarkdown } from 'mdast-util-from-markdown' import { toMarkdown } from 'mdast-util-to-markdown' import { type SerializeOptions } from 'next-mdx-remote/dist/types' @@ -7,6 +9,7 @@ import { readFile } from 'node:fs/promises' import { join, relative } from 'node:path' import rehypeSlug from 'rehype-slug' import emoji from 'remark-emoji' +import { visit } from 'unist-util-visit' import { genGuideMeta, genGuidesStaticParams } from '~/features/docs/GuidesMdx.utils' import { GuideTemplate, newEditLink } from '~/features/docs/GuidesMdx.template' @@ -16,7 +19,6 @@ import { UrlTransformFunction, linkTransform } from '~/lib/mdx/plugins/rehypeLin import remarkMkDocsAdmonition from '~/lib/mdx/plugins/remarkAdmonition' import { removeTitle } from '~/lib/mdx/plugins/remarkRemoveTitle' import remarkPyMdownTabs from '~/lib/mdx/plugins/remarkTabs' -import remarkGfm from 'remark-gfm' // We fetch these docs at build time from an external repo const org = 'supabase' @@ -185,21 +187,36 @@ const getContent = async (params: Params) => { const { content: contentWithoutFrontmatter } = matter(rawContent) - // This is the more robust way of doing it, but problems with the rewritten - // Markdown and handling of tables this way, so saving it for later. - // - // const mdxTree = fromMarkdown(contentWithoutFrontmatter) - // const maybeH1 = mdxTree.children[0] - // if (maybeH1 && maybeH1.type === 'heading' && (maybeH1 as Heading).depth === 1) { - // mdxTree.children.shift() - // } - // content = toMarkdown(mdxTree) + const mdxTree = fromMarkdown(contentWithoutFrontmatter, 'utf-8', { + extensions: [gfm()], + mdastExtensions: [gfmFromMarkdown()], + }) - content = contentWithoutFrontmatter - if (meta.title) { - const h1Regex = new RegExp(`(?:^|\n)# ${meta.title}\n+`) - content = content.replace(h1Regex, '') + // Remove redundant headings + const maybeH1 = mdxTree.children[0] + if (maybeH1 && maybeH1.type === 'heading' && (maybeH1 as Heading).depth === 1) { + mdxTree.children.shift() } + + // MDX should not be used in any of these files (they also need to be + // mkdocs-compatible) to literally display any angle brackets surrounding + // capitalized words. + visit( + mdxTree, + (node: Node) => + node.children?.some((child) => child.type === 'html' && child.value.match(/^<[A-Z]/)), + (node: Node) => { + node.children.forEach((child: Node) => { + if (child.type === 'html' && child.value.match(/^<[A-Z]/)) { + child.type = 'text' + } + }) + } + ) + + content = toMarkdown(mdxTree, { + extensions: [gfmToMarkdown()], + }) } return { diff --git a/apps/docs/features/docs/Reference.utils.ts b/apps/docs/features/docs/Reference.utils.ts index e8d9d0464750f..76b1903a32eca 100644 --- a/apps/docs/features/docs/Reference.utils.ts +++ b/apps/docs/features/docs/Reference.utils.ts @@ -1,3 +1,4 @@ +import { type Text } from 'mdast' import { fromMarkdown } from 'mdast-util-from-markdown' import { toMarkdown } from 'mdast-util-to-markdown' import { mdxFromMarkdown, mdxToMarkdown } from 'mdast-util-mdx' @@ -175,7 +176,7 @@ export function normalizeMarkdown(markdownUnescaped: string): string { mdastExtensions: [mdxFromMarkdown()], }) - visit(mdxTree, 'text', (node) => { + visit(mdxTree, 'text', (node: Text) => { node.value = node.value.replace(/\n/g, ' ') }) diff --git a/apps/docs/package.json b/apps/docs/package.json index 7f3e7dd242e8a..d879f0ec59abd 100644 --- a/apps/docs/package.json +++ b/apps/docs/package.json @@ -56,6 +56,7 @@ "markdown-toc": "^1.2.0", "mdast": "^3.0.0", "mdast-util-from-markdown": "^1.2.0", + "mdast-util-gfm": "^2.0.2", "mdast-util-mdx": "^2.0.0", "mdast-util-to-markdown": "^1.5.0", "mdast-util-to-string": "^3.1.1", diff --git a/package-lock.json b/package-lock.json index ddd04e8442d85..43d3eb2104154 100644 --- a/package-lock.json +++ b/package-lock.json @@ -881,6 +881,7 @@ "markdown-toc": "^1.2.0", "mdast": "^3.0.0", "mdast-util-from-markdown": "^1.2.0", + "mdast-util-gfm": "^2.0.2", "mdast-util-mdx": "^2.0.0", "mdast-util-to-markdown": "^1.5.0", "mdast-util-to-string": "^3.1.1", @@ -1121,6 +1122,17 @@ "node": ">=6" } }, + "apps/docs/node_modules/escape-string-regexp": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-5.0.0.tgz", + "integrity": "sha512-/veY75JbMK4j1yjvuUxuVsiS/hr/4iHs9FTT6cgTexxdE0Ly/glccBAkloH/DofkjRbZU3bnoj38mOmhkZ0lHw==", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "apps/docs/node_modules/execa": { "version": "8.0.1", "resolved": "https://registry.npmjs.org/execa/-/execa-8.0.1.tgz", @@ -1236,6 +1248,21 @@ "@jridgewell/sourcemap-codec": "^1.4.15" } }, + "apps/docs/node_modules/mdast-util-find-and-replace": { + "version": "2.2.2", + "resolved": "https://registry.npmjs.org/mdast-util-find-and-replace/-/mdast-util-find-and-replace-2.2.2.tgz", + "integrity": "sha512-MTtdFRz/eMDHXzeK6W3dO7mXUlF82Gom4y0oOgvHhh/HXZAGvIQDUvQ0SuUx+j2tv44b8xTHOm8K/9OoRFnXKw==", + "dependencies": { + "@types/mdast": "^3.0.0", + "escape-string-regexp": "^5.0.0", + "unist-util-is": "^5.0.0", + "unist-util-visit-parents": "^5.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, "apps/docs/node_modules/mdast-util-from-markdown": { "version": "1.3.1", "license": "MIT", @@ -1258,6 +1285,113 @@ "url": "https://opencollective.com/unified" } }, + "apps/docs/node_modules/mdast-util-gfm": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/mdast-util-gfm/-/mdast-util-gfm-2.0.2.tgz", + "integrity": "sha512-qvZ608nBppZ4icQlhQQIAdc6S3Ffj9RGmzwUKUWuEICFnd1LVkN3EktF7ZHAgfcEdvZB5owU9tQgt99e2TlLjg==", + "dependencies": { + "mdast-util-from-markdown": "^1.0.0", + "mdast-util-gfm-autolink-literal": "^1.0.0", + "mdast-util-gfm-footnote": "^1.0.0", + "mdast-util-gfm-strikethrough": "^1.0.0", + "mdast-util-gfm-table": "^1.0.0", + "mdast-util-gfm-task-list-item": "^1.0.0", + "mdast-util-to-markdown": "^1.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, + "apps/docs/node_modules/mdast-util-gfm-autolink-literal": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/mdast-util-gfm-autolink-literal/-/mdast-util-gfm-autolink-literal-1.0.3.tgz", + "integrity": "sha512-My8KJ57FYEy2W2LyNom4n3E7hKTuQk/0SES0u16tjA9Z3oFkF4RrC/hPAPgjlSpezsOvI8ObcXcElo92wn5IGA==", + "dependencies": { + "@types/mdast": "^3.0.0", + "ccount": "^2.0.0", + "mdast-util-find-and-replace": "^2.0.0", + "micromark-util-character": "^1.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, + "apps/docs/node_modules/mdast-util-gfm-footnote": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/mdast-util-gfm-footnote/-/mdast-util-gfm-footnote-1.0.2.tgz", + "integrity": "sha512-56D19KOGbE00uKVj3sgIykpwKL179QsVFwx/DCW0u/0+URsryacI4MAdNJl0dh+u2PSsD9FtxPFbHCzJ78qJFQ==", + "dependencies": { + "@types/mdast": "^3.0.0", + "mdast-util-to-markdown": "^1.3.0", + "micromark-util-normalize-identifier": "^1.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, + "apps/docs/node_modules/mdast-util-gfm-strikethrough": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/mdast-util-gfm-strikethrough/-/mdast-util-gfm-strikethrough-1.0.3.tgz", + "integrity": "sha512-DAPhYzTYrRcXdMjUtUjKvW9z/FNAMTdU0ORyMcbmkwYNbKocDpdk+PX1L1dQgOID/+vVs1uBQ7ElrBQfZ0cuiQ==", + "dependencies": { + "@types/mdast": "^3.0.0", + "mdast-util-to-markdown": "^1.3.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, + "apps/docs/node_modules/mdast-util-gfm-table": { + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/mdast-util-gfm-table/-/mdast-util-gfm-table-1.0.7.tgz", + "integrity": "sha512-jjcpmNnQvrmN5Vx7y7lEc2iIOEytYv7rTvu+MeyAsSHTASGCCRA79Igg2uKssgOs1i1po8s3plW0sTu1wkkLGg==", + "dependencies": { + "@types/mdast": "^3.0.0", + "markdown-table": "^3.0.0", + "mdast-util-from-markdown": "^1.0.0", + "mdast-util-to-markdown": "^1.3.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, + "apps/docs/node_modules/mdast-util-gfm-task-list-item": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/mdast-util-gfm-task-list-item/-/mdast-util-gfm-task-list-item-1.0.2.tgz", + "integrity": "sha512-PFTA1gzfp1B1UaiJVyhJZA1rm0+Tzn690frc/L8vNX1Jop4STZgOE6bxUhnzdVSB+vm2GU1tIsuQcA9bxTQpMQ==", + "dependencies": { + "@types/mdast": "^3.0.0", + "mdast-util-to-markdown": "^1.3.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, + "apps/docs/node_modules/micromark-util-character": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/micromark-util-character/-/micromark-util-character-1.2.0.tgz", + "integrity": "sha512-lXraTwcX3yH/vMDaFWCQJP1uIszLVebzUa3ZHdrgxr7KEU/9mL4mVgCpGbyhvNLNlauROiNUq7WN5u7ndbY6xg==", + "funding": [ + { + "type": "GitHub Sponsors", + "url": "https://github.com/sponsors/unifiedjs" + }, + { + "type": "OpenCollective", + "url": "https://opencollective.com/unified" + } + ], + "dependencies": { + "micromark-util-symbol": "^1.0.0", + "micromark-util-types": "^1.0.0" + } + }, "apps/docs/node_modules/micromark-util-types": { "version": "1.1.0", "funding": [ @@ -1405,6 +1539,31 @@ "node": ">=14.0.0" } }, + "apps/docs/node_modules/unist-util-is": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/unist-util-is/-/unist-util-is-5.2.1.tgz", + "integrity": "sha512-u9njyyfEh43npf1M+yGKDGVPbY/JWEemg5nH05ncKPfi+kBbKBJoTdsogMu33uhytuLlv9y0O7GH7fEdwLdLQw==", + "dependencies": { + "@types/unist": "^2.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, + "apps/docs/node_modules/unist-util-visit-parents": { + "version": "5.1.3", + "resolved": "https://registry.npmjs.org/unist-util-visit-parents/-/unist-util-visit-parents-5.1.3.tgz", + "integrity": "sha512-x6+y8g7wWMyQhL1iZfhIPhDAs7Xwbn9nRosDXl7qoPTSCy0yNxnKc+hWokFifWQIDGi154rdUqKvbCa4+1kLhg==", + "dependencies": { + "@types/unist": "^2.0.0", + "unist-util-is": "^5.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, "apps/docs/node_modules/vite-node": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/vite-node/-/vite-node-2.0.3.tgz",