diff --git a/demo-site/about.html b/demo-site/about.html index d6a0a9c..615cfa0 100644 --- a/demo-site/about.html +++ b/demo-site/about.html @@ -115,9 +115,9 @@
This paragraph is part of a group.
-Clicking anywhere in the group should open one markdown editor.
-All content should be editable together as markdown.
+This paragraph is part of a group.
+Clicking anywhere should open one markdown editor with rich formatting.
+All content should be editable together as markdown with proper HTML conversion.
${p.trim()}
`) + .join(''); + } + } + + /** + * Extract HTML content from a group of elements + * @param {HTMLElement[]} elements - Array of DOM elements + * @returns {string} - Combined HTML content + */ + extractGroupHTML(elements) { + const htmlParts = []; + + elements.forEach(element => { + // Wrap inner content in paragraph tags to preserve structure + const html = element.innerHTML.trim(); + if (html) { + // If element is already a paragraph, use its outer HTML + if (element.tagName.toLowerCase() === 'p') { + htmlParts.push(element.outerHTML); + } else { + // Wrap in paragraph tags + htmlParts.push(`${html}
`); + } + } + }); + + return htmlParts.join('\n'); + } + + /** + * Convert HTML content from group elements to markdown + * @param {HTMLElement[]} elements - Array of DOM elements + * @returns {string} - Markdown representation + */ + extractGroupMarkdown(elements) { + const html = this.extractGroupHTML(elements); + const markdown = this.htmlToMarkdown(html); + return markdown; + } + + /** + * Update group elements with markdown content + * @param {HTMLElement[]} elements - Array of DOM elements to update + * @param {string} markdown - Markdown content to render + */ + updateGroupElements(elements, markdown) { + const html = this.markdownToHtml(markdown); + + // Split HTML into paragraphs + const tempDiv = document.createElement('div'); + tempDiv.innerHTML = html; + + const paragraphs = Array.from(tempDiv.querySelectorAll('p, div, h1, h2, h3, h4, h5, h6')); + + // Handle case where we have more/fewer paragraphs than elements + const maxCount = Math.max(elements.length, paragraphs.length); + + for (let i = 0; i < maxCount; i++) { + if (i < elements.length && i < paragraphs.length) { + // Update existing element with corresponding paragraph + elements[i].innerHTML = paragraphs[i].innerHTML; + } else if (i < elements.length) { + // More elements than paragraphs - clear extra elements + elements[i].innerHTML = ''; + } else if (i < paragraphs.length) { + // More paragraphs than elements - create new element + const newElement = document.createElement('p'); + newElement.innerHTML = paragraphs[i].innerHTML; + + // Insert after the last existing element + const lastElement = elements[elements.length - 1]; + lastElement.parentNode.insertBefore(newElement, lastElement.nextSibling); + elements.push(newElement); // Add to our elements array for future updates + } + } + } +} + +// Export singleton instance +export const markdownConverter = new MarkdownConverter(); \ No newline at end of file