update: 更新导出TXT内容格式,优化拆书导入逻辑

This commit is contained in:
xiamuceer
2026-03-24 11:29:09 +08:00
parent 97c2226f9d
commit 7a3c2d9a9f
2 changed files with 47 additions and 36 deletions
+21 -35
View File
@@ -351,7 +351,7 @@ async def export_project_chapters(
):
"""
导出项目的所有章节内容为TXT文本文件
按章节顺序组织,包含项目基本信息
按章节顺序组织,使用便于再次拆书导入的纯章节格式
"""
try:
# 从认证中间件获取用户ID
@@ -388,46 +388,32 @@ async def export_project_chapters(
txt_content = []
txt_content.append("=" * 80)
txt_content.append(f"项目标题: {project.title}")
txt_content.append("=" * 80)
if project.description:
txt_content.append(f"\n简介: {project.description}\n")
if project.theme:
txt_content.append(f"主题: {project.theme}")
if project.genre:
txt_content.append(f"类型: {project.genre}")
txt_content.append(f"总章节数: {len(chapters)}")
txt_content.append(f"总字数: {project.current_words}")
txt_content.append("\n" + "=" * 80 + "\n\n")
for chapter in chapters:
# 只显示主章节号,不显示子索引
txt_content.append(f"{chapter.chapter_number}{chapter.title}")
txt_content.append("-" * 80)
txt_content.append("") # 空行
if chapter.content:
txt_content.append(chapter.content)
for idx, chapter in enumerate(chapters):
chapter_title = (chapter.title or "").strip() or f"未命名章节{chapter.chapter_number}"
raw_content = (chapter.content or "").strip()
if raw_content:
formatted_lines = []
for line in raw_content.splitlines():
stripped_line = line.strip()
if stripped_line:
formatted_lines.append(f"  {stripped_line}")
else:
txt_content.append("(本章暂无内容)")
formatted_lines.append("")
chapter_content = "\n".join(formatted_lines)
else:
chapter_content = "  (本章暂无内容)"
txt_content.append("\n\n" + "=" * 80 + "\n\n")
# 使用拆书强匹配可稳定识别的章节标题格式:第X章 标题
txt_content.append(f"{chapter.chapter_number}{chapter_title}")
txt_content.append(chapter_content)
txt_content.append(f"--- 全文完 ---")
# 获取当前时间
from datetime import datetime
export_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
txt_content.append(f"\n导出时间: {export_time}")
# 章节之间只保留一个空行,避免装饰性分割线干扰拆书识别
if idx < len(chapters) - 1:
txt_content.append("")
final_content = "\n".join(txt_content)
safe_title = "".join(c for c in project.title if c.isalnum() or c in (' ', '-', '_', '', '', ''))
safe_title = "".join(c for c in (project.title or "未命名项目") if c.isalnum() or c in (' ', '-', '_', '', '', ''))
filename = f"{safe_title}.txt"
from urllib.parse import quote
+25
View File
@@ -688,6 +688,31 @@ export default function BookImport() {
<p className="ant-upload-hint"> .txt 50MB</p>
</Dragger>
<Alert
type="info"
showIcon
message="支持的拆书 TXT 格式要求"
description={
<div style={{ lineHeight: 1.8 }}>
<div>1. <strong>.txt</strong> 使</div>
<div>2. <strong>1 </strong></div>
<div>3. </div>
<div>4. 线</div>
<div style={{ marginTop: 8 }}>
<pre style={{ margin: '8px 0 0', padding: 12, borderRadius: 8, background: token.colorFillAlter, whiteSpace: 'pre-wrap' }}>
{`第1章 初入江湖
这里是第1章正文第一段。
这里是第1章正文第二段。
第2章 雨夜追踪
这里是第2章正文内容。`}
</pre>
</div>
</div>
}
/>
<Space>
<Button
type="primary"