fix: support export markdown with inline math equation (#1164)

This commit is contained in:
Morn 2025-11-12 22:37:46 +08:00 • committed by GitHub
commit 7cc5d25788
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 31 additions and 1 deletions

View file

@ -15,8 +15,13 @@ class DeltaMarkdownEncoder extends Converter<Delta, String> {
if (op is TextInsert) { if (op is TextInsert) {
final attributes = op.attributes; final attributes = op.attributes;
if (attributes != null) { if (attributes != null) {
final formula = attributes[BuiltInAttributeKey.formula] ?? '';
buffer.write(_prefixSyntax(attributes)); buffer.write(_prefixSyntax(attributes));
buffer.write(op.text); if (formula.isNotEmpty) {
buffer.write(formula);
} else {
buffer.write(op.text);
}
buffer.write(_suffixSyntax(attributes)); buffer.write(_suffixSyntax(attributes));
} else { } else {
buffer.write(op.text); buffer.write(op.text);
@ -52,6 +57,10 @@ class DeltaMarkdownEncoder extends Converter<Delta, String> {
syntax += '['; syntax += '[';
} }
if (attributes[BuiltInAttributeKey.formula] != null) {
syntax += r'$';
}
return syntax; return syntax;
} }
@ -83,6 +92,10 @@ class DeltaMarkdownEncoder extends Converter<Delta, String> {
syntax += '_'; syntax += '_';
} }
if (attributes[BuiltInAttributeKey.formula] != null) {
syntax += r'$';
}
return syntax; return syntax;
} }
} }

View file

@ -137,5 +137,22 @@ void main() async {
'***<u>`Welcome`</u>*** ***~~to~~*** ***[AppFlowy](https://appflowy.io)***', '***<u>`Welcome`</u>*** ***~~to~~*** ***[AppFlowy](https://appflowy.io)***',
); );
}); });
test('formula', () {
final delta = Delta(
operations: [
TextInsert('This is a formula:'),
TextInsert(
'\$',
attributes: {
BuiltInAttributeKey.formula: 'E = MC ^ 2',
},
),
TextInsert('.'),
],
);
final result = DeltaMarkdownEncoder().convert(delta);
expect(result, 'This is a formula:\$E = MC ^ 2\$.');
});
}); });
} }