Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: handle nested divs in Accelerando source

[._.]/ Adam Eivycff926ef786d
diff --git a/server/services/rapidReader.js b/server/services/rapidReader.js
index f51b0f581c..45cecbec8e 100644
--- a/server/services/rapidReader.js
+++ b/server/services/rapidReader.js
@@ -37,6 +37,26 @@ const REQUIRED_MARKERS = [
'Chapter 1:',
];
+function extractBookHtml(html) {
+ const opening = /<div\b[^>]*\bid\s*=\s*(["'])book\1[^>]*>/i.exec(html);
+ if (!opening) return '';
+
+ const bodyStart = opening.index + opening[0].length;
+ const tags = /<!--[\s\S]*?-->|<(script|style|noscript)\b[\s\S]*?<\/\1\s*>|<div\b[^>]*>|<\/div\s*>/gi;
+ tags.lastIndex = bodyStart;
+ let depth = 1;
+ let tag;
+ while ((tag = tags.exec(html))) {
+ if (/^<div\b/i.test(tag[0]) && !/\/\s*>$/.test(tag[0])) {
+ depth += 1;
+ } else if (/^<\/div\b/i.test(tag[0])) {
+ depth -= 1;
+ if (depth === 0) return html.slice(bodyStart, tag.index);
+ }
+ }
+ return '';
+}
+
/**
* Extract the book body from the official single-page HTML edition and turn it
* into the plain text the RSVP reader expects. The marker checks keep an error
@@ -44,7 +64,7 @@ const REQUIRED_MARKERS = [
*/
export function extractAccelerandoText(html) {
if (typeof html !== 'string') return '';
- const bookHtml = html.match(/<div\b[^>]*\bid=["']book["'][^>]*>([\s\S]*?)<\/div>/i)?.[1];
+ const bookHtml = extractBookHtml(html);
if (!bookHtml) return '';
const text = htmlToText(bookHtml.replace(/<!--[\s\S]*?-->/g, ''), {
extraEntities: SOURCE_ENTITIES,
diff --git a/server/services/rapidReader.test.js b/server/services/rapidReader.test.js
index 6d1ad509af..3532d3de89 100644
--- a/server/services/rapidReader.test.js
+++ b/server/services/rapidReader.test.js
@@ -28,6 +28,11 @@ const SOURCE_HTML = `<!doctype html>
</div>
</body></html>`;
+const NESTED_SOURCE_HTML = SOURCE_HTML.replace(
+ ' </div>\n</body>',
+ ' <div class="nested"><p>Chapter 9: Finale</p></div>\n </div>\n</body>',
+);
+
beforeEach(() => {
vi.clearAllMocks();
rmSync(CACHE_FILE, { force: true });
@@ -48,6 +53,10 @@ describe('extractAccelerandoText', () => {
expect(service.extractAccelerandoText('<html><body>Temporary error</body></html>')).toBe('');
expect(service.extractAccelerandoText(null)).toBe('');
});
+
+ it('keeps the full book body when the source contains nested divs', () => {
+ expect(service.extractAccelerandoText(NESTED_SOURCE_HTML)).toContain('Chapter 9: Finale');
+ });
});
describe('getAccelerandoBook', () => {