-
Notifications
You must be signed in to change notification settings - Fork 305
KaTeX (1/n): Initial support for displaying basic KaTeX content #1408
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
f7a4721
b4b38d7
0106b95
671fa12
1fca686
7609852
c273b0a
0a8c846
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2013-2020 Khan Academy and other contributors | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -83,6 +83,13 @@ abstract class ZulipBinding { | |||||||||||||||||||||||||
/// a widget tree may not exist. | ||||||||||||||||||||||||||
Future<GlobalStore> getGlobalStore(); | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
/// Get the app's singleton [GlobalStore], null if not already loaded. | ||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||
/// Where possible, use [GlobalStoreWidget.of] to get access to a [GlobalStore]. | ||||||||||||||||||||||||||
/// Use this method only in contexts like notifications where | ||||||||||||||||||||||||||
/// a widget tree may not exist. | ||||||||||||||||||||||||||
GlobalStore? getGlobalStoreSync(); | ||||||||||||||||||||||||||
Comment on lines
+86
to
+91
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Because this new method doesn't cause the global store to get loaded if it wasn't already, it's really most useful in contexts where a widget tree does presumably exist (because that will have caused the global store to get loaded). |
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
/// Like [getGlobalStore], but assert this method was not previously called. | ||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||
/// This is used by the implementation of [GlobalStoreWidget], | ||||||||||||||||||||||||||
|
@@ -333,8 +340,17 @@ class LiveZulipBinding extends ZulipBinding { | |||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
@override | ||||||||||||||||||||||||||
Future<GlobalStore> getGlobalStore() => _globalStore ??= LiveGlobalStore.load(); | ||||||||||||||||||||||||||
Future<GlobalStore>? _globalStore; | ||||||||||||||||||||||||||
Future<GlobalStore> getGlobalStore() { | ||||||||||||||||||||||||||
return _globalStoreFuture ??= LiveGlobalStore.load().then((store) { | ||||||||||||||||||||||||||
return _globalStore = store; | ||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
@override | ||||||||||||||||||||||||||
GlobalStore? getGlobalStoreSync() => _globalStore; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
Future<GlobalStore>? _globalStoreFuture; | ||||||||||||||||||||||||||
GlobalStore? _globalStore; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
@override | ||||||||||||||||||||||||||
Future<GlobalStore> getGlobalStoreUniquely() { | ||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -6,6 +6,7 @@ import 'package:html/parser.dart'; | |||||||||||||||
import '../api/model/model.dart'; | ||||||||||||||||
import '../api/model/submessage.dart'; | ||||||||||||||||
import 'code_block.dart'; | ||||||||||||||||
import 'katex.dart'; | ||||||||||||||||
|
||||||||||||||||
/// A node in a parse tree for Zulip message-style content. | ||||||||||||||||
/// | ||||||||||||||||
|
@@ -341,22 +342,56 @@ class CodeBlockSpanNode extends ContentNode { | |||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
class MathBlockNode extends BlockContentNode { | ||||||||||||||||
const MathBlockNode({super.debugHtmlNode, required this.texSource}); | ||||||||||||||||
const MathBlockNode({ | ||||||||||||||||
super.debugHtmlNode, | ||||||||||||||||
required this.texSource, | ||||||||||||||||
required this.nodes, | ||||||||||||||||
}); | ||||||||||||||||
|
||||||||||||||||
final String texSource; | ||||||||||||||||
final List<KatexNode>? nodes; | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should get dartdoc in particular to clarify what null means. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, in fact I guess it can just copy what |
||||||||||||||||
|
||||||||||||||||
@override | ||||||||||||||||
bool operator ==(Object other) { | ||||||||||||||||
return other is MathBlockNode && other.texSource == texSource; | ||||||||||||||||
void debugFillProperties(DiagnosticPropertiesBuilder properties) { | ||||||||||||||||
super.debugFillProperties(properties); | ||||||||||||||||
properties.add(StringProperty('texSource', texSource)); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
@override | ||||||||||||||||
int get hashCode => Object.hash('MathBlockNode', texSource); | ||||||||||||||||
List<DiagnosticsNode> debugDescribeChildren() { | ||||||||||||||||
return nodes?.map((node) => node.toDiagnosticsNode()).toList() ?? const []; | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
class KatexNode extends ContentNode { | ||||||||||||||||
const KatexNode({ | ||||||||||||||||
required this.styles, | ||||||||||||||||
required this.text, | ||||||||||||||||
required this.nodes, | ||||||||||||||||
super.debugHtmlNode, | ||||||||||||||||
}) : assert((text != null) ^ (nodes != null)); | ||||||||||||||||
|
||||||||||||||||
final KatexSpanStyles styles; | ||||||||||||||||
|
||||||||||||||||
/// The text or a single character this KaTeX node contains, generally | ||||||||||||||||
/// observed to be the leaf node in the KaTeX HTML tree. | ||||||||||||||||
/// It will be null if [nodes] is non-null. | ||||||||||||||||
Comment on lines
+376
to
+378
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a case when this is present and the node wasn't a leaf in the KaTeX HTML tree? Between this doc and the doc on [nodes], it sounds like there can't be — if this is non-null, then [nodes] is null, which means there are no child nodes of this node in the KaTeX HTML tree, which is the definition of this node being a leaf in that tree. |
||||||||||||||||
final String? text; | ||||||||||||||||
Comment on lines
+376
to
+379
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is "a single character" a different case from "the text"? It sounds like that's just the text when the text happens to be only one character. |
||||||||||||||||
|
||||||||||||||||
/// The child nodes of this node in the KaTeX HTML tree. | ||||||||||||||||
/// It will be null if [text] is non-null. | ||||||||||||||||
final List<KatexNode>? nodes; | ||||||||||||||||
Comment on lines
+381
to
+383
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
|
||||||||||||||||
@override | ||||||||||||||||
void debugFillProperties(DiagnosticPropertiesBuilder properties) { | ||||||||||||||||
super.debugFillProperties(properties); | ||||||||||||||||
properties.add(StringProperty('texSource', texSource)); | ||||||||||||||||
properties.add(KatexSpanStylesProperty('styles', styles)); | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Neat but can be simplified:
Suggested change
In the Flutter tree that seems to be standard practice — search for references to the |
||||||||||||||||
properties.add(StringProperty('text', text)); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
@override | ||||||||||||||||
List<DiagnosticsNode> debugDescribeChildren() { | ||||||||||||||||
return nodes?.map((node) => node.toDiagnosticsNode()).toList() ?? const []; | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
|
@@ -822,23 +857,25 @@ class ImageEmojiNode extends EmojiNode { | |||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
class MathInlineNode extends InlineContentNode { | ||||||||||||||||
const MathInlineNode({super.debugHtmlNode, required this.texSource}); | ||||||||||||||||
const MathInlineNode({ | ||||||||||||||||
super.debugHtmlNode, | ||||||||||||||||
required this.texSource, | ||||||||||||||||
required this.nodes, | ||||||||||||||||
}); | ||||||||||||||||
|
||||||||||||||||
final String texSource; | ||||||||||||||||
|
||||||||||||||||
@override | ||||||||||||||||
bool operator ==(Object other) { | ||||||||||||||||
return other is MathInlineNode && other.texSource == texSource; | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
@override | ||||||||||||||||
int get hashCode => Object.hash('MathInlineNode', texSource); | ||||||||||||||||
final List<KatexNode>? nodes; | ||||||||||||||||
|
||||||||||||||||
@override | ||||||||||||||||
void debugFillProperties(DiagnosticPropertiesBuilder properties) { | ||||||||||||||||
super.debugFillProperties(properties); | ||||||||||||||||
properties.add(StringProperty('texSource', texSource)); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
@override | ||||||||||||||||
List<DiagnosticsNode> debugDescribeChildren() { | ||||||||||||||||
return nodes?.map((node) => node.toDiagnosticsNode()).toList() ?? const []; | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
class GlobalTimeNode extends InlineContentNode { | ||||||||||||||||
|
@@ -864,52 +901,6 @@ class GlobalTimeNode extends InlineContentNode { | |||||||||||||||
|
||||||||||||||||
//////////////////////////////////////////////////////////////// | ||||||||||||||||
|
||||||||||||||||
String? _parseMath(dom.Element element, {required bool block}) { | ||||||||||||||||
final dom.Element katexElement; | ||||||||||||||||
if (!block) { | ||||||||||||||||
assert(element.localName == 'span' && element.className == 'katex'); | ||||||||||||||||
|
||||||||||||||||
katexElement = element; | ||||||||||||||||
} else { | ||||||||||||||||
assert(element.localName == 'span' && element.className == 'katex-display'); | ||||||||||||||||
|
||||||||||||||||
if (element.nodes.length != 1) return null; | ||||||||||||||||
final child = element.nodes.single; | ||||||||||||||||
if (child is! dom.Element) return null; | ||||||||||||||||
if (child.localName != 'span') return null; | ||||||||||||||||
if (child.className != 'katex') return null; | ||||||||||||||||
katexElement = child; | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
// Expect two children span.katex-mathml, span.katex-html . | ||||||||||||||||
// For now we only care about the .katex-mathml . | ||||||||||||||||
if (katexElement.nodes.isEmpty) return null; | ||||||||||||||||
final child = katexElement.nodes.first; | ||||||||||||||||
if (child is! dom.Element) return null; | ||||||||||||||||
if (child.localName != 'span') return null; | ||||||||||||||||
if (child.className != 'katex-mathml') return null; | ||||||||||||||||
|
||||||||||||||||
if (child.nodes.length != 1) return null; | ||||||||||||||||
final grandchild = child.nodes.single; | ||||||||||||||||
if (grandchild is! dom.Element) return null; | ||||||||||||||||
if (grandchild.localName != 'math') return null; | ||||||||||||||||
if (grandchild.attributes['display'] != (block ? 'block' : null)) return null; | ||||||||||||||||
if (grandchild.namespaceUri != 'http://www.w3.org/1998/Math/MathML') return null; | ||||||||||||||||
|
||||||||||||||||
if (grandchild.nodes.length != 1) return null; | ||||||||||||||||
final greatgrand = grandchild.nodes.single; | ||||||||||||||||
if (greatgrand is! dom.Element) return null; | ||||||||||||||||
if (greatgrand.localName != 'semantics') return null; | ||||||||||||||||
|
||||||||||||||||
if (greatgrand.nodes.isEmpty) return null; | ||||||||||||||||
final descendant4 = greatgrand.nodes.last; | ||||||||||||||||
if (descendant4 is! dom.Element) return null; | ||||||||||||||||
if (descendant4.localName != 'annotation') return null; | ||||||||||||||||
if (descendant4.attributes['encoding'] != 'application/x-tex') return null; | ||||||||||||||||
|
||||||||||||||||
return descendant4.text.trim(); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
/// Parser for the inline-content subtrees within Zulip content HTML. | ||||||||||||||||
/// | ||||||||||||||||
/// The only entry point to this class is [parseBlockInline]. | ||||||||||||||||
|
@@ -920,9 +911,12 @@ String? _parseMath(dom.Element element, {required bool block}) { | |||||||||||||||
class _ZulipInlineContentParser { | ||||||||||||||||
InlineContentNode? parseInlineMath(dom.Element element) { | ||||||||||||||||
final debugHtmlNode = kDebugMode ? element : null; | ||||||||||||||||
final texSource = _parseMath(element, block: false); | ||||||||||||||||
if (texSource == null) return null; | ||||||||||||||||
return MathInlineNode(texSource: texSource, debugHtmlNode: debugHtmlNode); | ||||||||||||||||
final parsed = parseMath(element, block: false); | ||||||||||||||||
if (parsed == null) return null; | ||||||||||||||||
return MathInlineNode( | ||||||||||||||||
texSource: parsed.texSource, | ||||||||||||||||
nodes: parsed.nodes, | ||||||||||||||||
debugHtmlNode: debugHtmlNode); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
UserMentionNode? parseUserMention(dom.Element element) { | ||||||||||||||||
|
@@ -1624,10 +1618,11 @@ class _ZulipContentParser { | |||||||||||||||
})()); | ||||||||||||||||
|
||||||||||||||||
final firstChild = nodes.first as dom.Element; | ||||||||||||||||
final texSource = _parseMath(firstChild, block: true); | ||||||||||||||||
if (texSource != null) { | ||||||||||||||||
final parsed = parseMath(firstChild, block: true); | ||||||||||||||||
if (parsed != null) { | ||||||||||||||||
result.add(MathBlockNode( | ||||||||||||||||
texSource: texSource, | ||||||||||||||||
texSource: parsed.texSource, | ||||||||||||||||
nodes: parsed.nodes, | ||||||||||||||||
debugHtmlNode: kDebugMode ? firstChild : null)); | ||||||||||||||||
} else { | ||||||||||||||||
result.add(UnimplementedBlockContentNode(htmlNode: firstChild)); | ||||||||||||||||
|
@@ -1659,10 +1654,11 @@ class _ZulipContentParser { | |||||||||||||||
if (child case dom.Text(text: '\n\n')) continue; | ||||||||||||||||
|
||||||||||||||||
if (child case dom.Element(localName: 'span', className: 'katex-display')) { | ||||||||||||||||
final texSource = _parseMath(child, block: true); | ||||||||||||||||
if (texSource != null) { | ||||||||||||||||
final parsed = parseMath(child, block: true); | ||||||||||||||||
if (parsed != null) { | ||||||||||||||||
result.add(MathBlockNode( | ||||||||||||||||
texSource: texSource, | ||||||||||||||||
texSource: parsed.texSource, | ||||||||||||||||
nodes: parsed.nodes, | ||||||||||||||||
debugHtmlNode: debugHtmlNode)); | ||||||||||||||||
continue; | ||||||||||||||||
} | ||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For future reference, perhaps we can also mention where we found these fonts in the commit message with a link.