-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdebug-chapter-simple.js
More file actions
35 lines (28 loc) · 1.49 KB
/
Copy pathdebug-chapter-simple.js
File metadata and controls
35 lines (28 loc) · 1.49 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
// Test the fixed regex logic
const testCases = [
'<h1>Bonus Chapter: The Competition</h1>', // Should NOT split (subtitle)
'<h1>Bonus ChapterThree weeks after</h1>', // SHOULD split (merged body text)
'<h1>Chapter 1</h1>', // Should NOT split (no body)
'<h1>Chapter 1Dominik stood</h1>', // SHOULD split (merged body text)
];
testCases.forEach((html, i) => {
console.log(`\n=== Test Case ${i + 1} ===`);
console.log('Original HTML:', html);
// Step 1: Normalize multi-line headings
let result = 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}>`;
});
console.log('After Step 1:', result);
// Step 2a: Chapter N + merged text
result = result.replace(/<h([1-6])([^>]*)>(Chapter\s+\d+)([A-Z][a-z][^<]*)<\/h\1>/g, (m, lvl, attrs, cap, rest) => {
console.log(' → Split Chapter N pattern');
return `<h${lvl}${attrs}>${cap}</h${lvl}>\n<p>${rest}</p>`;
});
// Step 2b: Special chapters ONLY when NO subtitle punctuation
result = result.replace(/<h([1-6])([^>]*)>((?:Bonus|Epilogue|Prologue|Final|Extra)\s+Chapter)(?![\s:.\-])([A-Z][a-z][^<]*)<\/h\1>/g, (m, lvl, attrs, cap, rest) => {
console.log(' → Split Special Chapter pattern (no subtitle)');
return `<h${lvl}${attrs}>${cap}</h${lvl}>\n<p>${rest}</p>`;
});
console.log('After Step 2:', result);
});