refactor(turndown): remove text-node escape rules that never run

Turndown only applies rules to element nodes; text nodes always go through
its built-in escape(). The `escapeText` rules and their `escapeMarkdown`
helpers in HTML to Markdown, HTML to data and EPUB to Markdown were never
called. They also escaped `|` twice, which would have produced `\\|` had
anyone wired them up. Output is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Sebastian DanielssonandClaude Opus 5.5 authored and sharevb committed 2026-10-03 21:11:50 +02:00
1 parent 3872432a07
commit f1b20c7c18
3 files changed
-50

No files matched your search

@@ -4,14 +4,6 @@ import type { SpineItem } from 'epubjs/types/section';
import TurndownService from 'turndown';
import { gfm as addGFM } from '@guyplusplus/turndown-plugin-gfm';
function escapeMarkdown(text: string) {
return text
.replace(/\\/g, '\\\\') // escape backslash first
.replace(/([*_#>|`])/g, '\\$1') // escape common markdown symbols
.replace(/([\[\]\(\)])/g, '\\$1') // escape brackets and parentheses
.replace(/\|/g, '\\|'); // escape table pipes
}
interface SpineItems {
items: SpineItem[];
get(href: string | undefined): Section;
@@ -20,14 +12,6 @@ export async function extractTextAndMetaFromEPUB(file: File, format: 'text' | 'm
let turndownService: TurndownService;
if (format === 'markdown') {
turndownService = new TurndownService();
turndownService.addRule('escapeText', {
filter(node) {
return node.nodeType === 3; // text nodes
},
replacement(content) {
return escapeMarkdown(content);
},
});
addGFM(turndownService);
turndownService.addRule('normalizedHeading', {
filter: ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'],
-17
View File
@@ -34,23 +34,6 @@ const formats = [
const turndownService = new TurndownService();
function escapeMarkdown(text: string) {
return text
.replace(/\\/g, '\\\\') // escape backslash first
.replace(/([*_#>|`])/g, '\\$1') // escape common markdown symbols
.replace(/([\[\]\(\)])/g, '\\$1') // escape brackets and parentheses
.replace(/\|/g, '\\|'); // escape table pipes
}
turndownService.addRule('escapeText', {
filter(node) {
return node.nodeType === 3; // text nodes
},
replacement(content) {
return escapeMarkdown(content);
},
});
addGFM(turndownService);
async function convertContent() {
@@ -20,23 +20,6 @@ const emphasisStyle = useQueryParamOrStorage<string>({
const turndownService = new TurndownService();
function escapeMarkdown(text: string) {
return text
.replace(/\\/g, '\\\\') // escape backslash first
.replace(/([*_#>|`])/g, '\\$1') // escape common markdown symbols
.replace(/([\[\]\(\)])/g, '\\$1') // escape brackets and parentheses
.replace(/\|/g, '\\|'); // escape table pipes
}
turndownService.addRule('escapeText', {
filter(node) {
return node.nodeType === 3; // text nodes
},
replacement(content) {
return escapeMarkdown(content);
},
});
addGFM(turndownService);
turndownService.addRule('normalizedHeading', {