/** * The editor's schema (RFC-0014 phase 1): CommonMark + GFM as the site renders it (`marked` with * GFM in `@cloudhal/astro-renderer`), plus `sourceBlock` for anything the editor must not rewrite * and the body directive nodes (RFC-0015). * Every extension is MIT (ADR: no paid Tiptap add-ons). */ import { Badge, Button, Text } from "@cloudflare/kumo"; import { Code, PencilSimple } from "@phosphor-icons/react"; import { type AnyExtension, Node } from "@tiptap/core"; import Image, { type ImageOptions } from "@tiptap/extension-image"; import { TaskItem, TaskList } from "@tiptap/extension-list"; import { TableKit } from "@tiptap/extension-table"; import { Focus, Placeholder } from "@tiptap/extensions"; import { NodeViewWrapper, type ReactNodeViewProps, ReactNodeViewRenderer } from "@tiptap/react"; import StarterKit from "@tiptap/starter-kit"; import { useEffect, useRef, useState } from "react"; import { InsetPanel } from "../InsetPanel.tsx"; import { DirectiveContainer, DirectiveLeaf } from "./directives.tsx"; import { SlashCommand, type SlashOptions } from "./slash.ts"; /** Link targets the site keeps (the renderer's `safeHref`, plus `asset:` media links). */ export function allowedHref(url: string): boolean { const value = url.replace(/[\t\n\r]/g, "").trim(); if (/^(https?:|mailto:|tel:|asset:)/i.test(value)) return true; return !/^[a-z][a-z0-9+.-]*:/i.test(value) && !/^[/\\]{2}/.test(value); } export interface SourceBlockLabels { /** Badge on a block kept exactly as written, e.g. "Markdown". */ badge: string; /** Why it is not editable here, e.g. "Kept as written — edit it in Markdown mode." */ hint: string; /** The button that opens it in Markdown mode, e.g. "Edit as Markdown". */ edit?: string; /** A drawn ```surface: its preview's name, and the buttons that show or hide its code. */ preview?: string; showCode?: string; hideCode?: string; } interface SourceBlockOptions { labels: SourceBlockLabels; /** Opens Markdown mode with this block's text selected. */ onEdit?: ((markdown: string) => void) | undefined; /** Draws a ```surface as the site would (a whole HTML document, shown without scripts). */ preview?: ((markdown: string) => Promise) | undefined; } const SURFACE_SOURCE = /^ {0,3}(?:`{3,}|~{3,})[ \t]*surface(?:\s|$)/; /** * A ```surface drawn under its source block: the host's preview document in a sandboxed frame * (no scripts; same origin only so the frame can be sized to its content). */ function SurfacePreview({ markdown, preview, label, }: { markdown: string; preview: (markdown: string) => Promise; label: string; }) { const [html, setHtml] = useState(); const [height, setHeight] = useState(120); const frame = useRef(null); useEffect(() => { let live = true; preview(markdown).then( (h) => live && setHtml(h), () => live && setHtml(undefined), ); return () => { live = false; }; }, [markdown, preview]); if (!html) return null; return (