Improve code highlight

This commit is contained in:
Thomas Sileo 2022-07-12 22:24:15 +02:00
parent d32a56e38d
commit 9733c0b5c8
3 changed files with 26 additions and 10 deletions

View file

@ -50,6 +50,7 @@ div.highlight {
padding: 10px; padding: 10px;
overflow: auto; overflow: auto;
display: block; display: block;
margin: 20px 0;
} }
.box { .box {

View file

@ -88,6 +88,6 @@ async def markdownify(
# Handle custom emoji # Handle custom emoji
tags.extend(emoji.tags(content)) tags.extend(emoji.tags(content))
content = markdown(content, extensions=["mdx_linkify", "fenced_code", "codehilite"]) content = markdown(content, extensions=["mdx_linkify", "fenced_code"])
return content, tags, mentioned_actors return content, tags, mentioned_actors

View file

@ -3,6 +3,7 @@ from functools import lru_cache
from bs4 import BeautifulSoup # type: ignore from bs4 import BeautifulSoup # type: ignore
from pygments import highlight as phighlight # type: ignore from pygments import highlight as phighlight # type: ignore
from pygments.formatters import HtmlFormatter # type: ignore from pygments.formatters import HtmlFormatter # type: ignore
from pygments.lexers import get_lexer_by_name # type: ignore
from pygments.lexers import guess_lexer # type: ignore from pygments.lexers import guess_lexer # type: ignore
from app.config import CODE_HIGHLIGHTING_THEME from app.config import CODE_HIGHLIGHTING_THEME
@ -18,15 +19,29 @@ def highlight(html: str) -> str:
for code in soup.find_all("code"): for code in soup.find_all("code"):
if not code.parent.name == "pre": if not code.parent.name == "pre":
continue continue
# Replace <br> tags with line breaks (Mastodon sends code like this)
code_content = ( code_content = (
code.encode_contents().decode().replace("<br>", "\n").replace("<br/>", "\n") code.encode_contents().decode().replace("<br>", "\n").replace("<br/>", "\n")
) )
lexer = guess_lexer(code_content)
tag = BeautifulSoup( # If this comes from a microblog.pub instance we may have the language
phighlight(code_content, lexer, _FORMATTER), "html5lib" # in the class name
).body.next if "class" in code.attrs and code.attrs["class"][0].startswith("language-"):
pre = code.parent try:
pre.replaceWith(tag) lexer = get_lexer_by_name(
out = soup.body code.attrs["class"][0].removeprefix("language-")
out.name = "div" )
return str(out) except Exception:
lexer = guess_lexer(code_content)
else:
lexer = guess_lexer(code_content)
# Replace the code with Pygment output
code.parent.replaceWith(
BeautifulSoup(
phighlight(code_content, lexer, _FORMATTER), "html5lib"
).body.next
)
return soup.body.encode_contents().decode()