diff --git a/static/src/js/base.js b/static/src/js/base.js index 988f363..547e671 100644 --- a/static/src/js/base.js +++ b/static/src/js/base.js @@ -62,6 +62,12 @@ window.addEventListener("load", () => { document.querySelectorAll(".block-code").forEach((codeBlock) => { const clipboardIcon = codeBlock.querySelector(".code-copy"); + + // There may not be an icon + if (!clipboardIcon) { + return; + } + clipboardIcon.addEventListener("click", (event) => { event.preventDefault(); navigator.clipboard diff --git a/website/contrib/code_block/blocks.py b/website/contrib/code_block/blocks.py index e73f5f6..5f81942 100644 --- a/website/contrib/code_block/blocks.py +++ b/website/contrib/code_block/blocks.py @@ -4,6 +4,7 @@ from django.utils.safestring import mark_safe from pygments import highlight from pygments.formatters.html import HtmlFormatter from pygments.lexers import find_lexer_class, get_all_lexers +from pygments.lexers.special import TextLexer from wagtail.blocks import ( BooleanBlock, CharBlock, @@ -27,6 +28,15 @@ class CodeStructValue(StructValue): ) return mark_safe(highlight(self["source"], lexer, formatter)) + def header_text(self) -> str: + if filename := self["filename"]: + return filename + + if language := self["language"] and self["language"] != TextLexer.name: + return language + + return "" + class CodeBlock(StructBlock): filename = CharBlock(max_length=128, required=False) diff --git a/website/contrib/code_block/templates/contrib/blocks/code.html b/website/contrib/code_block/templates/contrib/blocks/code.html index 53877b1..05ecce7 100644 --- a/website/contrib/code_block/templates/contrib/blocks/code.html +++ b/website/contrib/code_block/templates/contrib/blocks/code.html @@ -1,7 +1,10 @@ -
- {% firstof value.filename value.language %} - - - -
+{% if value.header_text %} +
+ {{ value.header_text }} + + + +
+{% endif %} + {{ value.code }}