Thought Leadership Article Generator
Generate insightful articles from your core ideas.
Define Your Core Idea
Structure Your Article
Generate & Refine Your Article
Preview & Export Article
Your Article Title Will Appear Here
Your generated article content will appear here once it's ready.
Your generated article content will appear here.
'; let bodyHtml = bodyRaw .replace(/\*\*(.*?)\*\*/g, '$1') .replace(/^\* (.*?)$/gm, '- $1
$1
') .replace(/\n/g, ''); bodyHtml = bodyHtml.replace(/<\/ul>
- /g, '');
previewBodyEl.innerHTML = bodyHtml;
};
const handleDownloadPdf = () => {
if (!window.jspdf) {
console.error('jsPDF library not found.');
return;
}
const { jsPDF } = window.jspdf;
const pdf = new jsPDF({ orientation: 'portrait', unit: 'pt', format: 'a4' });
const pageW = pdf.internal.pageSize.getWidth();
const pageH = pdf.internal.pageSize.getHeight();
const margin = 50;
const maxLineWidth = pageW - margin * 2;
let cursorY = margin;
const checkPageBreak = (elementHeight) => {
if (cursorY + elementHeight > pageH - margin) {
pdf.addPage();
cursorY = margin;
}
};
// --- PDF Template: Blue Themed Article ---
pdf.setFillColor(239, 246, 255); // Blue-50
pdf.rect(0, 0, pageW, 120, 'F');
pdf.setFont('helvetica', 'bold').setFontSize(24).setTextColor(30, 64, 175); // Blue-800
const titleText = generatedTitleEl.value || 'Generated Article';
const titleLines = pdf.splitTextToSize(titleText, maxLineWidth);
pdf.text(titleLines, pageW / 2, 60, { align: 'center' });
cursorY = 140;
const bodyRawText = generatedBodyEl.value || 'No body content provided.';
const bodyLines = bodyRawText.split('\n');
bodyLines.forEach(line => {
const trimmedLine = line.trim();
checkPageBreak(20);
if (trimmedLine.startsWith('### ')) {
const headingText = trimmedLine.substring(4);
cursorY += 15;
checkPageBreak(30);
pdf.setFont('helvetica', 'bold').setFontSize(15).setTextColor(30, 58, 138); // Blue-900
pdf.text(headingText, margin, cursorY);
cursorY += 20;
} else if (trimmedLine.startsWith('* ')) {
const itemText = trimmedLine.substring(2);
pdf.setFont('helvetica', 'normal').setFontSize(11).setTextColor(51, 65, 85); // Slate-600
const textLines = pdf.splitTextToSize(itemText, maxLineWidth - 20);
checkPageBreak(textLines.length * 14);
pdf.circle(margin + 5, cursorY - 4, 2, 'F');
pdf.text(textLines, margin + 20, cursorY);
cursorY += textLines.length * 14 + 4;
} else if (trimmedLine) {
pdf.setFont('helvetica', 'normal').setFontSize(11).setTextColor(15, 23, 42); // Slate-900
const paraLines = pdf.splitTextToSize(trimmedLine, maxLineWidth);
checkPageBreak(paraLines.length * 14);
pdf.text(paraLines, margin, cursorY);
cursorY += paraLines.length * 14 + 12;
}
});
const pageCount = pdf.internal.getNumberOfPages();
for(let i = 1; i <= pageCount; i++) {
pdf.setPage(i);
pdf.setFont('helvetica', 'normal').setFontSize(9).setTextColor(148, 163, 184); // Slate-400
pdf.line(margin, pageH - 40, pageW - margin, pageH - 40);
pdf.text(`Page ${i} of ${pageCount}`, pageW / 2, pageH - 25, { align: 'center' });
}
const date = new Date().toISOString().slice(0, 10);
pdf.save(`Thought-Leadership-Article-${date}.pdf`);
};
// --- Event Listeners ---
if (generateBtn) generateBtn.addEventListener('click', handleGenerateClick);
if (downloadPdfBtn) downloadPdfBtn.addEventListener('click', handleDownloadPdf);
});
