@agentshouse/mdmodel 0.1.0-cbb41ab5ebbd2 → 0.1.0-cbd3c46de90bf

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.
Files changed (90) hide show
  1. package/API.md +27 -93
  2. package/README.md +0 -1
  3. package/dist/definition.d.ts.map +1 -1
  4. package/dist/definition.js +26 -4
  5. package/dist/document.d.ts +1 -0
  6. package/dist/document.d.ts.map +1 -1
  7. package/dist/document.js +21 -12
  8. package/dist/house-model.d.ts +66 -16
  9. package/dist/house-model.d.ts.map +1 -1
  10. package/dist/house-model.js +43 -11
  11. package/dist/index.d.ts +7 -9
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +5 -7
  14. package/dist/markdown.d.ts +0 -4
  15. package/dist/markdown.d.ts.map +1 -1
  16. package/dist/markdown.js +1 -34
  17. package/dist/paths.d.ts +1 -0
  18. package/dist/paths.d.ts.map +1 -1
  19. package/dist/paths.js +20 -4
  20. package/dist/query/language.d.ts +2 -0
  21. package/dist/query/language.d.ts.map +1 -1
  22. package/dist/query/language.js +3 -2
  23. package/dist/query/match.d.ts.map +1 -1
  24. package/dist/query/match.js +3 -24
  25. package/dist/query/rows.d.ts +4 -0
  26. package/dist/query/rows.d.ts.map +1 -1
  27. package/dist/query/rows.js +21 -0
  28. package/dist/query/sql.d.ts +2 -0
  29. package/dist/query/sql.d.ts.map +1 -1
  30. package/dist/query/sql.js +11 -0
  31. package/dist/query/walk.d.ts +6 -0
  32. package/dist/query/walk.d.ts.map +1 -1
  33. package/dist/query/walk.js +12 -1
  34. package/dist/reserved.d.ts +1 -0
  35. package/dist/reserved.d.ts.map +1 -1
  36. package/dist/reserved.js +15 -4
  37. package/dist/room-types.d.ts +2 -1
  38. package/dist/room-types.d.ts.map +1 -1
  39. package/dist/room-types.js +8 -0
  40. package/dist/types.d.ts +2 -21
  41. package/dist/types.d.ts.map +1 -1
  42. package/dist/validate.d.ts.map +1 -1
  43. package/dist/validate.js +7 -6
  44. package/dist/view.d.ts +5 -0
  45. package/dist/view.d.ts.map +1 -0
  46. package/dist/view.js +27 -0
  47. package/dist/yaml.d.ts +3 -1
  48. package/dist/yaml.d.ts.map +1 -1
  49. package/dist/yaml.js +153 -32
  50. package/fixtures/README.md +37 -54
  51. package/fixtures/cases/block-scalar-frontmatter/context.json +1 -0
  52. package/fixtures/cases/block-scalar-frontmatter/expected.json +1 -0
  53. package/fixtures/cases/block-scalar-frontmatter/library/notes/literal.md +17 -0
  54. package/fixtures/cases/block-scalar-frontmatter/library/skills/notes/SKILL.md +10 -0
  55. package/fixtures/cases/frontmatter-construct/context.json +1 -0
  56. package/fixtures/cases/frontmatter-construct/expected.json +7 -0
  57. package/fixtures/cases/frontmatter-construct/library/note.md +6 -0
  58. package/fixtures/cases/reference-to-any-file-name/context.json +1 -0
  59. package/fixtures/cases/reference-to-any-file-name/expected.json +1 -0
  60. package/fixtures/cases/reference-to-any-file-name/library/people/100%.md +4 -0
  61. package/fixtures/cases/reference-to-any-file-name/library/people/C#.md +4 -0
  62. package/fixtures/cases/reference-to-any-file-name/library/people/My notes.md +4 -0
  63. package/fixtures/cases/reference-to-any-file-name/library/people/[draft].md +4 -0
  64. package/fixtures/cases/reference-to-any-file-name/library/people/c (d).md +4 -0
  65. package/fixtures/cases/reference-to-any-file-name/library/people/caf/303/251.md +4 -0
  66. package/fixtures/cases/reference-to-any-file-name/library/people//320/236/321/202/321/207/321/221/321/202.md +4 -0
  67. package/fixtures/cases/reference-to-any-file-name/library/tasks/bracket.md +9 -0
  68. package/fixtures/cases/reference-to-any-file-name/library/tasks/cyrillic.md +9 -0
  69. package/fixtures/cases/reference-to-any-file-name/library/tasks/decomposed.md +9 -0
  70. package/fixtures/cases/reference-to-any-file-name/library/tasks/hash.md +9 -0
  71. package/fixtures/cases/reference-to-any-file-name/library/tasks/parenthesis.md +9 -0
  72. package/fixtures/cases/reference-to-any-file-name/library/tasks/percent.md +9 -0
  73. package/fixtures/cases/reference-to-any-file-name/library/tasks/spaced.md +9 -0
  74. package/fixtures/cases/reference-to-any-file-name/schema.yaml +10 -0
  75. package/fixtures/cases/referenced-missing-non-latin-file/context.json +1 -0
  76. package/fixtures/cases/referenced-missing-non-latin-file/expected.json +6 -0
  77. package/fixtures/cases/referenced-missing-non-latin-file/library/tasks/a.md +9 -0
  78. package/fixtures/cases/referenced-missing-non-latin-file/library/tasks/b.md +9 -0
  79. package/fixtures/cases/referenced-missing-non-latin-file/schema.yaml +10 -0
  80. package/fixtures/forms.json +24 -0
  81. package/fixtures/queries.json +204 -1
  82. package/package.json +1 -1
  83. package/dist/divide.d.ts +0 -3
  84. package/dist/divide.d.ts.map +0 -1
  85. package/dist/divide.js +0 -321
  86. package/dist/project.d.ts +0 -22
  87. package/dist/project.d.ts.map +0 -1
  88. package/dist/project.js +0 -101
  89. package/fixtures/division.json +0 -454
  90. package/fixtures/projection.json +0 -358
@@ -0,0 +1,9 @@
1
+ ---
2
+ type: task
3
+ ---
4
+ - **title**: Spaced
5
+ - **owner**: [Spaced](<../people/My notes.md>)
6
+
7
+ ## Description
8
+
9
+ See [them](../people/My%20notes.md).
@@ -0,0 +1,10 @@
1
+ person:
2
+ properties:
3
+ name: { type: string }
4
+ required: [ name ]
5
+ task:
6
+ properties:
7
+ title: { type: string }
8
+ owner: { type: string, reference: person }
9
+ description: { type: string, contentMediaType: text/markdown, minLength: 1, title: Description }
10
+ required: [ title, owner, description ]
@@ -0,0 +1 @@
1
+ { "binaryTargets": [] }
@@ -0,0 +1,6 @@
1
+ {
2
+ "verdict": "refuse",
3
+ "file": "people/Отчёт.md",
4
+ "rule": "referenced-missing-file",
5
+ "referrers": ["tasks/a.md", "tasks/b.md"]
6
+ }
@@ -0,0 +1,9 @@
1
+ ---
2
+ type: task
3
+ ---
4
+ - **title**: A
5
+ - **owner**: [A](../people/Отчёт.md)
6
+
7
+ ## Description
8
+
9
+ A.
@@ -0,0 +1,9 @@
1
+ ---
2
+ type: task
3
+ ---
4
+ - **title**: B
5
+ - **owner**: [B](<../people/Отчёт.md>)
6
+
7
+ ## Description
8
+
9
+ B.
@@ -0,0 +1,10 @@
1
+ person:
2
+ properties:
3
+ name: { type: string }
4
+ required: [ name ]
5
+ task:
6
+ properties:
7
+ title: { type: string }
8
+ owner: { type: string, reference: person }
9
+ description: { type: string, contentMediaType: text/markdown, minLength: 1, title: Description }
10
+ required: [ title, owner, description ]
@@ -342,6 +342,30 @@
342
342
  }
343
343
  ],
344
344
  "expected": "---\ntype: card\n---\n# Plan\n\n- **Status**: open\n"
345
+ },
346
+ {
347
+ "name": "a link and a reference to a moved name holding a space are written in angle brackets, its # encoded",
348
+ "type": "card",
349
+ "source": "---\ntype: card\n---\n# Plan\n\n- **Owner**: [Ana](<../people/a b.md>)\n\n## Notes\n\nSee [her](../people/a%20b.md#top) and [again](<../people/a b.md>).\n",
350
+ "changes": [
351
+ {
352
+ "kind": "reference",
353
+ "pointer": "/owner",
354
+ "value": "../people/c (d)%231.md",
355
+ "prior": "../people/a b.md"
356
+ },
357
+ {
358
+ "kind": "link",
359
+ "href": "../people/a%20b.md#top",
360
+ "to": "../people/c (d)#1.md"
361
+ },
362
+ {
363
+ "kind": "link",
364
+ "href": "../people/a b.md",
365
+ "to": "../people/c (d)#1.md"
366
+ }
367
+ ],
368
+ "expected": "---\ntype: card\n---\n# Plan\n\n- **Owner**: [Ana](<../people/c (d)%231.md>)\n\n## Notes\n\nSee [her](<../people/c (d)%231.md#top>) and [again](<../people/c (d)%231.md>).\n"
345
369
  }
346
370
  ]
347
371
  }
@@ -1,5 +1,5 @@
1
1
  {
2
- "schema": "task:\n type: object\n properties:\n title: { type: string }\n status: { enum: [open, done] }\n due: { type: string, format: date }\n at: { type: string, format: date-time }\n ends: { type: [string, \"null\"], format: date-time }\n place:\n type: object\n properties:\n city: { type: string }\n starts: { type: string, format: time }\n points: { type: number }\n count: { type: integer }\n done: { type: boolean }\n priority: { type: integer, enum: [1, 2, 3] }\n flagged: { enum: [true, false] }\n tier: { enum: [1, 2, null] }\n rank: { enum: [1, high] }\n code: { type: string, maxLength: 12 }\n owner: { type: string, reference: person }\n labels: { type: array, items: { type: string } }\n times: { type: array, items: { type: string, format: date-time } }\n steps:\n type: array\n items:\n type: object\n properties:\n name: { type: string }\n at: { type: string, format: date-time }\n indexed: [status, due, at, ends, starts, points, count, done, tier, code]\nperson:\n type: object\n properties:\n name: { type: string }\n",
2
+ "schema": "task:\n type: object\n properties:\n title: { type: string }\n status: { enum: [open, done] }\n due: { type: string, format: date }\n at: { type: string, format: date-time }\n ends: { type: [string, \"null\"], format: date-time }\n place:\n type: object\n properties:\n city: { type: string }\n starts: { type: string, format: time }\n points: { type: number }\n count: { type: integer }\n done: { type: boolean }\n priority: { type: integer, enum: [1, 2, 3] }\n flagged: { enum: [true, false] }\n tier: { enum: [1, 2, null] }\n rank: { enum: [1, high] }\n code: { type: string, maxLength: 12 }\n owner: { type: string, reference: person }\n blocked_by: { type: array, items: { type: string, reference: task } }\n labels: { type: array, items: { type: string } }\n times: { type: array, items: { type: string, format: date-time } }\n steps:\n type: array\n items:\n type: object\n properties:\n name: { type: string }\n at: { type: string, format: date-time }\n indexed: [status, due, at, ends, starts, points, count, done, tier, code]\nperson:\n type: object\n properties:\n name: { type: string }\n",
3
3
  "documents": {
4
4
  "task": {
5
5
  "path": "tasks/one.md",
@@ -16,6 +16,10 @@
16
16
  "count": 2,
17
17
  "done": false,
18
18
  "owner": "../people/ada.md",
19
+ "blocked_by": [
20
+ "two.md",
21
+ "three.md"
22
+ ],
19
23
  "labels": [
20
24
  "urgent",
21
25
  "home"
@@ -42,6 +46,7 @@
42
46
  "status": "done",
43
47
  "ends": null,
44
48
  "labels": [],
49
+ "blocked_by": [],
45
50
  "code": "Zeta"
46
51
  }
47
52
  },
@@ -85,6 +90,9 @@
85
90
  "text": null,
86
91
  "fields": {
87
92
  "title": "Four",
93
+ "blocked_by": [
94
+ "two.md"
95
+ ],
88
96
  "status": "done",
89
97
  "due": "2025-12-31",
90
98
  "at": "2026-10-29T23:30:00-10:00",
@@ -104,6 +112,9 @@
104
112
  "text": null,
105
113
  "fields": {
106
114
  "title": "Five",
115
+ "blocked_by": [
116
+ "three.md"
117
+ ],
107
118
  "status": "open",
108
119
  "due": "2026-01-05",
109
120
  "at": "2026-10-30T07:59:59.999999999Z",
@@ -123,6 +134,9 @@
123
134
  "text": null,
124
135
  "fields": {
125
136
  "title": "Six",
137
+ "blocked_by": [
138
+ "gone.md"
139
+ ],
126
140
  "status": "done",
127
141
  "due": "2026-10-30",
128
142
  "at": "2026-10-30T09:00:00Z",
@@ -1242,6 +1256,195 @@
1242
1256
  },
1243
1257
  "document": "task",
1244
1258
  "matches": true
1259
+ },
1260
+ {
1261
+ "name": "a hop keeps a Document whose reference names a matching Document",
1262
+ "query": {
1263
+ "hop": {
1264
+ "field": "/blocked_by",
1265
+ "where": {
1266
+ "/status": "open"
1267
+ }
1268
+ }
1269
+ },
1270
+ "document": "task-five",
1271
+ "matches": true,
1272
+ "refusedInMemory": "hop"
1273
+ },
1274
+ {
1275
+ "name": "a hop leaves out a Document whose references name no matching Document",
1276
+ "query": {
1277
+ "hop": {
1278
+ "field": "/blocked_by",
1279
+ "where": {
1280
+ "/status": "open"
1281
+ }
1282
+ }
1283
+ },
1284
+ "document": "task-four",
1285
+ "matches": false,
1286
+ "refusedInMemory": "hop"
1287
+ },
1288
+ {
1289
+ "name": "a hop keeps a list naming one matching Document and one that is not",
1290
+ "query": {
1291
+ "hop": {
1292
+ "field": "/blocked_by",
1293
+ "where": {
1294
+ "/status": "open"
1295
+ }
1296
+ }
1297
+ },
1298
+ "document": "task",
1299
+ "matches": true,
1300
+ "refusedInMemory": "hop"
1301
+ },
1302
+ {
1303
+ "name": "a hop leaves out an empty reference list",
1304
+ "query": {
1305
+ "hop": {
1306
+ "field": "/blocked_by",
1307
+ "where": {
1308
+ "/status": "open"
1309
+ }
1310
+ }
1311
+ },
1312
+ "document": "bare-task",
1313
+ "matches": false,
1314
+ "refusedInMemory": "hop"
1315
+ },
1316
+ {
1317
+ "name": "a hop leaves out a missing reference field",
1318
+ "query": {
1319
+ "hop": {
1320
+ "field": "/blocked_by",
1321
+ "where": {
1322
+ "/status": "open"
1323
+ }
1324
+ }
1325
+ },
1326
+ "document": "spaced-task",
1327
+ "matches": false,
1328
+ "refusedInMemory": "hop"
1329
+ },
1330
+ {
1331
+ "name": "a hop leaves out a reference to no Document",
1332
+ "query": {
1333
+ "hop": {
1334
+ "field": "/blocked_by",
1335
+ "where": {
1336
+ "/status": "open"
1337
+ }
1338
+ }
1339
+ },
1340
+ "document": "task-six",
1341
+ "matches": false,
1342
+ "refusedInMemory": "hop"
1343
+ },
1344
+ {
1345
+ "name": "none leaves out a Document whose reference names a matching Document",
1346
+ "query": {
1347
+ "hop": {
1348
+ "field": "/blocked_by",
1349
+ "where": {
1350
+ "/status": "open"
1351
+ },
1352
+ "none": true
1353
+ }
1354
+ },
1355
+ "document": "task-five",
1356
+ "matches": false,
1357
+ "refusedInMemory": "hop"
1358
+ },
1359
+ {
1360
+ "name": "none keeps a Document whose references name no matching Document",
1361
+ "query": {
1362
+ "hop": {
1363
+ "field": "/blocked_by",
1364
+ "where": {
1365
+ "/status": "open"
1366
+ },
1367
+ "none": true
1368
+ }
1369
+ },
1370
+ "document": "task-four",
1371
+ "matches": true,
1372
+ "refusedInMemory": "hop"
1373
+ },
1374
+ {
1375
+ "name": "none leaves out a list naming one matching Document and one that is not",
1376
+ "query": {
1377
+ "hop": {
1378
+ "field": "/blocked_by",
1379
+ "where": {
1380
+ "/status": "open"
1381
+ },
1382
+ "none": true
1383
+ }
1384
+ },
1385
+ "document": "task",
1386
+ "matches": false,
1387
+ "refusedInMemory": "hop"
1388
+ },
1389
+ {
1390
+ "name": "none keeps an empty reference list",
1391
+ "query": {
1392
+ "hop": {
1393
+ "field": "/blocked_by",
1394
+ "where": {
1395
+ "/status": "open"
1396
+ },
1397
+ "none": true
1398
+ }
1399
+ },
1400
+ "document": "bare-task",
1401
+ "matches": true,
1402
+ "refusedInMemory": "hop"
1403
+ },
1404
+ {
1405
+ "name": "none keeps a missing reference field",
1406
+ "query": {
1407
+ "hop": {
1408
+ "field": "/blocked_by",
1409
+ "where": {
1410
+ "/status": "open"
1411
+ },
1412
+ "none": true
1413
+ }
1414
+ },
1415
+ "document": "spaced-task",
1416
+ "matches": true,
1417
+ "refusedInMemory": "hop"
1418
+ },
1419
+ {
1420
+ "name": "none keeps a reference to no Document",
1421
+ "query": {
1422
+ "hop": {
1423
+ "field": "/blocked_by",
1424
+ "where": {
1425
+ "/status": "open"
1426
+ },
1427
+ "none": true
1428
+ }
1429
+ },
1430
+ "document": "task-six",
1431
+ "matches": true,
1432
+ "refusedInMemory": "hop"
1433
+ },
1434
+ {
1435
+ "name": "none keeps a Document whose referenced Document is of a type the hop does not name",
1436
+ "query": {
1437
+ "hop": {
1438
+ "field": "/blocked_by",
1439
+ "types": [
1440
+ "person"
1441
+ ],
1442
+ "none": true
1443
+ }
1444
+ },
1445
+ "document": "task-five",
1446
+ "matches": true,
1447
+ "refusedInMemory": "hop"
1245
1448
  }
1246
1449
  ],
1247
1450
  "orders": [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agentshouse/mdmodel",
3
- "version": "0.1.0-cbb41ab5ebbd2",
3
+ "version": "0.1.0-cbd3c46de90bf",
4
4
  "description": "MD Type language, parser, validator, structured change application, Markdown syntax profile, and conformance fixtures",
5
5
  "license": "MIT",
6
6
  "type": "module",
package/dist/divide.d.ts DELETED
@@ -1,3 +0,0 @@
1
- import type { Division, DivisionContext, Document } from './types.js';
2
- export declare function divide(document: Document, file: string, context: DivisionContext): Division;
3
- //# sourceMappingURL=divide.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"divide.d.ts","sourceRoot":"","sources":["../src/divide.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,QAAQ,EAAE,eAAe,EAAE,QAAQ,EAAgB,MAAM,YAAY,CAAC;AAwXpF,wBAAgB,MAAM,CAAC,QAAQ,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,eAAe,GAAG,QAAQ,CAe3F"}
package/dist/divide.js DELETED
@@ -1,321 +0,0 @@
1
- import { readingOf } from './document.js';
2
- import { blockRanges, headingsIn } from './markdown.js';
3
- import { firstEditFrom, projectionInput, rangeText, } from './project.js';
4
- import { refusal } from './refuse.js';
5
- const SMALLEST_BOUND = 2;
6
- function node(draft) {
7
- return {
8
- start: draft.start,
9
- end: draft.end,
10
- bodyStart: draft.bodyStart,
11
- heading: draft.heading,
12
- children: draft.children.map(node),
13
- };
14
- }
15
- function forest(state, range) {
16
- const headings = headingsIn(state.reading, range.start, range.end);
17
- const roots = [];
18
- const open = [];
19
- const first = headings[0]?.start ?? range.end;
20
- if (first > range.start) {
21
- roots.push({
22
- level: 0,
23
- start: range.start,
24
- end: first,
25
- bodyStart: range.start,
26
- heading: '',
27
- children: [],
28
- });
29
- }
30
- for (const heading of headings) {
31
- while ((open[open.length - 1]?.level ?? 0) >= heading.level) {
32
- open.pop().end = heading.start;
33
- }
34
- const draft = {
35
- level: heading.level,
36
- start: heading.start,
37
- end: range.end,
38
- bodyStart: heading.next,
39
- heading: `${textOf(state, { start: heading.start, end: heading.textEnd })}\n\n`,
40
- children: [],
41
- };
42
- (open[open.length - 1]?.children ?? roots).push(draft);
43
- open.push(draft);
44
- }
45
- return roots.map(node);
46
- }
47
- function isSpace(character) {
48
- return character === ' ' || character === '\t' || character === '\n' || character === '\r';
49
- }
50
- const TERMINATORS = new Set(['.', '!', '?', '…', '。', '!', '?']);
51
- function isTerminator(character) {
52
- return TERMINATORS.has(character);
53
- }
54
- const CLOSERS = new Set(['"', "'", ')', ']', '»', '”']);
55
- function isCloser(character) {
56
- return CLOSERS.has(character);
57
- }
58
- function sentenceBounds(text) {
59
- const bounds = [0];
60
- let i = 0;
61
- while (i < text.length) {
62
- if (!isTerminator(text[i])) {
63
- i += 1;
64
- continue;
65
- }
66
- let after = i + 1;
67
- while (after < text.length && isCloser(text[after]))
68
- after += 1;
69
- if (after >= text.length || !isSpace(text[after])) {
70
- i = after + 1;
71
- continue;
72
- }
73
- while (after < text.length && isSpace(text[after]))
74
- after += 1;
75
- if (after < text.length)
76
- bounds.push(after);
77
- i = after;
78
- }
79
- bounds.push(text.length);
80
- return bounds;
81
- }
82
- function isTrailingSurrogate(text, index) {
83
- const code = text.charCodeAt(index);
84
- return code >= 0xdc00 && code <= 0xdfff;
85
- }
86
- function characterLength(text, index) {
87
- return isTrailingSurrogate(text, index + 1) ? 2 : 1;
88
- }
89
- function boundOf(cutting, from, to, bound) {
90
- const limit = from + bound;
91
- if (limit >= to)
92
- return to;
93
- for (let index = limit; index > from + 1; index -= 1) {
94
- if (isSpace(cutting.text[index]))
95
- return index;
96
- }
97
- const inside = cutting.masks.find((mask) => limit > mask.start && limit < mask.end);
98
- if (inside !== undefined)
99
- return inside.start > from ? inside.start : Math.min(inside.end, to);
100
- return isTrailingSurrogate(cutting.text, limit) ? limit - 1 : limit;
101
- }
102
- function utf8Size(code) {
103
- if (code < 0x80)
104
- return 1;
105
- if (code < 0x800)
106
- return 2;
107
- if (code < 0x10000)
108
- return 3;
109
- return 4;
110
- }
111
- function byteBounds(content, spans) {
112
- const marks = [...new Set(spans.flatMap((span) => [span.start, span.end]))].sort((left, right) => left - right);
113
- const bytes = new Map();
114
- let index = 0;
115
- let count = 0;
116
- for (const mark of marks) {
117
- while (index < mark) {
118
- const code = content.codePointAt(index);
119
- count += utf8Size(code);
120
- index += code > 0xffff ? 2 : 1;
121
- }
122
- bytes.set(mark, count);
123
- }
124
- return spans.map((span) => ({
125
- ...span,
126
- start: bytes.get(span.start),
127
- end: bytes.get(span.end),
128
- }));
129
- }
130
- function textOf(state, extent) {
131
- return rangeText(state.content, extent, state.edits);
132
- }
133
- function sourceCutting(state, extent) {
134
- const source = state.content.slice(extent.start, extent.end);
135
- const masks = [];
136
- for (let index = firstEditFrom(state.edits, extent.start); index < state.edits.length; index += 1) {
137
- const edit = state.edits[index];
138
- if (edit.start > extent.end)
139
- break;
140
- if (edit.end > extent.end)
141
- continue;
142
- masks.push({ start: edit.start - extent.start, end: edit.end - extent.start });
143
- }
144
- let text = '';
145
- let cursor = 0;
146
- for (const mask of masks) {
147
- if (mask.end <= cursor)
148
- continue;
149
- const from = Math.max(mask.start, cursor);
150
- text += `${source.slice(cursor, from)}${'x'.repeat(mask.end - from)}`;
151
- cursor = mask.end;
152
- }
153
- text += source.slice(cursor);
154
- return {
155
- text,
156
- masks,
157
- piece: (from, to) => {
158
- const cut = { start: extent.start + from, end: extent.start + to };
159
- return { ...cut, text: textOf(state, cut) };
160
- },
161
- };
162
- }
163
- function valueCutting(value) {
164
- return {
165
- text: value.text,
166
- masks: [],
167
- piece: (from, to) => ({ start: value.start, end: value.end, text: value.text.slice(from, to) }),
168
- };
169
- }
170
- function prefixOf(state, body) {
171
- if (body.emitted === 0 || body.heading.length >= state.bound)
172
- return '';
173
- return body.heading;
174
- }
175
- function available(state, body) {
176
- return state.bound - prefixOf(state, body).length;
177
- }
178
- function emit(state, body, piece, bare = false) {
179
- if (piece.text.trim() === '')
180
- return;
181
- const text = (bare ? '' : prefixOf(state, body)) + piece.text;
182
- state.spans.push({ start: piece.start, end: piece.end, text });
183
- body.emitted += 1;
184
- }
185
- function merge(run) {
186
- return {
187
- start: Math.min(...run.map((unit) => unit.start)),
188
- end: Math.max(...run.map((unit) => unit.end)),
189
- text: run.map((unit) => unit.text).join(''),
190
- };
191
- }
192
- function divideUnits(state, body, units) {
193
- let run = [];
194
- let length = 0;
195
- const flush = () => {
196
- if (run.length === 0)
197
- return;
198
- emit(state, body, merge(run));
199
- run = [];
200
- length = 0;
201
- };
202
- for (const unit of units) {
203
- if (run.length > 0 && length + unit.text.length > available(state, body))
204
- flush();
205
- if (unit.text.length > available(state, body)) {
206
- flush();
207
- unit.descend();
208
- continue;
209
- }
210
- run.push(unit);
211
- length += unit.text.length;
212
- }
213
- flush();
214
- }
215
- function divideBound(state, body, cutting, range) {
216
- let start = range.start;
217
- while (start < range.end) {
218
- const bare = available(state, body) < characterLength(cutting.text, start);
219
- const room = bare ? state.bound : available(state, body);
220
- const stop = boundOf(cutting, start, range.end, room);
221
- const piece = cutting.piece(start, stop);
222
- if (cutting.masks.length === 0 || piece.text.length <= room) {
223
- emit(state, body, piece, bare);
224
- }
225
- else {
226
- divideBound(state, body, valueCutting(piece), { start: 0, end: piece.text.length });
227
- }
228
- start = stop;
229
- }
230
- }
231
- function divideSentences(state, body, cutting) {
232
- const cut = cutting.piece;
233
- const bounds = sentenceBounds(cutting.text);
234
- let index = 0;
235
- while (index + 1 < bounds.length) {
236
- const alone = { start: bounds[index], end: bounds[index + 1] };
237
- if (cut(alone.start, alone.end).text.length > available(state, body)) {
238
- divideBound(state, body, cutting, alone);
239
- index += 1;
240
- continue;
241
- }
242
- const carried = bounds[Math.max(index - 1, 0)];
243
- const from = cut(carried, alone.end).text.length <= available(state, body) ? carried : alone.start;
244
- let last = index;
245
- while (last + 2 < bounds.length &&
246
- cut(from, bounds[last + 2]).text.length <= available(state, body)) {
247
- last += 1;
248
- }
249
- emit(state, body, cut(from, bounds[last + 1]));
250
- index = last + 1;
251
- }
252
- }
253
- function blockUnit(state, body, block) {
254
- return {
255
- start: block.start,
256
- end: block.end,
257
- text: textOf(state, block),
258
- descend: () => divideSentences(state, body, sourceCutting(state, block)),
259
- };
260
- }
261
- function valueUnit(state, body, value) {
262
- return {
263
- start: value.start,
264
- end: value.end,
265
- text: value.text,
266
- descend: () => divideSentences(state, body, valueCutting(value)),
267
- };
268
- }
269
- function nodeUnit(state, subject) {
270
- return {
271
- start: subject.start,
272
- end: subject.end,
273
- text: textOf(state, subject),
274
- descend: () => divideNode(state, subject),
275
- };
276
- }
277
- function ownBlocks(subject, end, blocks) {
278
- const first = blocks[0];
279
- return first === undefined
280
- ? [{ start: subject.start, end }]
281
- : [{ start: subject.start, end: first.end }, ...blocks.slice(1)];
282
- }
283
- function divideNode(state, subject) {
284
- const end = subject.children[0]?.start ?? subject.end;
285
- const blocks = blockRanges(state.reading, subject.bodyStart, end);
286
- const children = { heading: '', emitted: 0 };
287
- if (blocks.length === 0 && subject.children.length > 0) {
288
- const [first, ...rest] = subject.children;
289
- const led = [{ ...first, start: subject.start }, ...rest];
290
- divideUnits(state, children, led.map((child) => nodeUnit(state, child)));
291
- return;
292
- }
293
- const body = { heading: subject.heading, emitted: 0 };
294
- const own = ownBlocks(subject, end, blocks);
295
- divideUnits(state, body, own.map((block) => blockUnit(state, body, block)));
296
- divideUnits(state, children, subject.children.map((child) => nodeUnit(state, child)));
297
- }
298
- function divideInput(state, input) {
299
- const body = { heading: '', emitted: 0 };
300
- divideUnits(state, body, [
301
- ...input.values.map((value) => valueUnit(state, body, value)),
302
- ...input.ranges.flatMap((range) => forest(state, range).map((subject) => nodeUnit(state, subject))),
303
- ]);
304
- }
305
- export function divide(document, file, context) {
306
- if (!Number.isInteger(context.bound) || context.bound < SMALLEST_BOUND) {
307
- return { ok: false, refusal: refusal(file, 'division-bound') };
308
- }
309
- const result = projectionInput(document, file, context);
310
- if (!result.ok)
311
- return { ok: false, refusal: result.refusal };
312
- const state = {
313
- content: document.content,
314
- reading: readingOf(document),
315
- edits: result.input.edits,
316
- bound: context.bound,
317
- spans: [],
318
- };
319
- divideInput(state, result.input);
320
- return { ok: true, spans: byteBounds(document.content, state.spans) };
321
- }