Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix line break handling for HTML h1 to markdown conversion #502

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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