refactor(import/utils.handleH1): simplify handleH1

This commit is contained in:
Panagiotis Papadopoulos 2025-01-21 00:25:46 +01:00
parent 1de9bc7c6f
commit 2296d1a6ba

View File

@ -3,16 +3,17 @@
function handleH1(content: string, title: string) {
let isFirstH1Handled = false;
content = content.replace(/<h1[^>]*>([^<]*)<\/h1>/gi, (match, text) => {
if (title.trim() === text.trim() && !isFirstH1Handled) {
return content.replace(/<h1[^>]*>([^<]*)<\/h1>/gi, (match, text) => {
const convertedContent = `<h2>${text}</h2>`;
// 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 `<h2>${text}</h2>`;
return title.trim() === text.trim() ? "" : convertedContent;
}
return convertedContent;
});
return content;
}
function extractHtmlTitle(content: string): string | null {