Merge pull request #1609 from famedly/krille/linebreaks-markdown
fix: Do only convert linebreaks to br tags in p blocks
This commit is contained in:
commit
c6e8b16647
|
|
@ -250,29 +250,33 @@ String markdown(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (stripPTags) {
|
|
||||||
ret = ret.replaceAll('<p>', '').replaceAll('</p>', '');
|
|
||||||
}
|
|
||||||
ret = ret
|
ret = ret
|
||||||
.trim()
|
.trim()
|
||||||
// Remove trailing linebreaks
|
// Remove trailing linebreaks
|
||||||
.replaceAll(RegExp(r'(<br />)+$'), '');
|
.replaceAll(RegExp(r'(<br />)+$'), '');
|
||||||
if (convertLinebreaks) {
|
if (convertLinebreaks) {
|
||||||
// Only convert linebreaks which are not in <pre> blocks
|
// Only convert linebreaks which are not in <pre> blocks
|
||||||
ret = ret.convertLinebreaksToBr();
|
ret = ret.convertLinebreaksToBr('p');
|
||||||
|
// Delete other linebreaks except for pre blocks:
|
||||||
|
ret = ret.convertLinebreaksToBr('pre', exclude: true, replaceWith: '');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (stripPTags) {
|
||||||
|
ret = ret.replaceAll('<p>', '').replaceAll('</p>', '');
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
extension on String {
|
extension on String {
|
||||||
String convertLinebreaksToBr() {
|
String convertLinebreaksToBr(String tagName,
|
||||||
final parts = split('pre>');
|
{bool exclude = false, String replaceWith = '<br/>'}) {
|
||||||
var convertLinebreaks = true;
|
final parts = split('$tagName>');
|
||||||
|
var convertLinebreaks = exclude;
|
||||||
for (var i = 0; i < parts.length; i++) {
|
for (var i = 0; i < parts.length; i++) {
|
||||||
if (convertLinebreaks) parts[i] = parts[i].replaceAll('\n', '<br/>');
|
if (convertLinebreaks) parts[i] = parts[i].replaceAll('\n', replaceWith);
|
||||||
convertLinebreaks = !convertLinebreaks;
|
convertLinebreaks = !convertLinebreaks;
|
||||||
}
|
}
|
||||||
return parts.join('pre>');
|
return parts.join('$tagName>');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -59,14 +59,20 @@ void main() {
|
||||||
'Snape killed <span data-mx-spoiler="">Dumbledoor <strong>bold</strong></span>');
|
'Snape killed <span data-mx-spoiler="">Dumbledoor <strong>bold</strong></span>');
|
||||||
});
|
});
|
||||||
test('multiple paragraphs', () {
|
test('multiple paragraphs', () {
|
||||||
expect(markdown('Heya!\n\nBeep'), '<p>Heya!</p><br/><p>Beep</p>');
|
expect(markdown('Heya!\n\nBeep'), '<p>Heya!</p><p>Beep</p>');
|
||||||
});
|
});
|
||||||
test('Other block elements', () {
|
test('Other block elements', () {
|
||||||
expect(markdown('# blah\n\nblubb'), '<h1>blah</h1><br/><p>blubb</p>');
|
expect(markdown('# blah\n\nblubb'), '<h1>blah</h1><p>blubb</p>');
|
||||||
});
|
});
|
||||||
test('linebreaks', () {
|
test('linebreaks', () {
|
||||||
expect(markdown('foxies\ncute'), 'foxies<br/>cute');
|
expect(markdown('foxies\ncute'), 'foxies<br/>cute');
|
||||||
});
|
});
|
||||||
|
test('lists', () {
|
||||||
|
expect(
|
||||||
|
markdown('So we have:\n- foxies\n- cats\n- dogs'),
|
||||||
|
'<p>So we have:</p><ul><li>foxies</li><li>cats</li><li>dogs</li></ul>',
|
||||||
|
);
|
||||||
|
});
|
||||||
test('emotes', () {
|
test('emotes', () {
|
||||||
expect(markdown(':fox:', getEmotePacks: () => emotePacks),
|
expect(markdown(':fox:', getEmotePacks: () => emotePacks),
|
||||||
'<img data-mx-emoticon="" src="mxc://roomfox" alt=":fox:" title=":fox:" height="32" vertical-align="middle" />');
|
'<img data-mx-emoticon="" src="mxc://roomfox" alt=":fox:" title=":fox:" height="32" vertical-align="middle" />');
|
||||||
|
|
@ -137,7 +143,7 @@ void main() {
|
||||||
'The first \n codeblock\n```dart\nvoid main(){\nprint(something);\n}\n```\nAnd the second code block\n```js\nmeow\nmeow\n```',
|
'The first \n codeblock\n```dart\nvoid main(){\nprint(something);\n}\n```\nAnd the second code block\n```js\nmeow\nmeow\n```',
|
||||||
convertLinebreaks: true,
|
convertLinebreaks: true,
|
||||||
),
|
),
|
||||||
'<p>The first<br/>codeblock</p><br/><pre><code class="language-dart">void main(){\nprint(something);\n}\n</code></pre><br/><p>And the second code block</p><br/><pre><code class="language-js">meow\nmeow\n</code></pre>',
|
'<p>The first<br/>codeblock</p><pre><code class="language-dart">void main(){\nprint(something);\n}\n</code></pre><p>And the second code block</p><pre><code class="language-js">meow\nmeow\n</code></pre>',
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue