mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-25 15:15:23 +03:00
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:
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 TurndownService from 'turndown';
|
||||||
import { gfm as addGFM } from '@guyplusplus/turndown-plugin-gfm';
|
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 {
|
interface SpineItems {
|
||||||
items: SpineItem[];
|
items: SpineItem[];
|
||||||
get(href: string | undefined): Section;
|
get(href: string | undefined): Section;
|
||||||
@@ -20,14 +12,6 @@ export async function extractTextAndMetaFromEPUB(file: File, format: 'text' | 'm
|
|||||||
let turndownService: TurndownService;
|
let turndownService: TurndownService;
|
||||||
if (format === 'markdown') {
|
if (format === 'markdown') {
|
||||||
turndownService = new TurndownService();
|
turndownService = new TurndownService();
|
||||||
turndownService.addRule('escapeText', {
|
|
||||||
filter(node) {
|
|
||||||
return node.nodeType === 3; // text nodes
|
|
||||||
},
|
|
||||||
replacement(content) {
|
|
||||||
return escapeMarkdown(content);
|
|
||||||
},
|
|
||||||
});
|
|
||||||
addGFM(turndownService);
|
addGFM(turndownService);
|
||||||
turndownService.addRule('normalizedHeading', {
|
turndownService.addRule('normalizedHeading', {
|
||||||
filter: ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'],
|
filter: ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'],
|
||||||
|
|||||||
@@ -34,23 +34,6 @@ const formats = [
|
|||||||
|
|
||||||
const turndownService = new TurndownService();
|
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);
|
addGFM(turndownService);
|
||||||
|
|
||||||
async function convertContent() {
|
async function convertContent() {
|
||||||
|
|||||||
@@ -20,23 +20,6 @@ const emphasisStyle = useQueryParamOrStorage<string>({
|
|||||||
|
|
||||||
const turndownService = new TurndownService();
|
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);
|
addGFM(turndownService);
|
||||||
|
|
||||||
turndownService.addRule('normalizedHeading', {
|
turndownService.addRule('normalizedHeading', {
|
||||||
|
|||||||
Reference in new issue
Block a user