Skip to content

Commit b34bd35

Browse files
committed
Update global; add syntax hilighting for <pre> blocks and add code fence copy button
1 parent cc64cf8 commit b34bd35

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

docs/_global/css/global.css

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
/* Fix issue with 'code' text in tables wrapping to multiple lines */
2-
td code {
1+
/*
2+
Fix issue with 'code' text in tables wrapping to multiple lines
3+
Excludes any preformatted code blocks within tables
4+
*/
5+
td code:not(.md-code__content) {
36
white-space: nowrap;
47
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Converts inline <pre lang="langcode">code</pre> blocks to pygments-highlighted html
2+
# Custom hook to highlight and format <pre lang="langcode">code</br>code</pre> blocks
3+
#
4+
# This is used for <pre/> blocks embedded in tables, as these otherwise are skipped by pygments
5+
#
6+
# Note: It's possible this can be done better with Python-markdown inline patterns (https://python-markdown.github.io/reference/markdown/inlinepatterns),
7+
# but the below works For Now
8+
9+
import re
10+
import markdown
11+
12+
import mkdocs
13+
from mkdocs.structure.pages import Page
14+
from mkdocs.config.defaults import MkDocsConfig
15+
from mkdocs.structure.files import Files
16+
17+
from pymdownx.highlight import HighlightExtension
18+
19+
# Get current mkdocs config
20+
user_extensions = MkDocsConfig.markdown_extensions.__dict__.get('configdata');
21+
highlight_config = dict()
22+
23+
if 'pymdownx.highlight' in user_extensions:
24+
highlight_config = user_extensions.get('pymdownx.highlight')
25+
26+
ExistingHighlighter = HighlightExtension().get_pymdownx_highlighter()(**highlight_config)
27+
28+
def on_page_content(html: str, page: Page, config: MkDocsConfig, files: Files, **kwargs):
29+
def inline_pre_to_code_fence(match):
30+
raw = match.group()
31+
32+
## Check for language
33+
lang = ''
34+
langmatch = re.search('lang="(.*?)"', raw)
35+
36+
if langmatch:
37+
lang = langmatch.group(1)
38+
39+
## Remove first tag
40+
pre = re.sub('<pre( lang=(".+"))?>', '', raw)
41+
42+
## Strip end tag
43+
pre = re.sub('</pre>', '', pre)
44+
45+
## Swap html linebreaks
46+
pre = re.sub('<br/>', '\n', pre)
47+
48+
return ExistingHighlighter.highlight(pre, lang)
49+
50+
result = re.sub('<pre( lang=(".+"))?>(.+)</pre>', inline_pre_to_code_fence, html)
51+
52+
return result

docs/_global/mkdocs.yml

+5
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,17 @@ extra:
1313
- docs/_global/js/global.js
1414

1515
hooks:
16+
- docs/_global/hooks/inline_pre_to_code_fence.py
1617
- docs/_global/hooks/merge_inherited_config.py
1718

1819
markdown_extensions:
1920
admonition: {}
2021
markdown_grid_tables: {}
2122
md_in_html: {}
2223
pymdownx.details: {}
24+
pymdownx.highlight:
25+
line_spans: __span
26+
pygments_lang_class: true
2327
pymdownx.superfences: {}
2428
pymdownx.tabbed:
2529
alternate_style: true
@@ -53,6 +57,7 @@ theme:
5357
- announce.dismiss
5458
- content.action.edit
5559
- content.action.view
60+
- content.code.copy
5661
- search.highlight
5762
- search.suggest
5863
- toc.follow

0 commit comments

Comments
 (0)