Debian Codegraph @

docs/src/markdown/extensions/highlight.md

raw
1 ---
2 icon: lucide/braces
3 ---
4 [:octicons-file-code-24:][_highlight]{: .source-link }
5
6 # Highlight
7
8 ## Overview
9
10 Highlight is an extension that adds support for code highlighting. Its purpose is to provide a single place to configure
11 syntax highlighting for code blocks. Both [InlineHilite](./inlinehilite.md) and [SuperFences](./superfences.md) can use
12 Highlight to configure their highlight settings, but Highlight will also run all non-fence code blocks through the
13 highlighter as well.
14
15 The Highlight extension is inspired by [CodeHilite][codehilite], but differs in features. PyMdown Extensions chooses not
16 to implement special language headers for standard Markdown code blocks like CodeHilite does; PyMdown Extensions takes
17 the position that language headers are better suited in fenced code blocks. So standard Markdown code blocks will just
18 be styled as plain text unless `guess_language` is enabled. If you wish to highlight lines and define line numbers per
19 code block, it is advised to use the SuperFences extension.
20
21 The Highlight extension can be included in Python Markdown by using the following:
22
23 ```py3
24 import markdown
25 md = markdown.Markdown(extensions=['pymdownx.highlight'])
26 ```
27
28 ## Syntax Highlighting
29
30 If [Pygments][pygments] is installed, it will be the default syntax highlighter, but if it is not, or if `use_pygments`
31 is turned off, code tags will be rendered in the HTML5 format for JavaScript highlighting:
32
33 ```html
34 <pre class="highlight"><code class="language-mylanguage"></code></pre>
35 ```
36
37 `highlight` is the default class name, and will be applied to code blocks, whether Pygments is disabled or not. This
38 class name can be changed with the `css_class` option.
39
40 By default, the prefix `language-` is applied to language classes when Pygments is disabled or when Pygments code blocks
41 enable language classes via `pygments_lang_class`. This prefix can be changed via the `language_prefix` option.
42
43 The language class, and any attributes added via the [`attr_list`][attr-list] extension, are added to the
44 `#!html <code>` element. The `code_attr_on_pre` option will force them to be attached to the `#!html <pre>` element.
45
46 All other options specifically control the Pygments highlighter and will have no affect if Pygments usage is disabled.
47
48 ## Extended Pygments Lexer Options
49
50 If using Pygments, some lexers have special settings. For instance, the `php` lexer has the option `startinline` which,
51 if enabled, will parse PHP syntax without requiring `#!php <?` at the beginning. Highlight allows you to configure
52 these settings via the option `extend_pygments_lang`.
53
54 `extend_pygments_lang` allows you to create a special Pygments alias language where you can configure the language
55 settings to your liking. So if you wanted to enable `startinline` for `php`, you might create a language alias called
56 `php-inline` that maps to `php` with `startinline` enabled. This would allow you to use `php` for normal PHP blocks,
57 but also allow you to do use `php-inline` for code without specifying `#!php <?`. You can also use
58 `extend_pygments_lang` to completely override the base language's options -- assuming your specified name overrides an
59 existing name.
60
61 `extend_pygments_lang`is an array of dictionaries. Each dictionary contains three keys: `name` which is the new name you
62 are adding, `lang` which is the language the new name maps to, and `options` which is a dictionary of the options you
63 wish to apply.
64
65 /// example | Highlight Example
66 To create the above mentioned `php-inline`, this example illustrates the configuration, and Markdown syntax you
67 would use.
68
69 //// tab | Output
70 `#!php-inline $a = array("foo" => 0, "bar" => 1);`
71 ////
72
73 //// tab | Markdown
74 ````
75 `#!php-inline $a = array("foo" => 0, "bar" => 1);`
76 ````
77 ////
78
79 //// tab | Config
80 ```py
81 extend_pygments_lang = [
82 {"name": "php-inline", "lang": "php", "options": {"startinline": True}}
83 ]
84 ```
85 ////
86 ///
87
88 /// new | New 9.2
89 `pygments_lang_class` added in 9.2.
90 ///
91
92 ## Preserve Leading/Trailing Newlines
93
94 /// new | New 10.1
95 ///
96
97 It is not uncommon for Markdown parsers to preserve both leading and trailing newlines in things like fenced code. This
98 doesn't usually happen for indented code blocks.
99
100 To be clear the [SuperFences](./superfences.md) extension does not strip leading and trailing new lines, this is the
101 default behavior of the Pygments syntax highlighter, not the SuperFences extension's parsing logic. When Pygments is
102 disabled, both leading and trailing newlines are preserved.
103
104 While it is rare for people to actually need leading and trailing newlines, if such behavior was desired, it can be
105 retained by disabling the [`stripnl`](#options) option in Highlight which will ensure that all Pygments lexers have this
106 option disabled.
107
108 Inline code blocks, such as those provided by [InlineHilite](./inlinehilite.md), will be unaffected. Indented code
109 blocks will also not be affected as leading and trailing newlines are never retained in indented code blocks.
110
111 ## Line Number Styles
112
113 Pygments has two available styles when outputting source code with line numbers enabled: `table` and `inline`. `table`
114 is the default output and creates a table output for lines with numbers. `inline` places the line numbers directly in
115 the source code output and can sometimes be undesirable as copy and paste will always copy the line numbers as well.
116
117 The Highlight extension provides a third line number style called `pymdownx-inline`. Instead of writing line numbers
118 directly in the pre like Pygments' default `inline` style does, it writes the line numbers as
119 `#!html <span class="lineno" data-linenos="1 "></span>`. This way the line numbers are un-selectable and can be
120 displayed with CSS:
121
122 ```css
123 [data-linenos]:before {
124 content: attr(data-linenos);
125 }
126 ```
127
128 Line number styles are set with the option `linenums_style` as described in [Options](#options).
129
130 ## Options
131
132 All options below control the Pygments' output. The three exceptions are `use_pygments` which disables Pygments,
133 `css_class` which sets the name of the class assigned to the generated code blocks, and `code_attr_on_pre` which only
134 apply when Pygments is disabled. Many of these options are demonstrated in the [SuperFences](./superfences.md) documentation.
135
136 Option | Type | Default | Description
137 ------------------------- | -------------- | ----------------------| -----------
138 `css_class` | string | `#!py3 'highlight'` | Default class to apply to the wrapper element on code blocks. Other extensions can override this.
139 `guess_lang` | bool \| string | `#!py3 False` | Guess what syntax language should be used if no language is specified. `#!py3 True` for always, `#!py3 False` for never, `#!py3 'block'` for block code only, and `#!py3 'inline'` for inline code only.
140 `default_lang` | string | `#!py3 ''` | The assumed highlight language of a code block when no language is set. If something other than plain text is desired for indented blocks or for fenced code blocks (via [SuperFences](./superfences.md)) that have no configured language, this can be set to a language other than plain text. InlineHilite requires you to specifically set its own option (`style_plain_text`) to control this for inline code blocks.
141 `pygments_style` | string | `#!py3 'default'` | Set the Pygments' style to use. This really only has an effect when used with `noclasses`.
142 `noclasses` | bool | `#!py3 False` | This will cause the styles to directly be written to the tag's style attribute instead of requiring a stylesheet.
143 `use_pygments` | bool | `#!py3 True` | Controls whether Pygments (if available) is used to style the code, or if the code will just be escaped and prepped for a JavaScript syntax highlighter.
144 `linenums` | bool | `#!py3 None` | Enable line numbers globally for *block* code. This will be ignored for *inline* code. If set to `#!py3 False` line numbers will be disabled globally and can not be turned on, not even per code block.
145 `linenums_special` | int | `#!py3 1` | Globally sets the specified nth lines' gutter with the class "special". This can be overridden in [SuperFences](./superfences.md) per fence if desired.
146 `linenums_style` | string | `#!py3 'table'` | Controls the output style when `linenums` are enabled. Supported styles are Pygments default `table` and `inline`, but also supported is the pymdown-extensions `pymdownx-inline` which provides a special inline mode, see [Line Number Styles](#line-number-styles) for more info.
147 `linenums_class` | string | `#!py3 'linenums'` | Controls the name of the line number class when Pygments is not used.
148 `extend_pygments_lang` | list | `#!py3 []` | A list of extended languages to add. See [Extended Pygments Lexer Options](#extended-pygments-lexer-options) for more info.
149 `language_prefix` | string | `#!py3 'language-'` | Controls the prefix applied to the language class when Pygments is not used. By default, uses the HTML5 prefix of `language-`.
150 `code_attr_on_pre` | bool | `#!py3 False` | By default, the language class and all attributes added via the [`attr_list`][attr-list] extension are attached to the `#!html <code>` element. This forces them to be attached to the `#!html <pre>` element.
151 `auto_title` | bool | `#!py3 False` | When using Pygments, for all code blocks generate a title header with the name of the lexer being used. The lexer name is pulled directly from Pygments and title cased appropriately.
152 `auto_title_map` | dict | `#!py3 {}` | A dictionary used to override certain titles returned by `auto_title`. Simply specify the title to override as the key and the desired title as the value.
153 `line_spans` | string | `#!py3 ''` | Controls the Pygments option of a similar name. If set to a nonempty string, e.g. `foo`, the formatter will wrap each output line in a `#!html <span>` tag with an id of `foo-<code_block_number>-<line_number>`.
154 `anchor_linenums` | bool | `#!py3 False` | Enables the Pygments option of a similar name. If set to `#!py True`, will wrap line numbers in `#!html <a>` tags. Used in combination with `linenums` and `line_anchors`. If `line_anchors` is not configured, `__codelineno` will be assumed as the ID prefix.
155 `line_anchors` | bool | `#!py3 False` | Controls the Pygments option of a similar name. If set to a nonempty string, e.g. `foo`, the formatter will insert an anchor tag with an id (and name) of `foo-<code_block_number>-<line_number>`.
156 `pygments_lang_class` | bool | `#!py3 False` | If set to True, the language name used will be included as a class attached to the element with the associated `language_prefix`.
157 `stripnl` | bool | `#!py3 True` | Strips leading and trailing newlines from code blocks. This is Pygments default behavior. Setting this to `#!py False` disables this and will retain leading and trailing newlines. This has no affect on inline code.
158
159 /// new | New 7.1
160 `linenums_class` was added in `7.1`.
161 ///
162
163 /// new | New 7.2
164 `linenums` now accepts `#!py3 None` as the default for allow line numbers to be enabled per code block.
165 `#!py3 False` now disables line numbers globally preventing line numbers even if specified per code block. `True`
166 still enables globally.
167 ///
168
169 /// new | New 9.0
170 `auto_title`, `auto_title_map`, `line_spans`, `anchor_linenums`, and `line_anchors` were all added in `9.0`.
171 ///
172
173 /// new | New 10.7
174 `default_lang` added in 10.7.
175 ///