From 99440cbdc6a60704d8842c504e6eb2d42f5369a8 Mon Sep 17 00:00:00 2001 From: Eric Han Date: Wed, 15 Feb 2023 17:23:23 +0800 Subject: [PATCH] fix line break handling for HTML h1 to markdown conversion --- __tests__/ExpensiMark-Markdown-test.js | 34 ++++++++++++++++++++++++-- lib/ExpensiMark.js | 2 +- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/__tests__/ExpensiMark-Markdown-test.js b/__tests__/ExpensiMark-Markdown-test.js index 8361c2c2..c17d126d 100644 --- a/__tests__/ExpensiMark-Markdown-test.js +++ b/__tests__/ExpensiMark-Markdown-test.js @@ -513,13 +513,20 @@ test('Test heading1 markdown when # is in the middle of the line', () => { test('Test html string to heading1 markdown', () => { const testString = '

This is a heading1

'; - const resultString = '\n# This is a heading1\n'; + const resultString = '# This is a heading1'; expect(parser.htmlToMarkdown(testString)).toBe(resultString); }); test('Test html to heading1 markdown when there are other tags inside h1 tag', () => { const testString = '

This is a heading1

'; - const resultString = '\n# This is a *heading1*\n'; + const resultString = '# This is a *heading1*'; + expect(parser.htmlToMarkdown(testString)).toBe(resultString); +}); + +test('Test html to heading1 markdown when h1 tag is in the beginning of the line', () => { + const testString = '

heading1

in the beginning of the line'; + const resultString = '# heading1\n' + + 'in the beginning of the line'; expect(parser.htmlToMarkdown(testString)).toBe(resultString); }); @@ -531,6 +538,29 @@ test('Test html to heading1 markdown when h1 tags are in the middle of the line' expect(parser.htmlToMarkdown(testString)).toBe(resultString); }); +test('Test html to heading1 markdown when h1 tag is in the end of the line', () => { + const testString = 'this line has an h1 in the end of the line

heading1

'; + const resultString = 'this line has an h1 in the end of the line' + + '\n# heading1'; + expect(parser.htmlToMarkdown(testString)).toBe(resultString); +}); + +test('Test html to heading1 markdown when there are 2 h1 tags in the line', () => { + const testString = 'this is the first heading1

heading1

this is the second heading1

heading1

'; + const resultString = 'this is the first heading1' + + '\n# heading1' + + '\nthis is the second heading1' + + '\n# heading1'; + expect(parser.htmlToMarkdown(testString)).toBe(resultString); +}); + +test('Test html to heading1 markdown when there are adjacent h1 tags in the line', () => { + const testString = '

heading1

heading2

'; + const resultString = '# heading1' + + '\n# heading2'; + expect(parser.htmlToMarkdown(testString)).toBe(resultString); +}); + test('Test link with multiline text do not loses markdown', () => { const testString = 'multiline\ntext'; const resultString = '[multiline\ntext](https://google.com/)'; diff --git a/lib/ExpensiMark.js b/lib/ExpensiMark.js index bac4b81c..c3decc65 100644 --- a/lib/ExpensiMark.js +++ b/lib/ExpensiMark.js @@ -230,7 +230,7 @@ export default class ExpensiMark { { name: 'heading1', regex: /\s*<(h1)(?:"[^"]*"|'[^']*'|[^'">])*>(.*?)<\/\1>(?![^<]*(<\/pre>|<\/code>))\s*/gi, - replacement: '\n# $2\n', + replacement: '[block]# $2[block]', }, { name: 'listItem',