Skip to content

Commit

Permalink
Harden stripping
Browse files Browse the repository at this point in the history
  • Loading branch information
johansatge committed Feb 18, 2024
1 parent 82dd58e commit eae1cb8
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
2 changes: 2 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('#', '')
Expand Down
3 changes: 3 additions & 0 deletions test/headings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <em style="color: black">with HTML</em>', 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', () => {
Expand Down Expand Up @@ -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 <em style="color: black">with HTML</em>|Title 1 level 2 <em style="color: black">with HTML</em>]]
- [[#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)
})
Expand All @@ -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 <em style="color: black">with HTML</em>
- 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)
})
Expand Down

0 comments on commit eae1cb8

Please sign in to comment.