@kubohiroya/turbowarp-markdown 0.5.0 → 0.6.1

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.ja.md CHANGED
@@ -28,6 +28,16 @@ TurboWarp 上で Markdown を immutable な構造化フラグメントとして
28
28
 
29
29
  `turbowarp-http-server` とは npm パッケージ依存では結合しません。Markdown 拡張で作ったフラグメントを `render Markdown` で文字列化し、HTTP レスポンス本文として渡してください。content type は `text/markdown; charset=utf-8` を明示する想定です。
30
30
 
31
+ ## Document Dataで解析・読み取りする
32
+
33
+ `turbowarp-markdown:v1:`の値は組み立て用の形式で、読み取るための木ではありません。箇条書きの項目やinlineの中身が入れ子の`sequence`に入ることがあり、中身が1つのときと複数のときで形も変わります。組み立てた文書の一部を読み取ったり置き換えたり、構造を順にたどったりするときは、`render Markdown`で文字列にして[TurboWarp Document Data](https://github.com/kubohiroya/turbowarp-document-data)へ渡してください。
34
+
35
+ ```text
36
+ parse Markdown (render Markdown (fragment)) as document [doc]
37
+ ```
38
+
39
+ Document Dataは解析したMarkdownをmdastの木として保持し、子は常に平らな`children`配列に並びます。位置を指定してtextを読み取り・置き換えるblockがあります。2つの拡張はnpmパッケージ依存を持たず、描画した文字列だけを受け渡すため、この拡張はサンドボックス内のまま動作します。Document Data自体はサンドボックスなしで動作します。
40
+
31
41
  ## 開発
32
42
 
33
43
  ```bash
package/README.md CHANGED
@@ -44,7 +44,7 @@ pnpm install --frozen-lockfile
44
44
  The package is version-pinned when used from npm:
45
45
 
46
46
  ```bash
47
- pnpm add --save-exact @kubohiroya/turbowarp-markdown@0.5.0
47
+ pnpm add --save-exact @kubohiroya/turbowarp-markdown@0.6.1
48
48
  ```
49
49
 
50
50
  ## Quick Start
@@ -266,6 +266,16 @@ Use `render Markdown [FRAGMENT]` when validation is not needed. Use `render Mark
266
266
 
267
267
  Validation is intentionally lightweight. It catches common mistakes such as a `list item` used outside a list and block fragments nested inside inline-only fragments. It also warns about empty documents, empty lists, empty headings, and links without visible content or destinations.
268
268
 
269
+ ## Parse and read with Document Data
270
+
271
+ The `turbowarp-markdown:v1:` values are a build format, not a tree meant for reading: list items and inline content can sit inside nested `sequence` fragments, and a fragment with one child has a different shape from one with several. To read or replace parts of a document you built, or to walk its structure, render it and pass the string to [TurboWarp Document Data](https://github.com/kubohiroya/turbowarp-document-data):
272
+
273
+ ```text
274
+ parse Markdown (render Markdown (fragment)) as document [doc]
275
+ ```
276
+
277
+ Document Data keeps the parsed Markdown as an mdast tree whose children are always flat `children` arrays, and its blocks read and replace text by location. The two extensions share no package dependency and exchange only the rendered string, so this extension keeps running in the sandbox; Document Data itself runs without the sandbox.
278
+
269
279
  ## Development
270
280
 
271
281
  ```bash
@@ -20,7 +20,7 @@ var extensionConfig = {
20
20
  blockIconURI: "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA0OCA0OCI+PHJlY3QgeD0iNiIgeT0iOCIgd2lkdGg9IjM2IiBoZWlnaHQ9IjMyIiByeD0iNCIgZmlsbD0iIzJGNjM4NiIvPjxwYXRoIGQ9Ik0xMyAzMFYxOGw1IDcgNS03djEyTTI3LjUgMjB2MTBNMjcuNSAyMGgyLjVhNSA1IDAgMCAxIDAgMTBoLTIuNSIgZmlsbD0ibm9uZSIgc3Ryb2tlPSIjZmZmZmZmIiBzdHJva2Utd2lkdGg9IjMiIHN0cm9rZS1saW5lY2FwPSJyb3VuZCIgc3Ryb2tlLWxpbmVqb2luPSJyb3VuZCIvPjwvc3ZnPg=="
21
21
  };
22
22
  //#endregion
23
- //#region node_modules/.pnpm/@kubohiroya+turbowarp-extension-data-editor@0.5.0_react@18.3.1/node_modules/@kubohiroya/turbowarp-extension-data-editor/dist/fixed-input-schema.js
23
+ //#region node_modules/.pnpm/@kubohiroya+turbowarp-extension-data-editor@0.8.1_react@18.3.1/node_modules/@kubohiroya/turbowarp-extension-data-editor/dist/fixed-input-schema.js
24
24
  var numberToken = /^-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?$/u;
25
25
  var invalidNumber = Object.freeze({
26
26
  kind: "invalid",
@@ -183,44 +183,71 @@ var fixedInputEditors = [...new Set(fields.map((f) => f.opcode))].map((opcode) =
183
183
  settings: null
184
184
  })
185
185
  }));
186
- /** Registration follows the advertised opcode membership and the optional host capability. */
186
+ /**
187
+ * Registration follows the advertised opcode membership and the optional host capability.
188
+ * Loading a project disposes the runtime, which releases the registrations, but the extension
189
+ * stays loaded for the next project: they are registered again once that project has loaded.
190
+ * Only the owner's dispose is final.
191
+ */
187
192
  function fixedInputRegistration(scratch) {
188
193
  const host = scratch ?? {};
189
194
  let active;
190
195
  let membership = "";
191
196
  const removers = [];
192
197
  let stopped = false;
198
+ let released = false;
199
+ let requested = [];
193
200
  const clear = () => {
194
- host.vm?.runtime?.off?.("RUNTIME_DISPOSED", shutdown);
201
+ host.vm?.runtime?.off?.("RUNTIME_DISPOSED", release);
202
+ host.vm?.runtime?.off?.("PROJECT_LOADED", restore);
195
203
  for (const remove of removers.splice(0).reverse()) remove();
196
204
  active = void 0;
197
205
  membership = "";
198
206
  };
207
+ const register = (visibleOpcodes) => {
208
+ const api = host.gui?.blockEditors;
209
+ const definitions = fixedInputEditors.filter((definition) => definition.opcodes.every((opcode) => visibleOpcodes.includes(opcode)));
210
+ const key = definitions.map((definition) => definition.editorId).join(",");
211
+ if (api === active && membership === key) return;
212
+ clear();
213
+ if (api?.apiVersion !== 1 || typeof api.registerFixedInputEditor !== "function" || !definitions.length) return;
214
+ try {
215
+ for (const definition of definitions) {
216
+ const remove = api.registerFixedInputEditor.call(api, definition);
217
+ if (typeof remove !== "function") throw new TypeError("Invalid fixed input cleanup.");
218
+ removers.push(remove);
219
+ }
220
+ active = api;
221
+ membership = key;
222
+ host.vm?.runtime?.on?.("RUNTIME_DISPOSED", release);
223
+ } catch (error) {
224
+ clear();
225
+ throw error;
226
+ }
227
+ };
228
+ /** The runtime of this project is gone; wait for the next project before registering again. */
229
+ const release = () => {
230
+ clear();
231
+ released = true;
232
+ host.vm?.runtime?.on?.("PROJECT_LOADED", restore);
233
+ };
234
+ const restore = () => {
235
+ if (stopped || !released) return;
236
+ released = false;
237
+ try {
238
+ register(requested);
239
+ } catch {}
240
+ };
199
241
  const shutdown = () => {
200
242
  stopped = true;
243
+ released = false;
201
244
  clear();
202
245
  };
203
246
  return {
204
247
  ensure(visibleOpcodes) {
205
- const api = host.gui?.blockEditors;
206
- const definitions = fixedInputEditors.filter((definition) => definition.opcodes.every((opcode) => visibleOpcodes.includes(opcode)));
207
- const key = definitions.map((definition) => definition.editorId).join(",");
208
- if (stopped || api === active && membership === key) return;
209
- clear();
210
- if (api?.apiVersion !== 1 || typeof api.registerFixedInputEditor !== "function" || !definitions.length) return;
211
- try {
212
- for (const definition of definitions) {
213
- const remove = api.registerFixedInputEditor.call(api, definition);
214
- if (typeof remove !== "function") throw new TypeError("Invalid fixed input cleanup.");
215
- removers.push(remove);
216
- }
217
- active = api;
218
- membership = key;
219
- host.vm?.runtime?.on?.("RUNTIME_DISPOSED", shutdown);
220
- } catch (error) {
221
- clear();
222
- throw error;
223
- }
248
+ requested = visibleOpcodes;
249
+ if (stopped || released) return;
250
+ register(visibleOpcodes);
224
251
  },
225
252
  dispose: shutdown
226
253
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubohiroya/turbowarp-markdown",
3
- "version": "0.5.0",
3
+ "version": "0.6.1",
4
4
  "description": "An immutable Markdown builder extension for TurboWarp.",
5
5
  "author": {
6
6
  "name": "Hiroya Kubo",
@@ -68,14 +68,14 @@
68
68
  "devDependencies": {
69
69
  "@eslint/js": "^9.39.5",
70
70
  "@kubohiroya/turbowarp-extension-manifest": "^0.5.0",
71
- "@kubohiroya/vite-plugin-turbowarp-extension": "^0.10.0",
71
+ "@kubohiroya/vite-plugin-turbowarp-extension": "^0.11.0",
72
72
  "@types/node": "^24.13.3",
73
73
  "eslint": "^9.39.5",
74
74
  "typescript": "^5.9.3",
75
75
  "typescript-eslint": "^8.66.0",
76
76
  "vite": "^8.2.2",
77
77
  "vitest": "^4.1.10",
78
- "@kubohiroya/turbowarp-extension-data-editor": "^0.5.0"
78
+ "@kubohiroya/turbowarp-extension-data-editor": "^0.8.1"
79
79
  },
80
80
  "gitHead": "a02baff13a8da2f82f8807d33fdc6c50e8505cb4",
81
81
  "x-turbowarp-source": {
@@ -75,38 +75,63 @@ interface OptionalFixedHost {
75
75
  gui?: {blockEditors?: {apiVersion?: number; registerFixedInputEditor?: (definition: FixedInputEditorOptions) => () => void}};
76
76
  vm?: {runtime?: {on?: (event: string, listener: () => void) => void; off?: (event: string, listener: () => void) => void}};
77
77
  }
78
- /** Registration follows the advertised opcode membership and the optional host capability. */
78
+ /**
79
+ * Registration follows the advertised opcode membership and the optional host capability.
80
+ * Loading a project disposes the runtime, which releases the registrations, but the extension
81
+ * stays loaded for the next project: they are registered again once that project has loaded.
82
+ * Only the owner's dispose is final.
83
+ */
79
84
  export function fixedInputRegistration(scratch: unknown) {
80
85
  const host = (scratch ?? {}) as OptionalFixedHost;
81
86
  let active: unknown;
82
87
  let membership = '';
83
88
  const removers: (() => void)[] = [];
84
89
  let stopped = false;
90
+ let released = false;
91
+ let requested: readonly string[] = [];
85
92
  const clear = () => {
86
- host.vm?.runtime?.off?.('RUNTIME_DISPOSED', shutdown);
93
+ host.vm?.runtime?.off?.('RUNTIME_DISPOSED', release);
94
+ host.vm?.runtime?.off?.('PROJECT_LOADED', restore);
87
95
  for (const remove of removers.splice(0).reverse()) remove();
88
96
  active = undefined;
89
97
  membership = '';
90
98
  };
91
- const shutdown = () => {stopped = true; clear();};
99
+ const register = (visibleOpcodes: readonly string[]) => {
100
+ const api = host.gui?.blockEditors;
101
+ const definitions = fixedInputEditors.filter(definition => definition.opcodes.every(opcode => visibleOpcodes.includes(opcode)));
102
+ const key = definitions.map(definition => definition.editorId).join(',');
103
+ if (api === active && membership === key) return;
104
+ clear();
105
+ if (api?.apiVersion !== 1 || typeof api.registerFixedInputEditor !== 'function' || !definitions.length) return;
106
+ try {
107
+ for (const definition of definitions) {
108
+ const remove = api.registerFixedInputEditor.call(api, definition);
109
+ if (typeof remove !== 'function') throw new TypeError('Invalid fixed input cleanup.');
110
+ removers.push(remove);
111
+ }
112
+ active = api;
113
+ membership = key;
114
+ host.vm?.runtime?.on?.('RUNTIME_DISPOSED', release);
115
+ } catch (error) {clear(); throw error;}
116
+ };
117
+ /** The runtime of this project is gone; wait for the next project before registering again. */
118
+ const release = () => {
119
+ clear();
120
+ released = true;
121
+ host.vm?.runtime?.on?.('PROJECT_LOADED', restore);
122
+ };
123
+ const restore = () => {
124
+ if (stopped || !released) return;
125
+ released = false;
126
+ // A refusal here must not break the VM's event dispatch; the next getInfo() retries.
127
+ try {register(requested);} catch {/* retried by the next ensure() */}
128
+ };
129
+ const shutdown = () => {stopped = true; released = false; clear();};
92
130
  return {
93
131
  ensure(visibleOpcodes: readonly string[]) {
94
- const api = host.gui?.blockEditors;
95
- const definitions = fixedInputEditors.filter(definition => definition.opcodes.every(opcode => visibleOpcodes.includes(opcode)));
96
- const key = definitions.map(definition => definition.editorId).join(',');
97
- if (stopped || (api === active && membership === key)) return;
98
- clear();
99
- if (api?.apiVersion !== 1 || typeof api.registerFixedInputEditor !== 'function' || !definitions.length) return;
100
- try {
101
- for (const definition of definitions) {
102
- const remove = api.registerFixedInputEditor.call(api, definition);
103
- if (typeof remove !== 'function') throw new TypeError('Invalid fixed input cleanup.');
104
- removers.push(remove);
105
- }
106
- active = api;
107
- membership = key;
108
- host.vm?.runtime?.on?.('RUNTIME_DISPOSED', shutdown);
109
- } catch (error) {clear(); throw error;}
132
+ requested = visibleOpcodes;
133
+ if (stopped || released) return;
134
+ register(visibleOpcodes);
110
135
  },
111
136
  dispose: shutdown
112
137
  };