Skip to content

Commit 612c91e

Browse files
gnpricePIG208
authored andcommitted
content [nfc]: Encapsulate parsing math nodes a bit more
1 parent cb9d185 commit 612c91e

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

lib/model/content.dart

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -860,8 +860,11 @@ String? _parseMath(dom.Element element, {required bool block}) {
860860
/// instance has been reset to its starting state, and can be re-used for
861861
/// parsing other subtrees.
862862
class _ZulipInlineContentParser {
863-
String? parseInlineMath(dom.Element element) {
864-
return _parseMath(element, block: false);
863+
InlineContentNode? parseInlineMath(dom.Element element) {
864+
final debugHtmlNode = kDebugMode ? element : null;
865+
final texSource = _parseMath(element, block: false);
866+
if (texSource == null) return null;
867+
return MathInlineNode(texSource: texSource, debugHtmlNode: debugHtmlNode);
865868
}
866869

867870
UserMentionNode? parseUserMention(dom.Element element) {
@@ -1017,9 +1020,7 @@ class _ZulipInlineContentParser {
10171020
}
10181021

10191022
if (localName == 'span' && className == 'katex') {
1020-
final texSource = parseInlineMath(element);
1021-
if (texSource == null) return unimplemented();
1022-
return MathInlineNode(texSource: texSource, debugHtmlNode: debugHtmlNode);
1023+
return parseInlineMath(element) ?? unimplemented();
10231024
}
10241025

10251026
// TODO more types of node
@@ -1054,8 +1055,11 @@ class _ZulipContentParser {
10541055
return inlineParser.parseBlockInline(nodes);
10551056
}
10561057

1057-
String? parseMathBlock(dom.Element element) {
1058-
return _parseMath(element, block: true);
1058+
BlockContentNode parseMathBlock(dom.Element element) {
1059+
final debugHtmlNode = kDebugMode ? element : null;
1060+
final texSource = _parseMath(element, block: true);
1061+
if (texSource == null) return UnimplementedBlockContentNode(htmlNode: element);
1062+
return MathBlockNode(texSource: texSource, debugHtmlNode: debugHtmlNode);
10591063
}
10601064

10611065
BlockContentNode parseListNode(dom.Element element) {
@@ -1477,9 +1481,7 @@ class _ZulipContentParser {
14771481
// The case with the `<br>\n` can happen when at the end of a quote;
14781482
// it seems like a glitch in the server's Markdown processing,
14791483
// so hopefully there just aren't any further such glitches.
1480-
final texSource = parseMathBlock(child);
1481-
if (texSource == null) return UnimplementedBlockContentNode(htmlNode: node);
1482-
return MathBlockNode(texSource: texSource, debugHtmlNode: debugHtmlNode);
1484+
return parseMathBlock(child);
14831485
}
14841486
}
14851487
}

0 commit comments

Comments
 (0)