From 58f53205fa4fd89aabaf7a692027b1c59d2a04d6 Mon Sep 17 00:00:00 2001 From: Cristi Paval Date: Mon, 10 Oct 2022 23:23:58 +0300 Subject: [PATCH] Handle white spaces before and after h1 tags when html to markdown. --- __tests__/ExpensiMark-Markdown-test.js | 20 ++++++++++++-------- lib/ExpensiMark.js | 4 ++-- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/__tests__/ExpensiMark-Markdown-test.js b/__tests__/ExpensiMark-Markdown-test.js index 86aca2d3..2eaae923 100644 --- a/__tests__/ExpensiMark-Markdown-test.js +++ b/__tests__/ExpensiMark-Markdown-test.js @@ -470,13 +470,17 @@ test('Test heading1 markdown replacement', () => { test('Test html string to heading1 markdown', () => { const testString = '

This is a heading1 because starts with # followed by a space


' + '

This is also a heading1 because starts with # followed by a space


' - + 'this is not a heading1 because does not start with #
' - + '#This is not a heading1 either because starts with a # but has no space after it
' - + 'this line has no special therefore it cannot be a heading1'; - const resultString = '# This is a heading1 because starts with # followed by a space\n' - + '# This is also a heading1 because starts with # followed by a space\n' - + 'this is not a heading1 because does not start with #\n' - + '#This is not a heading1 either because starts with a # but has no space after it\n' - + 'this line has no special therefore it cannot be a heading1'; + + 'this is not a heading1 because does not start with #
' + + '#This is not a heading1 either because starts with a # but has no space after it
' + + 'this line has no special therefore it cannot be a heading1
' + + 'this line has a

heading1

in the middle of the line'; + const resultString = '\n# This is a heading1 because starts with # followed by a space\n' + + '\n# This is also a heading1 because starts with # followed by a space\n' + + 'this is not a *heading1* because does not start with #\n' + + '#This is not a _heading1_ either because starts with a # but has no space after it\n' + + 'this line has no special therefore it cannot be a heading1\n' + + 'this line has a\n' + + '# heading1\n' + + 'in the middle of the line'; expect(parser.htmlToMarkdown(testString)).toBe(resultString); }); diff --git a/lib/ExpensiMark.js b/lib/ExpensiMark.js index e1942cd7..7a50013b 100644 --- a/lib/ExpensiMark.js +++ b/lib/ExpensiMark.js @@ -230,8 +230,8 @@ export default class ExpensiMark { }, { name: 'heading1', - regex: /<(h1)(?:"[^"]*"|'[^']*'|[^'">])*>(.*?)<\/\1>(?![^<]*(<\/pre>|<\/code>))/gi, - replacement: '# $2', + regex:/\s*<(h1)(?:"[^"]*"|'[^']*'|[^'">])*>(.*?)<\/\1>(?![^<]*(<\/pre>|<\/code>))\s*/gi, + replacement: '\n# $2\n', }, { name: 'italic',