|
| 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