From eae1cb8d532d6fbcc1643131a63ae7b7e3943a04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20Satg=C3=A9?= Date: Sun, 18 Feb 2024 12:39:01 +0100 Subject: [PATCH] Harden stripping --- main.js | 2 ++ test/headings.test.js | 3 +++ 2 files changed, 5 insertions(+) diff --git a/main.js b/main.js index e1694e9..acb22bb 100644 --- a/main.js +++ b/main.js @@ -175,6 +175,8 @@ function getMarkdownHeading(heading, options) { // Example: "# Some [[file.md|heading]]" must be translated to "[[#Some file.md heading|Some heading]]" text = text.replace(/\[\[([^\]]+)\|([^\]]+)\]\]/g, isForLink ? '$1 $2' : '$2') text = text.replace(/\[\[([^\]]+)\]\]/g, '$1') // Strip [[link]] format + // Replace malformed links & reserved wikilinks chars + text = text.replaceAll('[[', '').replaceAll('| ', isForLink ? '' : '- ').replaceAll('|', isForLink ? ' ' : '-') return text } const stripTags = (text) => text.replaceAll('#', '') diff --git a/test/headings.test.js b/test/headings.test.js index 2044b1e..84e9585 100644 --- a/test/headings.test.js +++ b/test/headings.test.js @@ -25,6 +25,7 @@ const testHeadingsWithSpecialChars = [ { heading: 'Title 1 `level 1` {with special chars}, **bold**, _italic_, #a-tag, ==highlighted== and ~~strikethrough~~ text', level: 1 }, { heading: 'Title 1 level 2 with HTML', level: 2 }, { heading: 'Title 1 level 2 [[wikilink1]] [[wikilink2|wikitext2]] [mdlink](https://mdurl)', level: 2 }, + { heading: 'Title 1 level 2 [[malformedlink a pi|pe | and [other chars]', level: 2 }, ] describe('Headings', () => { @@ -120,6 +121,7 @@ describe('Headings', () => { - [[#Title 1 \`level 1\` {with special chars}, **bold**, _italic_, a-tag, ==highlighted== and ~~strikethrough~~ text|Title 1 level 1 {with special chars}, bold, italic, #a-tag, highlighted and strikethrough text]] - [[#Title 1 level 2 with HTML|Title 1 level 2 with HTML]] - [[#Title 1 level 2 wikilink1 wikilink2 wikitext2 [mdlink](https://mdurl)|Title 1 level 2 wikilink1 wikitext2 mdlink]] + - [[#Title 1 level 2 malformedlink a pi pe and [other chars]|Title 1 level 2 malformedlink a pi-pe - and [other chars]]] `) expect(md).toEqual(expectedMd) }) @@ -132,6 +134,7 @@ describe('Headings', () => { - Title 1 \`level 1\` {with special chars}, **bold**, _italic_, #a-tag, ==highlighted== and ~~strikethrough~~ text - Title 1 level 2 with HTML - Title 1 level 2 [[wikilink1]] [[wikilink2|wikitext2]] [mdlink](https://mdurl) + - Title 1 level 2 [[malformedlink a pi|pe | and [other chars] `) expect(md).toEqual(expectedMd) })