Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 429fc24

Browse files
committedMar 26, 2025
feat: introduce new plugin - generate-help-json
1 parent bd81436 commit 429fc24

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed
 

‎docusaurus.config.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ const config = {
4747
],
4848
plugins: [
4949
"plugin-image-zoom",
50+
[
51+
path.resolve(__dirname, 'plugins/generate-help-json'),
52+
{
53+
docsPath: 'docs-sqlacc/docs',
54+
outputPath: 'sqlacc'
55+
},
56+
],
5057
function aliasPlugin(context, options) {
5158
return {
5259
name: "alias-plugin",

‎package-lock.json

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"@easyops-cn/docusaurus-search-local": "^0.40.1",
2222
"@mdx-js/react": "^3.0.0",
2323
"clsx": "^2.0.0",
24+
"gray-matter": "^4.0.3",
2425
"plugin-image-zoom": "github:flexanalytics/plugin-image-zoom",
2526
"prism-react-renderer": "^2.3.0",
2627
"qrcode.react": "^4.2.0",

‎plugins/generate-help-json/index.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
const matter = require("gray-matter");
4+
5+
function getAllMarkdownFiles(dir, fileList = []) {
6+
const files = fs.readdirSync(dir);
7+
8+
files.forEach((file) => {
9+
const filePath = path.join(dir, file);
10+
if (fs.statSync(filePath).isDirectory()) {
11+
getAllMarkdownFiles(filePath, fileList);
12+
} else if (file.endsWith(".md") || file.endsWith(".mdx")) {
13+
fileList.push(filePath);
14+
}
15+
});
16+
17+
return fileList;
18+
}
19+
20+
module.exports = function (context, options) {
21+
return {
22+
name: "generate-help-json",
23+
24+
async postBuild({ siteConfig, routesPaths, outDir }) {
25+
let docsDir;
26+
docsDir = path.join(context.siteDir, options.docsPath);
27+
if (!fs.existsSync(docsDir)) {
28+
console.warn(`Warning: Docs directory not found: ${docsDir}`);
29+
return;
30+
}
31+
32+
const markdownFiles = getAllMarkdownFiles(docsDir);
33+
const helpData = [];
34+
const formNameMap = {};
35+
const duplicates = [];
36+
37+
for (const fullPath of markdownFiles) {
38+
const filePath = path.relative(docsDir, fullPath);
39+
const fileContent = fs.readFileSync(fullPath, "utf8");
40+
41+
try {
42+
const { data: frontMatter } = matter(fileContent);
43+
44+
if (frontMatter && frontMatter.form_name) {
45+
let permalink = frontMatter.slug;
46+
if (!permalink) {
47+
console.warn(`WARNING: No slug found for ${filePath}`);
48+
}
49+
50+
if (formNameMap[frontMatter.form_name]) {
51+
duplicates.push({
52+
form_name: frontMatter.form_name,
53+
existingFile: formNameMap[frontMatter.form_name],
54+
newFile: filePath,
55+
});
56+
57+
console.warn(`WARNING: Duplicate form_name "${frontMatter.form_name}" found in:`);
58+
console.warn(` 1. ${formNameMap[frontMatter.form_name]}`);
59+
console.warn(` 2. ${filePath}`);
60+
console.warn(` Using the first occurrence for help.json`);
61+
62+
continue;
63+
}
64+
65+
formNameMap[frontMatter.form_name] = filePath;
66+
67+
helpData.push({
68+
Name: frontMatter.form_name,
69+
Path: permalink,
70+
});
71+
}
72+
} catch (err) {
73+
console.warn(`Error processing ${filePath}:`, err);
74+
}
75+
}
76+
77+
let targetDir = outDir;
78+
const outputPath = path.join(outDir, options.outputPath);
79+
80+
if (fs.existsSync(outputPath) && fs.statSync(outputPath).isDirectory()) {
81+
targetDir = outputPath;
82+
}
83+
84+
const helpJsonPath = path.join(targetDir, "help.json");
85+
fs.writeFileSync(helpJsonPath, JSON.stringify(helpData, null, 2));
86+
87+
if (duplicates.length > 0) {
88+
const duplicatesPath = path.join(targetDir, "help-duplicates.json");
89+
fs.writeFileSync(duplicatesPath, JSON.stringify(duplicates, null, 2));
90+
console.warn(`Found ${duplicates.length} duplicate form_name entries. See help-duplicates.json for details.`);
91+
}
92+
93+
console.log(`Generated HELP.json with ${helpData.length} entries in ${targetDir}`);
94+
},
95+
};
96+
};

0 commit comments

Comments
 (0)
Please sign in to comment.