From 598fa9cd89ff758ad24eaf05ae27bc4d0c0909c0 Mon Sep 17 00:00:00 2001 From: Leo Balter Date: Mon, 16 Apr 2018 14:31:38 -0400 Subject: [PATCH] Prepend the filename to each summary link --- package.json | 1 + scripts/summary.js | 27 ++++++++++++++++++++------- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 780ae36d..14589cfd 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "common-tags": "^1.4.0", "glob": "^7.1.2", "highlight.js": "^9.12.0", + "markdown-link": "^0.1.1", "markdown-toc": "^1.1.0", "minimist": "^1.2.0", "remarkable": "^1.7.1" diff --git a/scripts/summary.js b/scripts/summary.js index 82ed7f28..1c4f365b 100755 --- a/scripts/summary.js +++ b/scripts/summary.js @@ -7,6 +7,7 @@ const path = require("path"); // Third party objects const glob = require("glob"); const toc = require("markdown-toc"); +const mdlink = require("markdown-link", "mdlink"); const argv = require("minimist")(process.argv.slice(2), { string: ["_"] }); @@ -30,18 +31,30 @@ function main() { const writable = fs.createWriteStream(`${folder}/summary.md`, { flags: 'w' }); const filePattern = /^\w*-\d+\.[mM][dD]$/; - const notes = results.filter(file => { - return filePattern.test(path.basename(file)); - }); - notes.forEach(file => { + results.forEach(file => { + if (!filePattern.test(path.basename(file))) { + return; + } const contents = fs.readFileSync(file, "utf8"); - const rendered = toc(contents, { + const fileToc = toc(contents, { filter(str, ele) { return ele.lvl < 3; }, - append: "\n" - }).content; + append: "\n", + linkify({ lvl }, title, slug) { + // prepends the filename to the link + let content = mdlink(title, `${path.basename(file)}#${slug}`); + return { + content, + + // lvl is required to find the highest level links and order it + lvl + };; + } + }); + + const rendered = fileToc.content; writable.write(rendered); });