Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

1.3 KB
Raw Blame
/**
 * Markdown → plain-text normalization for strings that are about to be handed
 * to something that has no idea what markdown is (a model's text encoder, a
 * slug builder, a TTS engine).
 *
 * The motivating case: prompts authored in a markdown-ish textarea reach a
 * text-to-audio sidecar with their `**emphasis**` intact, and the encoder
 * tokenizes the asterisks as literal content — conditioning noise the author
 * never intended to write.
 */

/**
 * Strip common markdown wrappers, keeping the words inside them.
 * - `**bold**`     → `bold`
 * - `~~struck~~`   → `struck`
 * - `` `code` ``   → `code`
 * - `[text](url)`  → `text`
 * - HTML comments  → removed
 *
 * Leftover lone `*`/`_`/`~` become spaces rather than being deleted, so an
 * unbalanced marker can't fuse two words together. Whitespace is NOT collapsed
 * here — callers that care (slug builders) normalize it themselves, and
 * callers that don't (prompt text) keep their line structure intact.
 */
export function stripMarkdownEmphasis(text) {
  return String(text ?? '')
    .replace(/<!--[\s\S]*?-->/g, ' ')
    .replace(/\[([^\]]+)\]\([^)]*\)/g, '$1')
    .replace(/`([^`]*)`/g, '$1')
    .replace(/\*\*([^*]+)\*\*/g, '$1')
    .replace(/~~([^~]+)~~/g, '$1')
    .replace(/[*_~]/g, ' ');
}