How to implement custom plugin #396
Answered
by
Paillat-dev
Paillat-dev
asked this question in
Q&A
Replies: 2 comments 1 reply
-
You can read source code https://github.com/lepture/mistune/blob/master/src/mistune/plugins/spoiler.py You also can ask me to create a custom mistune plugin or directive for your needs with GitHub sponsor one time tier (Mistune enhance) |
Beta Was this translation helpful? Give feedback.
1 reply
-
import mistune
from mistune.plugins.formatting import strikethrough
from mistune.util import escape, safe_entity
from typing_extensions import override
if TYPE_CHECKING:
from mistune.core import BaseRenderer, InlineState
from mistune.inline_parser import InlineParser
from mistune.markdown import Markdown
from typing import Any
from mistune.helpers import PREVENT_BACKSLASH
from mistune.plugins.formatting import _parse_to_end # pyright: ignore[reportPrivateUsage,reportUnknownVariableType]
_SPOILER_END = re.compile(r"(?:" + PREVENT_BACKSLASH + r"\\\||[^\s\|])\|\|(?!\|)")
def parse_spoiler(inline: "InlineParser", m: Match[str], state: "InlineState") -> int | None:
return _parse_to_end(inline, m, state, "spoiler", _SPOILER_END) # pyright: ignore[reportUnknownVariableType]
def render_spoiler(renderer: "BaseRenderer", text: str) -> str: # noqa: ARG001 # pyright: ignore[reportUnusedParameter]
return "<discord-spoiler>" + text + "</discord-spoiler>"
def spoiler(md: "Markdown") -> None:
"""Add a mistune plugin to support spoiler tags. Similar to strikethrough but uses different syntax."""
md.inline.register(
"spoiler",
r"\|\|(?=[^\s\|])",
parse_spoiler,
before="link",
)
if md.renderer and md.renderer.NAME == "html":
md.renderer.register("spoiler", render_spoiler) |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
Paillat-dev
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I am trying to implement a plugin that matches the discord spoiler syntax (
||
for opening and closing, both inline and multiline) but I am struggling to understand how to do it and not really familiar with how to.Anyone could help ?
Beta Was this translation helpful? Give feedback.
All reactions