@crossworks/content-core 0.230.43
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.md +135 -0
- package/package.json +41 -0
- package/src/block-diff.test.ts +190 -0
- package/src/block-diff.ts +163 -0
- package/src/block-ids.test.ts +358 -0
- package/src/block-ids.ts +242 -0
- package/src/block-list.test.ts +241 -0
- package/src/block-list.ts +177 -0
- package/src/contacts-format.ts +260 -0
- package/src/doc-to-markdown.test.ts +194 -0
- package/src/doc-to-markdown.ts +315 -0
- package/src/formula-dimensions.test.ts +103 -0
- package/src/formula-dimensions.ts +231 -0
- package/src/formula-eval.ts +294 -0
- package/src/formula-seed.test.ts +175 -0
- package/src/formula-seed.ts +466 -0
- package/src/formula-signature.test.ts +336 -0
- package/src/formula-signature.ts +435 -0
- package/src/formula-spec.test.ts +458 -0
- package/src/formula-spec.ts +566 -0
- package/src/journal-options.test.ts +57 -0
- package/src/journal-options.ts +77 -0
- package/src/markdown-refs.test.ts +143 -0
- package/src/markdown-refs.ts +172 -0
- package/src/markdown-to-doc.test.ts +179 -0
- package/src/markdown-to-doc.ts +567 -0
- package/src/onboarding-questions.test.ts +75 -0
- package/src/onboarding-questions.ts +90 -0
- package/src/page-diff.test.ts +82 -0
- package/src/page-diff.ts +120 -0
- package/src/page-split.test.ts +141 -0
- package/src/page-split.ts +128 -0
- package/src/page-toc.test.ts +58 -0
- package/src/page-toc.ts +89 -0
- package/src/persona-bank.test.ts +67 -0
- package/src/persona-bank.ts +234 -0
- package/src/table-formula-mathjs.ts +259 -0
- package/src/table-formula.test.ts +157 -0
- package/src/table-formula.ts +496 -0
- package/src/table-model.test.ts +429 -0
- package/src/table-model.ts +870 -0
- package/src/thinking-tiers.ts +56 -0
- package/tsconfig.json +4 -0
- package/tsconfig.tsbuildinfo +1 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
# License
|
|
2
|
+
|
|
3
|
+
Mantle is **dual-licensed** by Cross Works Engineering (Pty) Ltd:
|
|
4
|
+
|
|
5
|
+
1. **Public license — Functional Source License 1.1 (MIT Future), reproduced in
|
|
6
|
+
full below.** Free to use, self-host, modify, and run for any purpose that is
|
|
7
|
+
not a Competing Use (see the _Permitted Purpose_ clause). Two years after each
|
|
8
|
+
version is first published, that version automatically converts to the MIT
|
|
9
|
+
license.
|
|
10
|
+
2. **Commercial license — see [`LICENSE-COMMERCIAL.md`](./LICENSE-COMMERCIAL.md).**
|
|
11
|
+
A paid agreement that lifts the FSL's _Competing Use_ restriction for partners
|
|
12
|
+
who want to embed Mantle in a commercial product or offer it as a service
|
|
13
|
+
during the two-year window. Contact **licensing@crossworks.engineering**.
|
|
14
|
+
|
|
15
|
+
For a plain-language explanation of how the two licenses fit together and what the
|
|
16
|
+
key terms mean, see [`LICENSING.md`](./LICENSING.md). Third-party open-source
|
|
17
|
+
components bundled with Mantle are attributed in
|
|
18
|
+
[`THIRD-PARTY-NOTICES.md`](./THIRD-PARTY-NOTICES.md).
|
|
19
|
+
|
|
20
|
+
The "Change Date" for each release is **two years after that release is first made
|
|
21
|
+
available** under this license; on the Change Date that release becomes available
|
|
22
|
+
under the MIT license (the _Grant of Future License_ below).
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
# Functional Source License, Version 1.1, MIT Future License
|
|
27
|
+
|
|
28
|
+
## Abbreviation
|
|
29
|
+
|
|
30
|
+
FSL-1.1-MIT
|
|
31
|
+
|
|
32
|
+
## Notice
|
|
33
|
+
|
|
34
|
+
Copyright 2026 Cross Works Engineering (Pty) Ltd
|
|
35
|
+
|
|
36
|
+
## Terms and Conditions
|
|
37
|
+
|
|
38
|
+
### Licensor ("We")
|
|
39
|
+
|
|
40
|
+
The party offering the Software under these Terms and Conditions.
|
|
41
|
+
|
|
42
|
+
### The Software
|
|
43
|
+
|
|
44
|
+
The "Software" is each version of the software that we make available under
|
|
45
|
+
these Terms and Conditions, as indicated by our inclusion of these Terms and
|
|
46
|
+
Conditions with the Software.
|
|
47
|
+
|
|
48
|
+
### License Grant
|
|
49
|
+
|
|
50
|
+
Subject to your compliance with this License Grant and the Patents,
|
|
51
|
+
Redistribution and Trademark clauses below, we hereby grant you the right to
|
|
52
|
+
use, copy, modify, create derivative works, publicly perform, publicly display
|
|
53
|
+
and redistribute the Software for any Permitted Purpose identified below.
|
|
54
|
+
|
|
55
|
+
### Permitted Purpose
|
|
56
|
+
|
|
57
|
+
A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
|
|
58
|
+
means making the Software available to others in a commercial product or
|
|
59
|
+
service that:
|
|
60
|
+
|
|
61
|
+
1. substitutes for the Software;
|
|
62
|
+
|
|
63
|
+
2. substitutes for any other product or service we offer using the Software
|
|
64
|
+
that exists as of the date we make the Software available; or
|
|
65
|
+
|
|
66
|
+
3. offers the same or substantially similar functionality as the Software.
|
|
67
|
+
|
|
68
|
+
Permitted Purposes specifically include using the Software:
|
|
69
|
+
|
|
70
|
+
1. for your internal use and access;
|
|
71
|
+
|
|
72
|
+
2. for non-commercial education;
|
|
73
|
+
|
|
74
|
+
3. for non-commercial research; and
|
|
75
|
+
|
|
76
|
+
4. in connection with professional services that you provide to a licensee
|
|
77
|
+
using the Software in accordance with these Terms and Conditions.
|
|
78
|
+
|
|
79
|
+
### Patents
|
|
80
|
+
|
|
81
|
+
To the extent your use for a Permitted Purpose would necessarily infringe our
|
|
82
|
+
patents, the license grant above includes a license under our patents. If you
|
|
83
|
+
make a claim against any party that the Software infringes or contributes to
|
|
84
|
+
the infringement of any patent, then your patent license to the Software ends
|
|
85
|
+
immediately.
|
|
86
|
+
|
|
87
|
+
### Redistribution
|
|
88
|
+
|
|
89
|
+
The Terms and Conditions apply to all copies, modifications and derivatives of
|
|
90
|
+
the Software.
|
|
91
|
+
|
|
92
|
+
If you redistribute any copies, modifications or derivatives of the Software,
|
|
93
|
+
you must include a copy of or a link to these Terms and Conditions and not
|
|
94
|
+
remove any copyright notices provided in or with the Software.
|
|
95
|
+
|
|
96
|
+
### Disclaimer
|
|
97
|
+
|
|
98
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR
|
|
99
|
+
IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
|
|
100
|
+
PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
|
|
101
|
+
|
|
102
|
+
IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
|
|
103
|
+
SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES,
|
|
104
|
+
EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
|
|
105
|
+
|
|
106
|
+
### Trademarks
|
|
107
|
+
|
|
108
|
+
Except for displaying the License Details and identifying us as the origin of
|
|
109
|
+
the Software, you have no right under these Terms and Conditions to use our
|
|
110
|
+
trademarks, trade names, service marks or product names.
|
|
111
|
+
|
|
112
|
+
## Grant of Future License
|
|
113
|
+
|
|
114
|
+
We hereby irrevocably grant you an additional license to use the Software under
|
|
115
|
+
the MIT license that is effective on the second anniversary of the date we make
|
|
116
|
+
the Software available. On or after that date, you may use the Software under
|
|
117
|
+
the MIT license, in which case the following will apply:
|
|
118
|
+
|
|
119
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
120
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
121
|
+
the Software without restriction, including without limitation the rights to
|
|
122
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
123
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
|
124
|
+
so, subject to the following conditions:
|
|
125
|
+
|
|
126
|
+
The above copyright notice and this permission notice shall be included in all
|
|
127
|
+
copies or substantial portions of the Software.
|
|
128
|
+
|
|
129
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
130
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
131
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
132
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
133
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
134
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
135
|
+
SOFTWARE.
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@crossworks/content-core",
|
|
3
|
+
"version": "0.230.43",
|
|
4
|
+
"description": "Browser-safe content logic — markdown, blocks, pages, tables, formulas — shared by the server and any client. Zero server deps by design: nothing here may reach @mantle/db, node-only APIs, or the network (the jackdaw-repo-split P0 boundary).",
|
|
5
|
+
"exports": {
|
|
6
|
+
"./markdown": "./src/markdown-to-doc.ts",
|
|
7
|
+
"./markdown-refs": "./src/markdown-refs.ts",
|
|
8
|
+
"./doc-to-markdown": "./src/doc-to-markdown.ts",
|
|
9
|
+
"./block-ids": "./src/block-ids.ts",
|
|
10
|
+
"./block-list": "./src/block-list.ts",
|
|
11
|
+
"./block-diff": "./src/block-diff.ts",
|
|
12
|
+
"./page-toc": "./src/page-toc.ts",
|
|
13
|
+
"./page-split": "./src/page-split.ts",
|
|
14
|
+
"./page-diff": "./src/page-diff.ts",
|
|
15
|
+
"./table-model": "./src/table-model.ts",
|
|
16
|
+
"./table-formula": "./src/table-formula.ts",
|
|
17
|
+
"./table-formula-mathjs": "./src/table-formula-mathjs.ts",
|
|
18
|
+
"./formula-spec": "./src/formula-spec.ts",
|
|
19
|
+
"./formula-eval": "./src/formula-eval.ts",
|
|
20
|
+
"./formula-seed": "./src/formula-seed.ts",
|
|
21
|
+
"./formula-dimensions": "./src/formula-dimensions.ts",
|
|
22
|
+
"./contacts-format": "./src/contacts-format.ts",
|
|
23
|
+
"./journal-options": "./src/journal-options.ts",
|
|
24
|
+
"./onboarding-questions": "./src/onboarding-questions.ts",
|
|
25
|
+
"./persona-bank": "./src/persona-bank.ts",
|
|
26
|
+
"./thinking-tiers": "./src/thinking-tiers.ts",
|
|
27
|
+
"./formula-signature": "./src/formula-signature.ts"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"marked": "^18.0.7",
|
|
31
|
+
"mathjs": "15.2.0"
|
|
32
|
+
},
|
|
33
|
+
"license": "SEE LICENSE IN LICENSE.md",
|
|
34
|
+
"repository": {
|
|
35
|
+
"type": "git",
|
|
36
|
+
"url": "https://github.com/crossworks-engineering/mantle"
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"typecheck": "tsc --noEmit"
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unit tests for diffBlocks — drives the AI-assist panel's diff summary
|
|
3
|
+
* + (later) the editor's per-block diff view. Stable per-block ids are
|
|
4
|
+
* the foundation; this test layer locks down the matching semantics.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { describe, expect, it } from 'vitest';
|
|
8
|
+
import { ensureBlockIds } from './block-ids';
|
|
9
|
+
import { diffBlocks } from './block-diff';
|
|
10
|
+
|
|
11
|
+
function blocked(doc: Record<string, unknown>): Record<string, unknown> {
|
|
12
|
+
return ensureBlockIds(doc);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
describe('diffBlocks', () => {
|
|
16
|
+
it('reports zero changes when doc and draft are identical', () => {
|
|
17
|
+
const doc = blocked({
|
|
18
|
+
type: 'doc',
|
|
19
|
+
content: [
|
|
20
|
+
{ type: 'paragraph', content: [{ type: 'text', text: 'hi' }] },
|
|
21
|
+
{ type: 'paragraph', content: [{ type: 'text', text: 'world' }] },
|
|
22
|
+
],
|
|
23
|
+
});
|
|
24
|
+
const d = diffBlocks(doc, doc);
|
|
25
|
+
expect(d.added).toEqual([]);
|
|
26
|
+
expect(d.removed).toEqual([]);
|
|
27
|
+
expect(d.changed).toEqual([]);
|
|
28
|
+
expect(d.unchangedCount).toBe(2);
|
|
29
|
+
expect(d.ordered).toEqual([]);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("reports an added block when the draft has one the doc doesn't", () => {
|
|
33
|
+
const doc = blocked({
|
|
34
|
+
type: 'doc',
|
|
35
|
+
content: [{ type: 'paragraph', content: [{ type: 'text', text: 'one' }] }],
|
|
36
|
+
});
|
|
37
|
+
// Draft = doc + a new paragraph (fresh id).
|
|
38
|
+
const draftContent = [
|
|
39
|
+
...(doc as { content: unknown[] }).content,
|
|
40
|
+
{ type: 'paragraph', attrs: { id: 'new-block' }, content: [{ type: 'text', text: 'two' }] },
|
|
41
|
+
];
|
|
42
|
+
const draft = { type: 'doc', content: draftContent };
|
|
43
|
+
|
|
44
|
+
const d = diffBlocks(doc, draft);
|
|
45
|
+
expect(d.added).toHaveLength(1);
|
|
46
|
+
expect(d.added[0]!.id).toBe('new-block');
|
|
47
|
+
expect(d.removed).toEqual([]);
|
|
48
|
+
expect(d.changed).toEqual([]);
|
|
49
|
+
expect(d.unchangedCount).toBe(1);
|
|
50
|
+
expect(d.ordered).toHaveLength(1);
|
|
51
|
+
expect(d.ordered[0]!.kind).toBe('added');
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('reports a removed block when the draft is missing one the doc has', () => {
|
|
55
|
+
type N = { attrs: { id: string } };
|
|
56
|
+
const doc = blocked({
|
|
57
|
+
type: 'doc',
|
|
58
|
+
content: [
|
|
59
|
+
{ type: 'paragraph', content: [{ type: 'text', text: 'keep' }] },
|
|
60
|
+
{ type: 'paragraph', content: [{ type: 'text', text: 'drop' }] },
|
|
61
|
+
],
|
|
62
|
+
});
|
|
63
|
+
const keepId = (doc as { content: N[] }).content[0]!.attrs.id;
|
|
64
|
+
const draft = blocked({
|
|
65
|
+
type: 'doc',
|
|
66
|
+
content: [
|
|
67
|
+
{ type: 'paragraph', attrs: { id: keepId }, content: [{ type: 'text', text: 'keep' }] },
|
|
68
|
+
],
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
const d = diffBlocks(doc, draft);
|
|
72
|
+
expect(d.added).toEqual([]);
|
|
73
|
+
expect(d.removed).toHaveLength(1);
|
|
74
|
+
expect(d.removed[0]!.preview).toBe('drop');
|
|
75
|
+
expect(d.changed).toEqual([]);
|
|
76
|
+
expect(d.unchangedCount).toBe(1);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('reports a changed block when same id has different content', () => {
|
|
80
|
+
type N = { attrs: { id: string } };
|
|
81
|
+
const doc = blocked({
|
|
82
|
+
type: 'doc',
|
|
83
|
+
content: [{ type: 'paragraph', content: [{ type: 'text', text: 'old text' }] }],
|
|
84
|
+
});
|
|
85
|
+
const id = (doc as { content: N[] }).content[0]!.attrs.id;
|
|
86
|
+
const draft = {
|
|
87
|
+
type: 'doc',
|
|
88
|
+
content: [
|
|
89
|
+
{ type: 'paragraph', attrs: { id }, content: [{ type: 'text', text: 'new text' }] },
|
|
90
|
+
],
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const d = diffBlocks(doc, draft);
|
|
94
|
+
expect(d.changed).toHaveLength(1);
|
|
95
|
+
expect(d.changed[0]!.from.preview).toBe('old text');
|
|
96
|
+
expect(d.changed[0]!.to.preview).toBe('new text');
|
|
97
|
+
expect(d.added).toEqual([]);
|
|
98
|
+
expect(d.removed).toEqual([]);
|
|
99
|
+
expect(d.unchangedCount).toBe(0);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('detects style-only changes (callout variant flip) as changed, not unchanged', () => {
|
|
103
|
+
const id = 'call-1';
|
|
104
|
+
const doc = {
|
|
105
|
+
type: 'doc',
|
|
106
|
+
content: [
|
|
107
|
+
{
|
|
108
|
+
type: 'callout',
|
|
109
|
+
attrs: { id, variant: 'info' },
|
|
110
|
+
content: [
|
|
111
|
+
{ type: 'paragraph', attrs: { id: 'p-1' }, content: [{ type: 'text', text: 'note' }] },
|
|
112
|
+
],
|
|
113
|
+
},
|
|
114
|
+
],
|
|
115
|
+
};
|
|
116
|
+
const draft = {
|
|
117
|
+
type: 'doc',
|
|
118
|
+
content: [
|
|
119
|
+
{
|
|
120
|
+
type: 'callout',
|
|
121
|
+
attrs: { id, variant: 'warning' }, // only the variant changed
|
|
122
|
+
content: [
|
|
123
|
+
{ type: 'paragraph', attrs: { id: 'p-1' }, content: [{ type: 'text', text: 'note' }] },
|
|
124
|
+
],
|
|
125
|
+
},
|
|
126
|
+
],
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const d = diffBlocks(doc, draft);
|
|
130
|
+
// The callout (id call-1) is changed; the inner paragraph (id p-1) is unchanged.
|
|
131
|
+
expect(d.changed.map((c) => c.to.id)).toContain(id);
|
|
132
|
+
// p-1's text is the same, JSON identical → unchanged.
|
|
133
|
+
expect(d.changed.map((c) => c.to.id)).not.toContain('p-1');
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('handles a complete restyle (every block changed)', () => {
|
|
137
|
+
type N = { attrs: { id: string } };
|
|
138
|
+
const doc = blocked({
|
|
139
|
+
type: 'doc',
|
|
140
|
+
content: [
|
|
141
|
+
{ type: 'paragraph', content: [{ type: 'text', text: 'A' }] },
|
|
142
|
+
{ type: 'paragraph', content: [{ type: 'text', text: 'B' }] },
|
|
143
|
+
{ type: 'paragraph', content: [{ type: 'text', text: 'C' }] },
|
|
144
|
+
],
|
|
145
|
+
});
|
|
146
|
+
const ids = (doc as { content: N[] }).content.map((b) => b.attrs.id);
|
|
147
|
+
const draft = {
|
|
148
|
+
type: 'doc',
|
|
149
|
+
content: [
|
|
150
|
+
{ type: 'paragraph', attrs: { id: ids[0] }, content: [{ type: 'text', text: 'AA' }] },
|
|
151
|
+
{ type: 'paragraph', attrs: { id: ids[1] }, content: [{ type: 'text', text: 'BB' }] },
|
|
152
|
+
{ type: 'paragraph', attrs: { id: ids[2] }, content: [{ type: 'text', text: 'CC' }] },
|
|
153
|
+
],
|
|
154
|
+
};
|
|
155
|
+
const d = diffBlocks(doc, draft);
|
|
156
|
+
expect(d.changed).toHaveLength(3);
|
|
157
|
+
expect(d.unchangedCount).toBe(0);
|
|
158
|
+
expect(d.added).toEqual([]);
|
|
159
|
+
expect(d.removed).toEqual([]);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it('ordered output preserves the document-walk order', () => {
|
|
163
|
+
type N = { attrs: { id: string } };
|
|
164
|
+
const doc = blocked({
|
|
165
|
+
type: 'doc',
|
|
166
|
+
content: [
|
|
167
|
+
{ type: 'paragraph', content: [{ type: 'text', text: 'A' }] },
|
|
168
|
+
{ type: 'paragraph', content: [{ type: 'text', text: 'B' }] },
|
|
169
|
+
{ type: 'paragraph', content: [{ type: 'text', text: 'C' }] },
|
|
170
|
+
],
|
|
171
|
+
});
|
|
172
|
+
const [aId, bId, cId] = (doc as { content: N[] }).content.map((b) => b.attrs.id);
|
|
173
|
+
// Draft: change A, leave B, drop C, add a new D at the end.
|
|
174
|
+
const draft = {
|
|
175
|
+
type: 'doc',
|
|
176
|
+
content: [
|
|
177
|
+
{ type: 'paragraph', attrs: { id: aId }, content: [{ type: 'text', text: 'AA' }] },
|
|
178
|
+
{ type: 'paragraph', attrs: { id: bId }, content: [{ type: 'text', text: 'B' }] },
|
|
179
|
+
{ type: 'paragraph', attrs: { id: 'new-d' }, content: [{ type: 'text', text: 'D' }] },
|
|
180
|
+
],
|
|
181
|
+
};
|
|
182
|
+
const d = diffBlocks(doc, draft);
|
|
183
|
+
// Expected ordering: walk draft (A=changed, B=unchanged-skipped, D=added),
|
|
184
|
+
// then removals from doc (C=removed).
|
|
185
|
+
expect(d.ordered.map((c) => c.kind)).toEqual(['changed', 'added', 'removed']);
|
|
186
|
+
expect((d.ordered[0]! as { kind: 'changed'; to: { id: string } }).to.id).toBe(aId);
|
|
187
|
+
expect((d.ordered[1]! as { kind: 'added'; block: { id: string } }).block.id).toBe('new-d');
|
|
188
|
+
expect((d.ordered[2]! as { kind: 'removed'; block: { id: string } }).block.id).toBe(cId);
|
|
189
|
+
});
|
|
190
|
+
});
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Block-level diff between two PM docs — typically the published `doc`
|
|
3
|
+
* vs the in-flight `draft_doc`. Powers the Phase 3a AI-assist panel
|
|
4
|
+
* ("Pages changed 3 blocks") and (later) the per-block visual diff in
|
|
5
|
+
* the editor.
|
|
6
|
+
*
|
|
7
|
+
* Algorithm: match blocks by stable id (the foundation that block-ids.ts
|
|
8
|
+
* provides). The four states per block are:
|
|
9
|
+
*
|
|
10
|
+
* - unchanged: same id in both, content equal → no entry
|
|
11
|
+
* - added: id only in draft → DRAFT introduced a new block
|
|
12
|
+
* - removed: id only in doc → DRAFT removed a block
|
|
13
|
+
* - changed: same id in both, content differs → DRAFT modified the block
|
|
14
|
+
*
|
|
15
|
+
* "Content equal" uses JSON.stringify of the block node (cheap, exact —
|
|
16
|
+
* captures attribute, mark, and structure changes, not just text).
|
|
17
|
+
*
|
|
18
|
+
* Returns flat lists, not a tree. The panel renders these grouped;
|
|
19
|
+
* downstream tools (commit, discard) operate at the doc level (the diff
|
|
20
|
+
* is just for displaying what's about to happen).
|
|
21
|
+
*
|
|
22
|
+
* Pure. Imports the same listBlocks helper as `page_blocks_list` so the
|
|
23
|
+
* shape of each entry matches what the agent already saw.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import { listBlocks, type BlockListEntry } from './block-list';
|
|
27
|
+
|
|
28
|
+
export type BlockChange =
|
|
29
|
+
| { kind: 'added'; block: BlockListEntry }
|
|
30
|
+
| { kind: 'removed'; block: BlockListEntry }
|
|
31
|
+
| { kind: 'changed'; from: BlockListEntry; to: BlockListEntry };
|
|
32
|
+
|
|
33
|
+
export type BlockDiff = {
|
|
34
|
+
/** Blocks present in draft, absent in doc — agent inserted these. */
|
|
35
|
+
added: BlockListEntry[];
|
|
36
|
+
/** Blocks present in doc, absent in draft — agent removed these. */
|
|
37
|
+
removed: BlockListEntry[];
|
|
38
|
+
/** Same id in both, content differs — agent rewrote these. */
|
|
39
|
+
changed: Array<{ from: BlockListEntry; to: BlockListEntry }>;
|
|
40
|
+
/** Total blocks in the draft that weren't touched. Useful for "23 of 25 blocks unchanged" UX. */
|
|
41
|
+
unchangedCount: number;
|
|
42
|
+
/** Flat list of every change in document order — for diff rendering. */
|
|
43
|
+
ordered: BlockChange[];
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Compute the block-level diff. Pass the doc (published) and draft.
|
|
48
|
+
* If either is null/empty, the diff is computed against an empty
|
|
49
|
+
* counterpart — callers can pass an empty doc when one side is missing.
|
|
50
|
+
*/
|
|
51
|
+
export function diffBlocks(
|
|
52
|
+
doc: Record<string, unknown>,
|
|
53
|
+
draft: Record<string, unknown>,
|
|
54
|
+
): BlockDiff {
|
|
55
|
+
const docBlocks = listBlocks(doc);
|
|
56
|
+
const draftBlocks = listBlocks(draft);
|
|
57
|
+
|
|
58
|
+
// We need the full nodes for content-equality, but listBlocks only
|
|
59
|
+
// returns previews. Walk both docs again to capture id → full JSON.
|
|
60
|
+
// Cheap (single pass each) — only done when something invokes us.
|
|
61
|
+
const docJsonById = collectBlockJson(doc);
|
|
62
|
+
const draftJsonById = collectBlockJson(draft);
|
|
63
|
+
|
|
64
|
+
const docById = new Map(docBlocks.map((b) => [b.id, b]));
|
|
65
|
+
const draftById = new Map(draftBlocks.map((b) => [b.id, b]));
|
|
66
|
+
|
|
67
|
+
const added: BlockListEntry[] = [];
|
|
68
|
+
const removed: BlockListEntry[] = [];
|
|
69
|
+
const changed: Array<{ from: BlockListEntry; to: BlockListEntry }> = [];
|
|
70
|
+
let unchangedCount = 0;
|
|
71
|
+
const ordered: BlockChange[] = [];
|
|
72
|
+
|
|
73
|
+
for (const draftBlock of draftBlocks) {
|
|
74
|
+
const docBlock = docById.get(draftBlock.id);
|
|
75
|
+
if (!docBlock) {
|
|
76
|
+
added.push(draftBlock);
|
|
77
|
+
ordered.push({ kind: 'added', block: draftBlock });
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
const docJson = docJsonById.get(draftBlock.id);
|
|
81
|
+
const draftJson = draftJsonById.get(draftBlock.id);
|
|
82
|
+
if (blockJsonEqual(docJson, draftJson)) {
|
|
83
|
+
unchangedCount++;
|
|
84
|
+
} else {
|
|
85
|
+
changed.push({ from: docBlock, to: draftBlock });
|
|
86
|
+
ordered.push({ kind: 'changed', from: docBlock, to: draftBlock });
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
// Blocks only in the doc (not in draft) are removals — preserve doc order
|
|
90
|
+
// for these, then concat after the draft-ordered changes so the consumer
|
|
91
|
+
// can render "everything in draft order, then what got removed".
|
|
92
|
+
for (const docBlock of docBlocks) {
|
|
93
|
+
if (!draftById.has(docBlock.id)) {
|
|
94
|
+
removed.push(docBlock);
|
|
95
|
+
ordered.push({ kind: 'removed', block: docBlock });
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return { added, removed, changed, unchangedCount, ordered };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// ─── Helpers ───────────────────────────────────────────────────────────
|
|
103
|
+
|
|
104
|
+
type AnyNode = {
|
|
105
|
+
type?: string;
|
|
106
|
+
attrs?: Record<string, unknown> | null;
|
|
107
|
+
content?: AnyNode[];
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Walk a doc and collect every addressable block's full JSON, keyed by
|
|
112
|
+
* id. Used for content-equality in the diff. We compare full node JSON
|
|
113
|
+
* rather than just text so style changes (callout variant flip, heading
|
|
114
|
+
* level change, mark added/removed) register as 'changed' even if the
|
|
115
|
+
* plaintext is identical.
|
|
116
|
+
*/
|
|
117
|
+
function collectBlockJson(doc: Record<string, unknown>): Map<string, AnyNode> {
|
|
118
|
+
const out = new Map<string, AnyNode>();
|
|
119
|
+
walkCollect(doc as AnyNode, out);
|
|
120
|
+
return out;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function walkCollect(node: AnyNode, out: Map<string, AnyNode>): void {
|
|
124
|
+
if (!node || typeof node !== 'object') return;
|
|
125
|
+
const id = node.attrs && typeof node.attrs.id === 'string' ? node.attrs.id : null;
|
|
126
|
+
if (id) out.set(id, node);
|
|
127
|
+
if (Array.isArray(node.content)) {
|
|
128
|
+
for (const child of node.content) walkCollect(child, out);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Compare two PM block nodes for content equality. Uses stable
|
|
134
|
+
* (sorted-key) JSON stringification so two nodes with the same fields
|
|
135
|
+
* in different orders compare equal — necessary because ensureBlockIds
|
|
136
|
+
* produces `{ type, content, attrs }` (spread + add) while hand-built
|
|
137
|
+
* nodes (e.g. agent tool output, autosave round-trips through different
|
|
138
|
+
* serializers) often have `{ type, attrs, content }`. JSON-string
|
|
139
|
+
* compare is fast enough for the panel diff (typical block ~200 bytes;
|
|
140
|
+
* even a 200-block page is ~40 KB of stringification — one-shot per
|
|
141
|
+
* diff call).
|
|
142
|
+
*/
|
|
143
|
+
function blockJsonEqual(a: AnyNode | undefined, b: AnyNode | undefined): boolean {
|
|
144
|
+
if (a === b) return true;
|
|
145
|
+
if (!a || !b) return false;
|
|
146
|
+
return stableStringify(a) === stableStringify(b);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** JSON.stringify with sorted object keys at every depth. Arrays keep
|
|
150
|
+
* their order (semantic in PM). Stable across the runtime — the same
|
|
151
|
+
* input always produces the same string. */
|
|
152
|
+
function stableStringify(value: unknown): string {
|
|
153
|
+
return JSON.stringify(value, (_key, v) => {
|
|
154
|
+
if (v && typeof v === 'object' && !Array.isArray(v)) {
|
|
155
|
+
const sorted: Record<string, unknown> = {};
|
|
156
|
+
for (const k of Object.keys(v as Record<string, unknown>).sort()) {
|
|
157
|
+
sorted[k] = (v as Record<string, unknown>)[k];
|
|
158
|
+
}
|
|
159
|
+
return sorted;
|
|
160
|
+
}
|
|
161
|
+
return v;
|
|
162
|
+
});
|
|
163
|
+
}
|