Add support for rendering wiki links

This way clicking on a wiki link also opens the corresponding note, if
it exists.
This commit is contained in:
Vishesh Handa
2020-07-09 00:54:47 +02:00
parent ad535f3be7
commit 352879b965
3 changed files with 50 additions and 8 deletions

View File

@ -3,20 +3,23 @@ import 'dart:io';
import 'package:cached_network_image/cached_network_image.dart'; import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_markdown/flutter_markdown.dart'; import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:markdown/markdown.dart' as md;
import 'package:gitjournal/core/note.dart';
import 'package:gitjournal/core/notes_folder_fs.dart';
import 'package:gitjournal/folder_views/common.dart'; import 'package:gitjournal/folder_views/common.dart';
import 'package:gitjournal/settings.dart'; import 'package:gitjournal/settings.dart';
import 'package:gitjournal/utils.dart'; import 'package:gitjournal/utils.dart';
import 'package:gitjournal/utils/logger.dart'; import 'package:gitjournal/utils/logger.dart';
import 'package:gitjournal/widgets/editor_scroll_view.dart'; import 'package:gitjournal/widgets/editor_scroll_view.dart';
import 'package:gitjournal/widgets/notes_backlinks.dart'; import 'package:gitjournal/widgets/notes_backlinks.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:url_launcher/url_launcher.dart'; import 'package:url_launcher/url_launcher.dart';
import 'package:path/path.dart' as p; import 'package:path/path.dart' as p;
import 'package:gitjournal/core/note.dart';
import 'package:gitjournal/core/notes_folder_fs.dart';
class NoteViewer extends StatelessWidget { class NoteViewer extends StatelessWidget {
final Note note; final Note note;
const NoteViewer({Key key, @required this.note}) : super(key: key); const NoteViewer({Key key, @required this.note}) : super(key: key);
@ -50,6 +53,12 @@ class NoteViewer extends StatelessWidget {
), ),
); );
// It's important to add both these inline syntaxes before the other
// syntaxes as the LinkSyntax intefers with both of these
var markdownExtensions = md.ExtensionSet.gitHubFlavored;
markdownExtensions.inlineSyntaxes.insert(0, WikiLinkSyntax2());
markdownExtensions.inlineSyntaxes.insert(1, TaskListSyntax());
final rootFolder = Provider.of<NotesFolderFS>(context); final rootFolder = Provider.of<NotesFolderFS>(context);
var view = EditorScrollView( var view = EditorScrollView(
child: Column( child: Column(
@ -63,6 +72,14 @@ class NoteViewer extends StatelessWidget {
styleSheet: markdownStyleSheet, styleSheet: markdownStyleSheet,
onTapLink: (String link) async { onTapLink: (String link) async {
var spec = link; var spec = link;
if (link.startsWith('[[') &&
link.endsWith(']]') &&
link.length > 4) {
// FIXME: What if the case is different?
spec = link.substring(2, link.length - 2) + ".md";
}
if (link.startsWith('./')) { if (link.startsWith('./')) {
spec = link.substring(2); spec = link.substring(2);
} }
@ -97,6 +114,7 @@ class NoteViewer extends StatelessWidget {
}, },
imageBuilder: (url, title, alt) => kDefaultImageBuilder( imageBuilder: (url, title, alt) => kDefaultImageBuilder(
url, note.parent.folderPath + p.separator, null, null), url, note.parent.folderPath + p.separator, null, null),
extensionSet: markdownExtensions,
), ),
), ),
const SizedBox(height: 16.0), const SizedBox(height: 16.0),
@ -208,3 +226,23 @@ Widget _handleDataSchemeUri(Uri uri, final double width, final double height) {
} }
return const SizedBox(); return const SizedBox();
} }
/// Parse [[term]]
class WikiLinkSyntax2 extends md.InlineSyntax {
static final String _pattern = r'\[\[([^\[\]]+)\]\]';
WikiLinkSyntax2() : super(_pattern);
@override
bool onMatch(md.InlineParser parser, Match match) {
// print("-------- WIKI LINK -------");
var term = match[1].trim();
var el = md.Element('a', [md.Text(term)]);
el.attributes['type'] = 'wiki';
el.attributes['href'] = '[[$term]]';
parser.addNode(el);
return true;
}
}

View File

@ -477,12 +477,14 @@ packages:
source: hosted source: hosted
version: "0.11.3+2" version: "0.11.3+2"
markdown: markdown:
dependency: transitive dependency: "direct overridden"
description: description:
name: markdown path: "."
url: "https://pub.dartlang.org" ref: HEAD
source: hosted resolved-ref: "2e87055c39b4b29939bb99ceff593880e2bcf20a"
version: "2.1.3" url: "https://github.com/GitJournal/markdown.git"
source: git
version: "2.1.5"
matcher: matcher:
dependency: transitive dependency: transitive
description: description:

View File

@ -72,6 +72,8 @@ dev_dependencies:
dependency_overrides: dependency_overrides:
intl: intl:
git: https://github.com/GitJournal/intl.git git: https://github.com/GitJournal/intl.git
markdown:
git: https://github.com/GitJournal/markdown.git
flutter: flutter:
uses-material-design: true uses-material-design: true