From fcd416debe878b1f6a316fe451c0324dcae7c4e0 Mon Sep 17 00:00:00 2001 From: Rongjian Zhang Date: Sun, 1 Nov 2020 23:43:51 +0800 Subject: [PATCH] refactor(gitee): render readme with webview --- lib/screens/ge_repo.dart | 28 ++++++++++++++-------------- lib/screens/gl_project.dart | 2 +- lib/utils/extensions.dart | 4 ++++ 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/lib/screens/ge_repo.dart b/lib/screens/ge_repo.dart index 236f221..662b0c1 100644 --- a/lib/screens/ge_repo.dart +++ b/lib/screens/ge_repo.dart @@ -1,16 +1,18 @@ +import 'dart:convert'; + import 'package:flutter/material.dart'; import 'package:git_touch/models/auth.dart'; import 'package:git_touch/models/gitee.dart'; -import 'package:git_touch/models/theme.dart'; import 'package:git_touch/scaffolds/refresh_stateful.dart'; import 'package:git_touch/utils/utils.dart'; import 'package:git_touch/widgets/app_bar_title.dart'; import 'package:git_touch/widgets/entry_item.dart'; -import 'package:git_touch/widgets/markdown_view.dart'; +import 'package:git_touch/widgets/html_view.dart'; 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 GeRepoScreen extends StatelessWidget { final String owner; @@ -27,13 +29,17 @@ class GeRepoScreen extends StatelessWidget { auth.fetchGitee('/repos/$owner/$name'), auth.fetchGitee('/repos/$owner/$name/readme'), ]); - return Tuple2( - GiteeRepo.fromJson(res[0]), - (res[1]['content'] as String)?.base64ToUtf8, - ); + final md = (res[1]['content'] as String)?.base64ToUtf8; + + 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)); + + return Tuple2(GiteeRepo.fromJson(res[0]), html.normalizedHtml); }, bodyBuilder: (t, setState) { - final theme = Provider.of(context); final p = t.item1; return Column( crossAxisAlignment: CrossAxisAlignment.stretch, @@ -95,13 +101,7 @@ class GeRepoScreen extends StatelessWidget { ], ), CommonStyle.verticalGap, - if (t.item2 != null) - Container( - padding: CommonStyle.padding, - color: theme.palette.background, - child: MarkdownView(t.item2), - ), - CommonStyle.verticalGap, + if (t.item2 != null) MarkdownHtmlView(t.item2) ], ); }, diff --git a/lib/screens/gl_project.dart b/lib/screens/gl_project.dart index 7970ffb..d38f8d5 100644 --- a/lib/screens/gl_project.dart +++ b/lib/screens/gl_project.dart @@ -46,7 +46,7 @@ class GlProjectScreen extends StatelessWidget { 'gfm': true, 'project': '${p.namespace.name}/${p.name}' }); - readme = '
' + res['html'] + '
'; + readme = (res['html'] as String).normalizedHtml; } return Tuple5( p, diff --git a/lib/utils/extensions.dart b/lib/utils/extensions.dart index ca54cdb..b1e9fa5 100644 --- a/lib/utils/extensions.dart +++ b/lib/utils/extensions.dart @@ -18,4 +18,8 @@ extension MyString on String { if (dotext.isEmpty) return null; return dotext.substring(1); } + + String get normalizedHtml { + return '
' + this + '
'; + } }