Skip to content

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Binary file added assets/KaTeX/KaTeX_AMS-Regular.ttf
Binary file not shown.
Binary file added assets/KaTeX/KaTeX_Caligraphic-Bold.ttf
Binary file not shown.
Binary file added assets/KaTeX/KaTeX_Caligraphic-Regular.ttf
Binary file not shown.
Binary file added assets/KaTeX/KaTeX_Fraktur-Bold.ttf
Binary file not shown.
Binary file added assets/KaTeX/KaTeX_Fraktur-Regular.ttf
Binary file not shown.
Binary file added assets/KaTeX/KaTeX_Main-Bold.ttf
Binary file not shown.
Binary file added assets/KaTeX/KaTeX_Main-BoldItalic.ttf
Binary file not shown.
Binary file added assets/KaTeX/KaTeX_Main-Italic.ttf
Binary file not shown.
Binary file added assets/KaTeX/KaTeX_Main-Regular.ttf
Binary file not shown.
Binary file added assets/KaTeX/KaTeX_Math-BoldItalic.ttf
Binary file not shown.
Binary file added assets/KaTeX/KaTeX_Math-Italic.ttf
Binary file not shown.
Binary file added assets/KaTeX/KaTeX_SansSerif-Bold.ttf
Binary file not shown.
Binary file added assets/KaTeX/KaTeX_SansSerif-Italic.ttf
Binary file not shown.
Binary file added assets/KaTeX/KaTeX_SansSerif-Regular.ttf
Binary file not shown.
Binary file added assets/KaTeX/KaTeX_Script-Regular.ttf
Binary file not shown.
Binary file added assets/KaTeX/KaTeX_Size1-Regular.ttf
Binary file not shown.
Binary file added assets/KaTeX/KaTeX_Size2-Regular.ttf
Binary file not shown.
Binary file added assets/KaTeX/KaTeX_Size3-Regular.ttf
Binary file not shown.
Binary file added assets/KaTeX/KaTeX_Size4-Regular.ttf
Binary file not shown.
Binary file added assets/KaTeX/KaTeX_Typewriter-Regular.ttf
Binary file not shown.
21 changes: 21 additions & 0 deletions assets/KaTeX/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fonts: Add KaTeX custom fonts
    
These fonts will be used in later commits to show KaTeX
content.

For future reference, perhaps we can also mention where we found these fonts in the commit message with a link.


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.
3 changes: 3 additions & 0 deletions lib/licenses.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import 'package:flutter/services.dart';
Stream<LicenseEntry> additionalLicenses() async* {
// Alphabetic by path.

yield LicenseEntryWithLineBreaks(
['KaTeX'],
await rootBundle.loadString('assets/KaTeX/LICENSE'));
yield LicenseEntryWithLineBreaks(
['Noto Color Emoji'],
await rootBundle.loadString('assets/Noto_Color_Emoji/LICENSE'));
Expand Down
20 changes: 18 additions & 2 deletions lib/model/binding.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// 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();
/// Get the app's singleton [GlobalStore] if already loaded, else null.
///
/// Where possible, use [GlobalStoreWidget.of] to get access to a [GlobalStore].
/// Use this method only in contexts where getting access to a [BuildContext]
/// is inconvenient.
GlobalStore? getGlobalStoreSync();

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],
Expand Down Expand Up @@ -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() {
Expand Down
134 changes: 65 additions & 69 deletions lib/model/content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should get dartdoc in particular to clarify what null means.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, in fact I guess it can just copy what MathParserResult.nodes has 🙂


@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
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: https://dart.dev/effective-dart/documentation#do-separate-the-first-sentence-of-a-doc-comment-into-its-own-paragraph

Suggested change
/// The child nodes of this node in the KaTeX HTML tree.
/// It will be null if [text] is non-null.
final List<KatexNode>? nodes;
/// The child nodes of this node in the KaTeX HTML tree.
///
/// It will be null if [text] is non-null.
final List<KatexNode>? nodes;


@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(StringProperty('texSource', texSource));
properties.add(KatexSpanStylesProperty('styles', styles));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat but can be simplified:

Suggested change
properties.add(KatexSpanStylesProperty('styles', styles));
properties.add(DiagnosticsProperty<KatexSpanStyles>('styles', styles));

In the Flutter tree that seems to be standard practice — search for references to the DiagnosticsProperty constructor and you'll find lots of call sites, for example as DiagnosticsProperty<RenderObject>.

properties.add(StringProperty('text', text));
}

@override
List<DiagnosticsNode> debugDescribeChildren() {
return nodes?.map((node) => node.toDiagnosticsNode()).toList() ?? const [];
}
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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].
Expand All @@ -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) {
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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;
}
Expand Down
Loading