Debian Codegraph @

docs/src/markdown/extensions/inlinehilite.md

raw
1 ---
2 icon: lucide/chevrons-left-right-ellipsis
3 ---
4 [:octicons-file-code-24:][_inlinehilite]{: .source-link }
5
6 # InlineHilite
7
8 ## Overview
9
10 InlineHilite is an inline code highlighter inspired by [CodeHilite][codehilite]. Borrowing from CodeHilite's existing
11 syntax, InlineHilite utilizes the following syntax to insert inline highlighted code: `` `:::language mycode` `` or
12 `` `#!language mycode` ``. In CodeHilite, ` #! ` means "use line numbers", but line numbers will never be used in
13 inline code regardless of which form is used. Use of one form or the other is purely for personal preference. As this
14 feature is discussed further, we will call these specifiers (` #! ` and ` ::: `) mock shebangs, mainly due to the first
15 options similar syntax to a real shebang.
16
17 ```text title="Inline Highlighted Code"
18 Here is some code: `#!py3 import pymdownx; pymdownx.__version__`.
19
20 The mock shebang will be treated like text here: ` #!js var test = 0; `.
21 ```
22
23 /// html | div.result
24 Here is some code: `#!py3 import pymdownx; pymdownx.__version__`
25
26 The mock shebang will be treated like text here: ` #!js var test = 0; `.
27 ///
28
29
30 When using the colon mock shebang, 3 or more colons can be used. Mock shebangs must come **immediately** after the
31 opening backtick(s) and must be followed by at least one space. If you need to escape a mock shebang, at the start of a
32 code block, just put a space before it, and it will be treated as part of the code.
33
34 The InlineHilite extension can be included in Python Markdown by using the following:
35
36 ```py3
37 import markdown
38 md = markdown.Markdown(extensions=['pymdownx.inlinehilite'])
39 ```
40
41 ## Code Highlighting
42
43 Assuming Pygments is installed, code highlighting will be handled by [Pygments][pygments]. If Pygments is not installed,
44 or disabled, code blocks will be output for JavaScript syntax highlighters as:
45
46
47 ```html
48 <code class="highlight language-mylanguage"></code>
49 ```
50
51 Highlighting can be further controlled via the [`pymdownx.highlight`](./highlight.md) extension. You must include
52 `pymdownx.highlight` in the extensions list in order to be able to configure it.
53
54 ## Using JavaScript Highlighters
55
56 If using [Pygments][pygments], the elements will be highlighted without issues, but you may need to adjust CSS to get
57 the general style of the inline block the way you like it.
58
59 If you are using a JavaScript highlighter, such as [`highlight.js`][highlightjs], you will most likely need to construct
60 a JavaScript method to target the inline blocks as these may not be targeted out of the box. You may also find it useful
61 to tag inline code with a different class than what is used for block code so you can also process and style them
62 differently. By default, it uses whatever [`pymdownx.highlight`](./highlight.md) uses, but this can be configured
63 independently for inline code in the [options](#options).
64
65 ## Custom Inline Blocks
66
67 Like [SuperFences](./superfences.md), InlineHilite now provides a support for custom inline blocks.
68
69 ```text title="Inline Math"
70 `#!math p(x|y) = \frac{p(y|x)p(x)}{p(y)}`
71 ```
72
73 /// html | div.result
74 `#!math p(x|y) = \frac{p(y|x)p(x)}{p(y)}`
75 ///
76
77 /// tip
78 For more indepth information on how to reproduce the example above, check out [Arithmatex Documentation
79 ](./arithmatex.md#alternative-math-blocks).
80 ///
81
82 Custom inline code blocks are created via the `custom_inline` option. `custom_inline` takes an array of dictionaries
83 where each dictionary defines a custom inline code block. The dictionaries requires the following keys:
84
85 Keys | Description
86 ----------- | -----------
87 `name` | The language name that is specified when using the fence in Markdown. If given `*`, it will override the base inline logic, the default for all inline names not handled by other custom inlines.
88 `class` | The class name assigned to the HTML element when converting from Markdown to HTML.
89 `format` | A function that formats the HTML output. The function should return either an ElementTree object or a string as HTML.
90
91 /// new | New in 7.0
92 Starting in 7.0, you can override the base inline logic (the syntax highlighter) by specifying the custom inline
93 with a name of `*`. This means that if an inline does not match any other custom inline, the default, fallback
94 inline would be handled by your custom `*` inline. This can be useful for tailoring a inline for a specific,
95 favorite JavaScript highlighter.
96 ///
97
98 ### Formatters
99
100 ```py3
101 def custom_formatter(source, language, css_class, md):
102 return el # Or string
103 ```
104
105 In general, formatters take four parameters:
106
107 1. The source found between the backticks.
108 2. The specified language.
109 3. The class name originally defined via the `class` option in the `custom_inline` entry.
110 4. The Markdown class object.
111
112 It Should return a either an ElementTree element or a string as HTML. When returning a string, InlineHilite will treat
113 the string as ready for HTML, so escape what needs to be escaped as it is expected to be fully ready HTML and will be
114 stashed and re-inserted at post processing.
115
116 When returning an ElementTree object, remember to wrap string content as `markdown.util.AtomicString` to prevent it from
117 being processed further, you can also stash raw HTML string content assigned to elements like InlineHilite does by
118 default above. InlineHilite will not try and guess what you intend, you must manage your content in the ElementTree
119 objects or the Markdown parser may apply other conversion to your HTML content.
120
121 ### Exception Handling
122
123 In general, if an exception occurs in the formatter, InlineHilite will gracefully ignore the formatter. If this occurs,
124 the code content will just be abandoned.
125
126 Some users may want such failures to not silently go by. InlineHilite exposes a special exception called
127 `InlineHiliteException` which, if raised, will not gracefully be handled. If `InlineHiliteException` is raised, this
128 will bubble all the way up and cause Markdown parsing to halt.
129
130 ```py
131 def format_fail(src, language, class_name, md):
132 """Inline formatter"""
133
134 raise InlineHiliteException('Fail!')
135 ```
136
137 `InlineHiliteException` can be used to raise other exceptions if desired.
138
139
140 ```py
141 def format_fail(src, language, class_name, md):
142 """Inline formatter"""
143
144 try:
145 raise ValueError('Nooo!')
146 except ValueError as e:
147 raise InlineHiliteException from e
148 ```
149
150 /// new | New 9.5
151 `InlineHiliteException` added in 9.5
152 ///
153
154 ## Options
155
156 Option | Type | Default | Description
157 ------------------------- | --------------- | ------------- | -----------
158 `css_class` | string | `#!py3 ''` | Class name is applied to the wrapper element of the code. If configured, this setting will override the `css_class` option of Highlight. If nothing is configured here or via or Highlight, the class `highlight` will be used.
159 `style_plain_text` | bool \| string | `#!py3 False` | When `guess_lang` is set to `#!py3 False`, InlineHilite will avoid applying classes to code blocks that do not explicitly set a language. If it is desired to have plain text styled like code, enable this to inject classes so that they can all be styled the same. By default, plain text is assumed, but if this option is set to a language string, the specified language will be used for any inline code blocks that does not explicitly set a language.
160 `custom_inline` | [dictionary] | `#!py3 []` | Custom inline code blocks.
161
162 /// new | New 10.7
163 `style_plain_text` now allows specifying a default language to be assumed.
164 ///