Skip to content

Commit 3c8f487

Browse files
committed
Make syntax highlighting aware of <code class="language-cpp">
It was trying to highlight class as a C++ keyword. Instead, we should move inside the <code> tags to do the highlighting.
1 parent f9bede0 commit 3c8f487

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

subdoc/lib/gen/markdown_to_html.cc

+16-2
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,25 @@ void apply_syntax_highlighting(std::string& str) noexcept {
130130
usize pos;
131131
auto view = std::string_view(str);
132132
while (true) {
133+
// Find the <pre> tags.
133134
pos = view.find("<pre>", pos);
134135
if (pos == std::string::npos) break;
135-
usize end_pos = view.find("</pre>", pos + 5u);
136+
pos += 5u;
137+
usize end_pos = view.find("</pre>", pos);
136138
if (end_pos == std::string::npos) break;
137139

140+
// Inside <pre>, find the <code> tags and move inside them.
141+
pos = [&](usize pos) {
142+
usize plain = view.find("<code>", pos);
143+
if (plain != std::string::npos) plain += strlen("<code>");
144+
usize with_lang = view.find("<code class=\"language-cpp\">", pos);
145+
if (with_lang != std::string::npos)
146+
with_lang += strlen("<code class=\"language-cpp\">");
147+
return sus::cmp::min(plain, with_lang);
148+
}(pos);
149+
if (pos == std::string::npos) break;
150+
end_pos = sus::cmp::min(end_pos, usize::from(view.find("</code>", pos)));
151+
138152
bool in_comment = false;
139153
bool in_string = false;
140154
bool in_char = false;
@@ -205,7 +219,7 @@ void apply_syntax_highlighting(std::string& str) noexcept {
205219
continue;
206220
}
207221

208-
// There's a <pre> tag at the start so we can always look backward one
222+
// There's a <code> tag at the start so we can always look backward one
209223
// char.
210224
sus::check(pos > 0u);
211225
char before = view[pos - 1u];

0 commit comments

Comments
 (0)