Skip to content

Commit dd03f72

Browse files
committed
content: Parse code block data-code-language attribute
We don't currently need this -- I wrote it when I thought proper styling depended on knowing whether the code block was declared with a language or not -- but it took some thinking-through to get just right, so might as well do it now in case we want it later (e.g., to include the language attribute in Flutter Semantics for code blocks).
1 parent fb01562 commit dd03f72

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

Diff for: lib/model/content.dart

+15-2
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,20 @@ class SpoilerNode extends BlockContentNode {
287287
}
288288

289289
class CodeBlockNode extends BlockContentNode {
290-
const CodeBlockNode(this.spans, {super.debugHtmlNode});
290+
const CodeBlockNode(
291+
this.spans, {
292+
this.codeLanguage,
293+
super.debugHtmlNode,
294+
});
291295

292296
final List<CodeBlockSpanNode> spans;
297+
final String? codeLanguage;
298+
299+
@override
300+
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
301+
super.debugFillProperties(properties);
302+
properties.add(StringProperty('codeLanguage', codeLanguage));
303+
}
293304

294305
@override
295306
List<DiagnosticsNode> debugDescribeChildren() {
@@ -943,9 +954,11 @@ class _ZulipContentParser {
943954

944955
BlockContentNode parseCodeBlock(dom.Element divElement) {
945956
assert(_debugParserContext == _ParserContext.block);
957+
String? codeLanguage;
946958
final mainElement = () {
947959
assert(divElement.localName == 'div'
948960
&& divElement.className == "codehilite");
961+
codeLanguage = divElement.attributes['data-code-language'];
949962

950963
if (divElement.nodes.length != 1) return null;
951964
final child = divElement.nodes[0];
@@ -1009,7 +1022,7 @@ class _ZulipContentParser {
10091022
spans.add(span);
10101023
}
10111024

1012-
return CodeBlockNode(spans, debugHtmlNode: debugHtmlNode);
1025+
return CodeBlockNode(spans, codeLanguage: codeLanguage, debugHtmlNode: debugHtmlNode);
10131026
}
10141027

10151028
BlockContentNode parseImageNode(dom.Element divElement) {

Diff for: test/model/content_test.dart

+6-5
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ class ContentExample {
281281
"```\nverb\natim\n```",
282282
expectedText: 'verb\natim',
283283
'<div class="codehilite"><pre><span></span><code>verb\natim\n</code></pre></div>', [
284-
CodeBlockNode([
284+
CodeBlockNode(codeLanguage: null, [
285285
CodeBlockSpanNode(text: 'verb\natim', type: CodeBlockSpanType.text),
286286
]),
287287
]);
@@ -294,7 +294,7 @@ class ContentExample {
294294
'<span></span><code><span class="kd">class</span><span class="w"> </span>'
295295
'<span class="nc">A</span><span class="w"> </span><span class="p">{}</span>'
296296
'\n</code></pre></div>', [
297-
CodeBlockNode([
297+
CodeBlockNode(codeLanguage: 'Dart', [
298298
CodeBlockSpanNode(text: 'class', type: CodeBlockSpanType.keywordDeclaration),
299299
CodeBlockSpanNode(text: ' ', type: CodeBlockSpanType.whitespace),
300300
CodeBlockSpanNode(text: 'A', type: CodeBlockSpanType.nameClass),
@@ -316,7 +316,7 @@ class ContentExample {
316316
'<span class="s">"world!</span><span class="se">\\n</span><span class="s">"</span>'
317317
'<span class="p">);</span>\n<span class="p">}</span>\n'
318318
'</code></pre></div>', [
319-
CodeBlockNode([
319+
CodeBlockNode(codeLanguage: 'Rust', [
320320
CodeBlockSpanNode(text: 'fn', type: CodeBlockSpanType.keyword),
321321
CodeBlockSpanNode(text: ' ', type: CodeBlockSpanType.text),
322322
CodeBlockSpanNode(text: 'main', type: CodeBlockSpanType.nameFunction),
@@ -378,7 +378,8 @@ class ContentExample {
378378
'<div class="codehilite">'
379379
'<pre><span></span><code>code block.\n</code></pre></div>\n\n'
380380
'<p>some content</p>', [
381-
CodeBlockNode([CodeBlockSpanNode(text: "code block.", type: CodeBlockSpanType.text)]),
381+
CodeBlockNode(codeLanguage: null,
382+
[CodeBlockSpanNode(text: "code block.", type: CodeBlockSpanType.text)]),
382383
ParagraphNode(links: null, nodes: [TextNode("some content")]),
383384
]);
384385

@@ -1062,7 +1063,7 @@ void main() {
10621063
ParagraphNode(wasImplicit: true, links: null, nodes: [TextNode('three')]),
10631064
]]),
10641065
]),
1065-
CodeBlockNode([
1066+
CodeBlockNode(codeLanguage: null, [
10661067
CodeBlockSpanNode(text: 'four', type: CodeBlockSpanType.text),
10671068
]),
10681069
ParagraphNode(wasImplicit: true, links: null, nodes: [TextNode('\n\n')]), // TODO avoid this; it renders wrong

0 commit comments

Comments
 (0)