// Get raw AI output safely
let content = $json.output || $json.text || $json.response || ”;

if (!content) {
throw new Error(‘No AI output found. Check your previous node mapping.’);
}

// Normalize line breaks
content = content.replace(/\r\n/g, ‘\n’);

// Convert markdown-style headers to HTML
content = content
.replace(/^### (.*$)/gim, ‘

$1

‘)
.replace(/^## (.*$)/gim, ‘

$1

‘)
.replace(/^# (.*$)/gim, ‘

$1

‘);

// Convert bullet points to

  • content = content.replace(/^\- (.*$)/gim, ‘

  • $1
  • ‘);

    // Wrap consecutive

  • into
      content = content.replace(/(

    • .*<\/li>)/gim, ‘
        $1

      ‘);
      content = content.replace(/<\/ul>\s*

        /g, ”);

        // Convert plain text into paragraphs
        content = content.replace(/^(?!|

          |

        • |<\/ul>)(.+)$/gim, ‘

          $1

          ‘);

          // Remove empty paragraphs
          content = content.replace(/

          \s*<\/p>/g, ”);

          // Trim output
          content = content.trim();

          return [
          {
          json: {
          html: content
          }
          }
          ];