add pill parsing to markdown

This commit is contained in:
Sorunome 2020-05-15 21:00:10 +02:00
parent 98d2f8d6bb
commit 9d1c7f16a5
No known key found for this signature in database
GPG key ID: B19471D07FC9BE9C
2 changed files with 18 additions and 1 deletions

View file

@ -73,12 +73,24 @@ class EmoteSyntax extends InlineSyntax {
} }
} }
class PillSyntax extends InlineSyntax {
PillSyntax() : super(r'([@#!][^\s:]*:[^\s]+\.\w+)');
@override
bool onMatch(InlineParser parser, Match match) {
final identifier = match[1];
final element = Element.text('a', identifier);
element.attributes['href'] = 'https://matrix.to/#/${identifier}';
parser.addNode(element);
return true;
}
}
String markdown(String text, [Map<String, Map<String, String>> emotePacks]) { String markdown(String text, [Map<String, Map<String, String>> emotePacks]) {
emotePacks ??= <String, Map<String, String>>{}; emotePacks ??= <String, Map<String, String>>{};
var ret = markdownToHtml(text, var ret = markdownToHtml(text,
extensionSet: ExtensionSet.commonMark, extensionSet: ExtensionSet.commonMark,
inlineSyntaxes: [StrikethroughSyntax(), LinebreakSyntax(), SpoilerSyntax(), EmoteSyntax(emotePacks)], inlineSyntaxes: [StrikethroughSyntax(), LinebreakSyntax(), SpoilerSyntax(), EmoteSyntax(emotePacks), PillSyntax()],
); );
var stripPTags = '<p>'.allMatches(ret).length <= 1; var stripPTags = '<p>'.allMatches(ret).length <= 1;

View file

@ -38,5 +38,10 @@ void main() {
expect(markdown(':invalid:', emotePacks), ':invalid:'); expect(markdown(':invalid:', emotePacks), ':invalid:');
expect(markdown(':room~invalid:', emotePacks), ':room~invalid:'); expect(markdown(':room~invalid:', emotePacks), ':room~invalid:');
}); });
test('pills', () {
expect(markdown('Hey @sorunome:sorunome.de!'), 'Hey <a href="https://matrix.to/#/@sorunome:sorunome.de">@sorunome:sorunome.de</a>!');
expect(markdown('#fox:sorunome.de: you all are awesome'), '<a href="https://matrix.to/#/#fox:sorunome.de">#fox:sorunome.de</a>: you all are awesome');
expect(markdown('!blah:example.org'), '<a href="https://matrix.to/#/!blah:example.org">!blah:example.org</a>');
});
}); });
} }