fix: render strong emphasis beside CJK punctuation

This commit is contained in:
2026-10-29 16:06:21 +08:00
parent 9d4bc8c91a
commit fac5cbb011
7 changed files with 150 additions and 6 deletions
+1 -1
View File
@@ -1,3 +1,3 @@
"""MDHelper package."""
__version__ = "0.2.2"
__version__ = "0.2.3"
+3 -1
View File
@@ -13,6 +13,8 @@ from xml.sax.saxutils import escape
from markdown_it import MarkdownIt
from markdown_it.token import Token
from .markdown_compat import cjk_strong_plugin
WP_NS = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
DOCX_EXPORT_ENGINES = {
@@ -163,7 +165,7 @@ def export_markdown_to_docx_python_docx(source: str, output_path: Path) -> Path:
def _markdown_blocks(source: str) -> list[ParagraphBlock | TableBlock]:
markdown = MarkdownIt("commonmark", {"html": True}).enable(["table", "strikethrough"])
markdown = MarkdownIt("commonmark", {"html": True}).enable(["table", "strikethrough"]).use(cjk_strong_plugin)
return _DocxBuilder(markdown.parse(source)).build()
+50
View File
@@ -0,0 +1,50 @@
"""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)
+2 -1
View File
@@ -18,6 +18,7 @@ from pygments.lexers.special import TextLexer
from pygments.util import ClassNotFound
from .markdown_utils import slugify_heading
from .markdown_compat import cjk_strong_plugin
from .media import is_qt_video_player_url, media_kind, media_mime_type
@@ -44,7 +45,7 @@ class MarkdownRenderer:
"typographer": False,
"breaks": False,
},
).enable(["table", "strikethrough"])
).enable(["table", "strikethrough"]).use(cjk_strong_plugin)
self.markdown.renderer.rules["fence"] = self._render_fence
self.markdown.renderer.rules["code_block"] = self._render_code_block