function formatMessage(message) {
        function escapeHTML(text) {
            return text
                //.replace(/&/g, "&amp;")
                .replace(/</g, "&lt;")
            //.replace(/>/g, "&gt;")
            //.replace(/"/g, "&quot;")
            //.replace(/'/g, "&#039;");
        }

        function processCodeBlock(match, language, code, inlineCode) {
            const finalCode = code || inlineCode || "";
            const detectedLanguage = (language && language.trim() !== "") ? language : "markup";
            const preservedFormatting = finalCode
                .split('\n')
                .map(line => escapeHTML(line))
                .join('\n');

            const highlightedCode = Prism.languages[detectedLanguage] ?
                Prism.highlight(preservedFormatting.trim(), Prism.languages[detectedLanguage], detectedLanguage) :
                escapeHTML(preservedFormatting);

            return `
        <div class="code-container">
          <div class="code-header">
            <span class="language-name">${language || "code"}</span>
            <div class="code-actions">
            <button class="copy-btn" onclick="copyCode(this)">📋 Copy</button>
            <button class="fullscreen-btn" onclick="openFullscreen(this)">⛶ Fullscreen</button>
            </div>
          </div>
          <pre><code class="language-${language || 'markup'}">${highlightedCode}</code></pre>
        </div>
      `;
        }

        function processLists(text) {
            return text
                .replace(/^([ \t]*)([-*]|\d+\.)\s*(.*)$/gm, (match, indent, marker, content) => {
                    const indentLevel = indent.length;
                    return `<li style="margin-left: ${indentLevel * 20}px;">${content.trim()}</li>`;
                })
                .replace(/(<li>.*?<\/li>)+/gs, match =>
                    `<${/\d+\./.test(match) ? 'ol' : 'ul'}>${match}</${/\d+\./.test(match) ? 'ol' : 'ul'}>`
                );
        }

        return message
            .split(/(```[\s\S]*?```|`[^`\n]+`|<urimage>[\s\S]*?<\/urimage>)/g)
            .map((part) => {
            // Check if the part is a urimage tag first
            if (part.startsWith("<urimage>") && part.endsWith("</urimage>")) {
                return part; // Return urimage content as-is
            }
            
            if (part.startsWith("```")) {
                return part.replace(/```([a-zA-Z]*)\n([\s\S]*?)\n```|```([^`]*)```/g, processCodeBlock);
            } else if (part.startsWith("`")) {
                return part.replace(/`([^`\n]+)`/g, (match, code) =>
                    `<code>${escapeHTML(code)}</code>`
                );
            } else {
                return escapeHTML(part);
            }
        })
        .join("")
            .replace(/^(#{1,6})\s*(.*?)$/gm, (match, hashes, content) => {
                const level = hashes.length;
                return `<h${level}>${content.trim()}</h${level}>`;
            })
            .replace(/^([ \t]*[-*\d]+\.?)\s*(.*)$/gm, processLists)
            .replace(/\*\*(.*?)\*\*/g, "<strong>$1</strong>")
            .replace(/\*(.*?)\*/g, "<em>$1</em>")
            .replace(/(?:^|\n\n)([^<\n].*?)(?=\n\n|$)/gs, (match, content) =>
                content.trim().split('\n\n').map(para => `<p>${para.trim()}</p>`).join('\n')
            )
            .replace(/<urimage>([\s\S]+?)<\/urimage>/g,
                '<div style="position: relative; width: 100%; aspect-ratio: 1 / 1; background-color: #d3d3d3; border-radius: 3px; overflow: hidden;">' +
                '<div style="position: absolute; top: 10px; left: 10px; font-family: Arial, sans-serif; font-size: 16px; color: #555; display: flex; align-items: center; gap: 5px;">' +
                '<i class="fas fa-spinner fa-spin" style="font-size: 18px;"></i>' +
                '<span>Loading&#8230;</span>' +
                '</div>' +
                '<img src="https://image.pollinations.ai/prompt/$1?width=1024&height=1024&enhance=false&nologo=true&privacy=true&safe=false&nsfw=true&isChild=false" style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; object-fit: cover; border-radius: 3px; visibility: hidden;" onload="this.style.visibility=\'visible\'; this.previousElementSibling.style.display=\'none\';">' +
                '</div>')
            .replace(/\[([^\]]+?)\]\(([^)]+?)\)/g, '<a href="$2" target="_blankspace">$1</a>');
    }