From c8ae56bfb32f523c2f209cefb99eee6d02118f47 Mon Sep 17 00:00:00 2001 From: Taki Date: Mon, 16 Nov 2026 08:35:34 +0700 Subject: [PATCH 1/8] feat(xhtml): add extractElementById and cleanFootnoteMarkup for cross-file footnotes --- src/xhtml.ts | 95 +++++++++++++++++++++++++++++++++++++++++++++ test/xhtml.test.mjs | 51 ++++++++++++++++++++++++ 2 files changed, 146 insertions(+) diff --git a/src/xhtml.ts b/src/xhtml.ts index 19dcbed..addfcb4 100644 --- a/src/xhtml.ts +++ b/src/xhtml.ts @@ -14,3 +14,98 @@ export function sanitizeXhtml(markup: string): string { .replace(eventAttributes, '') .replace(javascriptUrls, ''); } + +function escapeRegex(str: string): string { + return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); +} + +/** + * Cleans extracted footnote markup by removing back-references, return arrows, + * unwrapping self-referential anchors, and sanitizing. + */ +export function cleanFootnoteMarkup(markup: string): string { + const cleaned = markup + .replace(/]*(?:role=["'][^"']*doc-backlink[^"']*|epub:type=["'][^"']*backlink[^"']*|class=["'][^"']*(?:footnote-backref|backlink)[^"']*)[^>]*>[\s\S]*?<\/a>/gi, '') + .replace(/[\s\u00A0]*[\u21A9\u2191\u21B5][\s\u00A0]*/g, '') + .replace(/]*\bhref=["']#[^"']*["'][^>]*>([\s\S]*?)<\/a>/gi, '$1'); + + return sanitizeXhtml(cleaned).trim(); +} + +/** + * Extracts the element matching `targetId` from raw XHTML/HTML content. + * Handles block elements (