From b665784088992cbf3d1da66381288e46c0136771 Mon Sep 17 00:00:00 2001 From: Rongjian Zhang Date: Sun, 8 Nov 2020 15:00:13 +0800 Subject: [PATCH] refactor: adapt markdown engine --- lib/screens/bb_repo.dart | 7 +------ lib/screens/ge_repo.dart | 32 +++++++++++++++++++------------- lib/screens/gt_repo.dart | 35 ++++++++++++++++++++++++----------- lib/widgets/comment_item.dart | 2 +- 4 files changed, 45 insertions(+), 31 deletions(-) diff --git a/lib/screens/bb_repo.dart b/lib/screens/bb_repo.dart index 2bfbc9b..48c2ae0 100644 --- a/lib/screens/bb_repo.dart +++ b/lib/screens/bb_repo.dart @@ -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, ], ); diff --git a/lib/screens/ge_repo.dart b/lib/screens/ge_repo.dart index 1f76d42..dac43ef 100644 --- a/lib/screens/ge_repo.dart +++ b/lib/screens/ge_repo.dart @@ -21,23 +21,29 @@ class GeRepoScreen extends StatelessWidget { @override Widget build(BuildContext context) { - return RefreshStatefulScaffold>( + return RefreshStatefulScaffold>( title: AppBarTitle('Repository'), fetch: () async { final auth = context.read(); - 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) ], ); }, diff --git a/lib/screens/gt_repo.dart b/lib/screens/gt_repo.dart index 59ddad7..c6d854e 100644 --- a/lib/screens/gt_repo.dart +++ b/lib/screens/gt_repo.dart @@ -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>( + return RefreshStatefulScaffold>( title: AppBarTitle('Repository'), fetch: () async { final auth = context.read(); - 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(context); @@ -96,8 +110,7 @@ class GtRepoScreen extends StatelessWidget { ], ), CommonStyle.verticalGap, - if (t.item2 != null) MarkdownFlutterView(t.item2), - CommonStyle.verticalGap, + MarkdownView(t.item2), ], ); }, diff --git a/lib/widgets/comment_item.dart b/lib/widgets/comment_item.dart index 96c9037..4432d11 100644 --- a/lib/widgets/comment_item.dart +++ b/lib/widgets/comment_item.dart @@ -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 ],