Skip to content

Commit 4835d5b

Browse files
committed
[misc] use highlight.js for syntax highlighting in blog post
Toe get highlighting to work, we just need to import the CSS and run the highlight.js that does the highlighting in JS client side. We can add the lines at the top of the blog post to do this. I've made it only support bash and python for now to help with detection. But if we have a reason to, we can remove that and let it try them all. In a previous PR I've added the necessary <code> tags. But since we're highlighting nicely now, we can just remove the extra indendation. I've also noticed that we're pretty good at specifying the language in code blocks in the changelog. So we can take that language and use it in the code block as a class to tell highlight.js exactly what language that code block is in. If this is useful, we can remove the limitation of only python and bash support from the top configuration in the future. This is useful for smaller blocks of a few lines where maybe it doesn't detect the language properly.
1 parent 7959a20 commit 4835d5b

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

misc/gen_blog_post_html.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import os
1818
import re
1919
import sys
20+
import textwrap
2021

2122

2223
def format_lists(h: str) -> str:
@@ -44,16 +45,23 @@ def format_code(h: str) -> str:
4445
while i < len(a):
4546
if a[i].startswith(" ") or a[i].startswith("```"):
4647
indent = a[i].startswith(" ")
48+
language: str = ""
4749
if not indent:
50+
language = a[i][3:]
4851
i += 1
49-
r.append("<pre><code>")
52+
if language:
53+
r.append(f'<pre><code class="language-{language}">')
54+
else:
55+
r.append("<pre><code>")
5056
while i < len(a) and (
5157
(indent and a[i].startswith(" ")) or (not indent and not a[i].startswith("```"))
5258
):
5359
# Undo &gt; and &lt;
5460
line = a[i].replace("&gt;", ">").replace("&lt;", "<")
55-
if not indent:
56-
line = " " + line
61+
if indent:
62+
# Undo this extra level of indentation so it looks nice with
63+
# syntax highlighting CSS.
64+
line = line[4:]
5765
r.append(html.escape(line))
5866
i += 1
5967
r.append("</code></pre>")
@@ -64,7 +72,7 @@ def format_code(h: str) -> str:
6472
i += 1
6573
formatted = "\n".join(r)
6674
# remove empty first line for code blocks
67-
return re.sub(r"<code>\n", r"<code>", formatted)
75+
return re.sub(r"<code([^\>]*)>\n", r"<code\1>", formatted)
6876

6977

7078
def convert(src: str) -> str:
@@ -131,8 +139,16 @@ def convert(src: str) -> str:
131139
h,
132140
)
133141

134-
# Add missing top-level HTML tags
135-
h = '<html>\n<meta charset="utf-8" />\n<body>\n' + h + "</body>\n</html>"
142+
# Add top-level HTML tags and headers for syntax highlighting css/js
143+
h = f"""<html>
144+
<meta charset="utf-8" />
145+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/a11y-light.min.css">
146+
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
147+
<script>hljs.configure({{languages:["python","bash"]}});hljs.highlightAll();</script>
148+
<body>
149+
{h}
150+
</body>
151+
</html>"""
136152

137153
return h
138154

0 commit comments

Comments
 (0)