From 374f31056a76728aec2e0ead2076efd654d31861 Mon Sep 17 00:00:00 2001 From: Nico Rehwaldt Date: Tue, 29 Jun 2021 21:18:06 +0200 Subject: [PATCH 1/2] fix: do not parse Markdown inside HTML tags Closes https://github.com/developit/snarkdown/issues/98 --- src/index.js | 2 +- test/index.js | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 3938c5c..8a909b2 100644 --- a/src/index.js +++ b/src/index.js @@ -24,7 +24,7 @@ function encodeAttr(str) { /** Parse Markdown into an HTML String. */ export default function parse(md, prevLinks) { - let tokenizer = /((?:^|\n+)(?:\n---+|\* \*(?: \*)+)\n)|(?:^``` *(\w*)\n([\s\S]*?)\n```$)|((?:(?:^|\n+)(?:\t| {2,}).+)+\n*)|((?:(?:^|\n)([>*+-]|\d+\.)\s+.*)+)|(?:!\[([^\]]*?)\]\(([^)]+?)\))|(\[)|(\](?:\(([^)]+?)\))?)|(?:(?:^|\n+)([^\s].*)\n(-{3,}|={3,})(?:\n+|$))|(?:(?:^|\n+)(#{1,6})\s*(.+)(?:\n+|$))|(?:`([^`].*?)`)|( \n\n*|\n{2,}|__|\*\*|[_*]|~~)/gm, + let tokenizer = /((?:^|\n+)(?:\n---+|\* \*(?: \*)+)\n)|(?:^``` *(\w*)\n([\s\S]*?)\n```$)|((?:(?:^|\n+)(?:\t| {2,}).+)+\n*)|((?:(?:^|\n)([>*+-]|\d+\.)\s+.*)+)|(?:!\[([^\]]*?)\]\(([^)]+?)\))|(\[)|(\](?:\(([^)]+?)\))?)|(?:(?:^|\n+)([^\s].*)\n(-{3,}|={3,})(?:\n+|$))|(?:(?:^|\n+)(#{1,6})\s*(.+)(?:\n+|$))|(?:`([^`].*?)`)|( \n\n*|\n{2,}|__|\*\*|[_*]|~~)|<[^>]+>/gm, context = [], out = '', links = prevLinks || {}, diff --git a/test/index.js b/test/index.js index 5262b16..4c45013 100644 --- a/test/index.js +++ b/test/index.js @@ -151,6 +151,13 @@ describe('snarkdown()', () => { }); }); + describe('html', () => { + it('should not parse inside tags', () => { + expect(snarkdown('
')).to.equal('
'); + expect(snarkdown('a')).to.equal('a'); + }); + }); + describe('edge cases', () => { it('should close unclosed tags', () => { expect(snarkdown('*foo')).to.equal('foo'); From 5d39b3e009a46b89c473b88d0e6358f91026a7a2 Mon Sep 17 00:00:00 2001 From: Nico Rehwaldt Date: Tue, 29 Jun 2021 21:22:55 +0200 Subject: [PATCH 2/2] test: verify parsing outside HTML tag --- test/index.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/index.js b/test/index.js index 4c45013..d4aad91 100644 --- a/test/index.js +++ b/test/index.js @@ -156,6 +156,10 @@ describe('snarkdown()', () => { expect(snarkdown('
')).to.equal('
'); expect(snarkdown('a')).to.equal('a'); }); + + it('should parse outside HTML tags', () => { + expect(snarkdown('**a**')).to.equal('a'); + }); }); describe('edge cases', () => {