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.1.0
to
0.2.0
+21
TEST.md
# a
## Table of Contents
---
## a.a
## a.b
### a.b.a
### a.b.b
#### a.b.b.a
Money money money.
### a.b.c
## a.c
+13
-8

@@ -18,7 +18,7 @@ #!/usr/bin/env node

function generateToc(source, title) {
function generateToc(source, title, maxDepth) {
var toc = "";
var renderer = new marked.Renderer();
function addToToc(text, anchor, level) {
if (level === 1) return;
if (level === 1 || maxDepth && level > maxDepth + 1) return;
const spaces = new Array(level * 3 - 3).map(x => x).join(" ");

@@ -37,3 +37,3 @@ toc += `${spaces}- [${text}](#${anchor})\n`;

function transform(source, title) {
function transform(source, title, maxDepth) {
var tocPattern = new RegExp(`## ${title}([\\s\\S])+\\n---`);

@@ -44,3 +44,3 @@ if (!tocPattern.test(source)) {

}
var toc = generateToc(source, title);
var toc = generateToc(source, title, maxDepth);
return source.replace(tocPattern, `## ${title}\n\n${toc}\n---`);

@@ -53,4 +53,4 @@ }

function updateFile(file, title) {
fs.writeFileSync(file, transform(readFile(file), title));
function updateFile(file, title, maxDepth) {
fs.writeFileSync(file, transform(readFile(file), title, maxDepth));
}

@@ -73,2 +73,7 @@

})
.option("d", {
alias: "max-depth",
describe: "The max document tree depth to generate the TOC for (0 = all).",
default: 0,
})
.argv;

@@ -78,6 +83,6 @@

if (argv.write) {
updateFile(file, argv.title);
updateFile(file, argv.title, argv.maxDepth);
console.log("Updated " + file);
} else {
console.log(transform(readFile(file), argv.title));
console.log(transform(readFile(file), argv.title, argv.maxDepth));
}
{
"name": "toctoc",
"version": "0.1.0",
"version": "0.2.0",
"description": "Generates and maintain a table of contents of your README.md.",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -14,2 +14,3 @@ toctoc

- [Custom TOC heading](#custom-toc-heading)
- [Max TOC depth](#max-toc-depth)
- [For the adventurous](#for-the-adventurous)

@@ -70,2 +71,10 @@ - [License](#license)

### Max TOC depth
By default, the generated TOC will expose links to the deepest subsections of the document; to limit the maximum crawling depth, use the `-d` option:
```
$ toctoc -w README.md -d 2
```
### For the adventurous

@@ -77,3 +86,3 @@

"scripts": {
"prepublish": "./node_modules/.bin/toctoc README.md -w"
"prepublish": "./node_modules/.bin/toctoc README.md -d 2 -w"
},

@@ -80,0 +89,0 @@ ```