1
0
mirror of https://github.com/git-touch/git-touch synced 2024-12-15 09:56:15 +01:00

refactor: adapt markdown engine

This commit is contained in:
Rongjian Zhang 2020-11-08 15:00:13 +08:00
parent 5795413f25
commit b665784088
4 changed files with 45 additions and 31 deletions

View File

@ -75,12 +75,7 @@ class BbRepoScreen extends StatelessWidget {
],
),
CommonStyle.verticalGap,
if (t.item2 != null)
Container(
padding: CommonStyle.padding,
color: theme.palette.background,
child: MarkdownFlutterView(t.item2),
),
if (t.item2 != null) MarkdownFlutterView(t.item2),
CommonStyle.verticalGap,
],
);

View File

@ -21,23 +21,29 @@ class GeRepoScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return RefreshStatefulScaffold<Tuple2<GiteeRepo, String>>(
return RefreshStatefulScaffold<Tuple2<GiteeRepo, MarkdownViewData>>(
title: AppBarTitle('Repository'),
fetch: () async {
final auth = context.read<AuthModel>();
final res = await Future.wait([
auth.fetchGitee('/repos/$owner/$name'),
auth.fetchGitee('/repos/$owner/$name/readme'),
]);
final md = (res[1]['content'] as String)?.base64ToUtf8;
final repo = await auth.fetchGitee('/repos/$owner/$name').then((v) {
return GiteeRepo.fromJson(v);
});
final html = await http.post(
'${auth.activeAccount.domain}/api/v5/markdown',
headers: {'Authorization': 'token ${auth.token}'},
body: {'text': md},
).then((res) => utf8.decode(res.bodyBytes));
final md =
() => auth.fetchGitee('/repos/$owner/$name/readme').then((v) {
return (v['content'] as String)?.base64ToUtf8;
});
final html = () => md().then((v) async {
final res = await http.post(
'${auth.activeAccount.domain}/api/v5/markdown',
headers: {'Authorization': 'token ${auth.token}'},
body: {'text': v},
);
return utf8.decode(res.bodyBytes).normalizedHtml;
});
final readmeData = MarkdownViewData(context, md: md, html: html);
return Tuple2(GiteeRepo.fromJson(res[0]), html.normalizedHtml);
return Tuple2(repo, readmeData);
},
bodyBuilder: (t, setState) {
final p = t.item1;
@ -101,7 +107,7 @@ class GeRepoScreen extends StatelessWidget {
],
),
CommonStyle.verticalGap,
if (t.item2 != null) MarkdownWebView(t.item2)
MarkdownView(t.item2)
],
);
},

View File

@ -1,3 +1,5 @@
import 'dart:convert';
import 'package:filesize/filesize.dart';
import 'package:flutter/material.dart';
import 'package:git_touch/models/auth.dart';
@ -12,6 +14,7 @@ import 'package:git_touch/widgets/repo_header.dart';
import 'package:git_touch/widgets/table_view.dart';
import 'package:provider/provider.dart';
import 'package:tuple/tuple.dart';
import 'package:http/http.dart' as http;
class GtRepoScreen extends StatelessWidget {
final String owner;
@ -20,18 +23,29 @@ class GtRepoScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return RefreshStatefulScaffold<Tuple2<GiteaRepository, String>>(
return RefreshStatefulScaffold<Tuple2<GiteaRepository, MarkdownViewData>>(
title: AppBarTitle('Repository'),
fetch: () async {
final auth = context.read<AuthModel>();
final res = await Future.wait([
auth.fetchGitea('/repos/$owner/$name'),
auth.fetchGitea('/repos/$owner/$name/contents/README.md'),
]);
return Tuple2(
GiteaRepository.fromJson(res[0]),
(res[1]['content'] as String)?.base64ToUtf8,
);
final repo = await auth.fetchGitea('/repos/$owner/$name').then((v) {
return GiteaRepository.fromJson(v);
});
final md = () =>
auth.fetchGitea('/repos/$owner/$name/contents/README.md').then((v) {
return (v['content'] as String)?.base64ToUtf8;
});
final html = () => md().then((v) async {
final res = await http.post(
'${auth.activeAccount.domain}/api/v1/markdown/raw',
headers: {'Authorization': 'token ${auth.token}'},
body: v,
);
return utf8.decode(res.bodyBytes).normalizedHtml;
});
final readmeData = MarkdownViewData(context, md: md, html: html);
return Tuple2(repo, readmeData);
},
bodyBuilder: (t, setState) {
final theme = Provider.of<ThemeModel>(context);
@ -96,8 +110,7 @@ class GtRepoScreen extends StatelessWidget {
],
),
CommonStyle.verticalGap,
if (t.item2 != null) MarkdownFlutterView(t.item2),
CommonStyle.verticalGap,
MarkdownView(t.item2),
],
);
},

View File

@ -182,7 +182,7 @@ class CommentItem extends StatelessWidget {
),
]),
SizedBox(height: 12),
MarkdownFlutterView(body), // TODO: link
MarkdownFlutterView(body, padding: EdgeInsets.zero), // TODO: link
SizedBox(height: 12),
if (widgets != null) ...widgets
],