Skip to content

Commit

Permalink
refactor(gitee): render readme with webview
Browse files Browse the repository at this point in the history
  • Loading branch information
pd4d10 committed Nov 1, 2020
1 parent ac1b205 commit fcd416d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
28 changes: 14 additions & 14 deletions lib/screens/ge_repo.dart
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<ThemeModel>(context);
final p = t.item1;
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
Expand Down Expand Up @@ -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)
],
);
},
Expand Down
2 changes: 1 addition & 1 deletion lib/screens/gl_project.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class GlProjectScreen extends StatelessWidget {
'gfm': true,
'project': '${p.namespace.name}/${p.name}'
});
readme = '<div class="markdown-body">' + res['html'] + '</div>';
readme = (res['html'] as String).normalizedHtml;
}
return Tuple5(
p,
Expand Down
4 changes: 4 additions & 0 deletions lib/utils/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ extension MyString<T extends String> on String {
if (dotext.isEmpty) return null;
return dotext.substring(1);
}

String get normalizedHtml {
return '<div class="markdown-body">' + this + '</div>';
}
}

0 comments on commit fcd416d

Please sign in to comment.