Skip to content

Commit

Permalink
feat: 支持markdown 渲染 公式
Browse files Browse the repository at this point in the history
  • Loading branch information
brzhang666 committed Apr 24, 2023
1 parent 1401825 commit 26de3f6
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 3 deletions.
76 changes: 76 additions & 0 deletions lib/components/latex.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import 'package:flutter/material.dart';
import 'package:markdown_widget/markdown_widget.dart';
import 'package:flutter_math_fork/flutter_math.dart';
import 'package:markdown/markdown.dart' as m;

SpanNodeGeneratorWithTag latexGenerator = SpanNodeGeneratorWithTag(
tag: _latexTag,
generator: (e, config, visitor) =>
LatexNode(e.attributes, e.textContent, config));

const _latexTag = 'latex';

class LatexSyntax extends m.InlineSyntax {
LatexSyntax() : super(r'(\$\$[\s\S]+\$\$)|(\$.+\$)');

@override
bool onMatch(m.InlineParser parser, Match match) {
final input = match.input;
final matchValue = input.substring(match.start, match.end);
String content = '';
bool isInline = true;
const blockSyntax = '\$\$';
const inlineSyntax = '\$';
if (matchValue.startsWith(blockSyntax) &&
matchValue.endsWith(blockSyntax) &&
(matchValue != blockSyntax)) {
content = matchValue.substring(2, matchValue.length - 2);
isInline = false;
} else if (matchValue.startsWith(inlineSyntax) &&
matchValue.endsWith(inlineSyntax) &&
matchValue != inlineSyntax) {
content = matchValue.substring(1, matchValue.length - 1);
}
m.Element el = m.Element.text(_latexTag, matchValue);
el.attributes['content'] = content;
el.attributes['isInline'] = '$isInline';
parser.addNode(el);
return true;
}
}

class LatexNode extends SpanNode {
final Map<String, String> attributes;
final String textContent;
final MarkdownConfig config;

LatexNode(this.attributes, this.textContent, this.config);

@override
InlineSpan build() {
final content = attributes['content'] ?? '';
final isInline = attributes['isInline'] == 'true';
final style = parentStyle ?? config.p.textStyle;
if (content.isEmpty) return TextSpan(style: style, text: textContent);
final latex = Math.tex(
content,
mathStyle: MathStyle.text,
// textStyle: style.copyWith(color: isDark ? Colors.white : Colors.black),
textScaleFactor: 1,
onErrorFallback: (error) {
return Text(
textContent,
style: style.copyWith(color: Colors.red),
);
},
);
return WidgetSpan(
child: !isInline
? Container(
width: double.infinity,
margin: const EdgeInsets.symmetric(vertical: 16),
child: Center(child: latex),
)
: latex);
}
}
3 changes: 3 additions & 0 deletions lib/components/markdown.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_chatgpt/components/code_wrapper.dart';
import 'package:flutter_chatgpt/components/latex.dart';
import 'package:markdown_widget/markdown_widget.dart';

class Markdown extends StatelessWidget {
Expand All @@ -19,6 +20,8 @@ class Markdown extends StatelessWidget {
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: MarkdownGenerator(
inlineSyntaxes: [LatexSyntax()],
generators: [latexGenerator],
config: config.copy(configs: [
isDark
? PreConfig.darkConfig.copy(wrapper: codeWrapper)
Expand Down
62 changes: 59 additions & 3 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,14 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_math_fork:
dependency: "direct main"
description:
name: flutter_math_fork
sha256: fa511bdcb91fc35eac155ec5790883886dd9de6572fea549f1ad4660ef6804e9
url: "https://pub.dev"
source: hosted
version: "0.6.3+1"
flutter_spinkit:
dependency: transitive
description:
Expand All @@ -219,6 +227,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "5.1.0"
flutter_svg:
dependency: transitive
description:
name: flutter_svg
sha256: "6ff9fa12892ae074092de2fa6a9938fb21dbabfdaa2ff57dc697ff912fc8d4b2"
url: "https://pub.dev"
source: hosted
version: "1.1.6"
flutter_test:
dependency: "direct dev"
description: flutter
Expand Down Expand Up @@ -358,13 +374,13 @@ packages:
source: hosted
version: "1.1.1"
markdown:
dependency: transitive
dependency: "direct main"
description:
name: markdown
sha256: b3c60dee8c2af50ad0e6e90cceba98e47718a6ee0a7a6772c77846a0cc21f78b
sha256: d95a9d12954aafc97f984ca29baaa7690ed4d9ec4140a23ad40580bcdb6c87f5
url: "https://pub.dev"
source: hosted
version: "7.0.1"
version: "7.0.2"
markdown_widget:
dependency: "direct main"
description:
Expand Down Expand Up @@ -421,6 +437,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.8.2"
path_drawing:
dependency: transitive
description:
name: path_drawing
sha256: bbb1934c0cbb03091af082a6389ca2080345291ef07a5fa6d6e078ba8682f977
url: "https://pub.dev"
source: hosted
version: "1.0.1"
path_parsing:
dependency: transitive
description:
name: path_parsing
sha256: e3e67b1629e6f7e8100b367d3db6ba6af4b1f0bb80f64db18ef1fbabd2fa9ccf
url: "https://pub.dev"
source: hosted
version: "1.0.1"
path_provider:
dependency: "direct main"
description:
Expand Down Expand Up @@ -469,6 +501,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.5"
petitparser:
dependency: transitive
description:
name: petitparser
sha256: "49392a45ced973e8d94a85fdb21293fbb40ba805fc49f2965101ae748a3683b4"
url: "https://pub.dev"
source: hosted
version: "5.1.0"
platform:
dependency: transitive
description:
Expand Down Expand Up @@ -610,6 +650,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.4.16"
tuple:
dependency: transitive
description:
name: tuple
sha256: "0ea99cd2f9352b2586583ab2ce6489d1f95a5f6de6fb9492faaf97ae2060f0aa"
url: "https://pub.dev"
source: hosted
version: "2.0.1"
typed_data:
dependency: transitive
description:
Expand Down Expand Up @@ -730,6 +778,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.0"
xml:
dependency: transitive
description:
name: xml
sha256: "979ee37d622dec6365e2efa4d906c37470995871fe9ae080d967e192d88286b5"
url: "https://pub.dev"
source: hosted
version: "6.2.2"
yaml:
dependency: transitive
description:
Expand Down
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ dependencies:
uuid: ^3.0.7
dart_openai: ^1.8.4
markdown_widget: ^2.1.0
flutter_math_fork: ^0.6.3+1
markdown: ^7.0.2

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 26de3f6

Please sign in to comment.