@riebeckite/plugin-d2 0.0.31 → 0.0.33

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/README.md CHANGED
@@ -50,6 +50,13 @@ client -> server: request
50
50
  ```
51
51
  ````
52
52
 
53
+ ## Live example
54
+
55
+ ~~~d2
56
+ site: Riebeckite
57
+ content -> build -> deploy
58
+ ~~~
59
+
53
60
  ## Behavior
54
61
 
55
62
  ### Build
@@ -110,7 +117,7 @@ copy if the deployment has no outbound network access.
110
117
 
111
118
  ## Exports
112
119
 
113
- - `d2(options?)` — plugin factory (`d2Plugin` is an alias)
120
+ - `d2(options?)` — plugin factory (`d2` is an alias)
114
121
  - `initD2Diagrams` — client initializer
115
122
  - Types: `D2Options`, `D2ClientOptions`, `D2RenderMode`, `D2Layout`, `D2Theme`,
116
123
  `D2ModuleApi`
package/README_ja.md CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  <!-- Generated from docs/docs/plugins/d2.ja.md. Edit the canonical documentation in docs/docs/plugins and run `pnpm docs:sync`. -->
4
4
 
5
- ` ```d2 ` コードブロックを SVG の図として表示するプラグインです。既定ではビルド時に描画し、描画できなかった図だけをブラウザ側で再試行します。
5
+ ` ```d2 ` コードブロックを SVG の図として表示するプラグインです。既定ではビルド時に描画し、描画できなかった図だけをブラウザで描画し直します。
6
6
 
7
7
  [English](./README.md)
8
8
 
9
- ## 設定する
9
+ ## 設定
10
10
 
11
11
  ```ts
12
12
  import { defineConfig } from "@riebeckite/core";
@@ -44,23 +44,30 @@ client -> server: request
44
44
  ```
45
45
  ````
46
46
 
47
- ## どのように描画されるか
47
+ ## 実行例
48
48
 
49
- ### ビルド時
49
+ ~~~d2
50
+ site: Riebeckite
51
+ content -> build -> deploy
52
+ ~~~
53
+
54
+ ## 描画の仕組み
55
+
56
+ ### ビルド時の描画
50
57
 
51
58
  ` ```d2 ` のコードブロックは `figure.rb-d2` に置き換わります。
52
59
 
53
- - `figcaption.rb-d2__caption`: コードブロックの title、または `# caption:` 行
60
+ - `figcaption.rb-d2__caption`: コードブロックの `title`、または `# caption:` 行
54
61
  - `div.rb-d2__canvas`: 図本体(`role="img"`、キャプションがある場合はそれをラベルに使用)
55
62
  - `details.rb-d2__fallback`: 元の D2 記法を折りたたんで表示
56
63
 
57
64
  `render: "build"` では、D2.js の WebAssembly 版(`@d2lang/d2`)を Node 上で直接実行して静的な SVG を生成します。ブラウザ、ネットワーク接続、D2 本体のインストールはいずれも不要です。
58
65
 
59
- 生成される figure には常に `data-d2`、`data-d2-source`、`data-d2-layout` が付きます。`data-d2` は静的 SVG があれば `rendered`、クライアント側の描画に委ねる場合は `pending` になります。
66
+ 生成される `figure` には常に `data-d2`、`data-d2-source`、`data-d2-layout` が付きます。`data-d2` は静的 SVG があれば `rendered`、クライアント側の描画に委ねる場合は `pending` になります。
60
67
 
61
68
  構文エラーは `invalid-diagram`、描画環境の問題は `renderer-error` として区別して診断します。
62
69
 
63
- ### クライアント側
70
+ ### クライアント側の描画
64
71
 
65
72
  `initD2Diagrams` は保留中の図だけを描画します。D2.js がなければ jsDelivr から ESM として読み込みます。描画に失敗した場合は `data-d2="error"` となり、CSS のプレースホルダー表示に切り替わります。
66
73
 
@@ -70,31 +77,31 @@ client -> server: request
70
77
 
71
78
  | 項目 | 既定値 | 説明 |
72
79
  | --- | --- | --- |
73
- | `render` | `"build"` | `"build"` または `"client"` のいずれで描画するか |
80
+ | `render` | `"build"` | ビルド時とクライアント側のどちらで描画するか |
74
81
  | `theme` | `{ light: 0, dark: 1 }` | D2 のテーマ ID、または明暗別のテーマ ID |
75
82
  | `layout` | `"dagre"` | D2 のレイアウトエンジン(`"dagre"` または `"elk"`) |
76
- | `caption` | `true` | タイトルまたは `# caption:` をキャプションとして表示する |
83
+ | `caption` | `true` | `title` または `# caption:` をキャプションとして表示する |
77
84
  | `fallback` | `true` | 元の D2 記法を `<details>` に残す |
78
- | `className` | `"rb-d2"` | figure に付ける基準クラス |
85
+ | `className` | `"rb-d2"` | `figure` に付ける基準クラス |
79
86
 
80
- `"client"` はビルド時の描画を行いません。
87
+ `"client"` はビルド時に描画せず、ブラウザでのみ描画します。
81
88
 
82
89
  ## オフラインでの利用
83
90
 
84
91
  ビルド時の描画は完全にオフラインです。`@d2lang/d2` が同梱する WebAssembly を `node_modules` から読み込むだけで、外部通信は発生しません。クライアント側の描画(`render: "client"` またはビルド失敗時)は jsDelivr から D2.js を取得します。外部通信を避けたい場合は `moduleUrl` に自前でホストしたモジュールを指定してください。
85
92
 
86
- ## 出力のフック
93
+ ## 出力フック
87
94
 
88
95
  - `globalThis.d2` に読み込み済みの D2.js モジュールを設定すると、CDN からの読み込みを省略できます
89
96
  - `initD2Diagrams()` を直接呼ぶ場合は `api` と `moduleUrl` を渡せます
90
97
 
91
98
  ## 主なエクスポート
92
99
 
93
- - `d2(options?)`: プラグインを作成する(`d2Plugin` は別名)
100
+ - `d2(options?)`: プラグインを作成する(`d2` は別名)
94
101
  - `initD2Diagrams`: クライアント側の描画を初期化する
95
102
  - 型: `D2Options`、`D2ClientOptions`、`D2RenderMode`、`D2Layout`、`D2Theme`、`D2ModuleApi`
96
103
 
97
- ## 制限
104
+ ## 制限事項
98
105
 
99
106
  - 図ごとに新しい D2 ワーカーを起動するため、大量の図を含むビルドでは WASM の起動コストが繰り返し発生します
100
107
  - `layout: "elk"` は D2 の ELK エンジンを使うため `dagre` より遅くなります
package/dist/index.d.ts CHANGED
@@ -1,5 +1,3 @@
1
1
  import type { D2Options } from "./src/types.js";
2
2
  export type { D2ClientOptions, D2Layout, D2ModuleApi, D2Options, D2RenderMode, D2Theme, } from "./src/types.js";
3
3
  export declare function d2(options?: D2Options): import("@riebeckite/core").RiebeckitePlugin<D2Options>;
4
- /** Alias matching the `*Plugin` convention used by several packages. */
5
- export declare const d2Plugin: typeof d2;
package/dist/index.js CHANGED
@@ -14,52 +14,23 @@ var __export = (target, all) => {
14
14
  };
15
15
 
16
16
  // src/hast.ts
17
- function visitElements(node, visitor, parent, index) {
18
- if (isElementNode(node)) visitor(node, parent, index);
19
- for (const [childIndex, child] of [...getChildren(node)].entries()) {
20
- visitElements(
21
- child,
22
- visitor,
23
- hasChildren(node) ? node : parent,
24
- childIndex
25
- );
26
- }
27
- }
28
- function element(tagName, properties = {}, children = []) {
29
- return { type: "element", tagName, properties, children };
30
- }
31
- function text(value) {
32
- return { type: "text", value };
33
- }
34
- function getTextContent(node) {
35
- if (node.type === "text" && "value" in node && typeof node.value === "string") {
36
- return node.value;
37
- }
38
- return getChildren(node).map(getTextContent).join("");
39
- }
40
- function getStringProperty(node, key) {
41
- const value = node.properties?.[key];
42
- if (typeof value === "string") return value;
43
- if (Array.isArray(value)) return value.join(" ");
44
- return null;
45
- }
46
- function hasClass(node, className) {
47
- const value = node.properties?.className;
48
- const classes = Array.isArray(value) ? value.map(String) : typeof value === "string" ? value.split(/\s+/) : [];
49
- return classes.includes(className);
50
- }
51
- function isElementNode(node) {
52
- return node.type === "element";
53
- }
54
- function hasChildren(node) {
55
- return Array.isArray(node.children);
56
- }
57
- function getChildren(node) {
58
- const children = node.children;
59
- return Array.isArray(children) ? children : [];
60
- }
17
+ import {
18
+ element as sharedElement,
19
+ getStringProperty as sharedGetStringProperty,
20
+ getTextContent as sharedGetTextContent,
21
+ hasClass as sharedHasClass,
22
+ text as sharedText,
23
+ visitElements as sharedVisitElements
24
+ } from "@riebeckite/diagram";
25
+ var element, text, getTextContent, getStringProperty, hasClass, visitElements;
61
26
  var init_hast = __esm({
62
27
  "src/hast.ts"() {
28
+ element = sharedElement;
29
+ text = sharedText;
30
+ getTextContent = sharedGetTextContent;
31
+ getStringProperty = sharedGetStringProperty;
32
+ hasClass = sharedHasClass;
33
+ visitElements = sharedVisitElements;
63
34
  }
64
35
  });
65
36
 
@@ -271,10 +242,14 @@ function buildFigure(input) {
271
242
  );
272
243
  if (input.fallback) {
273
244
  children.push(
274
- element("details", { className: `${base}__fallback` }, [
275
- element("summary", {}, [text("Diagram source")]),
276
- element("pre", {}, [element("code", {}, [text(input.source)])])
277
- ])
245
+ element(
246
+ "details",
247
+ { className: `${base}__fallback rb-source-fallback` },
248
+ [
249
+ element("summary", {}, [text("Diagram source")]),
250
+ element("pre", {}, [element("code", {}, [text(input.source)])])
251
+ ]
252
+ )
278
253
  );
279
254
  }
280
255
  return element(
@@ -394,7 +369,6 @@ function d2(options = {}) {
394
369
  ]
395
370
  });
396
371
  }
397
- var d2Plugin = d2;
398
372
  function validateD2Options(options) {
399
373
  if (!options) return [];
400
374
  const issues = [];
@@ -434,6 +408,5 @@ function rehypeD2Lazy(options = {}) {
434
408
  };
435
409
  }
436
410
  export {
437
- d2,
438
- d2Plugin
411
+ d2
439
412
  };
@@ -1,7 +1,7 @@
1
1
  import type { ElementNode, HastNode, ParentNode, TextNode } from "./types.js";
2
- export declare function visitElements(node: HastNode, visitor: (node: ElementNode, parent?: ParentNode, index?: number) => void, parent?: ParentNode, index?: number): void;
3
- export declare function element(tagName: string, properties?: Record<string, unknown>, children?: HastNode[]): ElementNode;
4
- export declare function text(value: string): TextNode;
5
- export declare function getTextContent(node: HastNode): string;
6
- export declare function getStringProperty(node: ElementNode, key: string): string | null;
7
- export declare function hasClass(node: ElementNode, className: string): boolean;
2
+ export declare const element: (tagName: string, properties?: Record<string, unknown>, children?: HastNode[]) => ElementNode;
3
+ export declare const text: (value: string) => TextNode;
4
+ export declare const getTextContent: (node: HastNode) => string;
5
+ export declare const getStringProperty: (node: ElementNode, key: string) => string | null;
6
+ export declare const hasClass: (node: ElementNode, className: string) => boolean;
7
+ export declare const visitElements: (node: HastNode, visitor: (node: ElementNode, parent?: ParentNode, index?: number) => void, parent?: ParentNode, index?: number) => void;
@@ -63,7 +63,7 @@ export type TextNode = {
63
63
  type: "text";
64
64
  value: string;
65
65
  };
66
- export type RawNode = {
66
+ type RawNode = {
67
67
  type: "raw";
68
68
  value: string;
69
69
  };
@@ -71,3 +71,4 @@ export type HastNode = ElementNode | TextNode | RawNode | {
71
71
  type: string;
72
72
  [key: string]: unknown;
73
73
  };
74
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@riebeckite/plugin-d2",
3
- "version": "0.0.31",
3
+ "version": "0.0.33",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -22,7 +22,8 @@
22
22
  },
23
23
  "dependencies": {
24
24
  "@d2lang/d2": "^0.1.34",
25
- "@riebeckite/core": "0.0.31"
25
+ "@riebeckite/core": "0.0.33",
26
+ "@riebeckite/diagram": "0.0.33"
26
27
  },
27
28
  "publishConfig": {
28
29
  "access": "public"
package/style.css CHANGED
@@ -32,26 +32,6 @@
32
32
  height: auto;
33
33
  }
34
34
 
35
- .rb-d2__fallback {
36
- border-top: 1px solid var(--rb-d2-rule);
37
- color: var(--rb-d2-muted);
38
- font-size: 0.8125rem;
39
- }
40
-
41
- .rb-d2__fallback summary {
42
- cursor: pointer;
43
- padding: 0.5rem 0.75rem;
44
- }
45
-
46
- .rb-d2__fallback pre {
47
- overflow: auto;
48
- margin: 0 1rem;
49
- padding: 0.75rem;
50
- border-top: 1px solid var(--rb-d2-rule);
51
- color: var(--rb-d2-ink);
52
- background: color-mix(in srgb, var(--rb-d2-ink) 6%, transparent);
53
- }
54
-
55
35
  .rb-d2[data-d2="error"] .rb-d2__canvas::before,
56
36
  .rb-d2[data-d2="pending"] .rb-d2__canvas:empty::before {
57
37
  content: "Diagram could not be rendered.";