|
| 1 | +/** |
| 2 | + * Utilities for preserving XML/HTML tags in prompt text through TipTap's markdown roundtrip. |
| 3 | + * |
| 4 | + * Problem: When content like `<question>text</question>` is parsed by marked (via @tiptap/markdown), |
| 5 | + * the lexer tokenizes tags as HTML tokens. TipTap's parseHTMLToken then calls generateJSON which |
| 6 | + * creates DOM elements — unrecognized tags are stripped and only inner text survives. |
| 7 | + * |
| 8 | + * Solution: Three-step process: |
| 9 | + * 1. escapeXmlTags: Convert all tags to HTML entities before markdown parsing |
| 10 | + * so marked treats them as text, not HTML tokens. |
| 11 | + * 2. unescapeXmlEntities: After TipTap builds the ProseMirror document, walk the JSON tree |
| 12 | + * and decode </> back to </> in text nodes for proper visual display. |
| 13 | + * 3. unescapeXmlTags: After getMarkdown(), reverse any remaining entity-escaped tags |
| 14 | + * in the serialized output (safety net — typically a no-op). |
| 15 | + */ |
| 16 | + |
| 17 | +/** |
| 18 | + * Detect if content is legacy HTML from old getHTML() storage vs markdown. |
| 19 | + * Legacy content always starts with a block-level tag like <p>. |
| 20 | + * Anchored with ^ to avoid matching intentional HTML tags inside user prompts. |
| 21 | + * |
| 22 | + * @example |
| 23 | + * isHtmlContent('<p>some text</p>') // → true (legacy getHTML output) |
| 24 | + * isHtmlContent('<instruction>text</instruction>') // → false (user prompt) |
| 25 | + * |
| 26 | + * @param {string} content - Content to check |
| 27 | + * @returns {boolean} True if content looks like legacy HTML |
| 28 | + */ |
| 29 | +export const isHtmlContent = (content) => { |
| 30 | + if (!content || typeof content !== 'string') return false |
| 31 | + return /^\s*<(?:p|div|h[1-6]|ul|ol|blockquote|pre|table)\b/i.test(content) |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * Regex matching opening, closing, and self-closing XML/HTML tags. |
| 36 | + * Captures: (1) optional slash, (2) tag name, (3) optional attributes, (4) optional self-close slash |
| 37 | + */ |
| 38 | +const XML_TAG_REGEX = /<(\/?)([a-zA-Z][a-zA-Z0-9_.-]*)(\s[^>]*)?(\/?)>/g |
| 39 | + |
| 40 | +/** |
| 41 | + * Escape all XML/HTML tags to HTML entities so marked doesn't parse them as HTML. |
| 42 | + * In prompt editing context, users want tags preserved literally, not rendered. |
| 43 | + * |
| 44 | + * @example |
| 45 | + * escapeXmlTags('<instructions>Be helpful</instructions>') |
| 46 | + * // → '<instructions>Be helpful</instructions>' |
| 47 | + * |
| 48 | + * escapeXmlTags('<div><question>text</question></div>') |
| 49 | + * // → '<div><question>text</question></div>' |
| 50 | + * |
| 51 | + * @param {string} text - Raw markdown/text content |
| 52 | + * @returns {string} Content with tags escaped to HTML entities |
| 53 | + */ |
| 54 | +export function escapeXmlTags(text) { |
| 55 | + if (!text || typeof text !== 'string') return text |
| 56 | + return text.replace(XML_TAG_REGEX, (match, slash, tagName, attrs, selfClose) => { |
| 57 | + return `<${slash}${tagName}${attrs || ''}${selfClose}>` |
| 58 | + }) |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Unescape XML tag entities in ProseMirror JSON text nodes. |
| 63 | + * Call this after setContent() to fix the visual display in the editor. |
| 64 | + * Mutates the JSON in-place and returns it. |
| 65 | + * |
| 66 | + * @example |
| 67 | + * const json = { type: 'doc', content: [ |
| 68 | + * { type: 'paragraph', content: [{ type: 'text', text: '<question>What?</question>' }] } |
| 69 | + * ]} |
| 70 | + * unescapeXmlEntities(json) |
| 71 | + * // json.content[0].content[0].text → '<question>What?</question>' |
| 72 | + * |
| 73 | + * @param {object} json - ProseMirror document JSON from editor.getJSON() |
| 74 | + * @returns {object} The same JSON with decoded entities in text nodes |
| 75 | + */ |
| 76 | +export function unescapeXmlEntities(json) { |
| 77 | + if (json.text) { |
| 78 | + json.text = unescapeXmlTags(json.text) |
| 79 | + } |
| 80 | + if (json.content) { |
| 81 | + json.content.forEach(unescapeXmlEntities) |
| 82 | + } |
| 83 | + return json |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * Unescape all entity-escaped XML/HTML tags after markdown serialization. |
| 88 | + * |
| 89 | + * @example |
| 90 | + * unescapeXmlTags('<question>text</question>') |
| 91 | + * // → '<question>text</question>' |
| 92 | + * |
| 93 | + * unescapeXmlTags('<div>text</div>') |
| 94 | + * // → '<div>text</div>' |
| 95 | + * |
| 96 | + * unescapeXmlTags('<question>text</question>') |
| 97 | + * // → '<question>text</question>' (raw tags pass through unchanged) |
| 98 | + * |
| 99 | + * @param {string} text - Markdown output from TipTap |
| 100 | + * @returns {string} Content with tags restored to angle brackets |
| 101 | + */ |
| 102 | +export function unescapeXmlTags(text) { |
| 103 | + if (!text || typeof text !== 'string') return text |
| 104 | + return text.replace(/<(\/?)([a-zA-Z][a-zA-Z0-9_.-]*)(\s.*?)?(\/?)>/g, (match, slash, tagName, attrs, selfClose) => { |
| 105 | + return `<${slash}${tagName}${attrs || ''}${selfClose}>` |
| 106 | + }) |
| 107 | +} |
0 commit comments