Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: markdown user profile links #169

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions lib/widgets/markdown_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import 'package:git_touch/widgets/html_view.dart';
import 'package:provider/provider.dart';
import 'package:uri/uri.dart';
import 'package:path/path.dart' as path;
import 'package:markdown/markdown.dart' as md;
import 'package:charcode/charcode.dart';

import 'link.dart';

class MarkdownViewData {
final Future<String> future;
Expand Down Expand Up @@ -109,6 +113,18 @@ class MarkdownFlutterView extends StatelessWidget {
return Container(
padding: padding,
child: MarkdownBody(
builders: {
"userProfileLink": UserProfileLinkElementBuilder(),
},
extensionSet: md.ExtensionSet(
md.ExtensionSet.gitHubFlavored.blockSyntaxes,
[
UserProfileLink(),
md.EmojiSyntax(),
md.LinkSyntax(),
...md.ExtensionSet.gitHubFlavored.inlineSyntaxes
],
),
data: text,
selectable: true,
imageBuilder: (uri, title, alt) {
Expand Down Expand Up @@ -212,3 +228,26 @@ class MarkdownFlutterView extends StatelessWidget {
);
}
}

class UserProfileLink extends md.InlineSyntax {
// github username regex format
UserProfileLink()
: super(r'\B@([a-z0-9](?:-?[a-z0-9]){0,38})', startCharacter: $at);

@override
bool onMatch(md.InlineParser parser, Match match) {
var login = match[0].substring(1, match[0].length).trim();
md.Node el = md.Element.text("userProfileLink", login);
parser.addNode(el);
return true;
}
}

class UserProfileLinkElementBuilder extends MarkdownElementBuilder {
@override
Widget visitElementAfter(md.Element element, TextStyle prefferedStyle) {
return Link(
url: '/github/${element.textContent}',
child: Text('@' + element.textContent));
}
}