Skip to content

Commit c87d48b

Browse files
rajveermalviyagnprice
authored andcommitted
content: Move math block parsing to the callers of parseBlockContent
Prepares for zulip#1130, this commit is almost NFC with only difference that, in an error case we previously emitted a `ParagraphNode` containing an `UnimplementedInlineContentNode` (along with any adjacent nodes), now we emit a single `UnimplementedBlockContentNode` instead.
1 parent 043ae10 commit c87d48b

File tree

1 file changed

+47
-15
lines changed

1 file changed

+47
-15
lines changed

lib/model/content.dart

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,21 +1471,6 @@ class _ZulipContentParser {
14711471
}
14721472

14731473
if (localName == 'p' && className.isEmpty) {
1474-
// Oddly, the way a math block gets encoded in Zulip HTML is inside a <p>.
1475-
if (element.nodes case [dom.Element(localName: 'span') && var child, ...]) {
1476-
if (child.className == 'katex-display') {
1477-
if (element.nodes case [_]
1478-
|| [_, dom.Element(localName: 'br'),
1479-
dom.Text(text: "\n")]) {
1480-
// This might be too specific; we'll find out when we do #190.
1481-
// The case with the `<br>\n` can happen when at the end of a quote;
1482-
// it seems like a glitch in the server's Markdown processing,
1483-
// so hopefully there just aren't any further such glitches.
1484-
return parseMathBlock(child);
1485-
}
1486-
}
1487-
}
1488-
14891474
final parsed = parseBlockInline(element.nodes);
14901475
return ParagraphNode(debugHtmlNode: debugHtmlNode,
14911476
links: parsed.links,
@@ -1599,6 +1584,30 @@ class _ZulipContentParser {
15991584
for (final node in nodes) {
16001585
if (node is dom.Text && (node.text == '\n')) continue;
16011586

1587+
// Oddly, the way a math block gets encoded in Zulip HTML is inside a <p>.
1588+
if (node case dom.Element(localName: 'p', className: '', nodes: [
1589+
dom.Element(
1590+
localName: 'span',
1591+
className: 'katex-display') && final child, ...])) {
1592+
final BlockContentNode parsed;
1593+
if (node.nodes case [_]
1594+
|| [_, dom.Element(localName: 'br'),
1595+
dom.Text(text: "\n")]) {
1596+
// This might be too specific; we'll find out when we do #190.
1597+
// The case with the `<br>\n` can happen when at the end of a quote;
1598+
// it seems like a glitch in the server's Markdown processing,
1599+
// so hopefully there just aren't any further such glitches.
1600+
parsed = parseMathBlock(child);
1601+
} else {
1602+
parsed = UnimplementedBlockContentNode(htmlNode: node);
1603+
}
1604+
1605+
if (currentParagraph.isNotEmpty) consumeParagraph();
1606+
if (imageNodes.isNotEmpty) consumeImageNodes();
1607+
result.add(parsed);
1608+
continue;
1609+
}
1610+
16021611
if (_isPossibleInlineNode(node)) {
16031612
if (imageNodes.isNotEmpty) {
16041613
consumeImageNodes();
@@ -1642,6 +1651,29 @@ class _ZulipContentParser {
16421651
continue;
16431652
}
16441653

1654+
// Oddly, the way a math block gets encoded in Zulip HTML is inside a <p>.
1655+
if (node case dom.Element(localName: 'p', className: '', nodes: [
1656+
dom.Element(
1657+
localName: 'span',
1658+
className: 'katex-display') && final child, ...])) {
1659+
final BlockContentNode parsed;
1660+
if (node.nodes case [_]
1661+
|| [_, dom.Element(localName: 'br'),
1662+
dom.Text(text: "\n")]) {
1663+
// This might be too specific; we'll find out when we do #190.
1664+
// The case with the `<br>\n` can happen when at the end of a quote;
1665+
// it seems like a glitch in the server's Markdown processing,
1666+
// so hopefully there just aren't any further such glitches.
1667+
parsed = parseMathBlock(child);
1668+
} else {
1669+
parsed = UnimplementedBlockContentNode(htmlNode: node);
1670+
}
1671+
1672+
if (imageNodes.isNotEmpty) consumeImageNodes();
1673+
result.add(parsed);
1674+
continue;
1675+
}
1676+
16451677
final block = parseBlockContent(node);
16461678
if (block is ImageNode) {
16471679
imageNodes.add(block);

0 commit comments

Comments
 (0)