Skip to content

Commit e19071b

Browse files
committed
docs: Improve pycon no-prompt-selection recipe
1 parent 61c1666 commit e19071b

1 file changed

Lines changed: 43 additions & 6 deletions

File tree

docs/recipes.md

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -296,15 +296,17 @@ themselves, better reflecting our public API.
296296
show_submodules: no
297297
```
298298

299-
## Prevent selection of `>>>` in Python code blocks
299+
## Prevent selection of prompts and output in Python code blocks
300300

301-
To prevent the selection of `>>>` in Python code blocks,
302-
you can use the `pycon` syntax highlighting on your code block,
303-
and add some CSS rules to your site using MkDocs `extra_css` option:
301+
To prevent the selection of `>>>`, `...` and output in Python "Console" code blocks,
302+
you can use the `pycon` syntax highlighting on your code blocks,
303+
and add global CSS rules to your site using MkDocs `extra_css` option:
304304

305305
````md
306306
```pycon
307-
>>> print("Hello mkdocstrings!")
307+
>>> for word in ("Hello", "mkdocstrings!"):
308+
... print(word, end=" ")
309+
Hello mkdocstrings!
308310
```
309311
````
310312

@@ -319,6 +321,39 @@ extra_css:
319321
- css/code_select.css
320322
```
321323

324+
!!! warning
325+
The `.highlight .gp, .highlight .go` CSS selector can have unintended side-effects.
326+
To target `pycon` code blocks more specifically, you can configure the
327+
`pymdownx.highlight` extension to use Pygments and set language classes
328+
on code blocks:
329+
330+
```yaml title="mkdocs.yml"
331+
markdown_extensions:
332+
- pymdownx.highlight:
333+
use_pygments: true
334+
pygments_lang_class: true
335+
```
336+
337+
Then you can update the CSS selector like this:
338+
339+
```css title="docs/css/code_select.css"
340+
.language-pycon .gp, .language-pycon .go { /* Generic.Prompt, Generic.Output */
341+
user-select: none;
342+
}
343+
```
344+
345+
If you don't want to enable this globally,
346+
you can still use `style` tags in the relevant pages,
347+
with more accurate CSS selectors:
348+
349+
```html
350+
<style>
351+
#my-div .highlight .gp, #my-div .highlight .go { /* Generic.Prompt, Generic.Output */
352+
user-select: none;
353+
}
354+
</style>
355+
```
356+
322357
Try to select the following code block's text:
323358

324359
<style>
@@ -328,5 +363,7 @@ Try to select the following code block's text:
328363
</style>
329364

330365
```pycon
331-
>>> print("Hello mkdocstrings!")
366+
>>> for word in ("Hello", "mkdocstrings!"):
367+
... print(word, end=" ")
368+
Hello mkdocstrings!
332369
```

0 commit comments

Comments
 (0)