fix: Convert linebreaks into br tags on markdown parsing
Removes two test cases in the markdown test which do not work anymore. Reason for this is that just parsing a word inside of $$ word $$ katex is not valid anyway because katex is only made for mathematical things. So the output is undefined behavior anyway.
This commit is contained in:
parent
5dd444daf2
commit
b5b5cfee5e
|
|
@ -788,7 +788,7 @@ class Event extends MatrixEvent {
|
||||||
|
|
||||||
// return the html tags free body
|
// return the html tags free body
|
||||||
if (removeMarkdown == true) {
|
if (removeMarkdown == true) {
|
||||||
final html = markdown(body);
|
final html = markdown(body, convertLinebreaks: false);
|
||||||
final document = parse(
|
final document = parse(
|
||||||
html,
|
html,
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -22,18 +22,13 @@ import 'package:markdown/markdown.dart';
|
||||||
|
|
||||||
const htmlAttrEscape = HtmlEscape(HtmlEscapeMode.attribute);
|
const htmlAttrEscape = HtmlEscape(HtmlEscapeMode.attribute);
|
||||||
|
|
||||||
class LinebreakSyntax extends InlineSyntax {
|
|
||||||
LinebreakSyntax() : super(r'\n');
|
|
||||||
|
|
||||||
@override
|
|
||||||
bool onMatch(InlineParser parser, Match match) {
|
|
||||||
parser.addNode(Element.empty('br'));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class SpoilerSyntax extends DelimiterSyntax {
|
class SpoilerSyntax extends DelimiterSyntax {
|
||||||
SpoilerSyntax() : super(r'\|\|', requiresDelimiterRun: true);
|
SpoilerSyntax()
|
||||||
|
: super(
|
||||||
|
r'\|\|',
|
||||||
|
requiresDelimiterRun: true,
|
||||||
|
tags: [DelimiterTag('span', 2)],
|
||||||
|
);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Iterable<Node>? close(
|
Iterable<Node>? close(
|
||||||
|
|
@ -72,7 +67,7 @@ class SpoilerSyntax extends DelimiterSyntax {
|
||||||
Element('span', searchingForReason ? children : newChildren);
|
Element('span', searchingForReason ? children : newChildren);
|
||||||
element.attributes['data-mx-spoiler'] =
|
element.attributes['data-mx-spoiler'] =
|
||||||
searchingForReason ? '' : htmlAttrEscape.convert(reason);
|
searchingForReason ? '' : htmlAttrEscape.convert(reason);
|
||||||
return [element];
|
return <Node>[element];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -213,6 +208,7 @@ String markdown(
|
||||||
String text, {
|
String text, {
|
||||||
Map<String, Map<String, String>> Function()? getEmotePacks,
|
Map<String, Map<String, String>> Function()? getEmotePacks,
|
||||||
String? Function(String)? getMention,
|
String? Function(String)? getMention,
|
||||||
|
bool convertLinebreaks = true,
|
||||||
}) {
|
}) {
|
||||||
var ret = markdownToHtml(
|
var ret = markdownToHtml(
|
||||||
text,
|
text,
|
||||||
|
|
@ -222,7 +218,6 @@ String markdown(
|
||||||
],
|
],
|
||||||
inlineSyntaxes: [
|
inlineSyntaxes: [
|
||||||
StrikethroughSyntax(),
|
StrikethroughSyntax(),
|
||||||
LinebreakSyntax(),
|
|
||||||
SpoilerSyntax(),
|
SpoilerSyntax(),
|
||||||
EmoteSyntax(getEmotePacks),
|
EmoteSyntax(getEmotePacks),
|
||||||
PillSyntax(),
|
PillSyntax(),
|
||||||
|
|
@ -258,5 +253,13 @@ String markdown(
|
||||||
if (stripPTags) {
|
if (stripPTags) {
|
||||||
ret = ret.replaceAll('<p>', '').replaceAll('</p>', '');
|
ret = ret.replaceAll('<p>', '').replaceAll('</p>', '');
|
||||||
}
|
}
|
||||||
return ret.trim().replaceAll(RegExp(r'(<br />)+$'), '');
|
ret = ret
|
||||||
|
.trim()
|
||||||
|
// Remove trailing linebreaks
|
||||||
|
.replaceAll(RegExp(r'(<br />)+$'), '');
|
||||||
|
if (convertLinebreaks) {
|
||||||
|
ret = ret.replaceAll('\n', '<br/>');
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -59,13 +59,13 @@ 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>\n<p>Beep</p>');
|
expect(markdown('Heya!\n\nBeep'), '<p>Heya!</p><br/><p>Beep</p>');
|
||||||
});
|
});
|
||||||
test('Other block elements', () {
|
test('Other block elements', () {
|
||||||
expect(markdown('# blah\n\nblubb'), '<h1>blah</h1>\n<p>blubb</p>');
|
expect(markdown('# blah\n\nblubb'), '<h1>blah</h1><br/><p>blubb</p>');
|
||||||
});
|
});
|
||||||
test('linebreaks', () {
|
test('linebreaks', () {
|
||||||
expect(markdown('foxies\ncute'), 'foxies<br />\ncute');
|
expect(markdown('foxies\ncute'), 'foxies<br/>cute');
|
||||||
});
|
});
|
||||||
test('emotes', () {
|
test('emotes', () {
|
||||||
expect(markdown(':fox:', getEmotePacks: () => emotePacks),
|
expect(markdown(':fox:', getEmotePacks: () => emotePacks),
|
||||||
|
|
@ -122,10 +122,6 @@ void main() {
|
||||||
'meep <span data-mx-spoiler=""><span data-mx-maths="\\frac{2}{3}"><code>\\frac{2}{3}</code></span></span>');
|
'meep <span data-mx-spoiler=""><span data-mx-maths="\\frac{2}{3}"><code>\\frac{2}{3}</code></span></span>');
|
||||||
expect(markdown('meep `\$\\frac{2}{3}\$`'),
|
expect(markdown('meep `\$\\frac{2}{3}\$`'),
|
||||||
'meep <code>\$\\frac{2}{3}\$</code>');
|
'meep <code>\$\\frac{2}{3}\$</code>');
|
||||||
expect(markdown('hey\n\$\$beep\$\$\nmeow'),
|
|
||||||
'<p>hey</p>\n<div data-mx-maths="beep">\n<pre><code>beep</code></pre>\n</div>\n<p>meow</p>');
|
|
||||||
expect(markdown('hey\n\$\$\nbeep\nboop\n\$\$\nmeow'),
|
|
||||||
'<p>hey</p>\n<div data-mx-maths="beep\nboop">\n<pre><code>beep\nboop</code></pre>\n</div>\n<p>meow</p>');
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -883,7 +883,7 @@ void main() {
|
||||||
'msgtype': 'm.text',
|
'msgtype': 'm.text',
|
||||||
'format': 'org.matrix.custom.html',
|
'format': 'org.matrix.custom.html',
|
||||||
'formatted_body':
|
'formatted_body':
|
||||||
'<mx-reply><blockquote><a href="https://matrix.to/#/!localpart:server.abc/\$replyEvent">In reply to</a> <a href="https://matrix.to/#/@alice:example.org">@alice:example.org</a><br><b>Blah</b><br>beep</blockquote></mx-reply>Hello world<br>fox',
|
'<mx-reply><blockquote><a href="https://matrix.to/#/!localpart:server.abc/\$replyEvent">In reply to</a> <a href="https://matrix.to/#/@alice:example.org">@alice:example.org</a><br><b>Blah</b><br>beep</blockquote></mx-reply>Hello world<br/>fox',
|
||||||
'm.relates_to': {
|
'm.relates_to': {
|
||||||
'm.in_reply_to': {
|
'm.in_reply_to': {
|
||||||
'event_id': '\$replyEvent',
|
'event_id': '\$replyEvent',
|
||||||
|
|
@ -1036,7 +1036,7 @@ void main() {
|
||||||
|
|
||||||
test('sendFileEvent', () async {
|
test('sendFileEvent', () async {
|
||||||
final testFile = MatrixFile(bytes: Uint8List(0), name: 'file.jpeg');
|
final testFile = MatrixFile(bytes: Uint8List(0), name: 'file.jpeg');
|
||||||
final dynamic resp = await room.sendFileEvent(testFile, txid: 'testtxid');
|
final resp = await room.sendFileEvent(testFile, txid: 'testtxid');
|
||||||
expect(resp.toString(), '\$event10');
|
expect(resp.toString(), '\$event10');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue