-
Notifications
You must be signed in to change notification settings - Fork 130
/
markdown_view.dart
213 lines (198 loc) · 6.64 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import 'package:antd_mobile/antd_mobile.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.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:git_touch/widgets/html_view.dart';
import 'package:path/path.dart' as path;
import 'package:provider/provider.dart';
import 'package:uri/uri.dart';
class MarkdownViewData {
MarkdownViewData(
BuildContext context, {
required Future<String> Function() md,
required Future<String> Function() html,
}) : future = context.read<ThemeModel>().shouldUseMarkdownFlutterView
? md()
: html();
final Future<String> future;
}
class MarkdownView extends StatelessWidget {
const MarkdownView(this.data);
final MarkdownViewData? data;
@override
Widget build(BuildContext context) {
final theme = Provider.of<ThemeModel>(context);
if (data?.future == null) return Container();
if (theme.shouldUseMarkdownFlutterView) {
return FutureBuilder<String>(
future: data!.future,
builder: (context, snapshot) {
if (snapshot.data == null) {
return Container();
} else {
return MarkdownFlutterView(snapshot.data);
}
},
);
} else {
return FutureBuilder<String>(
future: data!.future,
builder: (context, snapshot) {
if (snapshot.data == null) {
return Container();
} else {
return MarkdownWebView(snapshot.data);
}
},
);
}
}
}
// TODO: Safari table width
class MarkdownWebView extends StatelessWidget {
const MarkdownWebView(this.html);
final String? html;
@override
Widget build(BuildContext context) {
final theme = Provider.of<ThemeModel>(context);
var css = theme.markdownCss ?? '';
if (theme.brightness == Brightness.dark) {
css += '''
html {
background-color: #000;
}
.markdown-body {
filter:invert(100%);
}
.markdown-body img {
filter:invert(100%);
}
''';
}
return HtmlView(html!, cssText: css);
}
}
class MarkdownFlutterView extends StatelessWidget {
const MarkdownFlutterView(
this.text, {
this.basePaths,
this.padding = const EdgeInsets.all(12),
});
final String? text;
final List<String>? basePaths;
final EdgeInsetsGeometry padding;
static Map<String, String?>? matchPattern(String url, String pattern) {
final uri = Uri.parse(url);
return UriParser(UriTemplate(pattern)).match(uri)?.parameters;
}
@override
Widget build(BuildContext context) {
final code = Provider.of<CodeModel>(context);
final basicStyle = TextStyle(
fontSize: 16, color: AntTheme.of(context).colorText, height: 1.5);
final hStyle =
basicStyle.copyWith(fontWeight: FontWeight.w600, height: 1.25);
return Container(
padding: padding,
child: 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: (text, url, title) {
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);
context.pushUrl(
'/${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 (final p in matchedPaths) {
final m = matchPattern(url, p);
if (m != null) {
context.pushUrl(
url.replaceFirst(RegExp(r'https://github.com'), '/github'));
}
}
}
launchStringUrl(url);
},
styleSheet: MarkdownStyleSheet(
a: basicStyle.copyWith(color: AntTheme.of(context).colorPrimary),
p: basicStyle,
code: basicStyle.copyWith(
backgroundColor: AntTheme.of(context).colorBox,
fontSize: 16 * 0.85,
height: 1.45,
fontFamily: code.fontStyle.fontFamily,
),
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: AntTheme.of(context).colorWeak),
em: basicStyle.copyWith(fontStyle: FontStyle.italic),
strong: basicStyle.copyWith(fontWeight: FontWeight.w600),
del: const TextStyle(decoration: TextDecoration.lineThrough),
blockquote:
basicStyle.copyWith(color: AntTheme.of(context).colorWeak),
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: const EdgeInsets.symmetric(horizontal: 16),
blockquoteDecoration: const BoxDecoration(
border:
Border(left: BorderSide(color: Color(0xffdfe2e5), width: 4)),
),
codeblockPadding: const EdgeInsets.all(16),
codeblockDecoration: BoxDecoration(
color: AntTheme.of(context).colorBox,
borderRadius: BorderRadius.circular(3),
),
horizontalRuleDecoration: BoxDecoration(
border: Border(
top: BorderSide(
width: 4,
color: AntTheme.of(context).colorBox,
),
),
),
),
// syntaxHighlighter: , // TODO:
),
);
}
}