Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

3.6 KB
Raw Blame
// Shared sanitized Markdown renderer for every surface that shows model or
// user authored text (brain chat, bounty briefs, manifest previews, memory
// seeds, NPC analysis). Full CommonMark + GFM via `marked`, then a strict
// DOMPurify pass so the result is safe to assign via innerHTML even if a
// renderer bug or a hostile payload slips through upstream.
//
// This replaces eight hand-rolled per-file renderers that each carried their
// own escape logic and subset of Markdown. Do not add another one; extend
// this module instead.

import { Marked } from 'marked';
import createDOMPurify from 'dompurify';

const marked = new Marked({ gfm: true, breaks: true, async: false });

// Markdown output only: no media, no forms, no style. Links are hardened in
// the hook below.
const SANITIZE_CFG = {
	ALLOWED_TAGS: [
		'p', 'br', 'hr', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
		'ul', 'ol', 'li', 'blockquote', 'pre', 'code',
		'strong', 'em', 'del', 'a', 'span',
		'table', 'thead', 'tbody', 'tr', 'th', 'td',
	],
	ALLOWED_ATTR: ['href', 'class', 'start', 'align', 'target', 'rel'],
};

const SAFE_HREF = /^(?:https?:|mailto:)/i;

// Built on first use: DOMPurify needs a live window, and this module is also
// imported by unit tests and by page bundles that load before body parse.
let purify = null;
function getPurify() {
	if (purify) return purify;
	purify = createDOMPurify(window);
	purify.addHook('afterSanitizeAttributes', (node) => {
		if (node.tagName !== 'A') return;
		const href = node.getAttribute('href') || '';
		const ok = SAFE_HREF.test(href) || href.startsWith('/') || href.startsWith('#');
		if (!ok) node.removeAttribute('href');
		node.setAttribute('target', '_blank');
		node.setAttribute('rel', 'noopener noreferrer nofollow');
	});
	return purify;
}

/**
 * Render Markdown to sanitized HTML.
 *
 * @param {string} src Markdown source (model output, user content).
 * @param {object} [opts]
 * @param {Record<string, string>} [opts.classes] CSS classes to add per
 *   selector, e.g. `{ pre: 'md-code', 'code:not(pre code)': 'md-ic' }`, so
 *   existing per-page stylesheets keep working.
 * @param {number} [opts.demoteHeadings] Shift headings down N levels
 *   (`# -> h3` for pages where the content sits under a section h2).
 * @returns {string} HTML string safe for innerHTML.
 */
export function renderMarkdown(src, { classes, demoteHeadings = 0 } = {}) {
	if (src == null || src === '') return '';
	const raw = marked.parse(String(src));
	const clean = getPurify().sanitize(raw, SANITIZE_CFG);
	if (!classes && !demoteHeadings) return clean;

	const tpl = document.createElement('template');
	tpl.innerHTML = clean;
	if (demoteHeadings > 0) {
		for (let lvl = 6; lvl >= 1; lvl--) {
			for (const el of [...tpl.content.querySelectorAll(`h${lvl}`)]) {
				const next = document.createElement(`h${Math.min(6, lvl + demoteHeadings)}`);
				next.innerHTML = el.innerHTML;
				for (const attr of el.attributes) next.setAttribute(attr.name, attr.value);
				el.replaceWith(next);
			}
		}
	}
	if (classes) {
		for (const [sel, cls] of Object.entries(classes)) {
			for (const el of tpl.content.querySelectorAll(sel)) el.classList.add(cls);
		}
	}
	return tpl.innerHTML;
}

/**
 * Strip Markdown syntax for plain-text contexts (voice narration, tooltips,
 * meta descriptions).
 */
export function stripMarkdown(src) {
	return String(src ?? '')
		.replace(/```[\s\S]*?```/g, ' ')
		.replace(/`([^`]+)`/g, '$1')
		.replace(/!?\[([^\]]*)\]\([^)\s]*\)/g, '$1')
		.replace(/^#{1,6}\s+/gm, '')
		.replace(/^\s*>\s?/gm, '')
		.replace(/^\s*[-*+]\s+/gm, '')
		.replace(/^\s*\d+[.)]\s+/gm, '')
		.replace(/(\*\*|__|[*_~])/g, '')
		.replace(/\s+/g, ' ')
		.trim();
}