astro-d2 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024-present, HiDeoo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,30 @@
1
+ <div align="center">
2
+ <h1>astro-d2 ✏️</h1>
3
+ <p>Astro integration and remark plugin to transform D2 Markdown code blocks into diagrams.</p>
4
+ </div>
5
+
6
+ <div align="center">
7
+ <a href="https://github.com/HiDeoo/astro-d2/actions/workflows/integration.yml">
8
+ <img alt="Integration Status" src="https://github.com/HiDeoo/astro-d2/actions/workflows/integration.yml/badge.svg" />
9
+ </a>
10
+ <a href="https://github.com/HiDeoo/astro-d2/blob/main/LICENSE">
11
+ <img alt="License" src="https://badgen.net/github/license/HiDeoo/astro-d2" />
12
+ </a>
13
+ <br />
14
+ </div>
15
+
16
+ ## Getting Started
17
+
18
+ Want to get started immediately? Check out the [getting started guide](https://astro-d2.vercel.app/getting-started/).
19
+
20
+ ## Features
21
+
22
+ An [Astro](https://astro.build/) integration and [remark](https://remark.js.org/) plugin to transform [D2](https://d2lang.com/) Markdown code blocks into diagrams.
23
+
24
+ Check out the [examples](https://astro-d2.vercel.app/examples/hello-world/) for previews of some diagrams you can create with D2.
25
+
26
+ ## License
27
+
28
+ Licensed under the MIT License, Copyright © HiDeoo.
29
+
30
+ See [LICENSE](https://github.com/HiDeoo/astro-d2/blob/main/LICENSE) for more information.
package/config.ts ADDED
@@ -0,0 +1,56 @@
1
+ import { z } from 'astro/zod'
2
+
3
+ export const AstroD2ConfigSchema = z
4
+ .object({
5
+ /**
6
+ * Defines the layout engine to use to generate the diagrams.
7
+ *
8
+ * @default 'dagre'
9
+ * @see https://d2lang.com/tour/layouts#layout-engines
10
+ */
11
+ layout: z.union([z.literal('dagre'), z.literal('elk'), z.literal('tala')]).default('dagre'),
12
+ /**
13
+ * The name of the output directory containing the generated diagrams relative to the `public/` directory.
14
+ *
15
+ * @default 'd2'
16
+ */
17
+ output: z.string().default('d2'),
18
+ /**
19
+ * Whether the Astro D2 integration should skip the generation of diagrams.
20
+ *
21
+ * This is useful to disable generating diagrams when deploying on platforms that do not have the D2 binary
22
+ * available. This will require you to build and commit the diagrams before deploying your site.
23
+ *
24
+ * @default false
25
+ */
26
+ skipGeneration: z.boolean().default(false),
27
+ /**
28
+ * The themes to use for the generated diagrams.
29
+ *
30
+ * @see https://d2lang.com/tour/themes
31
+ */
32
+ theme: z
33
+ .object({
34
+ /**
35
+ * The dark theme to use for the diagrams when the user's system preference is set to dark mode.
36
+ *
37
+ * To disable the dark theme and have all diagrams look the same, set this option to `false`.
38
+ *
39
+ * @default '200'
40
+ * @see https://d2lang.com/tour/themes
41
+ */
42
+ dark: z.union([z.string(), z.literal(false)]).default('200'),
43
+ /**
44
+ * The default theme to use for the diagrams.
45
+ *
46
+ * @default '0'
47
+ * @see https://d2lang.com/tour/themes
48
+ */
49
+ default: z.string().default('0'),
50
+ })
51
+ .default({}),
52
+ })
53
+ .default({})
54
+
55
+ export type AstroD2UserConfig = z.input<typeof AstroD2ConfigSchema>
56
+ export type AstroD2Config = z.output<typeof AstroD2ConfigSchema>
package/index.ts ADDED
@@ -0,0 +1,54 @@
1
+ import fs from 'node:fs/promises'
2
+ import path from 'node:path'
3
+
4
+ import type { AstroIntegration } from 'astro'
5
+
6
+ import { AstroD2ConfigSchema, type AstroD2UserConfig } from './config'
7
+ import { isD2Installed } from './libs/d2'
8
+ import { throwErrorWithHint } from './libs/integration'
9
+ import { remarkAstroD2 } from './libs/remark'
10
+
11
+ export type { AstroD2UserConfig } from './config'
12
+
13
+ export default function astroD2Integration(userConfig?: AstroD2UserConfig): AstroIntegration {
14
+ const parsedConfig = AstroD2ConfigSchema.safeParse(userConfig)
15
+
16
+ if (!parsedConfig.success) {
17
+ throwErrorWithHint(
18
+ `The provided D2 integration configuration is invalid.\n${parsedConfig.error.issues.map((issue) => issue.message).join('\n')}`,
19
+ )
20
+ }
21
+
22
+ const config = parsedConfig.data
23
+
24
+ return {
25
+ name: 'astro-d2-integration',
26
+ hooks: {
27
+ 'astro:config:setup': async ({ command, logger, updateConfig }) => {
28
+ if (command !== 'build' && command !== 'dev') {
29
+ return
30
+ }
31
+
32
+ if (config.skipGeneration) {
33
+ logger.warn("Skipping generation of D2 diagrams as the 'skipGeneration' option is enabled.")
34
+ } else {
35
+ if (!(await isD2Installed())) {
36
+ throwErrorWithHint(
37
+ 'Could not find D2. Please check the installation instructions at https://github.com/terrastruct/d2/blob/master/docs/INSTALL.md',
38
+ )
39
+ }
40
+
41
+ if (command === 'build') {
42
+ await fs.rm(path.join('public', config.output), { force: true, recursive: true })
43
+ }
44
+ }
45
+
46
+ updateConfig({
47
+ markdown: {
48
+ remarkPlugins: [[remarkAstroD2, config]],
49
+ },
50
+ })
51
+ },
52
+ },
53
+ }
54
+ }
@@ -0,0 +1,92 @@
1
+ import { z } from 'astro/zod'
2
+
3
+ export const AttributesSchema = z
4
+ .object({
5
+ /**
6
+ * When specified, the diagram will package multiple boards as 1 SVG which transitions through each board at the
7
+ * specified interval (in milliseconds).
8
+ */
9
+ animateInterval: z.string().optional(),
10
+ /**
11
+ * The dark theme to use for the diagrams when the user's system preference is set to dark mode.
12
+ *
13
+ * To disable the dark theme and have all diagrams look the same, set this attribute to `'false'`.
14
+ *
15
+ * @see https://d2lang.com/tour/themes
16
+ */
17
+ darkTheme: z
18
+ .string()
19
+ .optional()
20
+ .transform((value) => (value === 'false' ? false : value)),
21
+ /**
22
+ * The padding (in pixels) around the rendered diagram.
23
+ *
24
+ * @default 100
25
+ */
26
+ pad: z.coerce.number().default(100),
27
+ /**
28
+ * Whether to render the diagram as if it was sketched by hand.
29
+ *
30
+ * @default 'false'
31
+ */
32
+ sketch: z.union([z.literal('true'), z.literal('false')]).default('false'),
33
+ /**
34
+ * Defines the target board to render when using composition.
35
+ * Use `root` to target the root board.
36
+ *
37
+ * @see https://d2lang.com/tour/composition
38
+ */
39
+ target: z
40
+ .string()
41
+ .optional()
42
+ .transform((value) => (value === 'root' ? '' : value)),
43
+ /**
44
+ * The title of the diagram that will be used as the `alt` attribute of the generated image.
45
+ *
46
+ * @default 'Diagram'
47
+ */
48
+ title: z.string().default('Diagram'),
49
+ /**
50
+ * The default theme to use for the diagrams.
51
+ *
52
+ * @see https://d2lang.com/tour/themes
53
+ */
54
+ theme: z.string().optional(),
55
+ /**
56
+ * The width (in pixels) of the diagram.
57
+ */
58
+ width: z.coerce.number().optional(),
59
+ })
60
+ .default({})
61
+
62
+ const attributeRegex =
63
+ /(?<key>[^\s"'=]+)=(?:(?<noQuoteValue>\w+)|'(?<singleQuoteValue>[^']+)'|"(?<doubleQuoteValue>[^"]+))|(?<truthyKey>\w+)/g
64
+
65
+ export function getAttributes(attributesStr: string | null | undefined) {
66
+ return AttributesSchema.parse(parseAttributes(attributesStr))
67
+ }
68
+
69
+ function parseAttributes(attributesStr: string | null | undefined) {
70
+ if (!attributesStr) {
71
+ return {}
72
+ }
73
+
74
+ const matches = attributesStr.matchAll(attributeRegex)
75
+
76
+ const attributes: Record<string, string> = {}
77
+
78
+ for (const match of matches) {
79
+ const { key, noQuoteValue, singleQuoteValue, doubleQuoteValue, truthyKey } = match.groups ?? {}
80
+
81
+ const attributeKey = truthyKey ?? key
82
+ const attributeValue = truthyKey ? 'true' : noQuoteValue ?? singleQuoteValue ?? doubleQuoteValue
83
+
84
+ if (attributeKey && attributeValue) {
85
+ attributes[attributeKey] = attributeValue
86
+ }
87
+ }
88
+
89
+ return attributes
90
+ }
91
+
92
+ export type DiagramAttributes = z.infer<typeof AttributesSchema>
package/libs/d2.ts ADDED
@@ -0,0 +1,103 @@
1
+ import fs from 'node:fs/promises'
2
+
3
+ import type { AstroD2Config } from '../config'
4
+
5
+ import type { DiagramAttributes } from './attributes'
6
+ import { exec } from './exec'
7
+
8
+ const viewBoxRegex = /viewBox="\d+ \d+ (?<width>\d+) (?<height>\d+)"/
9
+
10
+ export async function isD2Installed() {
11
+ try {
12
+ await getD2Version()
13
+
14
+ return true
15
+ } catch {
16
+ return false
17
+ }
18
+ }
19
+
20
+ export async function generateD2Diagram(
21
+ config: AstroD2Config,
22
+ attributes: DiagramAttributes,
23
+ input: string,
24
+ outputPath: string,
25
+ ) {
26
+ const extraArgs = []
27
+
28
+ if (
29
+ (config.theme.dark !== false && attributes.darkTheme !== false) ||
30
+ (attributes.darkTheme !== undefined && attributes.darkTheme !== false)
31
+ ) {
32
+ extraArgs.push(`--dark-theme=${attributes.darkTheme ?? config.theme.dark}`)
33
+ }
34
+
35
+ if (attributes.animateInterval) {
36
+ extraArgs.push(`--animate-interval=${attributes.animateInterval}`)
37
+ }
38
+
39
+ if (attributes.target !== undefined) {
40
+ extraArgs.push(`--target='${attributes.target}'`)
41
+ }
42
+
43
+ try {
44
+ // The `-` argument is used to read from stdin instead of a file.
45
+ await exec(
46
+ 'd2',
47
+ [
48
+ `--layout=${config.layout}`,
49
+ `--theme=${attributes.theme ?? config.theme.default}`,
50
+ `--sketch=${attributes.sketch}`,
51
+ `--pad=${attributes.pad}`,
52
+ ...extraArgs,
53
+ '-',
54
+ outputPath,
55
+ ],
56
+ input,
57
+ )
58
+ } catch (error) {
59
+ throw new Error('Failed to generate D2 diagram.', { cause: error })
60
+ }
61
+
62
+ return await getD2DiagramSize(outputPath)
63
+ }
64
+
65
+ export async function getD2DiagramSize(diagramPath: string): Promise<D2Size> {
66
+ try {
67
+ const content = await fs.readFile(diagramPath, 'utf8')
68
+ const match = content.match(viewBoxRegex)
69
+ const { height, width } = match?.groups ?? {}
70
+
71
+ if (!height || !width) {
72
+ return
73
+ }
74
+
75
+ const computedHeight = Number.parseInt(height, 10)
76
+ const computedWidth = Number.parseInt(width, 10)
77
+
78
+ return { height: computedHeight, width: computedWidth }
79
+ } catch (error) {
80
+ throw new Error(`Failed to get D2 diagram size at '${diagramPath}'.`, { cause: error })
81
+ }
82
+ }
83
+
84
+ async function getD2Version() {
85
+ try {
86
+ const [version] = await exec('d2', ['--version'])
87
+
88
+ if (!version || !/^\d+\.\d+\.\d+$/.test(version)) {
89
+ throw new Error(`Invalid D2 version, got '${version}'.`)
90
+ }
91
+
92
+ return version
93
+ } catch (error) {
94
+ throw new Error('Failed to get D2 version.', { cause: error })
95
+ }
96
+ }
97
+
98
+ export type D2Size =
99
+ | {
100
+ height: number
101
+ width: number
102
+ }
103
+ | undefined
package/libs/exec.ts ADDED
@@ -0,0 +1,40 @@
1
+ import { spawn } from 'node:child_process'
2
+
3
+ export function exec(command: string, args: string[], stdin?: string) {
4
+ return new Promise<string[]>((resolve, reject) => {
5
+ const child = spawn(command, args, {
6
+ stdio: [],
7
+ })
8
+
9
+ const output: string[] = []
10
+ const errorMessage = `Unable to run command: '${command} ${args.join(' ')}'.`
11
+
12
+ child.stdout.on('data', (data: Buffer) => {
13
+ const lines = data
14
+ .toString()
15
+ .split('\n')
16
+ .filter((line) => line.length > 0)
17
+
18
+ output.push(...lines)
19
+ })
20
+
21
+ child.on('error', (error) => {
22
+ reject(new Error(errorMessage, { cause: error }))
23
+ })
24
+
25
+ child.on('close', (code) => {
26
+ if (code !== 0) {
27
+ reject(new Error(errorMessage))
28
+
29
+ return
30
+ }
31
+
32
+ resolve(output)
33
+ })
34
+
35
+ if (stdin) {
36
+ child.stdin.write(stdin)
37
+ child.stdin.end()
38
+ }
39
+ })
40
+ }
@@ -0,0 +1,8 @@
1
+ import { AstroError } from 'astro/errors'
2
+
3
+ export function throwErrorWithHint(message: string): never {
4
+ throw new AstroError(
5
+ message,
6
+ `See the error report above for more informations.\n\nIf you believe this is a bug, please file an issue at https://github.com/HiDeoo/astro-d2/issues/new/choose`,
7
+ )
8
+ }
package/libs/remark.ts ADDED
@@ -0,0 +1,102 @@
1
+ import path from 'node:path'
2
+
3
+ import type { Code, Html, Parent, Root } from 'mdast'
4
+ import { SKIP, visit } from 'unist-util-visit'
5
+ import type { VFile } from 'vfile'
6
+
7
+ import type { AstroD2Config } from '../config'
8
+
9
+ import { type DiagramAttributes, getAttributes } from './attributes'
10
+ import { generateD2Diagram, type D2Size, getD2DiagramSize } from './d2'
11
+ import { throwErrorWithHint } from './integration'
12
+
13
+ export function remarkAstroD2(config: AstroD2Config) {
14
+ return async function transformer(tree: Root, file: VFile) {
15
+ const d2Nodes: [node: Code, context: VisitorContext][] = []
16
+
17
+ visit(tree, 'code', (node, index, parent) => {
18
+ if (node.lang === 'd2') {
19
+ d2Nodes.push([node, { index, parent }])
20
+ }
21
+
22
+ return SKIP
23
+ })
24
+
25
+ if (d2Nodes.length === 0) {
26
+ return
27
+ }
28
+
29
+ await Promise.all(
30
+ d2Nodes.map(async ([node, { index, parent }], d2Index) => {
31
+ const outputPath = getOutputPaths(config, file, d2Index)
32
+ const attributes = getAttributes(node.meta)
33
+ let size: D2Size = undefined
34
+
35
+ if (config.skipGeneration) {
36
+ size = await getD2DiagramSize(outputPath.fsPath)
37
+ } else {
38
+ try {
39
+ size = await generateD2Diagram(config, attributes, node.value, outputPath.fsPath)
40
+ } catch {
41
+ throwErrorWithHint(
42
+ `Failed to generate the D2 diagram at ${node.position?.start.line ?? 0}:${node.position?.start.column ?? 0}.`,
43
+ )
44
+ }
45
+ }
46
+
47
+ if (parent && index !== undefined) {
48
+ parent.children.splice(index, 1, makHtmlImgNode(attributes, outputPath.imgPath, size))
49
+ }
50
+ }),
51
+ )
52
+ }
53
+ }
54
+
55
+ function makHtmlImgNode(attributes: DiagramAttributes, imgPath: string, size: D2Size): Html {
56
+ const htmlAttributes: Record<string, string> = {
57
+ alt: attributes.title,
58
+ decoding: 'async',
59
+ loading: 'lazy',
60
+ src: imgPath,
61
+ }
62
+
63
+ computeImgSize(htmlAttributes, attributes, size)
64
+
65
+ return {
66
+ type: 'html',
67
+ value: `<img ${Object.entries(htmlAttributes)
68
+ .map(([key, value]) => `${key}="${value}"`)
69
+ .join(' ')} />`,
70
+ }
71
+ }
72
+
73
+ function getOutputPaths(config: AstroD2Config, file: VFile, nodeIndex: number) {
74
+ const relativePath = path.relative(file.cwd, file.path).replace(/^src\/(content|pages)\//, '')
75
+ const parsedRelativePath = path.parse(relativePath)
76
+
77
+ const relativeOutputPath = path.join(parsedRelativePath.dir, `${parsedRelativePath.name}-${nodeIndex}.svg`)
78
+
79
+ return {
80
+ fsPath: path.join(file.cwd, 'public', config.output, relativeOutputPath),
81
+ imgPath: path.posix.join('/', config.output, relativeOutputPath),
82
+ }
83
+ }
84
+
85
+ function computeImgSize(htmlAttributes: Record<string, string>, attributes: DiagramAttributes, size: D2Size) {
86
+ if (attributes.width !== undefined) {
87
+ htmlAttributes['width'] = String(attributes.width)
88
+
89
+ if (size) {
90
+ const aspectRatio = size.height / size.width
91
+ htmlAttributes['height'] = String(Math.round(attributes.width * aspectRatio))
92
+ }
93
+ } else if (size) {
94
+ htmlAttributes['width'] = String(size.width)
95
+ htmlAttributes['height'] = String(size.height)
96
+ }
97
+ }
98
+
99
+ interface VisitorContext {
100
+ index: number | undefined
101
+ parent: Parent | undefined
102
+ }
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "astro-d2",
3
+ "version": "0.1.0",
4
+ "license": "MIT",
5
+ "description": "Astro integration and remark plugin to transform D2 Markdown code blocks into diagrams.",
6
+ "author": "HiDeoo <github@hideoo.dev> (https://hideoo.dev)",
7
+ "type": "module",
8
+ "exports": {
9
+ ".": "./index.ts",
10
+ "./package.json": "./package.json"
11
+ },
12
+ "dependencies": {
13
+ "unist-util-visit": "5.0.0"
14
+ },
15
+ "devDependencies": {
16
+ "@types/mdast": "4.0.3",
17
+ "remark": "15.0.1",
18
+ "vfile": "6.0.1",
19
+ "vitest": "1.2.2"
20
+ },
21
+ "peerDependencies": {
22
+ "astro": ">=4.0.0"
23
+ },
24
+ "engines": {
25
+ "node": ">=18"
26
+ },
27
+ "packageManager": "pnpm@8.15.1",
28
+ "publishConfig": {
29
+ "access": "public"
30
+ },
31
+ "sideEffects": false,
32
+ "keywords": [
33
+ "markdown",
34
+ "d2",
35
+ "diagram",
36
+ "astro-integration"
37
+ ],
38
+ "homepage": "https://github.com/HiDeoo/astro-d2",
39
+ "repository": {
40
+ "type": "git",
41
+ "url": "https://github.com/HiDeoo/astro-d2.git"
42
+ },
43
+ "bugs": "https://github.com/HiDeoo/astro-d2/issues",
44
+ "scripts": {
45
+ "test": "vitest",
46
+ "lint": "prettier -c --cache . && eslint . --cache --max-warnings=0"
47
+ }
48
+ }