From 0ee24a8c6828151675903e4cc78ab57d820bea8e Mon Sep 17 00:00:00 2001 From: Andrew Lovett-Barron Date: Mon, 1 Jul 2024 11:33:11 +0200 Subject: [PATCH] Got it kind of rendering properly! WOOOO --- src/jsoncanvas.ts | 22 +++++++--------- src/plugin.ts | 64 ++++++++++++++++++++++++++++++----------------- 2 files changed, 50 insertions(+), 36 deletions(-) diff --git a/src/jsoncanvas.ts b/src/jsoncanvas.ts index 2ee95e9..400b038 100644 --- a/src/jsoncanvas.ts +++ b/src/jsoncanvas.ts @@ -1,18 +1,13 @@ import { JSONCanvas, Edge, GenericNode } from "@trbn/jsoncanvas"; -import { h } from "hastscript"; -import { toHtml } from "hast-util-to-html"; -const { createCanvas, loadImage } = require("canvas"); +import { createCanvas, Canvas, CanvasRenderingContext2D } from "canvas"; export function validate(jsonCanvasData: JSONCanvas) { // Use the typescript lib to vlaidate? console.log(jsonCanvasData.toString()); return true; } -export function render( - jsc: JSONCanvas, - options: object -): HTMLCanvasElement | null { - console.log("render", jsc); +export function render(jsc: JSONCanvas, options: object): String | null { + console.log("render", jsc, options); // Init Canvas objects const { canvas, ctx } = initRender("jsc", 800, 600); @@ -28,14 +23,15 @@ export function render( jsc.getEdges().forEach((edge) => { const fromNode = jsc.getNodes().find((node) => node.id === edge.fromNode); const toNode = jsc.getNodes().find((node) => node.id === edge.toNode); - drawEdge(canvas, ctx, toNode, fromNode, edge); + if (toNode !== undefined && fromNode !== undefined) + drawEdge(canvas, ctx, toNode, fromNode, edge); }); - return canvas; + return canvas.toDataURL(); } function initRender(id: string, width: number, height: number) { - const canvas = createCanvas(width, height) as HTMLCanvasElement; + const canvas = createCanvas(width, height); if (!canvas) { console.error(`Canvas element with id '${id}' not found.`); return { canvas: null, ctx: null }; @@ -58,7 +54,7 @@ function initRender(id: string, width: number, height: number) { } function drawNode( - canvas: HTMLCanvasElement, + canvas: Canvas, ctx: CanvasRenderingContext2D, node: GenericNode | any ) { @@ -90,7 +86,7 @@ function drawNode( } function drawEdge( - canvas: HTMLCanvasElement, + canvas: Canvas, ctx: CanvasRenderingContext2D, toNode: GenericNode, fromNode: GenericNode, diff --git a/src/plugin.ts b/src/plugin.ts index 5554059..661eecd 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -1,5 +1,5 @@ import type { Plugin } from "unified"; -import type { ElementContent, Root } from "hast"; +import type { Element, ElementContent, Root } from "hast"; import { fromHtmlIsomorphic } from "hast-util-from-html-isomorphic"; import { visit } from "unist-util-visit"; import fs from "fs"; @@ -28,31 +28,28 @@ Things decide: */ export const rehypeJsonCanvas: Plugin<[], Root> = () => { - return (tree) => { - visit(tree, "element", (node, index, pre) => { - console.log(node, index, pre); + return async (tree) => { + const nodesToReplace = [] as Array; + + // Iterate over the markdown file as tree + visit(tree, "element", (node, index) => { + console.log(node, index); // only match image embeds if (node.tagName !== "img" || index === undefined) { return; } + console.log("Adding", node); + nodesToReplace.push(node); + // index = index += 1; + }); - // const nodeData = node.data ajec t; - const canvasPath = "jsoncanvas"; - const webcheck = canvasPath.trim().toLowerCase(); - - let canvasMarkdown = "Loading"; - if (webcheck.startsWith("https://")) { - fetch(canvasPath) - .then((res) => res.text()) - .then((text) => (canvasMarkdown = text)); - } else { - canvasMarkdown = fs.readFileSync(canvasPath, { - encoding: "utf8", - flag: "r", - }); - } + for (const node of nodesToReplace) { + const canvasPath = node.properties.src as string; + console.log("Detected", canvasPath); + let canvasMarkdown = await getCanvasFromEmbed(canvasPath); + console.log("Got markdown", canvasMarkdown); const jsonCanvasFromString = JSONCanvas.fromString(canvasMarkdown); let canvas; @@ -63,15 +60,36 @@ export const rehypeJsonCanvas: Plugin<[], Root> = () => { canvas = "
Not a properly formatted JsonCanvas
"; } - const canvasHast = fromHtmlIsomorphic(canvas, { + console.log(canvas); + + const canvasHast = fromHtmlIsomorphic(``, { fragment: true, }); node.properties = { ...node.properties, }; - node.tagName = "canvas"; + node.tagName = "div"; node.children = canvasHast.children as ElementContent[]; - index = index += 1; - }); + } }; }; + +async function getCanvasFromEmbed(path: string): Promise { + let canvasMarkdown = "Loading"; + const webcheck = path.trim().toLowerCase(); + + if (webcheck.startsWith("https://") || typeof window !== "undefined") { + await fetch(path) + .then((res) => res.text()) + .then((text) => (canvasMarkdown = text)); + } else { + // To accomodate ssr + canvasMarkdown = fs.readFileSync(path, { + encoding: "utf8", + flag: "r", + }); + } + if (canvasMarkdown === null) return ""; + + return canvasMarkdown; +}