Skip to content

Commit

Permalink
[DOCS] Add yarn snippet-check command (#6351)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdkini authored Nov 14, 2022
1 parent 57cd559 commit 8b358d2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
35 changes: 35 additions & 0 deletions scripts/remark-named-snippets/snippet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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

0 comments on commit 8b358d2

Please sign in to comment.