diff --git a/lib/partials/post.dart b/lib/partials/post.dart index 31935b3..0e9f350 100644 --- a/lib/partials/post.dart +++ b/lib/partials/post.dart @@ -152,7 +152,7 @@ class _PostBodyState extends State { child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ - PostTextRenderer(htmlRef: widget.content), + PostTextRenderer(htmlInput: widget.content), MediaAttachments(models: widget.media), ], ), diff --git a/lib/partials/post_text_renderer.dart b/lib/partials/post_text_renderer.dart index 91d9136..ba33869 100644 --- a/lib/partials/post_text_renderer.dart +++ b/lib/partials/post_text_renderer.dart @@ -1,33 +1,17 @@ import 'package:flutter/material.dart'; +import 'package:html/parser.dart'; class PostTextRenderer extends StatelessWidget { const PostTextRenderer({ - required this.html, + required this.htmlInput, Key? key, }) : super(key: key); - final String html; + final String htmlInput; @override Widget build(BuildContext context) { - String text = html; - text = handleParagraph(html); - text = handleBreak(text); - return SelectableText.rich(TextSpan(text: text, children: [])); + var document = parse(htmlInput); + return SelectableText.rich( + TextSpan(text: document.body?.text, children: [])); } } - -String handleParagraph(String input) { - RegExp opening = RegExp(r"

"); - RegExp closing = RegExp(r"

"); - - String out = input.replaceAll(closing, "\n\n").replaceAll(opening, ""); - if (out.endsWith("\n\n")) { - out = out.substring(0, out.length - 2); - } - - return out; -} - -String handleBreak(String input) { - return input.replaceAll("
", "\n"); -} diff --git a/pubspec.lock b/pubspec.lock index 2c9ba5b..63d1604 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -43,6 +43,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.16.0" + csslib: + dependency: transitive + description: + name: csslib + url: "https://pub.dartlang.org" + source: hosted + version: "0.17.2" cupertino_icons: dependency: "direct main" description: @@ -98,6 +105,13 @@ packages: description: flutter source: sdk version: "0.0.0" + html: + dependency: "direct main" + description: + name: html + url: "https://pub.dartlang.org" + source: hosted + version: "0.15.0" http: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index abd5587..e0b6a9b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -42,6 +42,7 @@ dependencies: intl: ^0.17.0 url_launcher: ^6.1.4 shelf: ^1.3.1 + html: ^0.15.0 dev_dependencies: flutter_test: