Added Markdown support

This commit is contained in:
Nathan Schneider
2025-05-07 15:00:20 -06:00
parent 8d54875c6d
commit 9450eeb817
4 changed files with 158 additions and 4 deletions

View File

@ -803,8 +803,39 @@ document.addEventListener('DOMContentLoaded', function() {
// Format field content with larger font size (12)
doc.setFontSize(12);
// Process markdown to plaintext for PDF
let processedContent = fieldValue;
try {
// Strip HTML tags from rendered markdown to get clean text
const temp = document.createElement('div');
temp.innerHTML = marked.parse(fieldValue);
// Convert links to format: text (url)
const links = temp.querySelectorAll('a');
links.forEach(link => {
const linkText = link.textContent;
const href = link.getAttribute('href');
// Only modify if href is different from text
if (href && href !== linkText) {
const replacement = `${linkText} (${href})`;
link.textContent = replacement;
}
});
// Bold text handling
const boldElements = temp.querySelectorAll('strong, b');
boldElements.forEach(el => {
el.textContent = `*${el.textContent}*`; // Surround with asterisks
});
// Get plain text content
processedContent = temp.textContent;
} catch (error) {
console.error('Error processing Markdown for PDF:', error);
}
// Use consistent width for all text
const textLines = doc.splitTextToSize(fieldValue, margins.width);
const textLines = doc.splitTextToSize(processedContent, margins.width);
doc.text(textLines, margins.left, yPos);
yPos += textLines.length * 6 + 8; // Slightly increase spacing for larger font
}
@ -933,7 +964,7 @@ document.addEventListener('DOMContentLoaded', function() {
});
});
// Function to update preview content
// Function to update preview content with Markdown support
function updatePreviewContent() {
// First, clear any existing preview divs
document.querySelectorAll('.preview-content').forEach(div => {
@ -951,7 +982,15 @@ document.addEventListener('DOMContentLoaded', function() {
// Create a div to replace the textarea for preview
const previewDiv = document.createElement('div');
previewDiv.className = 'preview-content';
previewDiv.textContent = textarea.value;
// Parse Markdown and set as HTML content
try {
previewDiv.innerHTML = marked.parse(textarea.value);
} catch (error) {
console.error('Error parsing Markdown:', error);
previewDiv.textContent = textarea.value;
}
previewDiv.dataset.forTextarea = textarea.id;
// Hide the textarea and insert the div