import zipfile import pytest from lxml import etree as ET from markdown_it import MarkdownIt from mdhelper.markdown_compat import cjk_strong_plugin from mdhelper.markdown_renderer import MarkdownRenderer from mdhelper.docx_exporter import export_markdown_to_docx SCREENSHOT_SOURCE = '''1. **先完成一项指标样稿。**选择一个有代表性的指标,提交完整写法。 2. **按分组开展调研。**每完成一组,检查指标覆盖情况。 3. **提交PPT并开展分享。**原则上每组安排一次。 4. **根据讨论修订。**将答辩中发现的问题补充到文档。 ''' @pytest.mark.parametrize("source,strong", [ ("**先完成一项指标样稿。**选择一个指标", "先完成一项指标样稿。"), ("前文**中文。**后文", "中文。"), ("前文**“术语”**后文", "“术语”"), ("**注意:**请先核对", "注意:"), ("**说明(可选)**补充", "说明(可选)"), ("**中文:**下一段", "中文:"), ('前文**"术语"**后文', '"术语"'), ("**中文。**Next", "中文。"), ("Prefix**“中文”**suffix", "“中文”"), ]) def test_cjk_strong_boundaries(source, strong): markdown = MarkdownIt("commonmark").use(cjk_strong_plugin) from html import escape result = markdown.render(source) assert "**" not in result assert "" + escape(strong) + "" in result def test_real_renderer_screenshot_list_and_adjacent_spans(tmp_path): renderer = MarkdownRenderer(tmp_path) rendered = renderer.render_source(SCREENSHOT_SOURCE, tmp_path / "sample.md") assert "**" not in renderer.markdown.render(SCREENSHOT_SOURCE) assert rendered.count("") == 4 assert "先完成一项指标样稿。选择" in rendered assert renderer.markdown.render("**甲。**乙**丙。**丁").count("") == 2 assert "中文。后文" in renderer.markdown.render("***中文。***后文") @pytest.mark.parametrize("source", [ r"\*\*中文。\*\*后文", "`**中文。**后文`", "```md\n**中文。**后文\n```", " **中文。**后文", "** 中文 **", "**中文 **", "**中文。", "*中文。*后文", "__中文。__后文", '**hello.**world', 'foo**"bar"**baz', '2 ** 3 ** 4', 'text', '[链接](https://example.com/**中文。**后文)', '', '---\n\n****\n', "**普通加粗** 与 *普通斜体*", ]) def test_existing_markdown_and_literal_markers_stay_unchanged(source): normal = MarkdownIt("commonmark", {"html": True}) compatible = MarkdownIt("commonmark", {"html": True}).use(cjk_strong_plugin) assert compatible.render(source) == normal.render(source) def test_math_is_still_protected(tmp_path): renderer = MarkdownRenderer(tmp_path) source = r'$\text{**说明。**后文} + x**2$' result = renderer.markdown.render(source) assert "math-inline" in result assert "" not in result assert "**说明。**后文" in result and "x**2" in result def test_headings_links_tables_and_nested_emphasis(tmp_path): renderer = MarkdownRenderer(tmp_path) source = '# **标题:**说明\n\n[**链接:**说明](https://example.com)\n\n| 项目 | 说明 |\n| --- | --- |\n| **术语:**正文 | **含*斜体*的句子。**继续 |\n' result = renderer.markdown.render(source) assert "**" not in result assert result.count("") == 4 and '斜体' in result @pytest.mark.parametrize("engine", ["builtin", "python-docx", "pandoc"]) def test_word_export_preserves_cjk_bold_and_body_boundaries(tmp_path, engine): path = export_markdown_to_docx(SCREENSHOT_SOURCE, tmp_path / (engine + '.docx'), engine=engine) with zipfile.ZipFile(path) as package: root = ET.fromstring(package.read('word/document.xml')) ns = {'w': 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'} all_text = ''.join(root.xpath('//w:t/text()', namespaces=ns)) bold_text = ''.join(root.xpath('//w:r[w:rPr/w:b[not(@w:val="0")]]/w:t/text()', namespaces=ns)) assert '**' not in all_text for expected in ('先完成一项指标样稿。', '按分组开展调研。', '提交PPT并开展分享。', '根据讨论修订。'): assert expected in bold_text assert '选择一个' not in bold_text and '每完成一组' not in bold_text