Skip to content

Commit

Permalink
[add] MDX index creator
Browse files Browse the repository at this point in the history
[migrate] update Badge document
  • Loading branch information
TechQuery committed Jan 26, 2024
1 parent ad274f0 commit 6b764df
Show file tree
Hide file tree
Showing 9 changed files with 594 additions and 108 deletions.
51 changes: 51 additions & 0 deletions create-index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { promises } from 'fs';
import { filter, traverse } from 'fs-match';
import { join } from 'path';
import { parse } from 'yaml';

const title = 'MDX index creator',
[folder] = process.argv.slice(2);

const MDXPattern = /\.(mdx?|markdown)$/,
sourceCode: string[] = [],
targetPath = join(folder, 'index.ts');

console.time(title);

(async () => {
for await (const filePath of filter(traverse(folder), MDXPattern)) {
const content = await promises.readFile(filePath, {
encoding: 'utf-8'
});
const [_, frontMatter] = content.match(/^---([\s\S]*?)\n---/m) || [];

const meta = JSON.stringify(parse(frontMatter), null, 4)
.slice(1, -1)
.trim();
const path = filePath.replaceAll('\\', '/').slice(folder.length);

sourceCode.push(`{
path: '${path.replace(MDXPattern, '').toLowerCase()}',
${meta},
component: lazy(loadMDX(() => import('./${path}')))
}`);
}

await promises.writeFile(
targetPath,
`// This file is created by "${title}" script,
// please don't edit it manually!
import { lazy } from 'web-cell';
import { loadMDX } from '../utility';
export default [
${sourceCode}
];
`
);

console.log(`[save] ${targetPath}`);
console.timeEnd(title);
})();
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,18 @@
"@types/lodash.groupby": "^4.6.9",
"@types/mdx": "^2.0.10",
"@types/node": "^18.19.9",
"fs-match": "^1.7.1",
"husky": "^9.0.5",
"lint-staged": "^15.2.0",
"parcel": "~2.11.0",
"parcel-transformer-mdx": "^0.3.1",
"postcss": "^8.4.33",
"postcss-modules": "^4.3.1",
"prettier": "^3.2.4",
"tsx": "^4.7.0",
"typescript": "~5.3.3",
"workbox-cli": "^7.0.0"
"workbox-cli": "^7.0.0",
"yaml": "^2.3.4"
},
"prettier": {
"singleQuote": true,
Expand All @@ -52,9 +55,9 @@
"*.{html,md,mdx,less,json,yml,ts,tsx}": "prettier --write"
},
"scripts": {
"prepare": "husky",
"prepare": "husky && npm run clean",
"test": "lint-staged",
"clean": "rm -rf .parcel-cache/ dist/",
"clean": "rm -rf .parcel-cache/ dist/ && tsx create-index source/document/",
"pack-tsx": "parcel build source/index.html --public-url .",
"build": "npm run clean && npm run pack-tsx && workbox generateSW",
"start": "npm run clean && parcel source/index.html --open"
Expand Down
Loading

1 comment on commit 6b764df

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for bootcell-document ready!

✅ Preview
https://bootcell-document-3qu8rkly8-techquery.vercel.app

Built with commit 6b764df.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.