Skip to content

Commit

Permalink
Merge pull request #522 from eh2077/allow-header-in-quote
Browse files Browse the repository at this point in the history
allow heading inside a quote
  • Loading branch information
pecanoro authored May 3, 2023
2 parents 3cdaa94 + e838894 commit afe4697
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
14 changes: 14 additions & 0 deletions __tests__/ExpensiMark-HTML-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -710,3 +710,17 @@ test('Test for link with no content', () => {
const resultString = '[ ](<a href="https://www.link.com" target="_blank" rel="noreferrer noopener">www.link.com</a>)';
expect(parser.replace(testString)).toBe(resultString);
});

test('Test quotes markdown replacement with heading inside', () => {
let testString = '> # heading';
expect(parser.replace(testString)).toBe('<blockquote><h1>heading</h1></blockquote>');

testString = '> # heading\n> test';
expect(parser.replace(testString)).toBe('<blockquote><h1>heading</h1>test</blockquote>');

testString = '> test\n> # heading\n> test';
expect(parser.replace(testString)).toBe('<blockquote>test<br /><h1>heading</h1>test</blockquote>');

testString = '> # heading A\n> # heading B';
expect(parser.replace(testString)).toBe('<blockquote><h1>heading A</h1><h1>heading B</h1></blockquote>');
});
14 changes: 14 additions & 0 deletions __tests__/ExpensiMark-HTMLToText-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,17 @@ test('Test replacement on mixed html', () => {

expect(parser.htmlToText(html)).toBe(text);
});

test('Test new line replacement on blockquote with heading inside', () => {
let testString = '<blockquote><h1>heading</h1></blockquote>';
expect(parser.htmlToText(testString)).toBe('heading');

testString = '<blockquote><h1>heading</h1>test</blockquote>';
expect(parser.htmlToText(testString)).toBe('heading\ntest');

testString = '<blockquote>test<br /><h1>heading</h1>test</blockquote>';
expect(parser.htmlToText(testString)).toBe('test\n\nheading\ntest');

testString = '<blockquote><h1>heading A</h1><h1>heading B</h1></blockquote>';
expect(parser.htmlToText(testString)).toBe('heading A\n\nheading B');
});
14 changes: 14 additions & 0 deletions __tests__/ExpensiMark-Markdown-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -641,3 +641,17 @@ test('Test codeFence backticks occupying a separate line while not introducing r
testInput = '<pre><br /><br />#&#32;test<br /><br /></pre>';
expect(parser.htmlToMarkdown(testInput)).toBe('```\n\n\n# test\n\n```');
});

test('Test blockquote with h1 inside', () => {
let testString = '<blockquote><h1>heading</h1></blockquote>';
expect(parser.htmlToMarkdown(testString)).toBe('\n> # heading\n');

testString = '<blockquote><h1>heading</h1>test</blockquote>';
expect(parser.htmlToMarkdown(testString)).toBe('\n> # heading\n> test\n');

testString = '<blockquote>test<br /><h1>heading</h1>test</blockquote>';
expect(parser.htmlToMarkdown(testString)).toBe('\n> test\n> # heading\n> test\n');

testString = '<blockquote><h1>heading A</h1><h1>heading B</h1></blockquote>';
expect(parser.htmlToMarkdown(testString)).toBe('\n> # heading A\n> # heading B\n');
});
21 changes: 19 additions & 2 deletions lib/ExpensiMark.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,10 @@ export default class ExpensiMark {
);
return this.modifyTextForQuote(regex, textToProcess, replacement);
},
replacement: g1 => `<blockquote>${g1}</blockquote>`,
replacement: (g1) => {
const replacedText = this.replace(g1, {filterRules: ['heading1'], shouldEscapeText: false});
return `<blockquote>${replacedText}</blockquote>`;
},
},
{
name: 'heading1',
Expand Down Expand Up @@ -261,7 +264,11 @@ export default class ExpensiMark {
name: 'quote',
regex: /\n?<(blockquote|q)(?:"[^"]*"|'[^']*'|[^'">])*>([\s\S]*?)<\/\1>(?![^<]*(<\/pre>|<\/code>))/gi,
replacement: (match, g1, g2) => {
const resultString = g2.trim().split('\n').map(m => `> ${m}`).join('\n');
const resultString = g2.replace(/(\[block\])+/g, '\n')
.trim()
.split('\n')
.map(m => `> ${m}`)
.join('\n');
return `\n${resultString}\n`;
},
},
Expand Down Expand Up @@ -299,6 +306,16 @@ export default class ExpensiMark {
regex: /<br[^>]*>/gi,
replacement: '\n',
},
{
name: 'blockquoteWrapHeadingOpen',
regex: /<blockquote><h1>/gi,
replacement: '<blockquote>',
},
{
name: 'blockquoteWrapHeadingClose',
regex: /<\/h1><\/blockquote>/gi,
replacement: '</blockquote>',
},
{
name: 'blockElementOpen',
regex: /(.|\s)<(blockquote|h1|pre)>/gi,
Expand Down

0 comments on commit afe4697

Please sign in to comment.