| 1 |
--- |
| 2 |
icon: lucide/asterisk |
| 3 |
--- |
| 4 |
[:octicons-file-code-24:][_betterem]{: .source-link } |
| 5 |
|
| 6 |
# BetterEm |
| 7 |
|
| 8 |
## Overview |
| 9 |
|
| 10 |
BetterEm is an extension that aims to improve emphasis (bold and italic) handling. |
| 11 |
|
| 12 |
BetterEm provides a **smart** which controls whether emphasis is processed mid-word or not. When `smart_enable` is |
| 13 |
enabled, mid-word emphasis is intelligently ignored. This can be applied to asterisk and underscore emphasis, but since |
| 14 |
it usually only desirable to avoid mid-word emphasis in with underscores, it is only enabled for underscores by |
| 15 |
default. |
| 16 |
|
| 17 |
With the default behavior, the feel will be very similar to GFM bold and italic, within the bounds of what Python |
| 18 |
Markdown is capable of. |
| 19 |
|
| 20 |
The BetterEm extension can be included in Python Markdown by using the following: |
| 21 |
|
| 22 |
```py3 |
| 23 |
import markdown |
| 24 |
md = markdown.Markdown(extensions=['pymdownx.betterem']) |
| 25 |
``` |
| 26 |
|
| 27 |
/// important | Reminder |
| 28 |
Remember to read the [Usage Notes](../usage_notes.md) for information that may be relevant when using this |
| 29 |
extension! |
| 30 |
/// |
| 31 |
|
| 32 |
## Rules |
| 33 |
|
| 34 |
/// note | Note |
| 35 |
For all examples on this page, underscores are __smart__ and asterisks are not. |
| 36 |
/// |
| 37 |
|
| 38 |
BetterEm requires that non-whitespace characters follow the opening token(s) and precede the closing token(s). |
| 39 |
|
| 40 |
```text title="Whitespace" |
| 41 |
This * won't emphasize * |
| 42 |
|
| 43 |
This *will emphasize* |
| 44 |
``` |
| 45 |
|
| 46 |
/// html | div.result |
| 47 |
This * won't emphasize * |
| 48 |
|
| 49 |
This *will emphasize* |
| 50 |
/// |
| 51 |
|
| 52 |
BetterEm allows for a more natural nested token feel. |
| 53 |
|
| 54 |
```text title="Nested Token" |
| 55 |
***I'm italic and bold* I am just bold.** |
| 56 |
|
| 57 |
***I'm bold and italic!** I am just italic.* |
| 58 |
``` |
| 59 |
|
| 60 |
/// html | div.result |
| 61 |
***I'm italic and bold* I am just bold.** |
| 62 |
|
| 63 |
***I'm bold and italic!** I am just italic.* |
| 64 |
/// |
| 65 |
|
| 66 |
BetterEm will try to prioritize the more sane option when nesting bold (`**`) between italic (`*`). |
| 67 |
|
| 68 |
```text title="Prioritize Best" |
| 69 |
*I'm italic. **I'm bold and italic.** I'm also just italic.* |
| 70 |
``` |
| 71 |
|
| 72 |
/// html | div.result |
| 73 |
*I'm italic. **I'm bold and italic.** I'm also just italic.* |
| 74 |
/// |
| 75 |
|
| 76 |
|
| 77 |
BetterEm will ensure smart mode doesn't terminate in scenarios where there are a large amount of consecutive tokens |
| 78 |
inside. |
| 79 |
|
| 80 |
```text title="Consecutive Token" |
| 81 |
___A lot of underscores____________is okay___ |
| 82 |
``` |
| 83 |
|
| 84 |
/// html | div.result |
| 85 |
___A lot of underscores____________is okay___ |
| 86 |
/// |
| 87 |
|
| 88 |
BetterEm will also ensure that smart mode breaks properly when an inner like token signifies an end. |
| 89 |
|
| 90 |
```text title="Smart Break" |
| 91 |
__This will all be bold __because of the placement of the center underscores.__ |
| 92 |
|
| 93 |
__This will all be bold __ because of the placement of the center underscores.__ |
| 94 |
|
| 95 |
__This will NOT all be bold__ because of the placement of the center underscores.__ |
| 96 |
|
| 97 |
__This will all be bold_ because of the token is less than that of the surrounding.__ |
| 98 |
``` |
| 99 |
|
| 100 |
/// html | div.result |
| 101 |
__This will all be bold __because of the placement of the center underscores.__ |
| 102 |
|
| 103 |
__This will all be bold __ because of the placement of the center underscores.__ |
| 104 |
|
| 105 |
__This will NOT all be bold__ because of the placement of the center underscores.__ |
| 106 |
|
| 107 |
__This will all be bold_ because the token count is less than that of the surrounding.__ |
| 108 |
/// |
| 109 |
|
| 110 |
BetterEm will allow non-smart emphasis to contain "floating" like tokens. |
| 111 |
|
| 112 |
```text title="Floating Token" |
| 113 |
*All will * be italic* |
| 114 |
|
| 115 |
*All will *be italic* |
| 116 |
|
| 117 |
*All will not* be italic* |
| 118 |
|
| 119 |
*All will not ** be italic* |
| 120 |
|
| 121 |
**All will * be bold** |
| 122 |
|
| 123 |
**All will *be bold** |
| 124 |
|
| 125 |
**All will not*** be bold** |
| 126 |
|
| 127 |
**All will not *** be bold** |
| 128 |
``` |
| 129 |
|
| 130 |
/// html | div.result |
| 131 |
*All will * be italic* |
| 132 |
|
| 133 |
*All will *be italic* |
| 134 |
|
| 135 |
*All will not* be italic* |
| 136 |
|
| 137 |
*All will ** be italic* |
| 138 |
|
| 139 |
**All will * be bold** |
| 140 |
|
| 141 |
**All will *be bold** |
| 142 |
|
| 143 |
**All will not*** be bold** |
| 144 |
|
| 145 |
**All will not ***be bold** |
| 146 |
/// |
| 147 |
|
| 148 |
## Options |
| 149 |
|
| 150 |
Option | Type | Default | Description |
| 151 |
-------------- | ------ | ------------------- | ----------- |
| 152 |
`smart_enable` | string | `#!py3 'underscore'` | A string that specifies whether smart should be enabled for `all`, `asterisk`, `underscore`, or `none`. |