Latest Threat Research:SANDWORM_MODE: Shai-Hulud-Style npm Worm Hijacks CI Workflows and Poisons AI Toolchains.Details
Socket
Book a DemoInstallSign in
Socket

toctoc

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

toctoc - npm Package Compare versions

Comparing version
0.2.4
to
0.3.0
+41
-11
bin/toctoc.js

@@ -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));
}
});
});
{
"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"

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