From 4fb34bdef7e0ebf7cff64404c28404fa75164687 Mon Sep 17 00:00:00 2001 From: gamer147 Date: Wed, 10 Dec 2025 13:40:33 -0500 Subject: [PATCH] [FA-misc] Should fix paragraph blocking --- .../lib/components/ChapterReaderPage.svelte | 5 ++- .../src/lib/utils/sanitizeChapter.ts | 32 ++++++++++++++++++- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/fictionarchive-web-astro/src/lib/components/ChapterReaderPage.svelte b/fictionarchive-web-astro/src/lib/components/ChapterReaderPage.svelte index 69572c4..97ae4aa 100644 --- a/fictionarchive-web-astro/src/lib/components/ChapterReaderPage.svelte +++ b/fictionarchive-web-astro/src/lib/components/ChapterReaderPage.svelte @@ -155,10 +155,9 @@
p]:text-foreground [&>p]:mb-6 [&>p]:leading-relaxed" > {@html sanitizedBody}
diff --git a/fictionarchive-web-astro/src/lib/utils/sanitizeChapter.ts b/fictionarchive-web-astro/src/lib/utils/sanitizeChapter.ts index 25d4ea7..8e98994 100644 --- a/fictionarchive-web-astro/src/lib/utils/sanitizeChapter.ts +++ b/fictionarchive-web-astro/src/lib/utils/sanitizeChapter.ts @@ -1,12 +1,42 @@ import DOMPurify from 'isomorphic-dompurify'; +/** + * Splits plain text into paragraphs based on newlines. + * Double newlines create new paragraphs, single newlines become
. + * If the content already contains HTML block elements, returns as-is. + */ +function wrapInParagraphs(text: string): string { + // Check if content already has block-level HTML elements + const hasBlockElements = /<(p|div|h[1-6]|ul|ol|blockquote|pre|table|hr)[>\s]/i.test(text); + if (hasBlockElements) { + return text; + } + + // Split on double newlines (paragraph breaks) + const paragraphs = text.split(/\n\s*\n/); + + return paragraphs + .map((para) => { + const trimmed = para.trim(); + if (!trimmed) return ''; + // Convert single newlines to
within paragraphs + const withBreaks = trimmed.replace(/\n/g, '
'); + return `

${withBreaks}

`; + }) + .filter(Boolean) + .join('\n'); +} + /** * Sanitizes chapter HTML content with extended allowed tags. * More permissive than the description sanitizer to support * formatted novel content including headings, lists, and images. + * Also wraps plain text in paragraph tags for better browser translate support. */ export function sanitizeChapterHtml(html: string): string { - return DOMPurify.sanitize(html, { + // First wrap in paragraphs if needed, then sanitize + const wrapped = wrapInParagraphs(html); + return DOMPurify.sanitize(wrapped, { ALLOWED_TAGS: [ // Basic formatting 'b', -- 2.49.1