|
| 1 | +const path = require('path') |
| 2 | +const getMarkdownData = require('../utils/get-markdown-data') |
| 3 | +const createFile = require('../utils/create-file') |
| 4 | +const getRuleContent = require('./taskforce-rule-page/get-rule-content') |
| 5 | +const getDefinitionContent = require('./taskforce-rule-page/get-definition-content') |
| 6 | +const program = require('commander') |
| 7 | + |
| 8 | +const rulesDirDefault = path.resolve(__dirname, '../node_modules/act-rules-community/_rules') |
| 9 | +const glossaryDirDefault = path.resolve(__dirname, '../node_modules/act-rules-community/pages/glossary') |
| 10 | + |
| 11 | +program |
| 12 | + .option('-i, --ruleIds <id_list>', 'comma separated list of IDs', val => val.split(',')) |
| 13 | + .option('-o, --outDir <dirname>', 'Path to output dir') |
| 14 | + .option('-r, --rulesDir <dirname>', 'Path to _rules directory') |
| 15 | + .option('-g, --glossaryDir <dirname>', 'Path to glossary directory') |
| 16 | + .parse(process.argv) |
| 17 | + |
| 18 | +taskforceMarkdown(program) |
| 19 | + .then(() => { |
| 20 | + console.log('Created taskforce markdown files') |
| 21 | + process.exit() |
| 22 | + }) |
| 23 | + .catch(e => { |
| 24 | + console.error(e) |
| 25 | + process.exit(1) |
| 26 | + }) |
| 27 | + |
| 28 | +async function taskforceMarkdown({ |
| 29 | + rulesDir = rulesDirDefault, |
| 30 | + glossaryDir = glossaryDirDefault, |
| 31 | + ruleIds = [], |
| 32 | + outDir = './content/', |
| 33 | +}) { |
| 34 | + const rulesData = getMarkdownData(rulesDir) |
| 35 | + const glossary = getMarkdownData(glossaryDir) |
| 36 | + const glossaryFiles = new Set() |
| 37 | + |
| 38 | + for (const ruleData of rulesData) { |
| 39 | + if (ruleIds.length && !ruleIds.includes(ruleData.frontmatter.id)) { |
| 40 | + continue |
| 41 | + } |
| 42 | + const { filepath, content } = buildTfRuleFile(ruleData, glossary) |
| 43 | + await createFile(path.resolve(outDir, filepath), content) |
| 44 | + |
| 45 | + const definitions = parseDefinitions(content) |
| 46 | + definitions.forEach(dfn => glossaryFiles.add(dfn)) |
| 47 | + } |
| 48 | + |
| 49 | + for (const definition of glossaryFiles) { |
| 50 | + const { filepath, content } = buildTfDefinitionFile(definition, glossary) |
| 51 | + await createFile(path.resolve(outDir, filepath), content) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +function buildTfRuleFile(ruleData, glossary) { |
| 56 | + return { |
| 57 | + filepath: ruleData.filename, |
| 58 | + content: getRuleContent(ruleData, glossary), |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +function buildTfDefinitionFile(definitionKey, glossary) { |
| 63 | + return { |
| 64 | + filepath: `glossary/${definitionKey}.md`, |
| 65 | + content: getDefinitionContent(definitionKey, glossary), |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +function parseDefinitions(content) { |
| 70 | + const definitionKeys = [] |
| 71 | + const matches = content.match(/{%[^%]*%}/g) |
| 72 | + matches.forEach(str => { |
| 73 | + const match = str.match(/{%\s+include_relative\s+glossary\/([^.]+).md\s+%}/i) |
| 74 | + if (match) { |
| 75 | + definitionKeys.push(match[1]) |
| 76 | + } |
| 77 | + }) |
| 78 | + return definitionKeys |
| 79 | +} |
0 commit comments