From 4835d5b27d34fe55cfce5c92d5eb600d00914f79 Mon Sep 17 00:00:00 2001 From: Valentin Stanciu <250871+svalentin@users.noreply.github.com> Date: Fri, 20 Dec 2024 16:09:16 +0000 Subject: [PATCH] [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 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. --- misc/gen_blog_post_html.py | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/misc/gen_blog_post_html.py b/misc/gen_blog_post_html.py index 00e167e4a3a25..06794a895b38b 100644 --- a/misc/gen_blog_post_html.py +++ b/misc/gen_blog_post_html.py @@ -17,6 +17,7 @@ import os import re import sys +import textwrap def format_lists(h: str) -> str: @@ -44,16 +45,23 @@ def format_code(h: str) -> str: while i < len(a): if a[i].startswith(" ") or a[i].startswith("```"): indent = a[i].startswith(" ") + language: str = "" if not indent: + language = a[i][3:] i += 1 - r.append("
")
+            if language:
+                r.append(f'
')
+            else:
+                r.append("
")
             while i < len(a) and (
                 (indent and a[i].startswith("    ")) or (not indent and not a[i].startswith("```"))
             ):
                 # Undo > and <
                 line = a[i].replace(">", ">").replace("<", "<")
-                if not indent:
-                    line = "    " + line
+                if indent:
+                    # Undo this extra level of indentation so it looks nice with
+                    # syntax highlighting CSS.
+                    line = line[4:]
                 r.append(html.escape(line))
                 i += 1
             r.append("
") @@ -64,7 +72,7 @@ def format_code(h: str) -> str: i += 1 formatted = "\n".join(r) # remove empty first line for code blocks - return re.sub(r"\n", r"", formatted) + return re.sub(r"]*)>\n", r"", formatted) def convert(src: str) -> str: @@ -131,8 +139,16 @@ def convert(src: str) -> str: h, ) - # Add missing top-level HTML tags - h = '\n\n\n' + h + "\n" + # Add top-level HTML tags and headers for syntax highlighting css/js + h = f""" + + + + + +{h} + +""" return h