mdat-plugin-tldraw 1.0.9 → 2.0.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/dist/index.d.ts CHANGED
@@ -1,7 +1,9 @@
1
+ import * as _$remark_mdat0 from "remark-mdat";
2
+
1
3
  //#region src/index.d.ts
2
4
  declare const _default: {
3
5
  tldraw: {
4
- content(options?: unknown): Promise<string>;
6
+ content(options?: unknown, context?: _$remark_mdat0.RuleContext | undefined): Promise<string>;
5
7
  };
6
8
  };
7
9
  //#endregion
package/dist/index.js CHANGED
@@ -1,25 +1,26 @@
1
1
  import { tldrawToImage } from "@kitschpatrol/tldraw-cli";
2
- import { loadConfig } from "mdat";
3
2
  import crypto from "node:crypto";
4
3
  import fs from "node:fs/promises";
5
4
  import path from "node:path";
6
- import { isFile } from "path-type";
5
+ import { isDirectory, isFile } from "path-type";
7
6
  import { z } from "zod";
8
7
  //#region src/index.ts
9
- var src_default = { tldraw: { async content(options) {
10
- const { alt = "tldraw diagram", src } = z.object({
8
+ var src_default = { tldraw: { async content(options, context) {
9
+ const { alt = "tldraw diagram", dest: destination = "./assets", src } = z.object({
11
10
  alt: z.string().optional(),
11
+ dest: z.string().optional(),
12
12
  src: z.string()
13
13
  }).parse(options);
14
- const config = await loadConfig();
15
- const { assetsPath } = config;
14
+ const markdownFilePath = context?.filePath ?? process.cwd();
15
+ const assetsPath = path.resolve(path.dirname(markdownFilePath), destination);
16
+ if (!await isDirectory(assetsPath)) throw new Error("Destination must be a directory`");
16
17
  await fs.mkdir(assetsPath, { recursive: true });
17
18
  let sourceHash = await isFile(src) ? await getFileHash(src) : void 0;
18
19
  if (sourceHash !== void 0) {
19
20
  const fileName = path.basename(src, path.extname(src));
20
21
  const possibleLightPath = path.join(assetsPath, `${fileName}-${sourceHash}-light.svg`);
21
22
  const possibleDarkPath = path.join(assetsPath, `${fileName}-${sourceHash}-dark.svg`);
22
- if (await isFile(possibleLightPath) && await isFile(possibleDarkPath)) return getPictureElement(config, possibleLightPath, possibleDarkPath, alt);
23
+ if (await isFile(possibleLightPath) && await isFile(possibleDarkPath)) return getPictureElement(markdownFilePath, possibleLightPath, possibleDarkPath, alt);
23
24
  }
24
25
  const [lightPath] = await tldrawToImage(src, {
25
26
  dark: false,
@@ -46,12 +47,10 @@ var src_default = { tldraw: { async content(options) {
46
47
  const assetName = path.basename(asset);
47
48
  if (assetName !== darkPathHashedName && assetName !== lightPathHashedName && assetName.startsWith(filePrefix) && assetName.endsWith(".svg")) await fs.rm(path.join(assetsPath, assetName));
48
49
  }
49
- return getPictureElement(config, lightPathHashed, darkPathHashed, alt);
50
+ return getPictureElement(markdownFilePath, lightPathHashed, darkPathHashed, alt);
50
51
  } } };
51
- function getPictureElement(config, lightPath, darkPath, alt) {
52
- const { packageFile } = config;
53
- if (packageFile === void 0) throw new Error("No package file found");
54
- const basePath = path.dirname(packageFile);
52
+ function getPictureElement(markdownFilePath, lightPath, darkPath, alt) {
53
+ const basePath = path.dirname(markdownFilePath);
55
54
  const relativeLightPath = path.relative(basePath, lightPath);
56
55
  return `<picture>
57
56
  <source media="(prefers-color-scheme: dark)" srcset="${path.relative(basePath, darkPath)}">
@@ -65,8 +64,9 @@ async function getFileHash(filePath) {
65
64
  hash.update(fileBuffer);
66
65
  return hash.digest("hex").slice(0, 8);
67
66
  }
67
+ const FILE_EXTENSION_REGEX = /\.[^./]+$/;
68
68
  function stripExtension(file) {
69
- return file.replace(/\.[^./]+$/, "");
69
+ return file.replace(FILE_EXTENSION_REGEX, "");
70
70
  }
71
71
  //#endregion
72
72
  export { src_default as default };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mdat-plugin-tldraw",
3
- "version": "1.0.9",
3
+ "version": "2.0.0",
4
4
  "description": "Mdat plugin to embed tldraw diagrams in Markdown files.",
5
5
  "keywords": [
6
6
  "markdown",
@@ -37,20 +37,21 @@
37
37
  "dependencies": {
38
38
  "@kitschpatrol/tldraw-cli": "^5.0.13",
39
39
  "path-type": "^6.0.0",
40
- "type-fest": "^5.5.0",
41
- "zod": "^3.25.76"
40
+ "zod": "^4.3.6"
42
41
  },
43
42
  "devDependencies": {
44
- "@kitschpatrol/shared-config": "^6.1.0",
43
+ "@kitschpatrol/shared-config": "^7.0.0",
45
44
  "@types/node": "~20.19.37",
46
45
  "bumpp": "^11.0.1",
47
- "mdat": "^1.4.2",
46
+ "mdat": "^2.0.0",
47
+ "remark-mdat": "^2.0.0",
48
48
  "tsdown": "^0.21.7",
49
49
  "typescript": "~5.9.3",
50
50
  "vitest": "^4.1.2"
51
51
  },
52
52
  "peerDependencies": {
53
- "mdat": "^1.1.0"
53
+ "mdat": "^2.0.0",
54
+ "remark-mdat": "^2.0.0"
54
55
  },
55
56
  "engines": {
56
57
  "node": ">=20.19.0"
package/readme.md CHANGED
@@ -1,5 +1,3 @@
1
- <!--+ Warning: Content inside HTML comment blocks was generated by mdat and may be overwritten. +-->
2
-
3
1
  <!-- title -->
4
2
 
5
3
  # mdat-plugin-tldraw
@@ -10,6 +8,7 @@
10
8
 
11
9
  [![NPM Package mdat-plugin-tldraw](https://img.shields.io/npm/v/mdat-plugin-tldraw.svg)](https://npmjs.com/package/mdat-plugin-tldraw)
12
10
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
11
+ [![CI](https://github.com/kitschpatrol/mdat-plugin-tldraw/actions/workflows/ci.yml/badge.svg)](https://github.com/kitschpatrol/mdat-plugin-tldraw/actions/workflows/ci.yml)
13
12
 
14
13
  <!-- /badges -->
15
14
 
@@ -65,13 +64,19 @@ export default {
65
64
  Assuming you have a `sketch.tldr` file in the root of your project, you can embed it in your Markdown file this:
66
65
 
67
66
  ```markdown
68
- <!-- tldraw { src: "./sketch.tldr" } -->
67
+ <!-- tldraw({ src: "./sketch.tldr" }) -->
69
68
  ```
70
69
 
71
70
  Then run the `mdat` CLI command on your Markdown file to expand the rule and embed the diagram:
72
71
 
72
+ ```sh
73
+ mdat
74
+ ```
75
+
76
+ To generate:
77
+
73
78
  ```markdown
74
- <!-- tldraw { src: "./sketch.tldr" } -->
79
+ <!-- tldraw({ src: "./sketch.tldr" }) -->
75
80
 
76
81
  <picture>
77
82
  <source media="(prefers-color-scheme: dark)" srcset="assets/sketch-63a3366c-dark.svg">
@@ -82,6 +87,40 @@ Then run the `mdat` CLI command on your Markdown file to expand the rule and emb
82
87
  <!-- /tldraw -->
83
88
  ```
84
89
 
90
+ By default, the generated assets are saved to an `./assets` folder adjacent the Markdown file.
91
+
92
+ You can customize this by passing a `dest` argument relative to the Markdown file, e.g.:
93
+
94
+ ```markdown
95
+ <!-- tldraw({ src: "./sketch.tldr", dest: "./sketches" }) -->
96
+ ```
97
+
98
+ ## Example Output
99
+
100
+ Give:
101
+
102
+ ```md
103
+ <!-- tldraw({ src: "./test/assets/tldraw-sketch.tldr" }) -->
104
+ ```
105
+
106
+ Receive:
107
+
108
+ <!-- tldraw({ src: "./test/assets/tldraw-sketch.tldr" }) -->
109
+
110
+ <picture>
111
+ <source media="(prefers-color-scheme: dark)" srcset="assets/tldraw-sketch-132cbdb8-dark.svg">
112
+ <source media="(prefers-color-scheme: light)" srcset="assets/tldraw-sketch-132cbdb8-light.svg">
113
+ <img alt="tldraw diagram" src="assets/tldraw-sketch-132cbdb8-light.svg">
114
+ </picture>
115
+
116
+ <!-- /tldraw -->
117
+
118
+ ## Compatibility
119
+
120
+ This version of the plugin is only compatible with `mdat` 2.x.
121
+
122
+ Older `mdat` 1.x compatible versions [remain available](https://www.npmjs.com/package/mdat-plugin-tldraw/v/1.0.9).
123
+
85
124
  ## Maintainers
86
125
 
87
126
  [@kitschpatrol](https://github.com/kitschpatrol)