From 666d9126a1fdb2b24ab0edd0b5b264c642dda245 Mon Sep 17 00:00:00 2001 From: Adam Sobotka Date: Sat, 22 Oct 2022 16:40:32 +0200 Subject: [PATCH 1/2] add ids to the headings Some MD processors support a very useful feature that allows for anchor navigation of processed markdown. I added a simple additional processing that adds this to the snarkdown. --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 3938c5c..66de958 100644 --- a/src/index.js +++ b/src/index.js @@ -91,7 +91,7 @@ export default function parse(md, prevLinks) { // Headings: else if (token[12] || token[14]) { t = 'h' + (token[14] ? token[14].length : (token[13]>'=' ? 1 : 2)); - chunk = '<'+t+'>' + parse(token[12] || token[15], links) + ''; + chunk = '<'+t+' id="'+ parse(token[12] || token[15]).replace(" ","-").toLowerCase() +'">' + parse(token[12] || token[15], links) + ''; } // `code`: else if (token[16]) { From 7f237189ffc050d710cc772411136c90ccfb8ff2 Mon Sep 17 00:00:00 2001 From: Adam Sobotka Date: Sat, 22 Oct 2022 17:04:37 +0200 Subject: [PATCH 2/2] found a small bug in case there is more than one space in header name --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 66de958..7ecbbc2 100644 --- a/src/index.js +++ b/src/index.js @@ -91,7 +91,7 @@ export default function parse(md, prevLinks) { // Headings: else if (token[12] || token[14]) { t = 'h' + (token[14] ? token[14].length : (token[13]>'=' ? 1 : 2)); - chunk = '<'+t+' id="'+ parse(token[12] || token[15]).replace(" ","-").toLowerCase() +'">' + parse(token[12] || token[15], links) + ''; + chunk = '<'+t+' id="'+ parse(token[12] || token[15]).replaceAll(" ","-").toLowerCase() +'">' + parse(token[12] || token[15], links) + ''; } // `code`: else if (token[16]) {