From 3f65043f21f299eb93d07f5d2a7b593907a0c0f9 Mon Sep 17 00:00:00 2001 From: wcqqq1214 Date: Tue, 27 Oct 2026 02:14:48 +0800 Subject: [PATCH] fix: preserve inline anchor text when parsing EPUB files --- astrbot/core/knowledge_base/parsers/epub_parser.py | 2 +- tests/test_epub_parser.py | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/astrbot/core/knowledge_base/parsers/epub_parser.py b/astrbot/core/knowledge_base/parsers/epub_parser.py index 159f6c73651..5d184ba2415 100644 --- a/astrbot/core/knowledge_base/parsers/epub_parser.py +++ b/astrbot/core/knowledge_base/parsers/epub_parser.py @@ -24,7 +24,7 @@ r"^(?:\d{1,3}|[ivxlcdm]{1,8}|[*†‡§¶]|↩|↑|back|return|返回|回到正文)$", re.I ) _FOOTNOTE_HREF_RE = re.compile( - r"(?:^#|[#/_-](?:fn|footnote|note|noteref|backlink|return|filepos)\b)", re.I + r"[#/_-](?:fn|footnote|note|noteref|backlink|return|filepos)\b", re.I ) _DOTTED_TOC_RE = re.compile(r"^\s*.+?\.{2,}\s*(?:\d+|[ivxlcdm]+)\s*$", re.I) _SEP_RE = re.compile(r"^\s*(?:[-=*_]){3,}\s*$") diff --git a/tests/test_epub_parser.py b/tests/test_epub_parser.py index b5e4aa06605..9fd64fc228c 100644 --- a/tests/test_epub_parser.py +++ b/tests/test_epub_parser.py @@ -5,7 +5,7 @@ import pytest -from astrbot.core.knowledge_base.parsers.epub_parser import EpubParser +from astrbot.core.knowledge_base.parsers.epub_parser import EpubParser, _strip_links from astrbot.core.knowledge_base.parsers.markitdown_parser import MarkitdownParser from astrbot.core.knowledge_base.parsers.util import select_parser @@ -132,6 +132,7 @@ def _make_epub_bytes_with_generic_content() -> bytes:

First

Lead text +

页内链接:需要离线备份才能继续。

Piura*5, continued.

Inside div
@@ -209,6 +210,7 @@ async def test_epub_parser_preserves_generic_container_text(): assert "**Title:**" not in result.text assert "# First" in result.text assert "Lead text" in result.text + assert "页内链接:需要离线备份才能继续。" in result.text assert r"Piura\*5, continued." in result.text assert "filepos" not in result.text assert r"[\*5]" not in result.text @@ -218,3 +220,11 @@ async def test_epub_parser_preserves_generic_container_text(): assert "Inside div" in result.text assert "Inside section" in result.text assert "| Cell A | Cell B |" in result.text + + +@pytest.mark.parametrize( + "link", + ["[1](#ref1)", "[footnote](#footnote-1)"], +) +def test_epub_link_cleanup_removes_footnotes(link): + assert _strip_links(f"Before {link} after") == "Before after"