nandi/oripublic Fork 0
b6cd929ab1425a4ef6073ea9c189917cca02cf3c
Commits
Clone
git clone https://git.rickub.com/nandi/ori.git
git clone ssh://git@rickub.com/nandi/ori.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

forked from bots-garden/ori

AsciiDocView.tsx · 58 lines · 1.6 KBTypeScript Blame HistoryRaw
✨ Workspace panel, selectors, previews, desktop app, sandbox template, resizable file tree, light/dark theme 76d62ac k33g yesterday1/**
2 * AsciiDocView renders an AsciiDoc document to HTML with Asciidoctor,
3 * lazy-loading the (heavy) converter on first use.
4 *
5 * The converted HTML comes from files in the user's own workspace, rendered
6 * for the user who owns them — the same trust model as opening the file in
7 * any editor's preview.
8 *
9 * @example
10 * <AsciiDocView content="= Title" />
11 */
12
13import { useEffect, useState } from "react";
14
15export function AsciiDocView({ content }: { content: string }) {
16 const [html, setHtml] = useState<string | null>(null);
17 const [error, setError] = useState<string | null>(null);
18
19 useEffect(() => {
20 let cancelled = false;
21 // @asciidoctor/core v4 exposes an async convert() as a named export
22 // (the v3 synchronous asciidoctor() factory is gone).
23 import("@asciidoctor/core")
24 .then(({ convert }) =>
25 convert(content, {
26 safe: "safe",
27 attributes: { showtitle: true, icons: "font" },
28 }),
29 )
30 .then((converted) => {
31 if (!cancelled) {
32 setHtml(String(converted));
33 }
34 })
35 .catch((cause: Error) => {
36 if (!cancelled) {
37 setError(`AsciiDoc rendering failed: ${cause.message}`);
38 }
39 });
40 return () => {
41 cancelled = true;
42 };
43 }, [content]);
44
45 if (error) {
46 return <p className="pane-error">{error}</p>;
47 }
48 if (html === null) {
49 return <p className="pane-empty">rendering</p>;
50 }
51 return (
52 <div
53 className="markdown asciidoc"
54 // biome-ignore lint/security/noDangerouslySetInnerHtml: Asciidoctor's output over the user's own workspace files, converted in safe mode — the accepted pattern for a local document preview.
55 dangerouslySetInnerHTML={{ __html: html }}
56 />
57 );
58}