Skip to content

Commit

Permalink
Handle white spaces before and after h1 tags when html to markdown.
Browse files Browse the repository at this point in the history
  • Loading branch information
cristipaval committed Oct 10, 2022
1 parent 56af2fe commit 58f5320
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
20 changes: 12 additions & 8 deletions __tests__/ExpensiMark-Markdown-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,13 +470,17 @@ test('Test heading1 markdown replacement', () => {
test('Test html string to heading1 markdown', () => {
const testString = '<h1>This is a heading1 because starts with # followed by a space</h1><br />'
+ '<h1>This is also a heading1 because starts with # followed by a space</h1><br />'
+ 'this is not a heading1 because does not start with #<br />'
+ '#This is not a heading1 either because starts with a # but has no space after it<br />'
+ '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 <strong>heading1</strong> because does not start with #<br />'
+ '#This is not a <em>heading1</em> either because starts with a # but has no space after it<br />'
+ 'this line has no special therefore it cannot be a heading1<br />'
+ 'this line has a <h1>heading1</h1> 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);
});
4 changes: 2 additions & 2 deletions lib/ExpensiMark.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 58f5320

Please sign in to comment.