Skip to content

Commit 6ceef24

Browse files
committed
Added support for code block titles
1 parent cb3220b commit 6ceef24

File tree

11 files changed

+104
-21
lines changed

11 files changed

+104
-21
lines changed

.stylelintrc

+8
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@
9898
"vh",
9999
"vw"
100100
],
101+
"value-keyword-case": [
102+
"lower",
103+
{
104+
"ignoreProperties": [
105+
"/^--/"
106+
]
107+
}
108+
],
101109
"value-list-comma-newline-after": null,
102110
"value-no-vendor-prefix": [
103111
true,

docs/reference/code-blocks.md

+39-8
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ markdown_extensions:
2525
- pymdownx.snippets
2626
```
2727
28+
The following sections discuss how to use different syntax highlighting features
29+
with [Pygments], the recommended highlighter, so they don't apply when using a
30+
JavaScript syntax highlighter.
31+
2832
See additional configuration options:
2933
3034
- [Highlight]
@@ -79,12 +83,6 @@ theme:
7983

8084
## Usage
8185

82-
This section discusses how to use different syntax highlighting features with
83-
[Pygments] – the default highlighter – so they don't apply when using
84-
a JavaScript syntax highlighter.
85-
86-
### Specifying the language
87-
8886
Code blocks must be enclosed with two separate lines containing three backticks.
8987
To add syntax highlighting to those blocks, add the language shortcode directly
9088
after the opening block. See the [list of available lexers] to find the
@@ -106,6 +104,39 @@ import tensorflow as tf
106104

107105
[list of available lexers]: https://pygments.org/docs/lexers/
108106

107+
### Adding a title
108+
109+
[:octicons-tag-24: 7.3.6][Title support] ·
110+
:octicons-beaker-24: Experimental
111+
112+
In order to provide additional context, a custom title can be added to a code
113+
block by using the `title="<custom title>"` option directly after the shortcode,
114+
e.g. to display the name of a file:
115+
116+
_Example_:
117+
118+
```` markdown
119+
``` py title="bubble_sort.py"
120+
def bubble_sort(items):
121+
for i in range(len(items)):
122+
for j in range(len(items) - 1 - i):
123+
if items[j] > items[j + 1]:
124+
items[j], items[j + 1] = items[j + 1], items[j]
125+
```
126+
````
127+
128+
_Result_:
129+
130+
``` py title="bubble_sort.py"
131+
def bubble_sort(items):
132+
for i in range(len(items)):
133+
for j in range(len(items) - 1 - i):
134+
if items[j] > items[j + 1]:
135+
items[j], items[j + 1] = items[j + 1], items[j]
136+
```
137+
138+
[Title support]: https://github.com/squidfunk/mkdocs-material/releases/tag/7.3.6
139+
109140
### Adding annotations
110141

111142
Code annotations can be placed anywhere in a code block where a comment for the
@@ -253,14 +284,14 @@ from within a code block:
253284
_Example_:
254285

255286
```` markdown
256-
```
287+
``` title=".browserslistrc"
257288
--8<--​ ".browserslistrc"
258289
```
259290
````
260291

261292
_Result_:
262293

263-
```
294+
``` title=".browserslistrc"
264295
last 4 years
265296
```
266297

docs/reference/data-tables.md

-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ See additional configuration options:
3030
3131
## Usage
3232
33-
### Using data tables
34-
3533
Data tables can be used at any position in your project documentation and can
3634
contain arbitrary Markdown, including inline code blocks, as well as [icons and
3735
emojis].

docs/setup/extensions/python-markdown-extensions.md

+25-5
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,8 @@ The following configuration options are supported:
342342
```
343343

344344
As an example, [Highlight.js], a JavaScript syntax highlighter, can be
345-
integrated with some [additional JavaScript] and [CSS][additional CSS]
346-
in `mkdocs.yml`:
345+
integrated with some [additional JavaScript] and [additional CSS] in
346+
`mkdocs.yml`:
347347

348348
=== ":octicons-file-code-16: docs/javascripts/highlight.js"
349349

@@ -363,7 +363,24 @@ The following configuration options are supported:
363363
- https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.7.2/styles/default.min.css
364364
```
365365

366-
Note that [Highlight.js] has no affiliation with the Highlight extension.
366+
Note that [Highlight.js] has no affiliation with the
367+
[Highlight][pymdownx.highlight] extension.
368+
369+
All following configuration options are only compatible with build-time
370+
syntax highlighting using [Pygments], so they don't apply if `use_pygments`
371+
is set to `false`.
372+
373+
`auto_title`{ #highlight-auto-title }
374+
375+
: :octicons-milestone-24: Default: `false` – This option will automatically
376+
add a [title] to all code blocks that shows the name of the language being
377+
used, e.g. `Python` is printed for a `py` block:
378+
379+
``` yaml
380+
markdown_extensions:
381+
- pymdownx.highlight:
382+
auto_title: true
383+
```
367384

368385
`linenums`{ #highlight-linenums }
369386

@@ -403,7 +420,8 @@ them at your own risk.
403420

404421
See reference for usage:
405422

406-
- [Specifying the language]
423+
- [Using code blocks]
424+
- [Adding a title]
407425
- [Adding line numbers]
408426
- [Highlighting specific lines]
409427
- [Custom syntax theme]
@@ -416,8 +434,10 @@ See reference for usage:
416434
[Pygments]: https://pygments.org
417435
[additional CSS]: ../../customization.md#additional-css
418436
[Highlight.js]: https://highlightjs.org/
437+
[title]: ../../reference/code-blocks.md#adding-a-title
419438
[Adding line numbers]: ../../reference/code-blocks.md#adding-line-numbers
420-
[Specifying the language]: ../../reference/code-blocks.md#specifying-the-language
439+
[Using code blocks]: ../../reference/code-blocks.md#usage
440+
[Adding a title]: ../../reference/code-blocks.md#adding-a-title
421441
[Highlighting specific lines]: ../../reference/code-blocks.md#highlighting-specific-lines
422442
[Custom syntax theme]: ../../reference/code-blocks.md#custom-syntax-theme
423443

docs/setup/extensions/python-markdown.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ No configuration options are available. See reference for usage:
340340

341341
[Tables]: https://python-markdown.github.io/extensions/tables/
342342
[Tables support]: https://github.com/squidfunk/mkdocs-material/releases/tag/0.1.0
343-
[Using data tables]: ../../reference/data-tables.md#using-data-tables
343+
[Using data tables]: ../../reference/data-tables.md#usage
344344
[Column alignment]: ../../reference/data-tables.md#column-alignment
345345

346346
## Superseded extensions

material/assets/stylesheets/main.cdeb8541.min.css renamed to material/assets/stylesheets/main.a57b2b03.min.css

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

material/assets/stylesheets/main.a57b2b03.min.css.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

material/assets/stylesheets/main.cdeb8541.min.css.map

-1
This file was deleted.

material/base.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
{% endif %}
4040
{% endblock %}
4141
{% block styles %}
42-
<link rel="stylesheet" href="{{ 'assets/stylesheets/main.cdeb8541.min.css' | url }}">
42+
<link rel="stylesheet" href="{{ 'assets/stylesheets/main.a57b2b03.min.css' | url }}">
4343
{% if config.theme.palette %}
4444
{% set palette = config.theme.palette %}
4545
<link rel="stylesheet" href="{{ 'assets/stylesheets/palette.3f5d1f46.min.css' | url }}">

requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ jinja2>=2.11.1
2323
markdown>=3.2
2424
mkdocs>=1.2.3
2525
mkdocs-material-extensions>=1.0
26-
pygments>=2.4
26+
pygments>=2.10
2727
pymdown-extensions>=9.0

src/assets/stylesheets/main/extensions/pymdownx/_highlight.scss

+26
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,25 @@
150150
background-color: var(--md-code-hl-color);
151151
}
152152

153+
// Code block title
154+
span.filename {
155+
position: relative;
156+
display: block;
157+
margin-top: 1em;
158+
padding: px2em(10.5px, 13.6px) px2em(16px, 13.6px);
159+
font-weight: 700;
160+
font-size: px2em(13.6px);
161+
background-color: var(--md-code-bg-color);
162+
border-bottom: px2rem(1px) solid var(--md-default-fg-color--lightest);
163+
border-top-left-radius: px2rem(2px);
164+
border-top-right-radius: px2rem(2px);
165+
166+
// Adjust spacing for code block
167+
+ pre {
168+
margin-top: 0;
169+
}
170+
}
171+
153172
// Code block line numbers (inline)
154173
[data-linenos]::before {
155174
position: sticky;
@@ -195,6 +214,13 @@
195214
margin: 0;
196215
}
197216

217+
// Code block title container
218+
th.filename {
219+
flex-grow: 1;
220+
padding: 0;
221+
text-align: left;
222+
}
223+
198224
// Code block line numbers - disable user selection, so code can be easily
199225
// copied without accidentally also copying the line numbers
200226
.linenos {

0 commit comments

Comments
 (0)