From 9d1c7f16a57169f98d65bbcd61f37daf2b0b6acb Mon Sep 17 00:00:00 2001 From: Sorunome Date: Fri, 15 May 2020 21:00:10 +0200 Subject: [PATCH] add pill parsing to markdown --- lib/src/utils/markdown.dart | 14 +++++++++++++- test/markdown_test.dart | 5 +++++ 2 files changed, 18 insertions(+), 1 deletion(-) 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'); + }); }); }