+41
-11
@@ -6,2 +6,4 @@ #!/usr/bin/env node | ||
| var marked = require("marked"); | ||
| var glob = require("glob"); | ||
| var isGlob = require("is-glob"); | ||
@@ -40,6 +42,6 @@ function removeTags(str) { | ||
| function transform(source, title, maxDepth) { | ||
| function transform(source, title, maxDepth, soft) { | ||
| var tocPattern = new RegExp(`## ${title}([\\s\\S])+\\n---`); | ||
| if (!tocPattern.test(source)) { | ||
| console.error("Couldn't find expected TOC pattern: " + tocPattern); | ||
| if (!tocPattern.test(source) && !soft) { | ||
| console.error(`Couldn't find expected TOC pattern: ${tocPattern}`); | ||
| process.exit(1); | ||
@@ -55,4 +57,4 @@ } | ||
| function updateFile(file, title, maxDepth) { | ||
| fs.writeFileSync(file, transform(readFile(file), title, maxDepth)); | ||
| function updateFile(file, title, maxDepth, soft) { | ||
| fs.writeFileSync(file, transform(readFile(file), title, maxDepth, soft)); | ||
| } | ||
@@ -80,10 +82,38 @@ | ||
| }) | ||
| .option("s", { | ||
| alias: "soft", | ||
| describe: "Soft mode prevent quit on error when TOC is not found.", | ||
| default: false, | ||
| }) | ||
| .option("e", { | ||
| alias: "extension", | ||
| describe: "Default file extension used when scanning a directory.", | ||
| default: ".md", | ||
| }) | ||
| .argv; | ||
| var file = argv._[0]; | ||
| if (argv.write) { | ||
| updateFile(file, argv.title, argv.maxDepth); | ||
| console.log("Updated " + file); | ||
| } else { | ||
| console.log(transform(readFile(file), argv.title, argv.maxDepth)); | ||
| var pattern = argv._[0]; | ||
| var options = { | ||
| nonull: true, | ||
| }; | ||
| try { | ||
| if (!isGlob(pattern) && fs.lstatSync(pattern).isDirectory()) { | ||
| pattern = `${pattern}/**/*${argv.extension}`; | ||
| } | ||
| } catch (e) { | ||
| console.error(`Couldn't find: ${pattern}`); | ||
| process.exit(1); | ||
| } | ||
| glob(pattern, options, (err, files) => { | ||
| files.forEach((file) => { | ||
| if (argv.write) { | ||
| updateFile(file, argv.title, argv.maxDepth, argv.soft); | ||
| console.log("Updated " + file); | ||
| } else { | ||
| console.log(transform(readFile(file), argv.title, argv.maxDepth, argv.soft)); | ||
| } | ||
| }); | ||
| }); |
+3
-1
| { | ||
| "name": "toctoc", | ||
| "version": "0.2.4", | ||
| "version": "0.3.0", | ||
| "description": "Generates and maintain a table of contents of your README.md.", | ||
@@ -31,2 +31,4 @@ "main": "index.js", | ||
| "dependencies": { | ||
| "glob": "^7.1.2", | ||
| "is-glob": "^4.0.0", | ||
| "marked": "^0.3.9", | ||
@@ -33,0 +35,0 @@ "yargs": "^4.3.1" |
+35
-2
@@ -15,2 +15,4 @@ toctoc | ||
| - [Max TOC depth](#max-toc-depth) | ||
| - [Soft TOC](#soft-toc) | ||
| - [Directory](#directory) | ||
| - [For the adventurous](#for-the-adventurous) | ||
@@ -56,3 +58,3 @@ - [License](#license) | ||
| ``` | ||
| $ toctoc -w README.md | ||
| $ toctoc README.md -w | ||
| ``` | ||
@@ -62,2 +64,15 @@ | ||
| ``` | ||
| $ toctoc docs/**/*.md -w | ||
| ``` | ||
| You can use a [glob](http://pubs.opengroup.org/onlinepubs/9699919799/functions/glob.html) pattern or a directory to match multiple files at once. | ||
| ``` | ||
| $ toctoc doc -w -e MD | ||
| ``` | ||
| If you use a directory, the file extensions searched is `.md`, if you wish to use a different file extension for markdown, use `-e` option. | ||
| ### Custom TOC heading | ||
@@ -78,5 +93,23 @@ | ||
| ``` | ||
| $ toctoc -w README.md -d 2 | ||
| $ toctoc README.md -w -d 2 | ||
| ``` | ||
| ### Soft TOC | ||
| By default, it will fail with an error if the targeted file(s) do not have any TOC. To remove this limitation, use `-s` option: | ||
| ``` | ||
| $ toctoc docs/**/*.md -w -s | ||
| ``` | ||
| ### Directory | ||
| By default, toctoc will use a `.md` extension if used with a directory. You can customise the extension to be founds by passing `-e` option. | ||
| ``` | ||
| $ toctoc docs -w -s -e .MD | ||
| ``` | ||
| ### For the adventurous | ||
@@ -83,0 +116,0 @@ |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
6567
31.39%102
34.21%124
36.26%4
100%2
100%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added