Skip to content

Commit

Permalink
Merge pull request #502 from eh2077/improve-html-h1-to-markdown-line-…
Browse files Browse the repository at this point in the history
…break-handling

fix line break handling for HTML h1 to markdown conversion
  • Loading branch information
danieldoglas authored Feb 16, 2023
2 parents 4f61e55 + 99440cb commit bf099f7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
34 changes: 32 additions & 2 deletions __tests__/ExpensiMark-Markdown-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<h1>This is a heading1</h1>';
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 = '<h1>This is a <strong>heading1</strong></h1>';
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 = '<h1>heading1</h1> in the beginning of the line';
const resultString = '# heading1\n'
+ 'in the beginning of the line';
expect(parser.htmlToMarkdown(testString)).toBe(resultString);
});

Expand All @@ -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<h1>heading1</h1>';
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<h1>heading1</h1>this is the second heading1<h1>heading1</h1>';
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 = '<h1>heading1</h1><h1>heading2</h1>';
const resultString = '# heading1'
+ '\n# heading2';
expect(parser.htmlToMarkdown(testString)).toBe(resultString);
});

test('Test link with multiline text do not loses markdown', () => {
const testString = '<a href="https://google.com/">multiline\ntext</a>';
const resultString = '[multiline\ntext](https://google.com/)';
Expand Down
2 changes: 1 addition & 1 deletion lib/ExpensiMark.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit bf099f7

Please sign in to comment.