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)
})