diff --git a/assets/js/hvac-ai-assist.js b/assets/js/hvac-ai-assist.js index b4ef64a0..52ed01a4 100644 --- a/assets/js/hvac-ai-assist.js +++ b/assets/js/hvac-ai-assist.js @@ -568,17 +568,20 @@ jQuery(document).ready(function($) { // Apply description (handle TinyMCE, regular textarea, and rich text editor) if (data.description) { + // Convert markdown to HTML for proper rich text editor formatting + const htmlContent = this.markdownToHtml(data.description); + // Try TinyMCE first if available if (typeof tinyMCE !== 'undefined' && tinyMCE.get('event_description')) { - tinyMCE.get('event_description').setContent(data.description); + tinyMCE.get('event_description').setContent(htmlContent); } else { - // Update the hidden textarea - $('#event_description, [name="event_description"]').val(data.description); + // Update the hidden textarea with HTML content + $('#event_description, [name="event_description"]').val(htmlContent); // Also update the visible rich text editor div if it exists const $richEditor = $('#event-description-editor'); if ($richEditor.length && $richEditor.is('[contenteditable]')) { - $richEditor.html(data.description); + $richEditor.html(htmlContent); } } } @@ -707,6 +710,85 @@ jQuery(document).ready(function($) { alert(message); }, + /** + * Convert markdown to HTML for rich text editor + */ + markdownToHtml: function(markdown) { + let html = markdown; + + // Convert headers (## -> h2, ### -> h3, etc.) + html = html.replace(/^### (.+)$/gm, '
tags for paragraphs + const lines = html.split('\n'); + const paragraphs = []; + let currentParagraph = ''; + + for (let line of lines) { + line = line.trim(); + + // Skip empty lines + if (line === '') { + if (currentParagraph) { + paragraphs.push(currentParagraph); + currentParagraph = ''; + } + continue; + } + + // If line is already wrapped in HTML tags, add it as is + if (line.match(/^<(h[1-6]|ul|li|strong|em)/)) { + if (currentParagraph) { + paragraphs.push(currentParagraph); + currentParagraph = ''; + } + paragraphs.push(line); + } else { + // Regular text line + if (currentParagraph) { + currentParagraph += ' ' + line; + } else { + currentParagraph = line; + } + } + } + + // Add final paragraph if exists + if (currentParagraph) { + paragraphs.push(currentParagraph); + } + + // Wrap non-HTML paragraphs in
tags + const formattedParagraphs = paragraphs.map(p => { + if (p.match(/^<(h[1-6]|ul|li)/)) { + return p; + } else { + return '
' + p + '
'; + } + }); + + return formattedParagraphs.join('\n'); + }, + /** * Escape HTML for safe display */ diff --git a/includes/class-hvac-event-form-builder.php b/includes/class-hvac-event-form-builder.php index 68ea4fe8..d67682a5 100644 --- a/includes/class-hvac-event-form-builder.php +++ b/includes/class-hvac-event-form-builder.php @@ -307,44 +307,11 @@ class HVAC_Event_Form_Builder extends HVAC_Form_Builder { 'required' => true, ]); - // Event description with rich text editor + // Event description with WordPress rich text editor $description_field = [ 'type' => 'custom', 'name' => 'event_description', - 'custom_html' => '