Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4b27c91

Browse files
committedApr 2, 2025
content: Support basic text styles for KaTeX content
This adds another experimental flag called `forceRenderKatex` which, if enabled, ignores any errors generated by the parser (like when encountering an unsupported CSS class) tries to do a "broken" render of the available span and their styles. Allowing the developer to test the different KaTeX content in the wild easily, while still in development.
1 parent 8a12a57 commit 4b27c91

File tree

5 files changed

+491
-36
lines changed

5 files changed

+491
-36
lines changed
 

‎lib/model/content.dart

+18-3
Original file line numberDiff line numberDiff line change
@@ -345,10 +345,12 @@ class CodeBlockSpanNode extends ContentNode {
345345
class MathBlockNode extends BlockContentNode {
346346
const MathBlockNode({
347347
super.debugHtmlNode,
348+
this.debugHasError = false,
348349
required this.texSource,
349350
required this.nodes,
350351
});
351352

353+
final bool debugHasError;
352354
final String texSource;
353355
final List<KatexNode>? nodes;
354356

@@ -366,11 +368,14 @@ class MathBlockNode extends BlockContentNode {
366368

367369
class KatexNode extends ContentNode {
368370
const KatexNode({
371+
required this.styles,
369372
required this.text,
370373
required this.nodes,
371374
super.debugHtmlNode,
372375
}) : assert((text != null) ^ (nodes != null));
373376

377+
final KatexSpanStyles styles;
378+
374379
/// The text or a single character this KaTeX span contains, generally
375380
/// observed to be the leaf node in the KaTeX HTML tree.
376381
/// It will be null if this span has child nodes.
@@ -383,6 +388,7 @@ class KatexNode extends ContentNode {
383388
@override
384389
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
385390
super.debugFillProperties(properties);
391+
properties.add(KatexSpanStylesProperty('styles', styles));
386392
properties.add(StringProperty('text', text));
387393
}
388394

@@ -856,10 +862,12 @@ class ImageEmojiNode extends EmojiNode {
856862
class MathInlineNode extends InlineContentNode {
857863
const MathInlineNode({
858864
super.debugHtmlNode,
865+
this.debugHasError = false,
859866
required this.texSource,
860867
required this.nodes,
861868
});
862869

870+
final bool debugHasError;
863871
final String texSource;
864872
final List<KatexNode>? nodes;
865873

@@ -898,7 +906,7 @@ class GlobalTimeNode extends InlineContentNode {
898906

899907
////////////////////////////////////////////////////////////////
900908
901-
({List<KatexNode>? spans, String texSource})? _parseMath(
909+
({List<KatexNode>? spans, bool debugHasError, String texSource})? _parseMath(
902910
dom.Element element, {
903911
required bool block,
904912
}) {
@@ -949,13 +957,18 @@ class GlobalTimeNode extends InlineContentNode {
949957
}
950958

951959
List<KatexNode>? spans;
960+
final parser = KatexParser();
952961
try {
953-
spans = KatexParser().parseKatexHTML(katexHtmlElement);
962+
spans = parser.parseKatexHTML(katexHtmlElement);
954963
} on KatexHtmlParseError catch (e, st) {
955964
assert(debugLog('$e\n$st'));
956965
}
957966

958-
return (spans: spans, texSource: texSource);
967+
return (
968+
spans: spans,
969+
debugHasError: parser.debugHasError,
970+
texSource: texSource,
971+
);
959972
} else {
960973
return null;
961974
}
@@ -976,6 +989,7 @@ class _ZulipInlineContentParser {
976989
return MathInlineNode(
977990
texSource: parsed.texSource,
978991
nodes: parsed.spans,
992+
debugHasError: parsed.debugHasError,
979993
debugHtmlNode: debugHtmlNode);
980994
}
981995

@@ -1683,6 +1697,7 @@ class _ZulipContentParser {
16831697
result.add(MathBlockNode(
16841698
texSource: parsed.texSource,
16851699
nodes: parsed.spans,
1700+
debugHasError: parsed.debugHasError,
16861701
debugHtmlNode: kDebugMode ? firstChild : null));
16871702
} else {
16881703
result.add(UnimplementedBlockContentNode(htmlNode: firstChild));

0 commit comments

Comments
 (0)
Please sign in to comment.