diff --git a/.github/workflows/mdx-content.yml b/.github/workflows/mdx-content.yml new file mode 100644 index 0000000..e097c40 --- /dev/null +++ b/.github/workflows/mdx-content.yml @@ -0,0 +1,27 @@ +name: Deploy MDX Content + +on: + push: + branches: + - main + paths: + - "data/pages/**" + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Set up Node.js + uses: actions/setup-node@v3 + with: + node-version: "18" + + - name: Install dependencies + run: npm install + + - name: Run MDX build or processing script + run: npm run build-mdx diff --git a/package.json b/package.json index 76f3ec0..9f29b42 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,8 @@ "dev": "next dev", "build": "next build", "start": "next start", - "lint": "next lint" + "lint": "next lint", + "build-mdx": "node scripts/build-mdx.js" }, "dependencies": { "@hookform/resolvers": "^3.6.0", diff --git a/scripts/build-mdx.js b/scripts/build-mdx.js new file mode 100644 index 0000000..5f2821e --- /dev/null +++ b/scripts/build-mdx.js @@ -0,0 +1,50 @@ +const fs = require("fs"); +const path = require("path"); +const { compile } = require("@mdx-js/mdx"); +const { remark } = require("remark"); +const { toVFile } = require("to-vfile"); +const { format } = require("prettier"); +const rehypeStringify = require("rehype-stringify"); +const remarkParse = require("remark-parse"); +const rehypeParse = require("rehype-parse"); +const { unified } = require("unified"); + +const mdxDir = path.join(__dirname, "../data/pages"); +const outputDir = path.join(__dirname, "../data/mdx"); + +if (!fs.existsSync(outputDir)) { + fs.mkdirSync(outputDir, { recursive: true }); +} + +fs.readdir(mdxDir, (err, files) => { + if (err) throw err; + + files.forEach(async (file) => { + if (path.extname(file) === ".mdx") { + const filePath = path.join(mdxDir, file); + const outputFilePath = path.join( + outputDir, + path.basename(file, ".mdx") + ".html" + ); + + try { + const mdxContent = fs.readFileSync(filePath, "utf8"); + + const processedContent = await compile(mdxContent, { + remarkPlugins: [remarkParse], + rehypePlugins: [rehypeStringify], + }); + + const html = processedContent.toString(); + + // ig optional? + const formattedHtml = format(html, { parser: "html" }); + + fs.writeFileSync(outputFilePath, formattedHtml); + console.log(`Processed ${file} -> ${outputFilePath}`); + } catch (error) { + console.error(`Failed to process ${file}:`, error); + } + } + }); +});