Skip to content

Lightpanda: the headless browser designed for AI and automation

markdown: keep the code language on fenced blocks

Adrià Arrufatdab3f81580f7
diff --git a/src/browser/markdown.zig b/src/browser/markdown.zig
index c4c756eac..ca3a91d7c 100644
--- a/src/browser/markdown.zig
+++ b/src/browser/markdown.zig
@@ -65,6 +65,29 @@ fn getAnchorLabel(el: *Element) ?[]const u8 {
return el.getAttributeInterned("aria-label") orelse el.getAttributeInterned("title");
}
+/// The fence info string for a `<pre>`: the `language-*` (or `lang-*`) class
+/// on the `<pre>` itself or on a `<code>` that is its first element child.
+fn codeLanguage(pre: *Element) ?[]const u8 {
+ if (classLanguage(pre)) |lang| return lang;
+ const child = pre.firstElementChild() orelse return null;
+ if (child.getTag() != .code) return null;
+ return classLanguage(child);
+}
+
+fn classLanguage(el: *Element) ?[]const u8 {
+ const class = el.getClassName() orelse return null;
+ var it = std.mem.tokenizeAny(u8, class, &std.ascii.whitespace);
+ while (it.next()) |token| {
+ for ([_][]const u8{ "language-", "lang-" }) |prefix| {
+ if (!std.mem.startsWith(u8, token, prefix)) continue;
+ const lang = token[prefix.len..];
+ // A backtick would end the fence's info string early.
+ if (lang.len > 0 and std.mem.indexOfScalar(u8, lang, '`') == null) return lang;
+ }
+ }
+ return null;
+}
+
// Iterative else large trees will stackoverflow
const Context = struct {
state: State,
@@ -208,7 +231,9 @@ const Context = struct {
self.state.last_char_was_newline = false;
},
.pre => {
- try self.writer.writeAll("```\n");
+ try self.writer.writeAll("```");
+ if (codeLanguage(el)) |lang| try self.writer.writeAll(lang);
+ try self.writer.writeByte('\n');
self.state.pre_node = el.asNode();
self.state.last_char_was_newline = true;
},
@@ -628,6 +653,30 @@ test "browser.markdown: code" {
);
}
+test "browser.markdown: code fence keeps the language" {
+ try testMarkdownHTML("<pre><code class=\"hljs language-zig\">const x = 1;</code></pre>",
+ \\
+ \\```zig
+ \\const x = 1;
+ \\```
+ \\
+ );
+ try testMarkdownHTML("<pre class=\"lang-sh\">ls</pre>",
+ \\
+ \\```sh
+ \\ls
+ \\```
+ \\
+ );
+ try testMarkdownHTML("<pre class=\"language-\">x</pre>",
+ \\
+ \\```
+ \\x
+ \\```
+ \\
+ );
+}
+
test "browser.markdown: block link" {
try testMarkdownHTML(
\\<a href="https://example.com">