修复 Markdown 与 HTML 单换行粘贴到 Word 的保留问题 - #78
Merged
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
该 PR 旨在解决从 HTML/Markdown 粘贴到 Word 时,“段落内单换行(\n)被合并成一行”的问题:对 HTML 侧把依赖 CSS white-space 显示出来的换行显式转换为 <br>,对 Markdown 侧新增可配置开关以启用 Pandoc 的 hard_line_breaks reader 扩展,从而在生成 DOCX/RTF/HTML/LaTeX 时保留段落内单换行。
Changes:
- HTML 预处理:对
style含white-space: pre-wrap/pre-line/break-spaces的节点,将文本节点中的\n转换为<br>,使 Pandoc 可正确输出 Word 硬换行。 - Markdown 转换链路:新增
markdown_hard_line_breaks配置并贯穿到 Pandoc 调用,开启时使用markdown+hard_line_breaks+...输入格式。 - 设置与文案:新增设置项与中/英/日 i18n 文案,并加入默认配置。
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| pastemd/service/preprocessor/html.py | 新增 HTML 预处理逻辑,将 CSS 保留的换行显式化为 <br> |
| pastemd/service/document/generator.py | 将 markdown_hard_line_breaks 配置透传到各 Markdown 转换接口 |
| pastemd/presentation/settings/dialog.py | 设置页新增“Markdown:保留段落内单换行”开关并保存配置 |
| pastemd/integrations/pandoc.py | 抽取 Markdown reader 格式构造,支持按开关启用 hard_line_breaks |
| pastemd/i18n/locales/zh-CN.json | 新增设置项中文文案 |
| pastemd/i18n/locales/ja-JP.json | 新增设置项日文文案 |
| pastemd/i18n/locales/en-US.json | 新增设置项英文文案 |
| pastemd/config/defaults.py | 新增默认配置 markdown_hard_line_breaks: False |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+46
to
+48
| for tag in soup.find_all(style=PRESERVE_NEWLINE_WHITE_SPACE_RE): | ||
| if not isinstance(tag, Tag): | ||
| continue |
Owner
Author
|
修复了 #45 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
变更内容
white-space: pre-wrap/pre-line/break-spaces的段落换行丢失问题hard_line_breaks,让段落内单换行在 Word 中保留为硬换行背景原因
部分来源复制出的 HTML 会把多行文本放在同一个带
white-space: pre-wrap的<p>中。浏览器能按 CSS 显示换行,但 Pandoc 转 DOCX 时不会把文本节点里的\n写成 Word 硬换行,导致选项被合并成一行。纯 Markdown 路径里,段落内单换行会被 Pandoc 按 Markdown 规范解析为
SoftBreak,写入 Word 时表现为空格。因此增加一个可配置开关,用于选择题、选项文本等需要逐行保留的场景。