From 328441b7473508a7c12b5e0ac796f00fd7ee5388 Mon Sep 17 00:00:00 2001 From: SkepticMystic Date: Sun, 5 May 2024 11:06:49 +0200 Subject: [PATCH] feat(view:codeblock:markmap): Markmap codeblocks alpha --- src/codeblocks/schema.ts | 4 +- src/components/Markmap/MarkmapDiagram.svelte | 16 +++ .../codeblocks/CodeblockMarkmap.svelte | 114 ++++++++++++++++++ src/utils/markmap.ts | 42 +++++++ 4 files changed, 174 insertions(+), 2 deletions(-) create mode 100644 src/components/Markmap/MarkmapDiagram.svelte create mode 100644 src/components/codeblocks/CodeblockMarkmap.svelte create mode 100644 src/utils/markmap.ts diff --git a/src/codeblocks/schema.ts b/src/codeblocks/schema.ts index 5310d49e..bba8a0ba 100644 --- a/src/codeblocks/schema.ts +++ b/src/codeblocks/schema.ts @@ -106,10 +106,10 @@ const build = (input: Record, data: InputData) => { .optional(), type: z - .enum(["tree", "mermaid"], { + .enum(["tree", "mermaid", "markmap"], { message: zod.error.invalid_enum( "type", - ["tree", "mermaid"], + ["tree", "mermaid", "markmap"], input["type"], ), }) diff --git a/src/components/Markmap/MarkmapDiagram.svelte b/src/components/Markmap/MarkmapDiagram.svelte new file mode 100644 index 00000000..b9548bbc --- /dev/null +++ b/src/components/Markmap/MarkmapDiagram.svelte @@ -0,0 +1,16 @@ + + + diff --git a/src/components/codeblocks/CodeblockMarkmap.svelte b/src/components/codeblocks/CodeblockMarkmap.svelte new file mode 100644 index 00000000..44e81bc8 --- /dev/null +++ b/src/components/codeblocks/CodeblockMarkmap.svelte @@ -0,0 +1,114 @@ + + +
+ + + {#if options.title} +

+ {options.title} +

+ {/if} + + {#if tree.length} +
+
+ +
+ + +
+ {:else} + +

No paths found.

+ {/if} +
diff --git a/src/utils/markmap.ts b/src/utils/markmap.ts new file mode 100644 index 00000000..4c07b15a --- /dev/null +++ b/src/utils/markmap.ts @@ -0,0 +1,42 @@ +import type { EdgeAttribute } from "src/graph/MyMultiGraph"; +import type { EdgeTree } from "src/graph/traverse"; +import type { ShowNodeOptions } from "src/interfaces/settings"; +import { Links } from "./links"; +import { untyped_pick } from "./objects"; +import { Paths } from "./paths"; +import { url_search_params } from "./url"; + +type Options = { + show_attributes?: EdgeAttribute[]; + show_node_options?: ShowNodeOptions; +}; + +const from_tree = (tree: EdgeTree[], options?: Options) => { + let markmap = ""; + + tree.forEach((item) => { + const hashes = "#".repeat(item.depth); + + const link = Links.ify( + item.edge.target_id, + Paths.show(item.edge.target_id, options?.show_node_options), + { link_kind: "markdown" }, + ); + + const attr = options?.show_attributes + ? ` (${url_search_params(untyped_pick(item.edge.attr, options.show_attributes), { trim_lone_param: true })})` + : ""; + + markmap += `${hashes} ${link}${attr}\n`; + + if (item.children.length) { + markmap += from_tree(item.children, options); + } + }); + + return markmap; +}; + +export const Markmap = { + from_tree, +};