From 2296d1a6ba351c861c8552ff6f3fdf3b453c4cac Mon Sep 17 00:00:00 2001 From: Panagiotis Papadopoulos Date: Tue, 21 Jan 2025 00:25:46 +0100 Subject: [PATCH] refactor(import/utils.handleH1): simplify handleH1 --- src/services/import/utils.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/services/import/utils.ts b/src/services/import/utils.ts index 2f4227f0a..41c42ae22 100644 --- a/src/services/import/utils.ts +++ b/src/services/import/utils.ts @@ -3,16 +3,17 @@ function handleH1(content: string, title: string) { let isFirstH1Handled = false; - content = content.replace(/]*>([^<]*)<\/h1>/gi, (match, text) => { - if (title.trim() === text.trim() && !isFirstH1Handled) { + return content.replace(/]*>([^<]*)<\/h1>/gi, (match, text) => { + const convertedContent = `

${text}

`; + + // strip away very first found h1 tag, if it matches the title + if (!isFirstH1Handled) { isFirstH1Handled = true; - return ""; // remove whole H1 tag - } else { - isFirstH1Handled = true; - return `

${text}

`; + return title.trim() === text.trim() ? "" : convertedContent; } + + return convertedContent; }); - return content; } function extractHtmlTitle(content: string): string | null {