@@ -5,7 +5,9 @@ import 'package:html/parser.dart';
5
5
6
6
import '../api/model/model.dart' ;
7
7
import '../api/model/submessage.dart' ;
8
+ import '../log.dart' ;
8
9
import 'code_block.dart' ;
10
+ import 'katex.dart' ;
9
11
10
12
/// A node in a parse tree for Zulip message-style content.
11
13
///
@@ -340,24 +342,83 @@ class CodeBlockSpanNode extends ContentNode {
340
342
}
341
343
}
342
344
343
- class MathBlockNode extends BlockContentNode {
344
- const MathBlockNode ({super .debugHtmlNode, required this .texSource});
345
+ sealed class BaseKatexSpan extends ContentNode {
346
+ const BaseKatexSpan ({super .debugHtmlNode});
347
+ }
345
348
346
- final String texSource;
349
+ class KatexNegativeRightMarginSpans extends BaseKatexSpan {
350
+ const KatexNegativeRightMarginSpans ({
351
+ required this .marginRightEm,
352
+ this .spans = const [],
353
+ super .debugHtmlNode,
354
+ });
355
+
356
+ final double marginRightEm;
357
+ final List <KatexSpan > spans;
347
358
348
359
@override
349
- bool operator == ( Object other ) {
350
- return other is MathBlockNode && other.texSource == texSource ;
360
+ List < DiagnosticsNode > debugDescribeChildren ( ) {
361
+ return spans. map ((node) => node. toDiagnosticsNode ()). toList () ;
351
362
}
363
+ }
364
+
365
+ class KatexSpan extends BaseKatexSpan {
366
+ const KatexSpan ({
367
+ required this .text,
368
+ required this .styles,
369
+
370
+ required this .classes,
371
+ required this .ancestorClasses,
372
+
373
+ required this .spans,
374
+
375
+ super .debugHtmlNode,
376
+ });
377
+
378
+ final String ? text;
379
+ final KatexSpanStyles styles;
380
+
381
+ final List <String > classes;
382
+ final List <List <String >> ancestorClasses;
383
+
384
+ final List <BaseKatexSpan > spans;
352
385
353
386
@override
354
- int get hashCode => Object .hash ('MathBlockNode' , texSource);
387
+ void debugFillProperties (DiagnosticPropertiesBuilder properties) {
388
+ super .debugFillProperties (properties);
389
+ properties.add (StringProperty ('text' , text));
390
+ properties.add (KatexSpanStylesProperty ('styles' , styles));
391
+ properties.add (StringProperty ('classes' , classes.join (', ' )));
392
+ properties.add (StringProperty ('ancestorClasses' , ancestorClasses.map (
393
+ (e) => '[${e .join (', ' )}]' ).join (', ' )));
394
+ }
395
+
396
+ @override
397
+ List <DiagnosticsNode > debugDescribeChildren () {
398
+ return spans.map ((node) => node.toDiagnosticsNode ()).toList ();
399
+ }
400
+ }
401
+
402
+ class MathBlockNode extends BlockContentNode {
403
+ const MathBlockNode ({
404
+ super .debugHtmlNode,
405
+ required this .texSource,
406
+ required this .spans,
407
+ });
408
+
409
+ final String texSource;
410
+ final List <BaseKatexSpan >? spans;
355
411
356
412
@override
357
413
void debugFillProperties (DiagnosticPropertiesBuilder properties) {
358
414
super .debugFillProperties (properties);
359
415
properties.add (StringProperty ('texSource' , texSource));
360
416
}
417
+
418
+ @override
419
+ List <DiagnosticsNode > debugDescribeChildren () {
420
+ return spans? .map ((node) => node.toDiagnosticsNode ()).toList () ?? const [];
421
+ }
361
422
}
362
423
363
424
class ImageNodeList extends BlockContentNode {
@@ -1626,8 +1687,15 @@ class _ZulipContentParser {
1626
1687
final firstChild = nodes.first as dom.Element ;
1627
1688
final texSource = _parseMath (firstChild, block: true );
1628
1689
if (texSource != null ) {
1690
+ List <BaseKatexSpan >? katexSpans;
1691
+ try {
1692
+ katexSpans = KatexParser ().parseKatexBlock (firstChild);
1693
+ } on KatexHtmlParseError catch (e, st) {
1694
+ assert (debugLog ('$e \n $st ' ));
1695
+ }
1629
1696
result.add (MathBlockNode (
1630
1697
texSource: texSource,
1698
+ spans: katexSpans,
1631
1699
debugHtmlNode: kDebugMode ? firstChild : null ));
1632
1700
} else {
1633
1701
result.add (UnimplementedBlockContentNode (htmlNode: firstChild));
@@ -1661,8 +1729,15 @@ class _ZulipContentParser {
1661
1729
if (child case dom.Element (localName: 'span' , className: 'katex-display' )) {
1662
1730
final texSource = _parseMath (child, block: true );
1663
1731
if (texSource != null ) {
1732
+ List <BaseKatexSpan >? katexSpans;
1733
+ try {
1734
+ katexSpans = KatexParser ().parseKatexBlock (child);
1735
+ } on KatexHtmlParseError catch (e, st) {
1736
+ assert (debugLog ('$e \n $st ' ));
1737
+ }
1664
1738
result.add (MathBlockNode (
1665
1739
texSource: texSource,
1740
+ spans: katexSpans,
1666
1741
debugHtmlNode: debugHtmlNode));
1667
1742
continue ;
1668
1743
}
0 commit comments