/
markdown_view.dart
131 lines (121 loc) · 4.49 KB
/
markdown_view.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import 'package:flutter/material.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:git_touch/models/code.dart';
import 'package:git_touch/models/theme.dart';
import 'package:git_touch/utils/utils.dart';
import 'package:provider/provider.dart';
import 'package:uri/uri.dart';
import 'package:path/path.dart' as path;
class MarkdownView extends StatelessWidget {
final String text;
final List<String> basePaths;
MarkdownView(this.text, {this.basePaths});
static Map<String, String> matchPattern(String url, String pattern) {
var uri = Uri.parse(url);
return UriParser(UriTemplate(pattern)).match(uri)?.parameters;
}
@override
Widget build(BuildContext context) {
final theme = Provider.of<ThemeModel>(context);
final code = Provider.of<CodeModel>(context);
final _basicStyle =
TextStyle(fontSize: 16, color: theme.palette.text, height: 1.5);
final _hStyle =
_basicStyle.copyWith(fontWeight: FontWeight.w600, height: 1.25);
return MarkdownBody(
data: text,
selectable: true,
imageBuilder: (uri, title, alt) {
if (uri.scheme == 'http' || uri.scheme == 'https') {
// TODO: svg support
// if (uri.path.endsWith('.svg')) {
// return SvgPicture.network(uri.toString());
// }
return Image.network(uri.toString());
} else {
return Container(); // TODO: relative path image
}
},
onTapLink: (url) {
final theme = context.read<ThemeModel>();
if (basePaths != null &&
!url.startsWith('https://') &&
!url.startsWith('http://')) {
// Treat as relative path
final x = basePaths.sublist(3).join('/');
var y = path.join(x, url);
if (y.startsWith('/')) y = y.substring(1);
return theme.push(context,
'/${basePaths[0]}/${basePaths[1]}/${basePaths[2]}?path=${y.urlencode}');
}
// TODO: Relative paths
if (url.startsWith('https://github.com')) {
const matchedPaths = [
'/{owner}/{name}/pull/{number}',
'/{owner}/{name}/issues/{number}',
'/{owner}/{name}',
'/{login}'
];
for (var p in matchedPaths) {
final m = matchPattern(url, p);
if (m != null) {
return theme.push(context,
url.replaceFirst(RegExp(r'https://github.com'), '/github'));
}
}
}
launchUrl(url);
},
styleSheet: MarkdownStyleSheet(
a: _basicStyle.copyWith(color: theme.palette.primary),
p: _basicStyle,
code: _basicStyle.copyWith(
backgroundColor: theme.palette.grayBackground,
fontSize: 16 * 0.85,
height: 1.45,
fontFamily: code.fontFamilyUsed,
),
h1: _hStyle.copyWith(fontSize: 32),
h2: _hStyle.copyWith(fontSize: 24),
h3: _hStyle.copyWith(fontSize: 20),
h4: _hStyle,
h5: _hStyle.copyWith(fontSize: 14),
h6: _hStyle.copyWith(
fontSize: 16 * 0.85, color: theme.palette.tertiaryText),
em: _basicStyle.copyWith(fontStyle: FontStyle.italic),
strong: _basicStyle.copyWith(fontWeight: FontWeight.w600),
del: const TextStyle(decoration: TextDecoration.lineThrough),
blockquote: _basicStyle.copyWith(color: theme.palette.tertiaryText),
img: _basicStyle,
checkbox: _basicStyle,
blockSpacing: 16,
listIndent: 32,
listBullet: _basicStyle,
// tableHead: _basicStyle,
tableBody: _basicStyle,
// tableHeadAlign: TextAlign.center,
// tableBorder: TableBorder.all(color: Colors.grey.shade300, width: 0),
// tableColumnWidth: const FlexColumnWidth(),
// tableCellsPadding: const EdgeInsets.fromLTRB(16, 8, 16, 8),
blockquotePadding: EdgeInsets.symmetric(horizontal: 16),
blockquoteDecoration: BoxDecoration(
border: Border(left: BorderSide(color: Color(0xffdfe2e5), width: 4)),
),
codeblockPadding: EdgeInsets.all(16),
codeblockDecoration: BoxDecoration(
color: theme.palette.grayBackground,
borderRadius: BorderRadius.circular(3),
),
horizontalRuleDecoration: BoxDecoration(
border: Border(
top: BorderSide(
width: 4,
color: theme.palette.grayBackground,
),
),
),
),
// syntaxHighlighter: , // TODO:
);
}
}