Skip to content

Commit

Permalink
for whatever reason I can't fix Astro component
Browse files Browse the repository at this point in the history
  • Loading branch information
stereobooster committed May 1, 2024
1 parent bc7e934 commit 4315922
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 38 deletions.
34 changes: 19 additions & 15 deletions packages/astro-graphviz/Graphviz.astro
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
---
import {
renderGraphviz,
type RenderGraphvizOptions,
} from "@beoe/rehype-graphviz";
import { getCache } from "@beoe/cache";
import { serialize } from "node:v8";
// I can't use internal modules - don't know why
// import { processGraphvizSvg } from "@beoe/rehype-graphviz";
// import { getCache } from "@beoe/cache";
import { Graphviz } from "@hpcc-js/wasm";
const graphviz = await Graphviz.load();
const cache = await getCache();
// const cache = await getCache();
interface Props extends RenderGraphvizOptions {}
interface Props {
code: string;
class?: string;
}
let svg: string;
const cacheKey = serialize(Astro.props);
svg = cache.get(cacheKey);
if (svg === undefined) {
svg = await renderGraphviz(Astro.props);
cache.set(cacheKey, svg);
}
// const cacheKey = Astro.props.code;
// svg = cache.get(cacheKey);
// if (svg === undefined) {
svg = graphviz.dot(Astro.props.code);
// cache.set(cacheKey, svg);
// }
---

<Fragment set:html={svg} />
<figure class={`beoe graphviz ${Astro.props.class || ""}`}>
<Fragment set:html={svg} />
</figure>
3 changes: 2 additions & 1 deletion packages/astro-graphviz/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
},
"dependencies": {
"@beoe/cache": "workspace:*",
"@beoe/rehype-graphviz": "workspace:*"
"@beoe/rehype-graphviz": "workspace:*",
"@hpcc-js/wasm": "^2.16.1"
},
"devDependencies": {
"astro": "^4.0.0"
Expand Down
6 changes: 4 additions & 2 deletions packages/cache/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
"directory": "packages/sqlitecache"
},
"exports": {
"types": "./dist/index.d.js",
"default": "./dist/index.js"
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
}
},
"main": "./dist/index.js",
"module": "./dist/index.js",
Expand Down
4 changes: 0 additions & 4 deletions packages/demo/src/content/docs/examples/graphviz.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ digraph finite_state_machine {

## astro-component

For unkown reason this fails during build

```
import { Graphviz } from "@beoe/astro-graphviz";

<Graphviz
Expand Down Expand Up @@ -63,4 +60,3 @@ import { Graphviz } from "@beoe/astro-graphviz";
8 -> 5 [label = "S(a)"];
}`}
/>
```
31 changes: 15 additions & 16 deletions packages/rehype-graphviz/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,28 @@ import type { Root } from "hast";
import { processGraphvizSvg } from "./graphviz.js";
import { rehypeCodeHook, type MapLike } from "@beoe/rehype-code-hook";

// import { Graphviz } from "@hpcc-js/wasm";
// const graphviz = await Graphviz.load();
// export const renderGraphviz = ({ code }: { code: string }) =>
// processGraphvizSvg(graphviz.dot(code));

import { waitFor } from "@beoe/rehype-code-hook";

export type RenderGraphvizOptions = { code: string; class?: string };

import { Graphviz } from "@hpcc-js/wasm";
const graphviz = await Graphviz.load();
export const renderGraphviz = (o: RenderGraphvizOptions) =>
processGraphvizSvg(graphviz.dot(o.code), o.class);

// import { waitFor } from "@beoe/rehype-code-hook";
/**
* If all graphviz diagrams are cached it would not even load module in memory.
* If there are diagrams, it would load module and first few renders would be async,
* but all consequent renders would be sync
*/
export const renderGraphviz = waitFor(
async () => {
// @ts-ignore
const Graphviz = (await import("@hpcc-js/wasm")).Graphviz;
return await Graphviz.load();
},
(graphviz) => (o: RenderGraphvizOptions) =>
processGraphvizSvg(graphviz.dot(o.code), o.class)
);
// export const renderGraphviz = waitFor(
// async () => {
// // @ts-ignore
// const Graphviz = (await import("@hpcc-js/wasm")).Graphviz;
// return await Graphviz.load();
// },
// (graphviz) => (o: RenderGraphvizOptions) =>
// processGraphvizSvg(graphviz.dot(o.code), o.class)
// );

export { processGraphvizSvg };

Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4315922

Please sign in to comment.