From 22c3586ae4bcd7c62e0a88b41f0649e6891e3ea5 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 1/2] [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 | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/misc/gen_blog_post_html.py b/misc/gen_blog_post_html.py index 00e167e4a3a2..30b4dd80b9b0 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,18 @@ 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 + # We're configuring hljs to highlight python and bash code. We can remove + # this configure call to make it try all the languages it supports. + h = f""" + + + + + +{h} + +""" return h From bf7dd836fffce4431cc1e8705de71823d422ee44 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 20 Dec 2024 16:33:57 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- misc/gen_blog_post_html.py | 1 - 1 file changed, 1 deletion(-) diff --git a/misc/gen_blog_post_html.py b/misc/gen_blog_post_html.py index 30b4dd80b9b0..8290e759ca66 100644 --- a/misc/gen_blog_post_html.py +++ b/misc/gen_blog_post_html.py @@ -17,7 +17,6 @@ import os import re import sys -import textwrap def format_lists(h: str) -> str: