Merge branch 'soru/markdown-pills' into 'master'

add pill parsing to markdown

See merge request famedly/famedlysdk!294
This commit is contained in:
Christian Pauly 2020-05-15 19:05:10 +00:00
commit 52721553cb
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>');
});
}); });
} }