Skip to content

Commit

Permalink
refactor: migrate to markdown_widget
Browse files Browse the repository at this point in the history
  • Loading branch information
PeronGH committed Jan 23, 2024
1 parent f5034e4 commit 48ab0e7
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 168 deletions.
23 changes: 0 additions & 23 deletions lib/utils/latex.dart

This file was deleted.

5 changes: 5 additions & 0 deletions lib/utils/map.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
extension CopyWithExtension<K, V> on Map<K, V> {
Map<K, V> copyWith(K key, V value) {
return Map<K, V>.from(this)..[key] = value;
}
}
30 changes: 9 additions & 21 deletions lib/widgets/code_element.dart
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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(),
Expand Down
40 changes: 12 additions & 28 deletions lib/widgets/message_tile.dart
Original file line number Diff line number Diff line change
@@ -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({
Expand Down Expand Up @@ -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
],
Expand Down
152 changes: 60 additions & 92 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down
Loading

0 comments on commit 48ab0e7

Please sign in to comment.