add pill parsing to markdown
This commit is contained in:
parent
98d2f8d6bb
commit
9d1c7f16a5
|
@ -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]) {
|
||||
emotePacks ??= <String, Map<String, String>>{};
|
||||
var ret = markdownToHtml(text,
|
||||
extensionSet: ExtensionSet.commonMark,
|
||||
inlineSyntaxes: [StrikethroughSyntax(), LinebreakSyntax(), SpoilerSyntax(), EmoteSyntax(emotePacks)],
|
||||
inlineSyntaxes: [StrikethroughSyntax(), LinebreakSyntax(), SpoilerSyntax(), EmoteSyntax(emotePacks), PillSyntax()],
|
||||
);
|
||||
|
||||
var stripPTags = '<p>'.allMatches(ret).length <= 1;
|
||||
|
|
|
@ -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 <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>');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue