Write HTML. Render video. Built for agents.
JavaScript aiai-agentsanimationclaude-codeffmpegframeworkgsaphtmlhtml-to-videomcpmotion-graphicspuppeteerrenderingtypescriptvideo-editor
import { createHash } from "node:crypto";
import {
SCENE_FONTS_ATTR,
SCENE_PART_ATTR,
SCENE_PARTS_META,
type SceneParts,
} from "../sceneParts";
import { injectTagsAtHeadStart, parseHTMLContent } from "./htmlDocument";
const hash = (text: string) => createHash("sha1").update(text).digest("hex").slice(0, 16);
/**
* Adds a {@link SceneParts} manifest so the runtime can tell a rebuild differs only inside scenes.
* `ignore` lists selectors for parts that change on every build without changing what is shown.
*/
export function addScenePartsManifest(html: string, ignore: readonly string[] = []): string {
const doc = parseHTMLContent(html);
const parts = [...doc.querySelectorAll(`[${SCENE_PART_ATTR}]`)];
if (parts.length === 0) return html;
const byScene = new Map<string, string[]>();
for (const el of parts) {
const scene = el.getAttribute(SCENE_PART_ATTR) ?? "";
byScene.set(scene, [...(byScene.get(scene) ?? []), el.outerHTML]);
}
// A marker keeps each part's place in `shared`, so moving or reordering a scene changes it.
for (const el of parts)
el.replaceWith(doc.createComment(`hf-scene:${el.getAttribute(SCENE_PART_ATTR)}`));
for (const el of [...ignore, `style[${SCENE_FONTS_ATTR}]`].flatMap((sel) => [
...doc.querySelectorAll(sel),
]))
el.remove();
const manifest: SceneParts = {
shared: hash(doc.toString()),
scenes: Object.fromEntries([...byScene].map(([scene, html]) => [scene, hash(html.join("\n"))])),
};
const content = JSON.stringify(manifest).replace(/&/g, "&").replace(/"/g, """);
return injectTagsAtHeadStart(html, `<meta name="${SCENE_PARTS_META}" content="${content}">`);
}