Skip to content

Commit

Permalink
Weird import fix
Browse files Browse the repository at this point in the history
  • Loading branch information
lovettbarron committed Jul 8, 2024
1 parent ccf0fe9 commit ca901d4
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 15 deletions.
Empty file removed example/example.md
Empty file.
12 changes: 11 additions & 1 deletion src/embed.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from "node:path"
import type { Element } from "hast"
import { s } from "hastscript"

Expand All @@ -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<Options>,
) {
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", {
Expand All @@ -34,10 +43,11 @@ export async function drawMarkdownEmbed(
svg: Element,
grp: Element,
node: GenericNode | any,
config?: Partial<Options>,
) {
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)
Expand Down
11 changes: 2 additions & 9 deletions src/jsoncanvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@ export function render(
drawEdge(svg, toNode, fromNode, edge, options)
}

return renderToBuffer(svg)
}

function renderToBuffer(svg: Element, config?: Partial<Options>): Element {
const options = applyDefaults(config)
console.log("Rendering", svg, options)
return svg
}

Expand All @@ -74,7 +68,6 @@ function initRender(
config?: Partial<Options>,
): Element {
const options = applyDefaults(config)
console.log(options)
const BASE_SVG_PROPS = {
version: "1.1",
xmlns: "http://www.w3.org/2000/svg",
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -55,6 +62,7 @@ export function applyDefaults(config: Partial<Options> = {}): 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:
Expand Down
18 changes: 13 additions & 5 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -82,22 +82,30 @@ export async function getCanvasFromEmbed(
config?: Partial<Options>,
): Promise<string> {
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())
.then((text) => {
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",
Expand Down

0 comments on commit ca901d4

Please sign in to comment.