UniAST HTML serialization

Last modified by Manuel Leduc on 2025/09/17 09:22

UniAST now provides a serializer to HTML. This is now the advised way to render HTML in Cristal. Other methods are or will be deprecated and removed.

Example of conversion from Markdown to HTML:

import { Container } from "inversify";
import type { UniAstToHTMLConverter } from "@xwiki/cristal-uniast-html";
import type { MarkdownToUniAstConverter } from "@xwiki/cristal-uniast-markdown";

async function markdownToHTML(
  source: string,
  container: Container,
): Promise<string> {
  // Mardown to uniast to html

  const md = container.get<MarkdownToUniAstConverter>(
    "MarkdownToUniAstConverter",
  );

  const uniAst = await md.parseMarkdown(source);

  if (uniAst instanceof Error) {
    throw uniAst;
  }
  const html = container.get<UniAstToHTMLConverter>("UniAstToHTMLConverter");
  const toHtml = html.toHtml(uniAst);
  if (toHtml instanceof Error) {
    throw toHtml;
  }
  return toHtml;
}

Get Connected