diff --git a/lib/src/utils/markdown.dart b/lib/src/utils/markdown.dart index f0577d2..b9ae5df 100644 --- a/lib/src/utils/markdown.dart +++ b/lib/src/utils/markdown.dart @@ -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> emotePacks]) { emotePacks ??= >{}; var ret = markdownToHtml(text, extensionSet: ExtensionSet.commonMark, - inlineSyntaxes: [StrikethroughSyntax(), LinebreakSyntax(), SpoilerSyntax(), EmoteSyntax(emotePacks)], + inlineSyntaxes: [StrikethroughSyntax(), LinebreakSyntax(), SpoilerSyntax(), EmoteSyntax(emotePacks), PillSyntax()], ); var stripPTags = '

'.allMatches(ret).length <= 1; diff --git a/test/markdown_test.dart b/test/markdown_test.dart index f11af69..58db060 100644 --- a/test/markdown_test.dart +++ b/test/markdown_test.dart @@ -38,5 +38,10 @@ void main() { expect(markdown(':invalid:', emotePacks), ':invalid:'); expect(markdown(':room~invalid:', emotePacks), ':room~invalid:'); }); + test('pills', () { + expect(markdown('Hey @sorunome:sorunome.de!'), 'Hey @sorunome:sorunome.de!'); + expect(markdown('#fox:sorunome.de: you all are awesome'), '#fox:sorunome.de: you all are awesome'); + expect(markdown('!blah:example.org'), '!blah:example.org'); + }); }); }