From 48ab0e7228fd679f3e074e9f576c51b8e44a9f1c Mon Sep 17 00:00:00 2001 From: peron Date: Tue, 23 Jan 2024 12:45:38 +0000 Subject: [PATCH] refactor: migrate to markdown_widget --- lib/utils/latex.dart | 23 ----- lib/utils/map.dart | 5 ++ lib/widgets/code_element.dart | 30 ++----- lib/widgets/message_tile.dart | 40 +++------ pubspec.lock | 152 ++++++++++++++-------------------- pubspec.yaml | 7 +- 6 files changed, 89 insertions(+), 168 deletions(-) delete mode 100644 lib/utils/latex.dart create mode 100644 lib/utils/map.dart diff --git a/lib/utils/latex.dart b/lib/utils/latex.dart deleted file mode 100644 index edf6bdf..0000000 --- a/lib/utils/latex.dart +++ /dev/null @@ -1,23 +0,0 @@ -import 'package:flutter_markdown_latex/flutter_markdown_latex.dart'; -import 'package:markdown/markdown.dart'; - -class BingLatexBlockSyntax extends LatexBlockSyntax { - @override - List parseChildLines(BlockParser parser) { - final childLines = super.parseChildLines(parser); - - return [Line("\\begin{aligned}"), ...childLines, Line("\\end{aligned}")]; - } -} - -class BingLatexInlineSyntax extends InlineSyntax { - BingLatexInlineSyntax() : super(r'\${2}(.+?)\${2}'); - - @override - bool onMatch(InlineParser parser, Match match) { - Element element = Element.text('latex', match[1] ?? ''); - element.attributes['displayMode'] = 'true'; - parser.addNode(element); - return true; - } -} diff --git a/lib/utils/map.dart b/lib/utils/map.dart new file mode 100644 index 0000000..fdc6ff4 --- /dev/null +++ b/lib/utils/map.dart @@ -0,0 +1,5 @@ +extension CopyWithExtension on Map { + Map copyWith(K key, V value) { + return Map.from(this)..[key] = value; + } +} diff --git a/lib/widgets/code_element.dart b/lib/widgets/code_element.dart index 821359d..a8a8d85 100644 --- a/lib/widgets/code_element.dart +++ b/lib/widgets/code_element.dart @@ -1,30 +1,18 @@ import 'package:flutter/material.dart'; -import 'package:flutter_highlighter/flutter_highlighter.dart'; -import 'package:flutter_highlighter/themes/atom-one-dark.dart'; -import 'package:flutter_markdown/flutter_markdown.dart'; -import 'package:markdown/markdown.dart' as md; +import 'package:flutter_highlight/flutter_highlight.dart'; +import 'package:flutter_highlight/themes/atom-one-dark.dart'; import 'package:google_fonts/google_fonts.dart'; import 'package:sydney_webui/utils/copy.dart'; import 'package:sydney_webui/utils/string.dart'; -class CodeElementBuilder extends MarkdownElementBuilder { - @override - Widget? visitElementAfter(md.Element element, TextStyle? preferredStyle) { - final textContent = element.textContent; - - if (!textContent.endsWith("\n")) { - // handle inline code - return Text("`$textContent`", style: GoogleFonts.robotoMono()); - } +class CodeElement extends StatelessWidget { + const CodeElement({super.key, this.language = "", this.textContent = ""}); - var language = ''; + final String language; + final String textContent; - if (element.attributes['class']?.startsWith('language-') ?? false) { - String lg = element.attributes['class'] as String; - language = lg.substring('language-'.length); - } - - // render code block + @override + Widget build(BuildContext context) { return Column( children: [ Container( @@ -61,7 +49,7 @@ class CodeElementBuilder extends MarkdownElementBuilder { theme: atomOneDarkTheme, // Specify padding - padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8), + padding: const EdgeInsets.all(16), // Specify text style textStyle: GoogleFonts.robotoMono(), diff --git a/lib/widgets/message_tile.dart b/lib/widgets/message_tile.dart index c3b2315..f031372 100644 --- a/lib/widgets/message_tile.dart +++ b/lib/widgets/message_tile.dart @@ -1,15 +1,13 @@ import 'dart:convert'; import 'package:flutter/material.dart'; -import 'package:flutter_markdown/flutter_markdown.dart' as md; import 'package:get/get.dart'; -import 'package:markdown/markdown.dart' as md; +import 'package:google_fonts/google_fonts.dart'; import 'package:sydney_webui/controller.dart'; import 'package:sydney_webui/models/message.dart'; import 'package:sydney_webui/utils/copy.dart'; -import 'package:sydney_webui/utils/latex.dart'; import 'package:sydney_webui/utils/url.dart'; +import 'package:markdown_widget/markdown_widget.dart'; import 'package:sydney_webui/widgets/code_element.dart'; -import 'package:flutter_markdown_latex/flutter_markdown_latex.dart'; class MessageTile extends StatelessWidget { const MessageTile({ @@ -129,30 +127,16 @@ class MessageTile extends StatelessWidget { expandedAlignment: Alignment.topLeft, childrenPadding: const EdgeInsets.symmetric(horizontal: 16), children: [ - md.MarkdownBody( - selectable: true, - data: message.content, - softLineBreak: true, - styleSheet: md.MarkdownStyleSheet( - blockquoteDecoration: BoxDecoration( - color: Get.theme.colorScheme.surfaceVariant, - borderRadius: BorderRadius.circular(8), - )), - extensionSet: md.ExtensionSet([ - ...md.ExtensionSet.gitHubFlavored.blockSyntaxes, - BingLatexBlockSyntax(), - ], [ - ...md.ExtensionSet.gitHubFlavored.inlineSyntaxes, - LatexInlineSyntax(), - BingLatexInlineSyntax() - ]), - onTapLink: (text, href, title) { - if (href != null) openUrl(href); - }, - builders: { - 'code': CodeElementBuilder(), - 'latex': LatexElementBuilder(textStyle: Get.textTheme.bodyLarge), - }), + MarkdownBlock( + data: message.content, + config: MarkdownConfig(configs: [ + PreConfig( + wrapper: (child, code, language) => + CodeElement(textContent: code, language: language)), + CodeConfig(style: GoogleFonts.robotoMono()), + const LinkConfig(onTap: openUrl) + ]), + ), ...typeSpecificContent, ...images ], diff --git a/pubspec.lock b/pubspec.lock index b841505..f1165d2 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -118,14 +118,14 @@ packages: description: flutter source: sdk version: "0.0.0" - flutter_highlighter: + flutter_highlight: dependency: "direct main" description: - name: flutter_highlighter - sha256: "93173afd47a9ada53f3176371755e7ea4a1065362763976d06d6adfb4d946e10" + name: flutter_highlight + sha256: "7b96333867aa07e122e245c033b8ad622e4e3a42a1a2372cbb098a2541d8782c" url: "https://pub.dev" source: hosted - version: "0.1.1" + version: "0.7.0" flutter_lints: dependency: "direct dev" description: @@ -134,30 +134,6 @@ packages: url: "https://pub.dev" source: hosted version: "3.0.1" - flutter_markdown: - dependency: "direct main" - description: - name: flutter_markdown - sha256: "30088ce826b5b9cfbf9e8bece34c716c8a59fa54461dcae1e4ac01a94639e762" - url: "https://pub.dev" - source: hosted - version: "0.6.18+3" - flutter_markdown_latex: - dependency: "direct main" - description: - name: flutter_markdown_latex - sha256: "87ccd52eb0ec39286205a9f355cd0b667e5551531e53fd5b85dd2bfabbe58630" - url: "https://pub.dev" - source: hosted - version: "0.1.4" - flutter_math_fork: - dependency: transitive - description: - name: flutter_math_fork - sha256: "94bee4642892a94939af0748c6a7de0ff8318feee588379dcdfea7dc5cba06c8" - url: "https://pub.dev" - source: hosted - version: "0.7.2" flutter_plugin_android_lifecycle: dependency: transitive description: @@ -166,14 +142,6 @@ packages: url: "https://pub.dev" source: hosted version: "2.0.17" - flutter_svg: - dependency: transitive - description: - name: flutter_svg - sha256: d39e7f95621fc84376bc0f7d504f05c3a41488c562f4a8ad410569127507402c - url: "https://pub.dev" - source: hosted - version: "2.0.9" flutter_test: dependency: "direct dev" description: flutter @@ -208,14 +176,14 @@ packages: url: "https://pub.dev" source: hosted version: "6.1.0" - highlighter: + highlight: dependency: transitive description: - name: highlighter - sha256: "92180c72b9da8758e1acf39a45aa305a97dcfe2fdc8f3d1d2947c23f2772bfbc" + name: highlight + sha256: "5353a83ffe3e3eca7df0abfb72dcf3fa66cc56b953728e7113ad4ad88497cf21" url: "https://pub.dev" source: hosted - version: "0.1.1" + version: "0.7.0" http: dependency: transitive description: @@ -264,6 +232,14 @@ packages: url: "https://pub.dev" source: hosted version: "7.1.1" + markdown_widget: + dependency: "direct main" + description: + name: markdown_widget + sha256: "4ac442aa4debbcf16c4030e1313167f18e0fa29f4d7b6f2ff23405d6d7689f6f" + url: "https://pub.dev" + source: hosted + version: "2.3.2+3" matcher: dependency: transitive description: @@ -296,14 +272,6 @@ packages: url: "https://pub.dev" source: hosted version: "1.0.4" - nested: - dependency: transitive - description: - name: nested - sha256: "03bac4c528c64c95c722ec99280375a6f2fc708eec17c7b3f07253b626cd2a20" - url: "https://pub.dev" - source: hosted - version: "1.0.0" pasteboard: dependency: "direct main" description: @@ -320,14 +288,6 @@ packages: url: "https://pub.dev" source: hosted version: "1.8.3" - path_parsing: - dependency: transitive - description: - name: path_parsing - sha256: e3e67b1629e6f7e8100b367d3db6ba6af4b1f0bb80f64db18ef1fbabd2fa9ccf - url: "https://pub.dev" - source: hosted - version: "1.0.1" path_provider: dependency: transitive description: @@ -408,14 +368,14 @@ packages: url: "https://pub.dev" source: hosted version: "3.7.3" - provider: + scroll_to_index: dependency: transitive description: - name: provider - sha256: "9a96a0a19b594dbc5bf0f1f27d2bc67d5f95957359b461cd9feb44ed6ae75096" + name: scroll_to_index + sha256: b707546e7500d9f070d63e5acf74fd437ec7eeeb68d3412ef7b0afada0b4f176 url: "https://pub.dev" source: hosted - version: "6.1.1" + version: "3.0.1" share_plus: dependency: "direct main" description: @@ -493,14 +453,6 @@ packages: url: "https://pub.dev" source: hosted version: "0.6.1" - tuple: - dependency: transitive - description: - name: tuple - sha256: a97ce2013f240b2f3807bcbaf218765b6f301c3eff91092bcfa23a039e7dd151 - url: "https://pub.dev" - source: hosted - version: "2.0.2" typed_data: dependency: transitive description: @@ -509,6 +461,30 @@ packages: url: "https://pub.dev" source: hosted version: "1.3.2" + url_launcher: + dependency: transitive + description: + name: url_launcher + sha256: c512655380d241a337521703af62d2c122bf7b77a46ff7dd750092aa9433499c + url: "https://pub.dev" + source: hosted + version: "6.2.4" + url_launcher_android: + dependency: transitive + description: + name: url_launcher_android + sha256: "507dc655b1d9cb5ebc756032eb785f114e415f91557b73bf60b7e201dfedeb2f" + url: "https://pub.dev" + source: hosted + version: "6.2.2" + url_launcher_ios: + dependency: transitive + description: + name: url_launcher_ios + sha256: "75bb6fe3f60070407704282a2d295630cab232991eb52542b18347a8a941df03" + url: "https://pub.dev" + source: hosted + version: "6.2.4" url_launcher_linux: dependency: transitive description: @@ -517,6 +493,14 @@ packages: url: "https://pub.dev" source: hosted version: "3.1.1" + url_launcher_macos: + dependency: transitive + description: + name: url_launcher_macos + sha256: b7244901ea3cf489c5335bdacda07264a6e960b1c1b1a9f91e4bc371d9e68234 + url: "https://pub.dev" + source: hosted + version: "3.1.0" url_launcher_platform_interface: dependency: transitive description: @@ -549,30 +533,6 @@ packages: url: "https://pub.dev" source: hosted version: "4.2.2" - vector_graphics: - dependency: transitive - description: - name: vector_graphics - sha256: "18f6690295af52d081f6808f2f7c69f0eed6d7e23a71539d75f4aeb8f0062172" - url: "https://pub.dev" - source: hosted - version: "1.1.9+2" - vector_graphics_codec: - dependency: transitive - description: - name: vector_graphics_codec - sha256: "531d20465c10dfac7f5cd90b60bbe4dd9921f1ec4ca54c83ebb176dbacb7bb2d" - url: "https://pub.dev" - source: hosted - version: "1.1.9+2" - vector_graphics_compiler: - dependency: transitive - description: - name: vector_graphics_compiler - sha256: "03012b0a33775c5530576b70240308080e1d5050f0faf000118c20e6463bc0ad" - url: "https://pub.dev" - source: hosted - version: "1.1.9+2" vector_math: dependency: transitive description: @@ -581,6 +541,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.4" + visibility_detector: + dependency: transitive + description: + name: visibility_detector + sha256: dd5cc11e13494f432d15939c3aa8ae76844c42b723398643ce9addb88a5ed420 + url: "https://pub.dev" + source: hosted + version: "0.4.0+2" web: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index d4ccc51..faa4c54 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -33,14 +33,13 @@ dependencies: get: get_storage: file_picker: ^6.1.1 - flutter_markdown: google_fonts: - markdown: - flutter_highlighter: pasteboard: image: - flutter_markdown_latex: share_plus: + markdown: + markdown_widget: + flutter_highlight: dev_dependencies: flutter_test: