sailfork / t3code-github / packages/ shared/ src/ markdownPipeline.ts

import type { Paragraph, PhrasingContent, Root, RootContent } from "mdast";
import { remarkKeepWindowsPaths } from "./markdownWindowsPaths.ts";
import { type PluggableList, type Processor, type Transformer, unified } from "unified";
import remarkParse from "remark-parse";
import remarkRehype from "remark-rehype";
import { find, html } from "property-information";
import rehypeRaw from "rehype-raw";
import rehypeSanitize, { defaultSchema } from "rehype-sanitize";
import remarkBreaks from "remark-breaks";
import remarkGfm from "remark-gfm";
import { remarkGithubAlerts } from "./markdownGithubAlerts.ts";
import { remarkNormalizeListItemIndentation } from "./markdownListIndentation.ts";
import {
  CODEX_ARTIFACT_TEMPLATE_HAST_PROPERTIES,
  remarkCodexDirectives,
} from "./codexMarkdownDirectives.ts";
import { isWindowsDrivePathHref, normalizeMarkdownLinkDestination } from "./markdownLinks.ts";
import { THREAD_LINK_PROTOCOL } from "./threadLinks.ts";

type MarkdownImageHastNode = {
  type?: string;
  value?: string;
  tagName?: string;
  properties?: Record<string, unknown>;
  children?: MarkdownImageHastNode[];
};

function meaningfulHastChildren(node: MarkdownImageHastNode): MarkdownImageHastNode[] {
  return (node.children ?? []).filter(
    (child) => !(child.type === "text" && (child as { value?: string }).value?.trim() === ""),
  );
}

const STANDALONE_IMAGE_BLOCKS = new Set([
  "p",
  "div",
  "li",
  "td",
  "th",
  "figure",
  "center",
  "blockquote",
]);

function soleImageDescendant(node: MarkdownImageHastNode): MarkdownImageHastNode | undefined {
  const children = meaningfulHastChildren(node);
  if (children.length !== 1) return undefined;
  const only = children[0];
  if (only?.type !== "element") return undefined;
  if (only.tagName === "img") return only;
  return only.tagName === "a" || only.tagName === "strong" || only.tagName === "em"
    ? soleImageDescendant(only)
    : undefined;
}

function markStandaloneImages(node: MarkdownImageHastNode) {
  if (node.type === "root" || (node.tagName && STANDALONE_IMAGE_BLOCKS.has(node.tagName))) {
    const image = soleImageDescendant(node);
    if (image) image.properties = { ...image.properties, dataStandalone: true };
  }
  node.children?.forEach((child) => {
    if (child.type === "element") markStandaloneImages(child);
  });
}

function rehypePreserveBareAnchorPlaceholders() {
  return (tree: MarkdownImageHastNode) => {
    const anchors: Array<MarkdownImageHastNode | null> = [];
    let rawTextTag: string | undefined;
    const visit = (node: MarkdownImageHastNode) => {
      if (node.type === "raw" && typeof node.value === "string") {
        // Scan whole tags, attributes, and comments so embedded closing-anchor text cannot match a placeholder.
        const tags = /<!--[\s\S]*?(?:-->|$)|<\/?[A-Za-z](?:[^"'<>]|"[^"]*"|'[^']*')*>/g;
        let offset = 0;
        while (rawTextTag !== "plaintext") {
          // Raw text ends at its closing tag even inside comment-looking text.
          const matcher = rawTextTag ? new RegExp(`</${rawTextTag}\\s*>`, "gi") : tags;
          matcher.lastIndex = offset;
          const match = matcher.exec(node.value);
          if (!match) break;
          const [tag] = match;
          offset = matcher.lastIndex;
          if (rawTextTag) {
            rawTextTag = undefined;
            continue;
          }
          if (tag.startsWith("<!--")) continue;
          const closing = /^<\/([a-z]+)\s*>$/i.exec(tag)?.[1]?.toLowerCase();
          const opening = /^<([a-z]+)(?:\s|\/?>)/i.exec(tag)?.[1]?.toLowerCase();
          if (
            opening &&
            /^(?:script|style|textarea|title|xmp|iframe|noembed|noframes|plaintext)$/.test(opening)
          ) {
            rawTextTag = opening;
          } else if (opening === "a") {
            anchors.push(node.value === tag && /^<a\s*\/?>$/i.test(tag) ? node : null);
          } else if (closing === "a") {
            anchors.pop();
          }
        }
      }
      node.children?.forEach(visit);
    };

    visit(tree);
    for (const anchor of anchors) {
      if (anchor) anchor.type = "text";
    }
  };
}

const COLOR_SCHEME_MEDIA = /^\(\s*prefers-color-scheme\s*:\s*(dark|light)\s*\)$/i;

/**
 * The renderer resolves an `<img>`'s `src` (workspace files, host media) but never a `<source>`'s
 * `srcset`, which would load unresolved whenever its media matched. A color-scheme source moves
 * onto the `<img>` for the renderer to pick by theme; every source is then dropped.
 */
function moveColorSchemeSourcesToImage(picture: MarkdownImageHastNode) {
  if (!picture.children) return;
  const image = picture.children.find((child) => child.tagName === "img");
  for (const source of picture.children) {
    if (source.tagName !== "source" || !image) continue;
    const media = source.properties?.media;
    const scheme = typeof media === "string" ? COLOR_SCHEME_MEDIA.exec(media)?.[1] : undefined;
    const srcSet = source.properties?.srcSet;
    const candidate = Array.isArray(srcSet) ? srcSet[0] : srcSet;
    // A candidate's URL runs to the first space (a descriptor such as `2x` may follow).
    const url =
      typeof candidate === "string" ? candidate.trim().split(/\s/, 1)[0]?.replace(/,+$/, "") : "";
    const key = scheme?.toLowerCase() === "dark" ? "dataDarkSrc" : "dataLightSrc";
    // As in a browser, the first source that matches a scheme is the one shown.
    if (scheme && url && image.properties?.[key] === undefined) {
      // The sanitizer reads a drive letter as a protocol; as a file URL it resolves to the same path.
      const value = isWindowsDrivePathHref(url) ? `file:///${url.replaceAll("\\", "/")}` : url;
      image.properties = { ...image.properties, [key]: value };
    }
  }
  picture.children = picture.children.filter((child) => child.tagName !== "source");
}

function rehypePreserveImageSourceMeta() {
  return (tree: MarkdownImageHastNode) => {
    const visit = (node: MarkdownImageHastNode) => {
      const src = node.properties?.src;
      const title = node.properties?.title;
      if (node.type === "element" && node.tagName === "picture") {
        moveColorSchemeSourcesToImage(node);
      }
      if (node.type === "element" && node.tagName === "img") {
        node.properties = {
          ...node.properties,
          ...(typeof src === "string" && isWindowsDrivePathHref(src) ? { dataLocalSrc: src } : {}),
          ...(typeof title === "string" ? { dataMarkdownTitle: title } : {}),
        };
      }
      node.children?.forEach(visit);
    };

    visit(tree);
    markStandaloneImages(tree);
  };
}

const IMAGE_SRC_PROTOCOLS = [...(defaultSchema.protocols?.src ?? []), "file", "t3-context"];

const CHAT_MARKDOWN_SANITIZE_SCHEMA = {
  ...defaultSchema,
  attributes: {
    ...defaultSchema.attributes,
    "*": (defaultSchema.attributes?.["*"] ?? []).filter((attribute) => attribute !== "title"),
    // Fence languages plus the two math classes the web chat's math plugin emits.
    code: [
      ["className", /^language-./, "math-inline", "math-display"],
      "dataCodeMeta",
      "dataInlineCode",
    ],
    blockquote: [...(defaultSchema.attributes?.blockquote ?? []), "dataAlert"],
    div: [...(defaultSchema.attributes?.div ?? []), ...CODEX_ARTIFACT_TEMPLATE_HAST_PROPERTIES],
    a: [...(defaultSchema.attributes?.a ?? []), "dataPullRequestAutolink"],
    img: [
      ...(defaultSchema.attributes?.img ?? []),
      "dataDarkSrc",
      "dataLightSrc",
      "dataLocalSrc",
      "dataMarkdownTitle",
      "dataStandalone",
    ],
  },
  protocols: {
    ...defaultSchema.protocols,
    href: [
      ...(defaultSchema.protocols?.href ?? []),
      "file",
      "t3-citation",
      "t3-context",
      THREAD_LINK_PROTOCOL,
    ],
    src: IMAGE_SRC_PROTOCOLS,
    dataDarkSrc: IMAGE_SRC_PROTOCOLS,
    dataLightSrc: IMAGE_SRC_PROTOCOLS,
  },
} satisfies Parameters<typeof rehypeSanitize>[0];

export const CHAT_MARKDOWN_REMARK_PLUGINS: PluggableList = [
  remarkGfm,
  remarkKeepAutolinkSuffixes,
  remarkKeepWindowsPaths,
  remarkGithubAlerts,
  remarkNormalizeListItemIndentation,
  remarkCodexDirectives,
  remarkBreakAroundImageLines,
  remarkPreserveCodeMeta,
  remarkNormalizeLinksAndTagInlineCode,
];

export const CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS: PluggableList = [
  remarkGfm,
  remarkKeepAutolinkSuffixes,
  remarkKeepWindowsPaths,
  remarkGithubAlerts,
  remarkNormalizeListItemIndentation,
  remarkCodexDirectives,
  remarkBreakAroundImageLines,
  remarkBreaks,
  remarkPreserveCodeMeta,
  remarkNormalizeLinksAndTagInlineCode,
];

// Start tags an HTML parser closes an open `<p>` for.
const CLOSES_PARAGRAPH = new Set(
  "address article aside blockquote center details dialog dir div dl fieldset figcaption figure footer form h1 h2 h3 h4 h5 h6 header hgroup hr li listing main menu nav ol p plaintext pre search section summary table ul xmp dd dt".split(
    " ",
  ),
);
const HEADING = /^h[1-6]$/;

interface HtmlScope {
  readonly anchor: boolean;
  readonly paragraph: boolean;
  readonly heading: boolean;
}

/**
 * Whether the tree needs the HTML parser: raw HTML, or nesting it repairs (a footnote call in a
 * link label nests `<a>`). Otherwise it only rewrites properties as hast-util-to-parse5 does and
 * drops `data`, both repeated here, and moves blank text that renders the same.
 */
function needsHtmlParser(node: MarkdownImageHastNode, scope: HtmlScope): boolean {
  if (node.type === "raw") return true;
  let inner = scope;
  if (node.type === "element" && node.tagName) {
    const { tagName } = node;
    const heading = HEADING.test(tagName);
    if (
      (tagName === "a" && scope.anchor) ||
      (scope.paragraph && CLOSES_PARAGRAPH.has(tagName)) ||
      (heading && scope.heading)
    ) {
      return true;
    }
    inner = {
      anchor: scope.anchor || tagName === "a",
      paragraph: scope.paragraph || tagName === "p",
      heading: scope.heading || heading,
    };
    const properties = node.properties ?? {};
    for (const key of Object.keys(properties)) {
      const value = properties[key];
      const info = find(html, key);
      if (
        value === false ||
        value === null ||
        value === undefined ||
        Number.isNaN(value) ||
        (!value && info.boolean)
      ) {
        delete properties[key];
      } else if (value === true && !info.boolean && !info.overloadedBoolean) {
        properties[key] = "";
      }
    }
    delete (node as { data?: unknown }).data;
  }
  return (node.children ?? []).some((child) => needsHtmlParser(child, inner));
}

const TOP_SCOPE: HtmlScope = { anchor: false, paragraph: false, heading: false };

/** rehype-raw reparses the whole tree through an HTML parser, which most trees can skip. */
function rehypeRawWhenPresent(this: Processor) {
  const transform = rehypeRaw.call(this) as unknown as (
    tree: MarkdownImageHastNode,
    file: unknown,
  ) => unknown;
  return (tree: MarkdownImageHastNode, file: unknown) =>
    needsHtmlParser(tree, TOP_SCOPE) ? transform(tree, file) : undefined;
}

export const CHAT_MARKDOWN_REHYPE_PLUGINS: PluggableList = [
  rehypePreserveBareAnchorPlaceholders,
  rehypeRawWhenPresent,
  rehypePreserveImageSourceMeta,
  [rehypeSanitize, CHAT_MARKDOWN_SANITIZE_SCHEMA],
];

const isImageNode = (node: PhrasingContent | undefined): boolean =>
  node?.type === "image" ||
  node?.type === "imageReference" ||
  ((node?.type === "link" || node?.type === "linkReference") &&
    node.children.length === 1 &&
    isImageNode(node.children[0]));
const endsLine = (node: PhrasingContent | undefined) =>
  node === undefined ||
  node.type === "break" ||
  (node.type === "text" && /\n[ \t]*$/.test(node.value));
const startsLine = (node: PhrasingContent | undefined) =>
  node === undefined ||
  node.type === "break" ||
  (node.type === "text" && /^[ \t]*\n/.test(node.value));

function breakLinesAroundImages(paragraph: Paragraph) {
  const children = paragraph.children;
  if (!children.some(isImageNode)) return;
  const ownsLine = (index: number) =>
    isImageNode(children[index]) &&
    endsLine(children[index - 1]) &&
    startsLine(children[index + 1]);
  paragraph.children = children.flatMap((child, index): PhrasingContent[] => {
    if (child.type !== "text") return [child];
    const afterImage = ownsLine(index - 1);
    const beforeImage = ownsLine(index + 1);
    if (!afterImage && !beforeImage) return [child];
    const parts: PhrasingContent[] = [];
    let value = child.value;
    if (afterImage) {
      value = value.replace(/^[ \t]*\n/, "");
      parts.push({ type: "break" });
    }
    const breakAfter = beforeImage && /\n[ \t]*$/.test(value);
    if (breakAfter) value = value.replace(/[ \t]*\n[ \t]*$/, "");
    if (value) parts.push({ type: "text", value });
    if (breakAfter) parts.push({ type: "break" });
    return parts;
  });
}

/**
 * Agents label screenshots with a line like `**Palette**` right above the image. CommonMark
 * joins those lines into one paragraph, which puts the image inline beside its label. An
 * image alone on its source line keeps that line; prose and inline badges still wrap.
 */
function remarkBreakAroundImageLines() {
  return (tree: Root) => {
    const visit = (node: Root | RootContent) => {
      if (node.type === "paragraph") breakLinesAroundImages(node);
      else if ("children" in node) node.children.forEach(visit);
    };
    visit(tree);
  };
}

type MarkdownAstNode = {
  type?: string;
  meta?: unknown;
  url?: string;
  data?: {
    hProperties?: Record<string, unknown>;
  };
  children?: MarkdownAstNode[];
};

function remarkKeepAutolinkSuffixes(): Transformer<Root> {
  return (tree, file) => {
    const source = String(file);
    const visit = (node: Root | RootContent) => {
      if (!("children" in node) || node.type === "link" || node.type === "linkReference") return;
      for (let index = 0; index < node.children.length; index++) {
        const child = node.children[index];
        const next = node.children[index + 1];
        if (child?.type === "link" && /^https?:\/\//i.test(child.url) && next?.type === "text") {
          const suffix = /^[_~]+/.exec(next.value)?.[0];
          if (!suffix) continue;
          const label = child.children.length === 1 ? child.children[0] : undefined;
          if (label?.type !== "text") continue;
          if (label.value !== child.url && `http://${label.value}` !== child.url) continue;
          const start = child.position?.start.offset;
          const end = child.position?.end.offset;
          const bare =
            !child.position ||
            (start !== undefined &&
              end !== undefined &&
              source.slice(start, end) === label.value &&
              source.slice(end, end + suffix.length) === suffix);
          if (bare) {
            child.url += suffix;
            label.value += suffix;
            next.value = next.value.slice(suffix.length);
            if (child.position && end !== undefined) {
              const boundary = {
                ...child.position.end,
                column: child.position.end.column + suffix.length,
                offset: end + suffix.length,
              };
              child.position.end = boundary;
              if (label.position) label.position.end = boundary;
              if (next.position) next.position.start = boundary;
            }
            if (!next.value) node.children.splice(index + 1, 1);
          }
        }
        if (child) visit(child);
      }
    };
    visit(tree);
  };
}

function remarkPreserveCodeMeta() {
  return (tree: MarkdownAstNode) => {
    const visit = (node: MarkdownAstNode) => {
      if (node.type === "code" && typeof node.meta === "string" && node.meta.trim().length > 0) {
        node.data = {
          ...node.data,
          hProperties: {
            ...node.data?.hProperties,
            dataCodeMeta: node.meta.trim(),
          },
        };
      }
      node.children?.forEach(visit);
    };

    visit(tree);
  };
}

function remarkNormalizeLinksAndTagInlineCode() {
  return (tree: MarkdownAstNode) => {
    const visit = (node: MarkdownAstNode, insideLink: boolean) => {
      if (
        (node.type === "link" || node.type === "definition") &&
        typeof node.url === "string" &&
        /^[A-Za-z]:[\\/]/.test(node.url)
      ) {
        node.url = `file:///${node.url.replaceAll("\\", "/")}`;
      }
      if (node.type === "inlineCode" && !insideLink) {
        node.data = {
          ...node.data,
          hProperties: {
            ...node.data?.hProperties,
            dataInlineCode: "",
          },
        };
      }
      const childInsideLink = insideLink || node.type === "link" || node.type === "linkReference";
      node.children?.forEach((child) => visit(child, childInsideLink));
    };

    visit(tree, false);
  };
}

export function shouldPreserveAssistantLineBreaks(text: string): boolean {
  return /^★ Insight(?:\s|─)/mu.test(text);
}

const imageSourceProcessor = unified()
  .use(remarkParse)
  .use(CHAT_MARKDOWN_REMARK_PLUGINS)
  .use(remarkRehype, { allowDangerousHtml: true })
  .use(CHAT_MARKDOWN_REHYPE_PLUGINS);

interface ImageSourceNode {
  readonly type: string;
  readonly tagName?: string;
  readonly properties?: {
    readonly src?: unknown;
    readonly dataLocalSrc?: unknown;
    readonly dataDarkSrc?: unknown;
    readonly dataLightSrc?: unknown;
  };
  readonly children?: ReadonlyArray<ImageSourceNode>;
}

/**
 * The sources of the images chat markdown shows, read as ChatMarkdown's `img` renderer reads
 * them. Code, comments, and plain links show no image, so they add none.
 */
export function chatMarkdownImageSources(markdown: string): Array<string> {
  const sources: Array<string> = [];
  const visit = (node: ImageSourceNode) => {
    if (node.type === "element" && node.tagName === "img") {
      const { src, dataLocalSrc, dataDarkSrc, dataLightSrc } = node.properties ?? {};
      // The sanitizer drops a Windows drive path from `src`; the renderer reads it from here.
      if (typeof dataLocalSrc === "string") {
        sources.push(normalizeMarkdownLinkDestination(dataLocalSrc).replaceAll("\\", "/"));
      } else if (typeof src === "string") {
        sources.push(normalizeMarkdownLinkDestination(src));
      }
      // A `<picture>` shows one of these instead, by the viewer's theme.
      for (const variant of [dataDarkSrc, dataLightSrc]) {
        if (typeof variant === "string") sources.push(normalizeMarkdownLinkDestination(variant));
      }
    }
    node.children?.forEach(visit);
  };
  visit(imageSourceProcessor.runSync(imageSourceProcessor.parse(markdown)) as ImageSourceNode);
  return sources;
}