Files
MDHelper/mdhelper/markdown_compat.py

51 lines
2.0 KiB
Python

"""Small, shared Markdown compatibility rules for East Asian prose."""
from __future__ import annotations
import unicodedata
from markdown_it import MarkdownIt
from markdown_it.rules_inline import emphasis
from markdown_it.rules_inline.state_inline import StateInline
def _wide_letter(char: str) -> bool:
return unicodedata.east_asian_width(char) in ("W", "F") and unicodedata.category(char)[0] in ("L", "N")
def _punctuation(char: str) -> bool:
return unicodedata.category(char).startswith("P")
def _cjk_punctuation_boundary(punctuation: str, outside: str, inside: str) -> bool:
return _punctuation(punctuation) and (
unicodedata.east_asian_width(punctuation) in ("W", "F")
or _wide_letter(outside)
or _wide_letter(inside)
)
def _tokenize_emphasis(state: StateInline, silent: bool) -> bool:
start, first_delimiter = state.pos, len(state.delimiters)
matched = emphasis.tokenize(state, silent)
if not matched or state.src[start] != "*" or state.pos - start < 2:
return matched
def char_at(position: int) -> str:
return state.src[position] if 0 <= position < state.posMax else " "
before, after = char_at(start - 1), char_at(state.pos)
# Keep the upstream tokenizer/pairing algorithm, including nesting and the
# rule of three. Relax only punctuation boundaries of ** (or *** etc.) in
# CJK prose. Escapes, code, links and protected math never reach this rule.
allow_open = not before.isspace() and _cjk_punctuation_boundary(after, before, char_at(state.pos + 1))
allow_close = not after.isspace() and _cjk_punctuation_boundary(before, after, char_at(start - 2))
if allow_open or allow_close:
for delimiter in state.delimiters[first_delimiter:]:
delimiter.open = delimiter.open or allow_open
delimiter.close = delimiter.close or allow_close
return matched
def cjk_strong_plugin(markdown: MarkdownIt) -> None:
markdown.inline.ruler.at("emphasis", _tokenize_emphasis)