-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdebug-chapter-detection.js
More file actions
114 lines (100 loc) · 4.52 KB
/
Copy pathdebug-chapter-detection.js
File metadata and controls
114 lines (100 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const path = require('path');
const fs = require('fs').promises;
async function debugChapterDetection() {
const filePath = path.join(__dirname, 'web/data/samples/Blood and Memory - Bonus Scenes - Alyssa Evans.docx');
console.log('=== DEBUGGING CHAPTER DETECTION ===');
console.log('File:', filePath);
console.log('');
try {
// Step 1: Convert DOCX to HTML
const mammoth = require('mammoth');
const arrayBuffer = await fs.readFile(filePath);
const result = await mammoth.convertToHtml({ buffer: arrayBuffer });
console.log('--- RAW HTML OUTPUT FROM MAMMOTH ---');
console.log(result.value.substring(0, 2000)); // First 2000 chars
console.log('...');
console.log('');
// Step 2: Show what htmlToMarkdownStructure produces
const htmlToMarkdownStructure = (html) => {
// STEP 1: Normalize multi-line headings FIRST
html = html.replace(/<h([1-6])([^>]*)>([\s\S]*?)<\/h\1>/gi, (match, level, attrs, inner) => {
const cleanInner = inner.replace(/<[^>]*>/g, '').replace(/\s+/g, ' ').trim();
return `<h${level}${attrs}>${cleanInner}</h${level}>`;
});
// STEP 2: Split heading content if first paragraph is inlined WITHOUT proper separation
html = html.replace(/<h([1-6])([^>]*)>(Chapter\s+\d+)([A-Z][a-z][^<]*)<\/h\1>/g, (m, lvl, attrs, cap, rest) => {
return `<h${lvl}${attrs}>${cap}</h${lvl}>\n<p>${rest}</p>`;
});
html = html.replace(/<h([1-6])([^>]*)>((?:Chapter|Ch\.?|Part|Section)\s+[A-Za-z0-9ivxlcdm]+)([A-Z][a-z][^<]*)<\/h\1>/g, (m, lvl, attrs, cap, rest) => {
return `<h${lvl}${attrs}>${cap}</h${lvl}>\n<p>${rest}</p>`;
});
// Only split special chapters when NO subtitle punctuation is present
html = html.replace(/<h([1-6])([^>]*)>((?:Bonus|Epilogue|Prologue|Final|Extra)\s+Chapter)(?![\s:.\-])([A-Z][a-z][^<]*)<\/h\1>/g, (m, lvl, attrs, cap, rest) => {
return `<h${lvl}${attrs}>${cap}</h${lvl}>\n<p>${rest}</p>`;
});
return html
.replace(/<h1[^>]*>(.*?)<\/h1>/gi, (_m, inner) => `# ${inner.trim()}\n\n`)
.replace(/<h2[^>]*>(.*?)<\/h2>/gi, (_m, inner) => `## ${inner.trim()}\n\n`)
.replace(/<h3[^>]*>(.*?)<\/h3>/gi, (_m, inner) => `### ${inner.trim()}\n\n`)
.replace(/<h4[^>]*>(.*?)<\/h4>/gi, (_m, inner) => `#### ${inner.trim()}\n\n`)
.replace(/<h5[^>]*>(.*?)<\/h5>/gi, (_m, inner) => `##### ${inner.trim()}\n\n`)
.replace(/<h6[^>]*>(.*?)<\/h6>/gi, (_m, inner) => `###### ${inner.trim()}\n\n`)
.replace(/<p[^>]*>(.*?)<\/p>/gi, (match, content) => {
const text = content.trim();
const chapterPatterns = [
/^chapter\s+/i,
/^ch\.?\s*\d+/i,
/^\d+\.\s*[^.]/,
/^part\s+\d+/i,
/^section\s+\d+/i,
/^bonus\s+chapter/i,
/^epilogue\s+chapter/i,
/^prologue\s+chapter/i,
/^final\s+chapter/i,
/^extra\s+chapter/i
];
const looksLikeChapter = chapterPatterns.some(pattern => pattern.test(text)) && text.length < 150;
if (looksLikeChapter) {
return text + '\n\n';
}
return text + '\n\n';
})
.replace(/<[^>]*>/g, '')
.replace(/^(#\s+Chapter\s+\d+)([A-Z][a-z]+)/gm, (_, h, rest) => `${h}\n\n${rest}`)
.replace(/ /g, ' ')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/&/g, '&')
.replace(/[ \t]+/g, ' ')
.replace(/\n{3,}/g, '\n\n')
.trim();
};
const markdown = htmlToMarkdownStructure(result.value);
console.log('--- CONVERTED MARKDOWN STRUCTURE ---');
console.log(markdown.substring(0, 3000)); // First 3000 chars
console.log('...');
console.log('');
console.log('Full markdown length:', markdown.length, 'characters');
console.log('');
// Step 3: Show all lines that might be chapter markers
console.log('--- POTENTIAL CHAPTER MARKER LINES ---');
const lines = markdown.split(/\r?\n/);
lines.forEach((line, i) => {
const trimmed = line.trim();
if (trimmed && (
/^#+\s*chapter/i.test(trimmed) ||
/^chapter/i.test(trimmed) ||
/^bonus/i.test(trimmed) ||
/^epilogue/i.test(trimmed) ||
/^prologue/i.test(trimmed) ||
/^part\s+\d+/i.test(trimmed) ||
/^ch\./i.test(trimmed)
)) {
console.log(`Line ${i + 1}: "${trimmed}"`);
}
});
} catch (error) {
console.error('Error:', error);
}
}
debugChapterDetection();