This hook wraps the standard function Misago uses to create a token stream from markup.
Token stream is a list of the Token instances from markdown_it.tokens module.
This hook can be imported from misago.parser.hooks:
from misago.parser.hooks import tokenize_hookdef custom_tokenize_filter(
action: TokenizeHookAction,
parser: MarkdownIt,
markup: str,
processors: list[Callable[[list[Token]],
list[Token] | None]],
) -> list[Token]:
...A function implemented by a plugin that can be registered in this hook.
Misago function used to create a token stream from markup.
See the action section for details.
A MarkdownIt instance used to parse the markup string.
A str to tokenize.
A list of callables that each accept a single argument (a list of tokens) and return either a updated list of tokens or None if no changes were made.
A list of Token instances.
def tokenize_action(
parser: MarkdownIt,
markup: str,
processors: list[Callable[[list[Token]],
list[Token] | None]],
) -> list[Token]:
...Misago function used to create a token stream from markup.
A MarkdownIt instance used to parse the markup string.
A str to tokenize.
A list of callables that each accept a single argument (a list of tokens) and return either a updated list of tokens or None if no changes were made.
A list of Token instances.
The code below implements a custom filter function that includes custom processor for token stream:
from typing import Callable
from markdown_it import MarkdownIt
from markdown_it.tokens import Token
from misago.parser.hooks import tokenize_hook
def custom_tokens_processor(tokens: list[Token]) -> list[Token] | None:
return tokens # Return changed `tokens` list or None
@tokenize_hook.append_filter
def tokenize_with_custom_tokens_processor(
action,
parser: MarkdownIt,
markup: str,
processors: list[Callable[[list[Token]], list[Token] | None]],
) -> list[Token]:
processors.append(custom_tokens_processor)
return action(parser, markup, processors)