From ca901d4bf81bf9a50eee5198fd4022fbe8c50421 Mon Sep 17 00:00:00 2001 From: Andrew Lovett-Barron Date: Mon, 8 Jul 2024 13:12:29 +0200 Subject: [PATCH] Weird import fix --- example/example.md | 0 src/embed.ts | 12 +++++++++++- src/jsoncanvas.ts | 11 ++--------- src/options.ts | 8 ++++++++ src/plugin.ts | 18 +++++++++++++----- 5 files changed, 34 insertions(+), 15 deletions(-) delete mode 100644 example/example.md diff --git a/example/example.md b/example/example.md deleted file mode 100644 index e69de29..0000000 diff --git a/src/embed.ts b/src/embed.ts index aa9afcc..d576edb 100644 --- a/src/embed.ts +++ b/src/embed.ts @@ -1,3 +1,4 @@ +import path from "node:path" import type { Element } from "hast" import { s } from "hastscript" @@ -8,12 +9,20 @@ import type { GenericNode } from "@trbn/jsoncanvas" // import { applyDefaults, Options } from "./options"; import { getCanvasFromEmbed } from "./plugin" +import { type Options, applyDefaults } from "./options" + // This renders out the images export async function drawEmbedded( svg: Element, grp: Element, node: GenericNode | any, + config?: Partial, ) { + const options = applyDefaults(config) + console.log( + "Test", + options.assetPath ? path.join(options.assetPath, node.file) : node.file, + ) if (node.type === "file" && svg) { if (node.file.match(/\.(jpg|jpeg|png|gif)$/i)) { const image = s("image", { @@ -34,10 +43,11 @@ export async function drawMarkdownEmbed( svg: Element, grp: Element, node: GenericNode | any, + config?: Partial, ) { if (node.type === "file" && svg) { if (node.file.match(/\.(md|mdx)$/i)) { - const mdFile = await getCanvasFromEmbed(node.file) + const mdFile = await getCanvasFromEmbed(node.file, config) const mdast = fromMarkdown(mdFile) const hast = toHast(mdast) diff --git a/src/jsoncanvas.ts b/src/jsoncanvas.ts index c421355..b2cf699 100644 --- a/src/jsoncanvas.ts +++ b/src/jsoncanvas.ts @@ -59,12 +59,6 @@ export function render( drawEdge(svg, toNode, fromNode, edge, options) } - return renderToBuffer(svg) -} - -function renderToBuffer(svg: Element, config?: Partial): Element { - const options = applyDefaults(config) - console.log("Rendering", svg, options) return svg } @@ -74,7 +68,6 @@ function initRender( config?: Partial, ): Element { const options = applyDefaults(config) - console.log(options) const BASE_SVG_PROPS = { version: "1.1", xmlns: "http://www.w3.org/2000/svg", @@ -148,8 +141,8 @@ async function drawNode( group.children.push(rect) - drawEmbedded(svg, group, node) - drawMarkdownEmbed(svg, group, node) + drawEmbedded(svg, group, node, config) + drawMarkdownEmbed(svg, group, node, config) // Group Label if (node.label) { diff --git a/src/options.ts b/src/options.ts index 70bca4d..b448aba 100644 --- a/src/options.ts +++ b/src/options.ts @@ -16,6 +16,13 @@ export interface Options { */ assetPath?: string | null + /** + * Define an markdown path where the .md files will be searched for WHEN EMBEDDING ONLY. This will add the md path before the filename. Otherwise uses assetPath defaults + * + * Defaults to null + */ + mdPath?: string | null + /** * Render mode. Determines the canvas output mode * @@ -55,6 +62,7 @@ export function applyDefaults(config: Partial = {}): Options { ? true : config.openEmbededInNewTab, assetPath: config.assetPath === undefined ? null : config.assetPath, + mdPath: config.mdPath === undefined ? config.assetPath : config.mdPath, renderMode: config.renderMode === undefined ? "canvas" : config.renderMode, canvasBuffer: config.canvasBuffer === undefined ? 30 : config.canvasBuffer, nodeStrokeWidth: diff --git a/src/plugin.ts b/src/plugin.ts index fafb7fd..e32a855 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -61,7 +61,7 @@ export const rehypeJsonCanvas: Plugin<[], Root> = ( let canvas = null if (validate(jsonCanvasFromString)) { - canvas = render(jsonCanvasFromString, {}) + canvas = render(jsonCanvasFromString, config) } else { canvas = h("div", "Not a properly formatted JsonCanvas") } @@ -82,10 +82,14 @@ export async function getCanvasFromEmbed( config?: Partial, ): Promise { const options = applyDefaults(config) - console.log(options) let canvasMarkdown = "" const webcheck = markdownPath.trim().toLowerCase() + // https://stackoverflow.com/questions/190852/how-can-i-get-file-extensions-with-javascript/12900504#12900504 + const extension = webcheck.slice( + (Math.max(0, webcheck.lastIndexOf(".")) || Number.POSITIVE_INFINITY) + 1, + ) + if (webcheck.startsWith("https://") || typeof window !== "undefined") { await fetch(markdownPath) .then((res) => res.text()) @@ -93,11 +97,15 @@ export async function getCanvasFromEmbed( canvasMarkdown = text }) } else { + const opPath = + extension === "md" ? options.mdPath : options.assetPath || null + + console.log("opPath", opPath) // To accomodate ssr - const ssrPath = options.assetPath - ? path.join(process.cwd(), options.assetPath, markdownPath) + const ssrPath = opPath + ? path.join(process.cwd(), opPath, markdownPath) : path.join(process.cwd(), markdownPath) - + console.log(ssrPath) try { canvasMarkdown = fs.readFileSync(ssrPath, { encoding: "utf8",