Commit 96cf077
authored
✨ Add
Adds a `make_fence_rule()` factory function that generates fence parsing
rules with configurable options, enabling reuse of the fence logic for
custom marker characters (e.g. `:` for colon fences) without code
duplication.
## Motivation
The `colon_fence` plugin in `mdit-py-plugins` duplicates nearly all of
the fence parsing logic, differing only in the marker character (`:` vs
`` ` ``/`~`), token type, and info-string restrictions. This makes
maintenance harder and prevents features like exact-match closing from
being shared.
## Changes
- **New `make_fence_rule()` factory** in fence.py with parameters:
- `markers`: tuple of valid fence marker characters (default `("~",
"\`")`)
- `token_type`: token type name to emit (default `"fence"`)
- `exact_match`: when `True`, closing fence must have **exactly** the
same marker count as the opener (default `False`). This enables nested
fences (e.g. `::::` wrapping `:::`).
- `disallow_marker_in_info`: marker characters that reject the fence if
found in the info string (default `("\`",)` per CommonMark)
- `min_markers`: minimum marker count to form a fence (default `3`)
- **`fence` remains a module-level export**: `fence = make_fence_rule()`
— fully backwards compatible, identical behavior to the previous
implementation.
- **Exported from `markdown_it.rules_block`** — added `make_fence_rule`
to `__all__`.
## Usage
```python
from markdown_it import MarkdownIt
from markdown_it.rules_block.fence import make_fence_rule
# Colon fence (replaces mdit-py-plugins/colon_fence duplicated logic)
md = MarkdownIt()
md.block.ruler.before("fence", "colon_fence",
make_fence_rule(markers=(":",), token_type="colon_fence", disallow_marker_in_info=()))
# Override standard fence with exact-match closing (for MyST nested directives)
md.block.ruler.at("fence", make_fence_rule(exact_match=True))
# Combine: all markers with exact matching
md.block.ruler.at("fence", make_fence_rule(
markers=("~", "`", ":"), exact_match=True))
```
## Performance
No regression by design — configuration is captured in the closure at
rule-creation time (no runtime dict lookups or extra branches in the hot
path). The `closing_matcher` callable is resolved once at factory time.
## Tests
21 new tests covering:
- Colon-fence-like marker behavior
- Exact-match closing (nesting patterns)
- `ruler.at()` override of standard fence
- `min_markers` customization
- `disallow_marker_in_info` variations
Full existing test suite (981 tests) passes unchanged.make_fence_rule() factory for configurable fence markers (#394)1 parent 3b4ff6d commit 96cf077
3 files changed
Lines changed: 374 additions & 67 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
18 | | - | |
| 19 | + | |
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
2 | 5 | | |
3 | 6 | | |
4 | 7 | | |
5 | 8 | | |
6 | 9 | | |
7 | 10 | | |
8 | 11 | | |
9 | | - | |
10 | | - | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
11 | 44 | | |
12 | | - | |
13 | | - | |
14 | | - | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
15 | 51 | | |
16 | | - | |
17 | | - | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
18 | 55 | | |
19 | | - | |
20 | | - | |
| 56 | + | |
| 57 | + | |
21 | 58 | | |
22 | | - | |
| 59 | + | |
| 60 | + | |
23 | 61 | | |
24 | | - | |
25 | | - | |
| 62 | + | |
26 | 63 | | |
27 | | - | |
28 | | - | |
29 | | - | |
| 64 | + | |
| 65 | + | |
30 | 66 | | |
31 | | - | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
32 | 70 | | |
33 | | - | |
34 | | - | |
| 71 | + | |
35 | 72 | | |
36 | | - | |
37 | | - | |
| 73 | + | |
| 74 | + | |
38 | 75 | | |
39 | | - | |
40 | | - | |
| 76 | + | |
| 77 | + | |
41 | 78 | | |
42 | | - | |
43 | | - | |
44 | | - | |
| 79 | + | |
| 80 | + | |
45 | 81 | | |
46 | | - | |
47 | | - | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
48 | 85 | | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
| 86 | + | |
| 87 | + | |
55 | 88 | | |
56 | | - | |
57 | | - | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
58 | 95 | | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
64 | 104 | | |
65 | | - | |
66 | | - | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
67 | 112 | | |
68 | | - | |
69 | | - | |
70 | 113 | | |
71 | | - | |
72 | | - | |
| 114 | + | |
73 | 115 | | |
74 | | - | |
| 116 | + | |
| 117 | + | |
75 | 118 | | |
76 | | - | |
77 | | - | |
78 | | - | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
79 | 128 | | |
80 | | - | |
81 | | - | |
| 129 | + | |
| 130 | + | |
82 | 131 | | |
83 | | - | |
84 | | - | |
| 132 | + | |
85 | 133 | | |
86 | | - | |
87 | | - | |
88 | | - | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
89 | 139 | | |
90 | | - | |
91 | | - | |
| 140 | + | |
92 | 141 | | |
93 | | - | |
| 142 | + | |
94 | 143 | | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | 144 | | |
101 | | - | |
| 145 | + | |
| 146 | + | |
0 commit comments