`;
} finally {
showLoadingState(false);
}
}
function parseAndRenderContent(text, type) {
const lines = text.split('\n').filter(Boolean);
let html = '
';
const parsedContent = { title: '', sections: [] };
let currentSection = null;
lines.forEach(line => {
const titleMatch = line.match(/^\[TITLE\]\s*(.*)/);
const introMatch = line.match(/^\[INTRODUCTION\]\s*(.*)/);
const conclusionMatch = line.match(/^\[CONCLUSION\]\s*(.*)/);
const checklistMatch = line.match(/^\[CHECKLIST\]/);
const chapterMatch = line.match(/^\[CHAPTER \d+\]\s*(.*)/);
const dayMatch = line.match(/^\[DAY \d+\]\s*(.*)/);
const itemMatch = line.match(/^\s*-\s*(.*)/);
if (titleMatch) {
const title = titleMatch[1].trim();
parsedContent.title = title;
html += ``;
}
}
function showLoadingState(isLoading) {
if (isLoading) {
previewPlaceholder.classList.add('hidden');
resultsPreview.innerHTML = '
${title}
`; } else if (introMatch) { const intro = introMatch[1].trim(); currentSection = { heading: 'Introduction', items: [intro] }; parsedContent.sections.push(currentSection); html += `Introduction
${intro}
`; } else if (conclusionMatch) { const conclusion = conclusionMatch[1].trim(); currentSection = { heading: 'Conclusion', items: [conclusion] }; parsedContent.sections.push(currentSection); html += `Conclusion
${conclusion}
`; } else if (chapterMatch || dayMatch || checklistMatch) { const heading = (chapterMatch || dayMatch)?.[1]?.trim() || 'Checklist'; currentSection = { heading: heading, items: [] }; parsedContent.sections.push(currentSection); html += `${heading}
`; if(type === 'checklist') html += ''; else html += '
';
return { parsedContent, html };
}
function renderContent(text, type) {
const { parsedContent, html } = parseAndRenderContent(text, type);
if (parsedContent.title) {
lastGeneratedContent = parsedContent;
previewPlaceholder.classList.add('hidden');
resultsPreview.innerHTML = html;
} else {
resultsPreview.innerHTML = `- ';
} else if (itemMatch && currentSection) {
const item = itemMatch[1].trim();
currentSection.items.push(item);
if(type === 'checklist') {
html += `
- ${item} `; } } }); if (html.endsWith('
${item}
`;
} else {
html += `- ') || html.endsWith('
')) {
html += type === 'checklist' ? '
' : 'The AI could not generate content in the expected format. Please try again.
';
generateBtn.disabled = true;
generateBtn.textContent = 'Generating...';
showFeedback('AI is crafting your content...', false, 0);
} else {
generateBtn.disabled = false;
generateBtn.textContent = 'Generate Content';
}
}
downloadPdfBtn.addEventListener('click', () => {
if (!lastGeneratedContent) {
showFeedback('Please generate content before downloading.', true, 3000);
return;
}
showFeedback('Generating PDF...', false, 2000);
try {
const { jsPDF } = window.jspdf;
const pdf = new jsPDF({ unit: 'pt', format: 'letter' });
const pageMargin = 72;
const pageWidth = pdf.internal.pageSize.getWidth();
const textWidth = pageWidth - (pageMargin * 2);
let cursorY = pageMargin;
function addText(text, size, style, spaceAfter) {
if (cursorY > pdf.internal.pageSize.getHeight() - pageMargin) {
pdf.addPage();
cursorY = pageMargin;
}
pdf.setFont('Helvetica', style);
pdf.setFontSize(size);
const lines = pdf.splitTextToSize(text, textWidth);
pdf.text(lines, pageMargin, cursorY);
cursorY += (lines.length * size * 1.2) + spaceAfter;
}
addText(lastGeneratedContent.title, 22, 'bold', 30);
lastGeneratedContent.sections.forEach(section => {
addText(section.heading, 16, 'bold', 10);
section.items.forEach(item => {
addText( (section.heading === 'Checklist' || item.startsWith('•') ? '' : '• ') + item, 11, 'normal', 8);
});
cursorY += 15;
});
pdf.save('lead-magnet-content.pdf');
} catch(err) {
console.error("PDF Generation Error: ", err);
showFeedback("Error generating PDF.", true, 3000);
}
});
let feedbackTimeout;
function showFeedback(message, isError = false, duration = 3000) {
clearTimeout(feedbackTimeout);
feedbackMessage.innerHTML = message;
feedbackMessage.style.color = isError ? '#ef4444' : '#4a5568';
if (duration !== 0) {
feedbackTimeout = setTimeout(() => { feedbackMessage.innerHTML = ''; }, duration);
}
}
});
