diff --git a/package.json b/package.json index 42b06c8cff15..fca96790be0f 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,8 @@ "serve": "docusaurus serve", "write-translations": "docusaurus write-translations", "write-heading-ids": "docusaurus write-heading-ids", - "lint": "standard --fix" + "lint": "standard --fix", + "snippet-check": "node scripts/remark-named-snippets/snippet.js" }, "dependencies": { "@cmfcmf/docusaurus-search-local": "^0.11.0", diff --git a/scripts/remark-named-snippets/snippet.js b/scripts/remark-named-snippets/snippet.js index 514fff4444b5..5508fb292f90 100644 --- a/scripts/remark-named-snippets/snippet.js +++ b/scripts/remark-named-snippets/snippet.js @@ -19,6 +19,7 @@ function constructSnippetMap (dir) { `A snippet named ${name} has already been defined elsewhere` ) } + delete snippet.name // Remove duplicate filename to clean up stdout snippetMap[name] = snippet } @@ -153,4 +154,38 @@ function sanitizeText (text) { .trim() } +/** + * Organize parsed snippets by source filename. + * If provided, input filenames will filter this output. + * + * Note that this is what is run if this file is invoked by Node. + * An alias `yarn snippet-check` is defined in `package.json` for convenience. + */ +function main() { + const snippets = parseDirectory(".") + const targetFiles = process.argv.slice(2) + + let out = {} + for (const snippet of snippets) { + // If no explicit args are provided, default to all snippets + // Else, ensure that the snippet's source file was requested by the user + const file = snippet.file + if (targetFiles.length > 0 && !(targetFiles.includes(file))) { + continue + } + if (!(file in out)) { + out[file] = [] + } + delete snippet.file // Remove duplicate filename to clean up stdout + out[file].push(snippet) + } + console.log(out) +} + + +if (require.main === module) { + main(); +} + + module.exports = constructSnippetMap