Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

add codemod to convert meta export to yaml frontmatter (#20585)

Charis85f128eae27b
diff --git a/apps/docs/.gitignore b/apps/docs/.gitignore
index b18b47afe4b..155cb496ae3 100644
--- a/apps/docs/.gitignore
+++ b/apps/docs/.gitignore
@@ -23,3 +23,7 @@ yarn-error.log*
**/*/generated/**/*
!**/*/generated/.gitkeep
!**/*/generated/**/.gitkeep
+
+# Temporary to avoid storing a lot of duplicate Markdown
+# before finalizing the codemod
+/content
diff --git a/apps/docs/package.json b/apps/docs/package.json
index 86ad2449f31..3f497954d6d 100644
--- a/apps/docs/package.json
+++ b/apps/docs/package.json
@@ -14,6 +14,7 @@
"build:sitemap": "node ./internals/generate-sitemap.mjs",
"embeddings": "tsx scripts/search/generate-embeddings.ts",
"embeddings:refresh": "npm run embeddings -- --refresh",
+ "codemod:frontmatter": "node ./scripts/codemod/mdx-meta.mjs && prettier --write \"content/**/*.mdx\"",
"postbuild": "node ./internals/generate-sitemap.mjs"
},
"dependencies": {
@@ -75,6 +76,7 @@
"@types/node": "^18.11.9",
"@types/react": "^18.2.24",
"@types/unist": "^2.0.6",
+ "acorn": "^8.11.3",
"config": "*",
"dotenv": "^16.0.3",
"ejs": "^3.1.8",
diff --git a/apps/docs/scripts/codemod/mdx-meta.mjs b/apps/docs/scripts/codemod/mdx-meta.mjs
new file mode 100644
index 00000000000..8e00d256604
--- /dev/null
+++ b/apps/docs/scripts/codemod/mdx-meta.mjs
@@ -0,0 +1,78 @@
+// @ts-check
+
+/**
+ * Copies MDX files from the `pages` directory to the `content` directory,
+ * replacing frontmatter in `meta` with YAML frontmatter.
+ */
+
+import { parse } from 'acorn'
+import { fromMarkdown } from 'mdast-util-from-markdown'
+import { mdxFromMarkdown } from 'mdast-util-mdx'
+import { mdxjs } from 'micromark-extension-mdxjs'
+import { mkdir, readdir, readFile, writeFile } from 'node:fs/promises'
+import { dirname, extname, join } from 'node:path'
+import { fileURLToPath } from 'node:url'
+
+const __dirname = dirname(fileURLToPath(import.meta.url))
+const ROOT_DIR = join(__dirname, '../..')
+const PAGES_DIR = join(ROOT_DIR, 'pages/guides')
+const CONTENT_DIR = join(ROOT_DIR, 'content/guides')
+
+function convertToYaml(properties) {
+ let result = '---\n'
+
+ for (const property of properties) {
+ const key = property.key.name
+ const value = property.value.value
+ result += `${key}: "${value}"\n`
+ }
+
+ result += '---\n\n'
+ return result
+}
+
+async function main() {
+ try {
+ const origDirContents = await readdir(PAGES_DIR, { recursive: true })
+ const origMdxFiles = origDirContents.filter((filename) => extname(filename) === '.mdx')
+
+ await Promise.all(
+ origMdxFiles.map(async (filename) => {
+ const content = await readFile(join(PAGES_DIR, filename), 'utf-8')
+
+ const mdxTree = fromMarkdown(content, {
+ extensions: [mdxjs()],
+ mdastExtensions: [mdxFromMarkdown()],
+ })
+
+ const meta = mdxTree.children.find(
+ (node) => node.type === 'mdxjsEsm' && node.value.trim().startsWith('export const meta')
+ )
+ if (!meta) return
+
+ const linesIncl = [meta.position.start.line, meta.position.end.line]
+
+ const parsedMeta = parse(meta.value, { ecmaVersion: 2020, sourceType: 'module' })
+ const yamlString = convertToYaml(
+ parsedMeta.body[0].declaration.declarations[0].init.properties
+ )
+
+ const lines = content.split('\n')
+ lines.splice(
+ meta.position.start.line - 1,
+ meta.position.end.line - meta.position.start.line + 1
+ )
+ const contentWithoutMeta = lines.join('\n')
+ const contentWithFrontmatter = yamlString + contentWithoutMeta
+
+ const destinationPath = join(CONTENT_DIR, filename)
+ await mkdir(dirname(destinationPath), { recursive: true })
+ writeFile(destinationPath, contentWithFrontmatter)
+ })
+ )
+ } catch (err) {
+ console.error(err)
+ }
+}
+
+main()
diff --git a/package-lock.json b/package-lock.json
index 6aa90954208..c616c6b7e41 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -543,6 +543,7 @@
"@types/node": "^18.11.9",
"@types/react": "^18.2.24",
"@types/unist": "^2.0.6",
+ "acorn": "^8.11.3",
"config": "*",
"dotenv": "^16.0.3",
"ejs": "^3.1.8",
@@ -12862,8 +12863,9 @@
}
},
"node_modules/acorn": {
- "version": "8.10.0",
- "license": "MIT",
+ "version": "8.11.3",
+ "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.11.3.tgz",
+ "integrity": "sha512-Y9rRfJG5jcKOE0CLisYbojUjIrIEE7AGMzA/Sm4BslANhbS+cDMpgBdcPT91oJ7OuJ9hYJBx59RjbhxVnrF8Xg==",
"bin": {
"acorn": "bin/acorn"
},