mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-07-02 04:47:01 +08:00
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:
@ -3,20 +3,23 @@ import 'dart:io';
|
||||
import 'package:cached_network_image/cached_network_image.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.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/settings.dart';
|
||||
import 'package:gitjournal/utils.dart';
|
||||
import 'package:gitjournal/utils/logger.dart';
|
||||
import 'package:gitjournal/widgets/editor_scroll_view.dart';
|
||||
import 'package:gitjournal/widgets/notes_backlinks.dart';
|
||||
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
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 {
|
||||
final Note note;
|
||||
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);
|
||||
var view = EditorScrollView(
|
||||
child: Column(
|
||||
@ -63,6 +72,14 @@ class NoteViewer extends StatelessWidget {
|
||||
styleSheet: markdownStyleSheet,
|
||||
onTapLink: (String link) async {
|
||||
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('./')) {
|
||||
spec = link.substring(2);
|
||||
}
|
||||
@ -97,6 +114,7 @@ class NoteViewer extends StatelessWidget {
|
||||
},
|
||||
imageBuilder: (url, title, alt) => kDefaultImageBuilder(
|
||||
url, note.parent.folderPath + p.separator, null, null),
|
||||
extensionSet: markdownExtensions,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16.0),
|
||||
@ -208,3 +226,23 @@ Widget _handleDataSchemeUri(Uri uri, final double width, final double height) {
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
12
pubspec.lock
12
pubspec.lock
@ -477,12 +477,14 @@ packages:
|
||||
source: hosted
|
||||
version: "0.11.3+2"
|
||||
markdown:
|
||||
dependency: transitive
|
||||
dependency: "direct overridden"
|
||||
description:
|
||||
name: markdown
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "2.1.3"
|
||||
path: "."
|
||||
ref: HEAD
|
||||
resolved-ref: "2e87055c39b4b29939bb99ceff593880e2bcf20a"
|
||||
url: "https://github.com/GitJournal/markdown.git"
|
||||
source: git
|
||||
version: "2.1.5"
|
||||
matcher:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
@ -72,6 +72,8 @@ dev_dependencies:
|
||||
dependency_overrides:
|
||||
intl:
|
||||
git: https://github.com/GitJournal/intl.git
|
||||
markdown:
|
||||
git: https://github.com/GitJournal/markdown.git
|
||||
|
||||
flutter:
|
||||
uses-material-design: true
|
||||
|
Reference in New Issue
Block a user