Skip to content

Commit a872ea2

Browse files
authored
Replace default parser with markdown-it-py (#1901)
1 parent 4d4b449 commit a872ea2

File tree

152 files changed

+4413
-14148
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

152 files changed

+4413
-14148
lines changed

dev-docs/plugins/hooks/clean-displayed-url-hook.md

-80
This file was deleted.
+59-86
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# `create_parser_hook`
22

3-
This hook wraps the standard function that Misago uses to create a markup parser instance.
3+
This hook wraps the standard function that Misago uses to create a configured MarkdownIt instance.
44

55

66
## Location
@@ -17,15 +17,14 @@ from misago.parser.hooks import create_parser_hook
1717
```python
1818
def custom_create_parser_filter(
1919
action: CreateParserHookAction,
20-
context: 'ParserContext',
2120
*,
22-
block_patterns: list[Pattern],
23-
inline_patterns: list[Pattern],
24-
pre_processors: list[Callable[[Parser, str],
25-
str]],
26-
post_processors: list[Callable[[Parser, list[dict]],
27-
list[dict]]],
28-
) -> Parser:
21+
config: str | PresetType,
22+
options_update: Mapping[str, Any] | None=None,
23+
enable: str | Iterable[str] | None=None,
24+
disable: str | Iterable[str] | None=None,
25+
plugins: Iterable[Callable[[MarkdownIt],
26+
None]] | None=None,
27+
) -> MarkdownIt:
2928
...
3029
```
3130

@@ -36,146 +35,120 @@ A function implemented by a plugin that can be registered in this hook.
3635

3736
#### `action: CreateParserHookAction`
3837

39-
A standard Misago function used to create a markup parser instance or the next filter function from another plugin.
38+
A standard Misago function used to create a configured MarkdownIt instance or the next filter function from another plugin.
4039

4140
See the [action](#action) section for details.
4241

4342

44-
#### `context: ParserContext`
45-
46-
An instance of the `ParserContext` data class that contains dependencies used during parsing.
47-
43+
#### `config: str | PresetType`
4844

49-
#### `block_patterns: list[Pattern]`
45+
A `str` with the name of the preset to use, or a `PresetType` instance.
5046

51-
A list of `Pattern` instances of block patterns to be used by the parser.
5247

48+
#### `options_update: Mapping[str, Any] | None = None`
5349

54-
#### `inline_patterns: list[Pattern]`
50+
A `Mapping` with preset's options overrides.
5551

56-
A list of `Pattern` instances of inline patterns to be used by the parser.
5752

53+
#### `enable: str | Iterable[str] | None = None`
5854

59-
#### `pre_processors: list[Callable[[Parser, str], str]]`
55+
Argument to call the `MarkdownIt.enable(...)` method with. If empty or `None`, `enable()` will not be called.
6056

61-
A list of pre-processor functions called by the parser to prepare markup for parsing.
6257

63-
A pre-processor function should have the following signature:
58+
#### `disable: str | Iterable[str] | None = None`
6459

65-
```python
66-
def custom_postprocessor(parser: Parser, markup: str) -> str:
67-
# Do something with the 'markup'...
68-
return markup
69-
```
60+
Argument to call the `MarkdownIt.disable(...)` method with. If empty or `None`, `disable()` will not be called.
7061

7162

72-
#### `post_processors: list[Callable[[Parser, list[dict]], list[dict]]]`
63+
#### `plugins: Iterable[Callable[[MarkdownIt], None]] | None = None`
7364

74-
A list of post-processor functions called by the parser to finalize the AST.
75-
76-
A post-processor function should have the following signature:
77-
78-
```python
79-
def custom_postprocessor(parser: Parser, ast: list[dict]) -> list[dict]:
80-
# Do something with the 'ast'...
81-
return ast
82-
```
65+
A list of `MarkdownIt` plugins. Each plugin must be a callable that accepts a single argument: the `MarkdownIt` instance.
8366

8467

8568
### Return value
8669

87-
An instance of the `Parser` class.
70+
An instance of the `MarkdownIt` class.
8871

8972

9073
## Action
9174

9275
```python
9376
def create_parser_action(
94-
context: 'ParserContext',
9577
*,
96-
block_patterns: list[Pattern],
97-
inline_patterns: list[Pattern],
98-
pre_processors: list[Callable[[Parser, str],
99-
str]],
100-
post_processors: list[Callable[[Parser, list[dict]],
101-
list[dict]]],
102-
) -> Parser:
78+
config: str | PresetType,
79+
options_update: Mapping[str, Any] | None=None,
80+
enable: str | Iterable[str] | None=None,
81+
disable: str | Iterable[str] | None=None,
82+
plugins: Iterable[Callable[[MarkdownIt],
83+
None]] | None=None,
84+
) -> MarkdownIt:
10385
...
10486
```
10587

106-
A standard Misago function used to create a markup parser instance or the next filter function from another plugin.
88+
A standard Misago function used to create a configured MarkdownIt instance or the next filter function from another plugin.
10789

10890

10991
### Arguments
11092

111-
#### `context: ParserContext`
93+
#### `config: str | PresetType`
11294

113-
An instance of the `ParserContext` data class that contains dependencies used during parsing.
95+
A `str` with the name of the preset to use, or a `PresetType` instance.
11496

11597

116-
#### `block_patterns: list[Pattern]`
98+
#### `options_update: Mapping[str, Any] | None = None`
11799

118-
A list of `Pattern` instances of block patterns to be used by the parser.
100+
A `Mapping` with preset's options overrides.
119101

120102

121-
#### `inline_patterns: list[Pattern]`
103+
#### `enable: str | Iterable[str] | None = None`
122104

123-
A list of `Pattern` instances of inline patterns to be used by the parser.
105+
Argument to call the `MarkdownIt.enable(...)` method with. If empty or `None`, `enable()` will not be called.
124106

125107

126-
#### `pre_processors: list[Callable[[Parser, str], str]]`
108+
#### `disable: str | Iterable[str] | None = None`
127109

128-
A list of pre-processor functions called by the parser to prepare markup for parsing.
110+
Argument to call the `MarkdownIt.disable(...)` method with. If empty or `None`, `disable()` will not be called.
129111

130-
A pre-processor function should have the following signature:
131112

132-
```python
133-
def custom_postprocessor(parser: Parser, markup: str) -> str:
134-
# Do something with the 'markup'...
135-
return markup
136-
```
137-
138-
139-
#### `post_processors: list[Callable[[Parser, list[dict]], list[dict]]]`
140-
141-
A list of post-processor functions called by the parser to finalize the AST.
142-
143-
A post-processor function should have the following signature:
113+
#### `plugins: Iterable[Callable[[MarkdownIt], None]] | None = None`
144114

145-
```python
146-
def custom_postprocessor(parser: Parser, ast: list[dict]) -> list[dict]:
147-
# Do something with the 'ast'...
148-
return ast
149-
```
115+
A list of `MarkdownIt` plugins. Each plugin must be a callable that accepts a single argument: the `MarkdownIt` instance.
150116

151117

152118
### Return value
153119

154-
An instance of the `Parser` class.
120+
An instance of the `MarkdownIt` class.
155121

156122

157123
## Example
158124

159-
The code below implements a custom filter function that adds new block pattern to the parser:
125+
The code below implements a custom filter function that disables automatic linkification in parsed messages:
160126

161127
```python
162-
from misago.parser.context import ParserContext
163-
from misago.parser.hooks import create_parser_hook
164-
from misago.parser.parser import Parser
128+
from typing import Any, Callable, Iterable, Mapping
165129

166-
from .patterns import PluginPattern
130+
from markdown_it import MarkdownIt
131+
from markdown_it.utils import PresetType
132+
from misago.parser.hooks import create_parser_hook
167133

168134

169135
@create_parser_hook.append_filter
170-
def create_parser_with_custom_pattern(
136+
def create_customized_parser(
171137
action,
172-
context: ParserContext,
173138
*,
174-
block_patterns,
175-
**kwargs,
139+
config: str | PresetType,
140+
options_update: Mapping[str, Any] | None = None,
141+
enable: str | Iterable[str] | None = None,
142+
disable: str | Iterable[str] | None = None,
143+
plugins: Iterable[Callable[[MarkdownIt], None]] | None = None,
176144
) -> Parser:
177-
block_patterns.append(PluginPattern)
178-
179-
# Call the next function in chain
180-
return action(context, block_patterns=block_patterns, **kwargs)
145+
options_update["linkify"] = False
146+
147+
return action(
148+
config=config,
149+
options_update=options_update,
150+
enable=enable,
151+
disable=disable,
152+
plugins=plugins,
153+
)
181154
```

0 commit comments

Comments
 (0)