/**
* AsciiDocView renders an AsciiDoc document to HTML with Asciidoctor,
* lazy-loading the (heavy) converter on first use.
*
* The converted HTML comes from files in the user's own workspace, rendered
* for the user who owns them — the same trust model as opening the file in
* any editor's preview.
*
* @example
*
*/
import { useEffect, useState } from "react";
export function AsciiDocView({ content }: { content: string }) {
const [html, setHtml] = useState(null);
const [error, setError] = useState(null);
useEffect(() => {
let cancelled = false;
// @asciidoctor/core v4 exposes an async convert() as a named export
// (the v3 synchronous asciidoctor() factory is gone).
import("@asciidoctor/core")
.then(({ convert }) =>
convert(content, {
safe: "safe",
attributes: { showtitle: true, icons: "font" },
}),
)
.then((converted) => {
if (!cancelled) {
setHtml(String(converted));
}
})
.catch((cause: Error) => {
if (!cancelled) {
setError(`AsciiDoc rendering failed: ${cause.message}`);
}
});
return () => {
cancelled = true;
};
}, [content]);
if (error) {
return {error}
;
}
if (html === null) {
return rendering…
;
}
return (
);
}