feat: VS Code-like syntax highlighting for code blocks
Code blocks in documents are now highlighted server-side with Pygments according to their language (Markdown fences and TinyMCE code samples), using VS Code "Dark+" colours in the viewer and a light variant in the print view. A language label is shown next to the copy button. Blocks marked as plain text/Cisco config stay unchanged; blocks without a language are only coloured when the language is detected confidently. Works offline, no CDN needed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
8458cbe8cf
commit
f7e1e21c24
6 files changed
+130
-1
No files matched your search
@@ -0,0 +1,30 @@
|
||||
import importlib.util
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
|
||||
spec = importlib.util.spec_from_file_location(
|
||||
"documentation_highlighting",
|
||||
Path(__file__).parents[1] / "netbox_documentation" / "highlighting.py",
|
||||
)
|
||||
highlighting = importlib.util.module_from_spec(spec)
|
||||
sys.modules[spec.name] = highlighting
|
||||
spec.loader.exec_module(highlighting)
|
||||
|
||||
|
||||
def test_highlights_markdown_fence_with_language():
|
||||
result = highlighting.highlight_code_blocks('<pre><code class="language-python">def f():\n return 1\n</code></pre>')
|
||||
assert 'class="doc-hl" data-lang="Python"' in result
|
||||
assert '<span class="k">def</span>' in result
|
||||
|
||||
|
||||
def test_highlights_tinymce_code_sample_and_keeps_escaping():
|
||||
result = highlighting.highlight_code_blocks('<pre class="language-markup"><code><b onclick="x">hi</b></code></pre>')
|
||||
assert 'data-lang="HTML"' in result
|
||||
assert "<b" not in result.replace("<span", "")
|
||||
assert "<" in result
|
||||
|
||||
|
||||
def test_plain_text_and_unknown_language_stay_untouched():
|
||||
for block in ('<pre class="language-none">interface Gi0/1</pre>', '<pre><code class="language-unknownlang">x</code></pre>'):
|
||||
assert highlighting.highlight_code_blocks(block) == block
|
||||
Reference in new issue
Block a user