@jsb188/mday 1.1.2
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/package.json +142 -0
- package/src/constants/agentMemory.ts +166 -0
- package/src/constants/agentNote.ts +212 -0
- package/src/constants/aiChat.ts +10 -0
- package/src/constants/billing.ts +126 -0
- package/src/constants/campaign.ts +236 -0
- package/src/constants/contact.ts +72 -0
- package/src/constants/csvExport.ts +133 -0
- package/src/constants/dataTable.ts +22 -0
- package/src/constants/dataTableFieldTypes.generated.ts +59 -0
- package/src/constants/docAccess.ts +33 -0
- package/src/constants/dt.ts +234 -0
- package/src/constants/email.ts +146 -0
- package/src/constants/fieldDoc.ts +104 -0
- package/src/constants/graphqlReactivity.ts +196 -0
- package/src/constants/inboundContact.ts +8 -0
- package/src/constants/integration.ts +13 -0
- package/src/constants/log.ts +21 -0
- package/src/constants/logDisplay/README.md +88 -0
- package/src/constants/logDisplay/defaults.ts +39 -0
- package/src/constants/logDisplay/index.ts +27 -0
- package/src/constants/logDisplay/operations/arable.ts +16 -0
- package/src/constants/logDisplay/operations/farmersMarket.ts +10 -0
- package/src/constants/logDisplay/operations/foodManufacturing.ts +13 -0
- package/src/constants/logDisplay/shared/accounting.ts +13 -0
- package/src/constants/logDisplay/shared/interactions.ts +13 -0
- package/src/constants/logDisplay/shared/rackTemperatures.ts +42 -0
- package/src/constants/logTaxonomy.generated.ts +2199 -0
- package/src/constants/noteDoc.ts +77 -0
- package/src/constants/organization.ts +60 -0
- package/src/constants/pageRoute.ts +23 -0
- package/src/constants/qrCode.ts +30 -0
- package/src/constants/sheet.ts +132 -0
- package/src/constants/sms.ts +18 -0
- package/src/constants/square.ts +16 -0
- package/src/constants/storage.ts +85 -0
- package/src/constants/usage.ts +82 -0
- package/src/constants/workflowSteps.ts +60 -0
- package/src/schemas/editLogEntry.ts +879 -0
- package/src/schemas/inboundContact.ts +77 -0
- package/src/schemas/logsQuery.ts +93 -0
- package/src/types/account.d.ts +81 -0
- package/src/types/agentMemory.d.ts +100 -0
- package/src/types/agentNote.d.ts +441 -0
- package/src/types/billing.d.ts +537 -0
- package/src/types/campaign.d.ts +1533 -0
- package/src/types/contact.d.ts +15 -0
- package/src/types/csvExport.d.ts +213 -0
- package/src/types/dataAnalysis.d.ts +44 -0
- package/src/types/dataTable.d.ts +159 -0
- package/src/types/definitionElement.d.ts +35 -0
- package/src/types/docAccess.d.ts +96 -0
- package/src/types/dt.d.ts +257 -0
- package/src/types/email.d.ts +231 -0
- package/src/types/emailReplyReport.d.ts +76 -0
- package/src/types/fieldDoc.d.ts +702 -0
- package/src/types/fieldDocContract.d.ts +90 -0
- package/src/types/inboundContact.d.ts +65 -0
- package/src/types/integration.d.ts +42 -0
- package/src/types/log.d.ts +338 -0
- package/src/types/logDisplay.types.ts +85 -0
- package/src/types/organization.d.ts +350 -0
- package/src/types/pageRoute.d.ts +48 -0
- package/src/types/qrCode.d.ts +96 -0
- package/src/types/richText.d.ts +81 -0
- package/src/types/sheet.d.ts +847 -0
- package/src/types/sms.d.ts +38 -0
- package/src/types/storage.d.ts +481 -0
- package/src/types/usage.d.ts +106 -0
- package/src/types/workflow.d.ts +435 -0
- package/src/utils/agentMemory.ts +15 -0
- package/src/utils/agentNote.ts +850 -0
- package/src/utils/aiChat.ts +43 -0
- package/src/utils/aiChatWork.ts +64 -0
- package/src/utils/billing.ts +37 -0
- package/src/utils/campaign.ts +1173 -0
- package/src/utils/campaignAI.ts +72 -0
- package/src/utils/campaignAccess.ts +239 -0
- package/src/utils/campaignEmail.ts +601 -0
- package/src/utils/campaignEmailCompiler.ts +372 -0
- package/src/utils/campaignEmailImages.ts +60 -0
- package/src/utils/campaignEmailStyles.ts +544 -0
- package/src/utils/campaignLifecycle.ts +104 -0
- package/src/utils/campaignPreview.ts +285 -0
- package/src/utils/contact.ts +50 -0
- package/src/utils/csvExport.ts +19 -0
- package/src/utils/dataTable.ts +311 -0
- package/src/utils/dataTableFieldTypeRegistry.ts +106 -0
- package/src/utils/dataTableRelativeDate.ts +447 -0
- package/src/utils/definitionElement.ts +84 -0
- package/src/utils/docAccess.ts +383 -0
- package/src/utils/documentName.ts +53 -0
- package/src/utils/email.ts +379 -0
- package/src/utils/emailReplyReport.test.ts +187 -0
- package/src/utils/emailReplyReport.ts +143 -0
- package/src/utils/fieldDoc.ts +157 -0
- package/src/utils/fieldDocAnswers.ts +128 -0
- package/src/utils/fieldDocDefaults.ts +36 -0
- package/src/utils/fieldDocProgram.ts +36 -0
- package/src/utils/fieldDocSummary.ts +30 -0
- package/src/utils/log.ts +357 -0
- package/src/utils/logDisplay.helpers.ts +177 -0
- package/src/utils/logDisplay.ts +87 -0
- package/src/utils/logRegistryValue.ts +90 -0
- package/src/utils/logStoreRead.ts +262 -0
- package/src/utils/logTaxonomy.helpers.ts +17 -0
- package/src/utils/logTaxonomy.ts +76 -0
- package/src/utils/logTaxonomyDisplay.ts +395 -0
- package/src/utils/noteDoc.ts +609 -0
- package/src/utils/organization.ts +669 -0
- package/src/utils/pageRoute.ts +71 -0
- package/src/utils/qrCode.helpers.ts +189 -0
- package/src/utils/qrCode.test.ts +519 -0
- package/src/utils/qrCode.ts +273 -0
- package/src/utils/richTextHTML.ts +385 -0
- package/src/utils/sheet.ts +262 -0
- package/src/utils/sheetAxisMove.ts +616 -0
- package/src/utils/sheetBootstrap.ts +1402 -0
- package/src/utils/sheetCellLiteral.ts +101 -0
- package/src/utils/sheetCommand.ts +358 -0
- package/src/utils/sheetDesign.ts +705 -0
- package/src/utils/sheetExport.ts +186 -0
- package/src/utils/sheetFeatureReference.ts +183 -0
- package/src/utils/sheetFormulaArgs.ts +168 -0
- package/src/utils/sheetFormulaCompile.ts +475 -0
- package/src/utils/sheetFormulaConditionalAggregate.ts +254 -0
- package/src/utils/sheetFormulaCriteria.ts +656 -0
- package/src/utils/sheetFormulaDate.ts +444 -0
- package/src/utils/sheetFormulaEvaluation.ts +899 -0
- package/src/utils/sheetFormulaEvaluator.ts +997 -0
- package/src/utils/sheetFormulaFunctionHandlers.ts +403 -0
- package/src/utils/sheetFormulaLogical.ts +116 -0
- package/src/utils/sheetFormulaLookup.ts +488 -0
- package/src/utils/sheetFormulaMath.ts +48 -0
- package/src/utils/sheetFormulaParser.ts +729 -0
- package/src/utils/sheetFormulaRange.ts +330 -0
- package/src/utils/sheetFormulaReference.ts +703 -0
- package/src/utils/sheetFormulaReferenceAggregates.ts +313 -0
- package/src/utils/sheetFormulaReferenceDates.ts +237 -0
- package/src/utils/sheetFormulaReferenceLogical.ts +330 -0
- package/src/utils/sheetFormulaReferenceLookup.ts +341 -0
- package/src/utils/sheetFormulaReferenceMath.ts +95 -0
- package/src/utils/sheetFormulaReferenceRewrite.ts +220 -0
- package/src/utils/sheetFormulaReferenceText.ts +462 -0
- package/src/utils/sheetFormulaSyntax.ts +1027 -0
- package/src/utils/sheetFormulaText.ts +430 -0
- package/src/utils/sheetJournal.ts +200 -0
- package/src/utils/sheetNumberFormat.ts +1012 -0
- package/src/utils/sheetPreview.ts +68 -0
- package/src/utils/sheetSequence.ts +118 -0
- package/src/utils/sheetSnapshot.ts +739 -0
- package/src/utils/sheetStructureEdit.ts +914 -0
- package/src/utils/sheetStructureReferences.ts +389 -0
- package/src/utils/square.ts +32 -0
- package/src/utils/storage.ts +198 -0
- package/src/utils/table-route.ts +262 -0
- package/src/utils/time.ts +12 -0
- package/src/utils/workflow.ts +506 -0
- package/src/utils/workflowLogFingerprint.ts +302 -0
- package/src/utils/workflowLogRevisionSnapshot.ts +424 -0
- package/src/utils/workflowProgress.ts +280 -0
- package/src/utils/workflowTriggers.ts +61 -0
- package/tests/agentNote.test.ts +395 -0
- package/tests/campaignAI.test.ts +119 -0
- package/tests/campaignAccess.test.ts +239 -0
- package/tests/campaignEmail.test.ts +441 -0
- package/tests/campaignLifecycle.test.ts +134 -0
- package/tests/campaignPublication.test.ts +309 -0
- package/tests/dataTableFieldTypeRegistry.test.ts +111 -0
- package/tests/dataTableRelativeDate.test.ts +412 -0
- package/tests/docAccess.test.ts +525 -0
- package/tests/editLogEntry.test.ts +112 -0
- package/tests/email.test.ts +39 -0
- package/tests/fieldDocProgram.test.ts +114 -0
- package/tests/logDisplay.fixtures.ts +67 -0
- package/tests/logDisplay.test.ts +227 -0
- package/tests/noteDoc.test.ts +514 -0
- package/tests/richTextHTML.test.ts +185 -0
- package/tests/sheetAxisMove.test.ts +352 -0
- package/tests/sheetBootstrap.test.ts +1967 -0
- package/tests/sheetFeatureReference.test.ts +22 -0
- package/tests/sheetFormulaCriteria.test.ts +269 -0
- package/tests/sheetFormulaEvaluator.test.ts +468 -0
- package/tests/sheetFormulaFunctions.test.ts +538 -0
- package/tests/sheetFormulaLanguage.test.ts +361 -0
- package/tests/sheetFormulaParity.fixtures.ts +872 -0
- package/tests/sheetFormulaParity.test.ts +188 -0
- package/tests/sheetFormulaRange.test.ts +167 -0
- package/tests/sheetFormulaReference.test.ts +78 -0
- package/tests/sheetFormulaReferenceRewrite.test.ts +117 -0
- package/tests/sheetFormulaTextDate.test.ts +512 -0
- package/tests/sheetSequence.test.ts +22 -0
- package/tests/sheetSnapshot.test.ts +134 -0
- package/tests/sheetStructureEdit.test.ts +537 -0
- package/tests/sheetStructureReferences.test.ts +589 -0
- package/tests/workflowLifecycleStatus.test.ts +60 -0
- package/tests/workflowLogFingerprint.test.ts +203 -0
- package/tests/workflowLogRevisionSnapshot.test.ts +96 -0
- package/tests/workflowProgress.test.ts +180 -0
- package/tests/workflowTriggers.test.ts +29 -0
- package/tsconfig.json +12 -0
- package/vitest.config.ts +8 -0
|
@@ -0,0 +1,514 @@
|
|
|
1
|
+
import { NOTE_DOC_VERSION, NOTE_MAX_BLOCKS, NOTE_MAX_TEXT_CHARS } from '@jsb188/mday/constants/noteDoc.ts';
|
|
2
|
+
import type { RichTextContent, RichTextInlineContent } from '@jsb188/mday/types/richText.d.ts';
|
|
3
|
+
import { createEmptyNoteDoc, noteDocToPlainText, sanitizeNoteDoc } from '@jsb188/mday/utils/noteDoc.ts';
|
|
4
|
+
import { describe, expect, it } from 'vitest';
|
|
5
|
+
|
|
6
|
+
/*
|
|
7
|
+
* Build one doc root around the given raw block list.
|
|
8
|
+
*/
|
|
9
|
+
function buildDoc(content: any[]): any {
|
|
10
|
+
return {
|
|
11
|
+
type: 'doc',
|
|
12
|
+
version: NOTE_DOC_VERSION,
|
|
13
|
+
content,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/*
|
|
18
|
+
* Build one paragraph with a single plain text run.
|
|
19
|
+
*/
|
|
20
|
+
function buildParagraph(text: string): any {
|
|
21
|
+
return {
|
|
22
|
+
type: 'paragraph',
|
|
23
|
+
content: [{ type: 'text', text }],
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/*
|
|
28
|
+
* Build one image block with valid attrs.
|
|
29
|
+
*/
|
|
30
|
+
function buildImage(storageId: string | number, extraAttrs: Record<string, any> = {}): any {
|
|
31
|
+
return {
|
|
32
|
+
type: 'image',
|
|
33
|
+
attrs: {
|
|
34
|
+
storageId,
|
|
35
|
+
fileUri: `uploads/img-${storageId}.png`,
|
|
36
|
+
...extraAttrs,
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
describe('createEmptyNoteDoc', () => {
|
|
42
|
+
it('builds one versioned doc with a single empty paragraph', () => {
|
|
43
|
+
expect(createEmptyNoteDoc()).toEqual({
|
|
44
|
+
type: 'doc',
|
|
45
|
+
version: NOTE_DOC_VERSION,
|
|
46
|
+
content: [{ type: 'paragraph', content: [] }],
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
describe('sanitizeNoteDoc', () => {
|
|
52
|
+
it('coerces unknown roots to an empty doc', () => {
|
|
53
|
+
for (const input of [null, undefined, 'hello', 42, [], { type: 'paragraph' }, { content: [] }]) {
|
|
54
|
+
expect(sanitizeNoteDoc(input)).toEqual(createEmptyNoteDoc());
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('guarantees a non-empty doc and always stamps the current version', () => {
|
|
59
|
+
const output = sanitizeNoteDoc({ type: 'doc', version: 99, content: [] });
|
|
60
|
+
expect(output.version).toBe(NOTE_DOC_VERSION);
|
|
61
|
+
expect(output.content).toEqual([{ type: 'paragraph', content: [] }]);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('keeps a legal document intact', () => {
|
|
65
|
+
const input = buildDoc([
|
|
66
|
+
{
|
|
67
|
+
type: 'heading',
|
|
68
|
+
attrs: { level: 2 },
|
|
69
|
+
content: [{ type: 'text', text: 'Title' }],
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
type: 'paragraph',
|
|
73
|
+
content: [{ type: 'text', text: 'Body', marks: [{ type: 'bold' }] }],
|
|
74
|
+
},
|
|
75
|
+
{ type: 'divider' },
|
|
76
|
+
{
|
|
77
|
+
type: 'bulletList',
|
|
78
|
+
content: [
|
|
79
|
+
{ type: 'listItem', content: [buildParagraph('one')] },
|
|
80
|
+
{ type: 'listItem', content: [buildParagraph('two')] },
|
|
81
|
+
],
|
|
82
|
+
},
|
|
83
|
+
{ type: 'blockquote', content: [buildParagraph('quoted')] },
|
|
84
|
+
buildImage('55', { alt: 'photo', width: 320 }),
|
|
85
|
+
]);
|
|
86
|
+
expect(sanitizeNoteDoc(input).content).toEqual(input.content);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('drops unknown block types and unknown inline types', () => {
|
|
90
|
+
const output = sanitizeNoteDoc(
|
|
91
|
+
buildDoc([
|
|
92
|
+
{ type: 'iframe', content: [{ type: 'text', text: 'evil' }] },
|
|
93
|
+
{
|
|
94
|
+
type: 'paragraph',
|
|
95
|
+
content: [
|
|
96
|
+
{ type: 'emoji', name: 'x' },
|
|
97
|
+
{ type: 'text', text: 'ok' },
|
|
98
|
+
],
|
|
99
|
+
},
|
|
100
|
+
]),
|
|
101
|
+
);
|
|
102
|
+
expect(output.content).toEqual([{ type: 'paragraph', content: [{ type: 'text', text: 'ok' }] }]);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('strips client-only fields and omits unfinished image uploads', () => {
|
|
106
|
+
const output = sanitizeNoteDoc(
|
|
107
|
+
buildDoc([
|
|
108
|
+
{
|
|
109
|
+
type: 'paragraph',
|
|
110
|
+
_key: 'k1',
|
|
111
|
+
junk: true,
|
|
112
|
+
attrs: { junk: 1 },
|
|
113
|
+
content: [{ type: 'text', text: 'a', junk: 2 }],
|
|
114
|
+
},
|
|
115
|
+
{ ...buildImage('9'), _uploading: true, _key: 'k2' },
|
|
116
|
+
]),
|
|
117
|
+
);
|
|
118
|
+
expect(output.content[0]).toEqual({
|
|
119
|
+
type: 'paragraph',
|
|
120
|
+
content: [{ type: 'text', text: 'a' }],
|
|
121
|
+
});
|
|
122
|
+
expect(output.content).toHaveLength(1);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it('clamps heading levels into 1-3', () => {
|
|
126
|
+
const output = sanitizeNoteDoc(
|
|
127
|
+
buildDoc([
|
|
128
|
+
{ type: 'heading', attrs: { level: 0 }, content: [] },
|
|
129
|
+
{ type: 'heading', attrs: { level: 5 }, content: [] },
|
|
130
|
+
{ type: 'heading', attrs: { level: '2' }, content: [] },
|
|
131
|
+
{ type: 'heading', content: [] },
|
|
132
|
+
]),
|
|
133
|
+
);
|
|
134
|
+
expect(output.content.map((block: RichTextContent) => block.attrs?.level)).toEqual([1, 3, 2, 1]);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it('whitelists mark types and drops duplicates', () => {
|
|
138
|
+
const output = sanitizeNoteDoc(
|
|
139
|
+
buildDoc([
|
|
140
|
+
{
|
|
141
|
+
type: 'paragraph',
|
|
142
|
+
content: [
|
|
143
|
+
{
|
|
144
|
+
type: 'text',
|
|
145
|
+
text: 'x',
|
|
146
|
+
marks: [{ type: 'bold' }, { type: 'bold' }, { type: 'blink' }, { type: 'italic', attrs: { junk: 1 } }],
|
|
147
|
+
},
|
|
148
|
+
],
|
|
149
|
+
},
|
|
150
|
+
]),
|
|
151
|
+
);
|
|
152
|
+
expect((output.content[0].content![0] as RichTextInlineContent).marks).toEqual([{ type: 'bold' }, { type: 'italic' }]);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it('validates color/highlight tokens against the palette', () => {
|
|
156
|
+
const output = sanitizeNoteDoc(
|
|
157
|
+
buildDoc([
|
|
158
|
+
{
|
|
159
|
+
type: 'paragraph',
|
|
160
|
+
content: [
|
|
161
|
+
{
|
|
162
|
+
type: 'text',
|
|
163
|
+
text: 'x',
|
|
164
|
+
marks: [
|
|
165
|
+
{ type: 'color', attrs: { token: 'red' } },
|
|
166
|
+
{ type: 'highlight', attrs: { token: '#ff0000' } },
|
|
167
|
+
],
|
|
168
|
+
},
|
|
169
|
+
],
|
|
170
|
+
},
|
|
171
|
+
]),
|
|
172
|
+
);
|
|
173
|
+
expect((output.content[0].content![0] as RichTextInlineContent).marks).toEqual([{ type: 'color', attrs: { token: 'red' } }]);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it('keeps http(s)/mailto links and kills javascript: URLs', () => {
|
|
177
|
+
const output = sanitizeNoteDoc(
|
|
178
|
+
buildDoc([
|
|
179
|
+
{
|
|
180
|
+
type: 'paragraph',
|
|
181
|
+
content: [
|
|
182
|
+
{
|
|
183
|
+
type: 'text',
|
|
184
|
+
text: 'a',
|
|
185
|
+
marks: [{ type: 'link', attrs: { href: 'https://example.com' } }],
|
|
186
|
+
},
|
|
187
|
+
{
|
|
188
|
+
type: 'text',
|
|
189
|
+
text: 'b',
|
|
190
|
+
marks: [{ type: 'link', attrs: { href: 'mailto:x@y.com' } }],
|
|
191
|
+
},
|
|
192
|
+
{
|
|
193
|
+
type: 'text',
|
|
194
|
+
text: 'c',
|
|
195
|
+
marks: [{ type: 'link', attrs: { href: ' HTTP://example.com ' } }],
|
|
196
|
+
},
|
|
197
|
+
{
|
|
198
|
+
type: 'text',
|
|
199
|
+
text: 'd',
|
|
200
|
+
marks: [{ type: 'link', attrs: { href: 'javascript:alert(1)' } }],
|
|
201
|
+
},
|
|
202
|
+
{
|
|
203
|
+
type: 'text',
|
|
204
|
+
text: 'e',
|
|
205
|
+
marks: [{ type: 'link', attrs: { href: ' JaVaScRiPt:alert(1)' } }],
|
|
206
|
+
},
|
|
207
|
+
{
|
|
208
|
+
type: 'text',
|
|
209
|
+
text: 'f',
|
|
210
|
+
marks: [{ type: 'link', attrs: { href: 'data:text/html,x' } }],
|
|
211
|
+
},
|
|
212
|
+
],
|
|
213
|
+
},
|
|
214
|
+
]),
|
|
215
|
+
);
|
|
216
|
+
const runs = output.content[0].content as RichTextInlineContent[];
|
|
217
|
+
expect(runs[0].marks).toEqual([{ type: 'link', attrs: { href: 'https://example.com' } }]);
|
|
218
|
+
expect(runs[1].marks).toEqual([{ type: 'link', attrs: { href: 'mailto:x@y.com' } }]);
|
|
219
|
+
expect(runs[2].marks).toEqual([{ type: 'link', attrs: { href: 'HTTP://example.com' } }]);
|
|
220
|
+
// The three unsafe runs lose their link mark and merge into one run
|
|
221
|
+
expect(runs[3]).toEqual({ type: 'text', text: 'def' });
|
|
222
|
+
expect(runs).toHaveLength(4);
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
it('drops empty text runs and merges adjacent runs with identical mark sets', () => {
|
|
226
|
+
const output = sanitizeNoteDoc(
|
|
227
|
+
buildDoc([
|
|
228
|
+
{
|
|
229
|
+
type: 'paragraph',
|
|
230
|
+
content: [
|
|
231
|
+
{ type: 'text', text: '' },
|
|
232
|
+
{
|
|
233
|
+
type: 'text',
|
|
234
|
+
text: 'a',
|
|
235
|
+
marks: [{ type: 'bold' }, { type: 'italic' }],
|
|
236
|
+
},
|
|
237
|
+
{
|
|
238
|
+
type: 'text',
|
|
239
|
+
text: 'b',
|
|
240
|
+
marks: [{ type: 'italic' }, { type: 'bold' }],
|
|
241
|
+
},
|
|
242
|
+
{ type: 'text', text: 'c', marks: [{ type: 'bold' }] },
|
|
243
|
+
{ type: 'hardBreak' },
|
|
244
|
+
{ type: 'text', text: 'd', marks: [{ type: 'bold' }] },
|
|
245
|
+
],
|
|
246
|
+
},
|
|
247
|
+
]),
|
|
248
|
+
);
|
|
249
|
+
expect(output.content[0].content).toEqual([
|
|
250
|
+
{
|
|
251
|
+
type: 'text',
|
|
252
|
+
text: 'ab',
|
|
253
|
+
marks: [{ type: 'bold' }, { type: 'italic' }],
|
|
254
|
+
},
|
|
255
|
+
{ type: 'text', text: 'c', marks: [{ type: 'bold' }] },
|
|
256
|
+
{ type: 'hardBreak' },
|
|
257
|
+
{ type: 'text', text: 'd', marks: [{ type: 'bold' }] },
|
|
258
|
+
]);
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
it('drops image blocks with missing or non-string storage ids', () => {
|
|
262
|
+
const output = sanitizeNoteDoc(
|
|
263
|
+
buildDoc([
|
|
264
|
+
{ type: 'image', attrs: { fileUri: 'uploads/no-id.png' } },
|
|
265
|
+
{ type: 'image', attrs: { storageId: '3' } },
|
|
266
|
+
buildImage(7, { width: '300', alt: '' }),
|
|
267
|
+
buildImage('8', { width: '300', alt: '' }),
|
|
268
|
+
]),
|
|
269
|
+
);
|
|
270
|
+
expect(output.content).toEqual([
|
|
271
|
+
{
|
|
272
|
+
type: 'image',
|
|
273
|
+
attrs: { storageId: '8', fileUri: 'uploads/img-8.png' },
|
|
274
|
+
},
|
|
275
|
+
]);
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
it('wraps stray text blocks and inline runs found inside lists / at doc level', () => {
|
|
279
|
+
const output = sanitizeNoteDoc(
|
|
280
|
+
buildDoc([
|
|
281
|
+
{ type: 'text', text: 'stray' },
|
|
282
|
+
{
|
|
283
|
+
type: 'bulletList',
|
|
284
|
+
content: [
|
|
285
|
+
buildParagraph('loose'),
|
|
286
|
+
{
|
|
287
|
+
type: 'heading',
|
|
288
|
+
attrs: { level: 1 },
|
|
289
|
+
content: [{ type: 'text', text: 'head' }],
|
|
290
|
+
},
|
|
291
|
+
],
|
|
292
|
+
},
|
|
293
|
+
]),
|
|
294
|
+
);
|
|
295
|
+
expect(output.content).toEqual([
|
|
296
|
+
{ type: 'paragraph', content: [{ type: 'text', text: 'stray' }] },
|
|
297
|
+
{
|
|
298
|
+
type: 'bulletList',
|
|
299
|
+
content: [
|
|
300
|
+
{
|
|
301
|
+
type: 'listItem',
|
|
302
|
+
content: [{ type: 'paragraph', content: [{ type: 'text', text: 'loose' }] }],
|
|
303
|
+
},
|
|
304
|
+
{
|
|
305
|
+
type: 'listItem',
|
|
306
|
+
content: [{ type: 'paragraph', content: [{ type: 'text', text: 'head' }] }],
|
|
307
|
+
},
|
|
308
|
+
],
|
|
309
|
+
},
|
|
310
|
+
]);
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
it('unwraps illegal containers instead of keeping bad nesting', () => {
|
|
314
|
+
const output = sanitizeNoteDoc(
|
|
315
|
+
buildDoc([
|
|
316
|
+
// listItem directly under doc splices its children in
|
|
317
|
+
{ type: 'listItem', content: [buildParagraph('lifted')] },
|
|
318
|
+
// lists and headings inside a blockquote flatten to paragraphs
|
|
319
|
+
{
|
|
320
|
+
type: 'blockquote',
|
|
321
|
+
content: [
|
|
322
|
+
{
|
|
323
|
+
type: 'heading',
|
|
324
|
+
attrs: { level: 2 },
|
|
325
|
+
content: [{ type: 'text', text: 'h' }],
|
|
326
|
+
},
|
|
327
|
+
{
|
|
328
|
+
type: 'bulletList',
|
|
329
|
+
content: [{ type: 'listItem', content: [buildParagraph('li')] }],
|
|
330
|
+
},
|
|
331
|
+
],
|
|
332
|
+
},
|
|
333
|
+
]),
|
|
334
|
+
);
|
|
335
|
+
expect(output.content).toEqual([
|
|
336
|
+
{ type: 'paragraph', content: [{ type: 'text', text: 'lifted' }] },
|
|
337
|
+
{
|
|
338
|
+
type: 'blockquote',
|
|
339
|
+
content: [
|
|
340
|
+
{ type: 'paragraph', content: [{ type: 'text', text: 'h' }] },
|
|
341
|
+
{ type: 'paragraph', content: [{ type: 'text', text: 'li' }] },
|
|
342
|
+
],
|
|
343
|
+
},
|
|
344
|
+
]);
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
it('drops void leaves with no legal spot and lists left without items', () => {
|
|
348
|
+
const output = sanitizeNoteDoc(
|
|
349
|
+
buildDoc([{ type: 'bulletList', content: [{ type: 'divider' }, buildImage('4')] }, buildParagraph('kept')]),
|
|
350
|
+
);
|
|
351
|
+
expect(output.content).toEqual([{ type: 'paragraph', content: [{ type: 'text', text: 'kept' }] }]);
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
it('fills emptied listItems and blockquotes with one empty paragraph', () => {
|
|
355
|
+
const output = sanitizeNoteDoc(
|
|
356
|
+
buildDoc([
|
|
357
|
+
{
|
|
358
|
+
type: 'bulletList',
|
|
359
|
+
content: [{ type: 'listItem', content: [{ type: 'divider' }] }],
|
|
360
|
+
},
|
|
361
|
+
{ type: 'blockquote', content: [] },
|
|
362
|
+
]),
|
|
363
|
+
);
|
|
364
|
+
expect(output.content).toEqual([
|
|
365
|
+
{
|
|
366
|
+
type: 'bulletList',
|
|
367
|
+
content: [{ type: 'listItem', content: [{ type: 'paragraph', content: [] }] }],
|
|
368
|
+
},
|
|
369
|
+
{ type: 'blockquote', content: [{ type: 'paragraph', content: [] }] },
|
|
370
|
+
]);
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
it('flattens lists nested past NOTE_MAX_DEPTH into text-preserving paragraphs', () => {
|
|
374
|
+
// list(1) > li(2) > list(3) > li(4) > list(5) > li(6): the depth-6
|
|
375
|
+
// container flattens into one paragraph, its nested text joined by
|
|
376
|
+
// hard breaks, wrapped back into the depth-5 list's listItem
|
|
377
|
+
const overDeepItem = {
|
|
378
|
+
type: 'listItem',
|
|
379
|
+
content: [
|
|
380
|
+
buildParagraph('a'),
|
|
381
|
+
{
|
|
382
|
+
type: 'bulletList',
|
|
383
|
+
content: [{ type: 'listItem', content: [buildParagraph('b')] }],
|
|
384
|
+
},
|
|
385
|
+
],
|
|
386
|
+
};
|
|
387
|
+
const output = sanitizeNoteDoc(
|
|
388
|
+
buildDoc([
|
|
389
|
+
{
|
|
390
|
+
type: 'bulletList',
|
|
391
|
+
content: [
|
|
392
|
+
{
|
|
393
|
+
type: 'listItem',
|
|
394
|
+
content: [
|
|
395
|
+
{
|
|
396
|
+
type: 'bulletList',
|
|
397
|
+
content: [
|
|
398
|
+
{
|
|
399
|
+
type: 'listItem',
|
|
400
|
+
content: [{ type: 'bulletList', content: [overDeepItem] }],
|
|
401
|
+
},
|
|
402
|
+
],
|
|
403
|
+
},
|
|
404
|
+
],
|
|
405
|
+
},
|
|
406
|
+
],
|
|
407
|
+
},
|
|
408
|
+
]),
|
|
409
|
+
);
|
|
410
|
+
|
|
411
|
+
const level3List = (output.content[0] as any).content[0].content[0].content[0].content[0] as RichTextContent;
|
|
412
|
+
expect(level3List.type).toBe('bulletList');
|
|
413
|
+
expect(level3List.content).toEqual([
|
|
414
|
+
{
|
|
415
|
+
type: 'listItem',
|
|
416
|
+
content: [
|
|
417
|
+
{
|
|
418
|
+
type: 'paragraph',
|
|
419
|
+
content: [{ type: 'text', text: 'a' }, { type: 'hardBreak' }, { type: 'text', text: 'b' }],
|
|
420
|
+
},
|
|
421
|
+
],
|
|
422
|
+
},
|
|
423
|
+
]);
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
it('truncates the tail past NOTE_MAX_BLOCKS', () => {
|
|
427
|
+
const output = sanitizeNoteDoc(buildDoc(Array.from({ length: NOTE_MAX_BLOCKS + 50 }, (_, i) => buildParagraph(`p${i}`))));
|
|
428
|
+
expect(output.content).toHaveLength(NOTE_MAX_BLOCKS);
|
|
429
|
+
expect((output.content[NOTE_MAX_BLOCKS - 1].content![0] as RichTextInlineContent).text).toBe(`p${NOTE_MAX_BLOCKS - 1}`);
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
it('truncates text past NOTE_MAX_TEXT_CHARS', () => {
|
|
433
|
+
const output = sanitizeNoteDoc(
|
|
434
|
+
buildDoc([buildParagraph('x'.repeat(NOTE_MAX_TEXT_CHARS - 5)), buildParagraph('y'.repeat(50)), buildParagraph('z')]),
|
|
435
|
+
);
|
|
436
|
+
expect((output.content[0].content![0] as RichTextInlineContent).text).toHaveLength(NOTE_MAX_TEXT_CHARS - 5);
|
|
437
|
+
// Second paragraph keeps only the 5 chars left in the budget
|
|
438
|
+
expect((output.content[1].content![0] as RichTextInlineContent).text).toBe('yyyyy');
|
|
439
|
+
// Later blocks survive with their text dropped
|
|
440
|
+
expect(output.content[2]).toEqual({ type: 'paragraph', content: [] });
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
it('spends the text budget on hardBreaks so a hardBreak flood truncates', () => {
|
|
444
|
+
const output = sanitizeNoteDoc(
|
|
445
|
+
buildDoc([
|
|
446
|
+
{
|
|
447
|
+
type: 'paragraph',
|
|
448
|
+
content: Array.from({ length: NOTE_MAX_TEXT_CHARS + 5000 }, () => ({
|
|
449
|
+
type: 'hardBreak',
|
|
450
|
+
})),
|
|
451
|
+
},
|
|
452
|
+
]),
|
|
453
|
+
);
|
|
454
|
+
// Only NOTE_MAX_TEXT_CHARS hardBreaks survive (1 char each)
|
|
455
|
+
expect(output.content[0].content).toHaveLength(NOTE_MAX_TEXT_CHARS);
|
|
456
|
+
// The spent budget also blocks text in later blocks
|
|
457
|
+
const after = sanitizeNoteDoc(
|
|
458
|
+
buildDoc([
|
|
459
|
+
{
|
|
460
|
+
type: 'paragraph',
|
|
461
|
+
content: Array.from({ length: NOTE_MAX_TEXT_CHARS }, () => ({
|
|
462
|
+
type: 'hardBreak',
|
|
463
|
+
})),
|
|
464
|
+
},
|
|
465
|
+
buildParagraph('overflow'),
|
|
466
|
+
]),
|
|
467
|
+
);
|
|
468
|
+
expect(after.content[1]).toEqual({ type: 'paragraph', content: [] });
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
it('survives hostile deeply-nested input without overflowing', () => {
|
|
472
|
+
// 500 levels of listItem-in-listItem raw nesting (every level illegal)
|
|
473
|
+
let node: any = buildParagraph('deep');
|
|
474
|
+
for (let i = 0; i < 500; i++) {
|
|
475
|
+
node = { type: 'listItem', content: [node] };
|
|
476
|
+
}
|
|
477
|
+
const output = sanitizeNoteDoc(buildDoc([node]));
|
|
478
|
+
expect(output.type).toBe('doc');
|
|
479
|
+
expect(output.content.length).toBeGreaterThan(0);
|
|
480
|
+
});
|
|
481
|
+
});
|
|
482
|
+
|
|
483
|
+
describe('noteDocToPlainText', () => {
|
|
484
|
+
it('renders one line per text-bearing block, with hardBreaks as newlines', () => {
|
|
485
|
+
const doc = sanitizeNoteDoc(
|
|
486
|
+
buildDoc([
|
|
487
|
+
{
|
|
488
|
+
type: 'heading',
|
|
489
|
+
attrs: { level: 1 },
|
|
490
|
+
content: [{ type: 'text', text: 'Title' }],
|
|
491
|
+
},
|
|
492
|
+
{
|
|
493
|
+
type: 'paragraph',
|
|
494
|
+
content: [{ type: 'text', text: 'line one' }, { type: 'hardBreak' }, { type: 'text', text: 'line two' }],
|
|
495
|
+
},
|
|
496
|
+
{ type: 'divider' },
|
|
497
|
+
buildImage('1'),
|
|
498
|
+
{
|
|
499
|
+
type: 'bulletList',
|
|
500
|
+
content: [
|
|
501
|
+
{ type: 'listItem', content: [buildParagraph('item a')] },
|
|
502
|
+
{ type: 'listItem', content: [buildParagraph('item b')] },
|
|
503
|
+
],
|
|
504
|
+
},
|
|
505
|
+
{ type: 'paragraph', content: [] },
|
|
506
|
+
]),
|
|
507
|
+
);
|
|
508
|
+
expect(noteDocToPlainText(doc)).toBe('Title\nline one\nline two\nitem a\nitem b');
|
|
509
|
+
});
|
|
510
|
+
|
|
511
|
+
it('returns an empty string for an empty doc', () => {
|
|
512
|
+
expect(noteDocToPlainText(createEmptyNoteDoc())).toBe('');
|
|
513
|
+
});
|
|
514
|
+
});
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { NOTE_DOC_VERSION } from '@jsb188/mday/constants/noteDoc.ts';
|
|
2
|
+
import {
|
|
3
|
+
containsRichTextHTMLMarkup,
|
|
4
|
+
richTextHTMLToDocument,
|
|
5
|
+
} from '@jsb188/mday/utils/richTextHTML.ts';
|
|
6
|
+
import { describe, expect, it } from 'vitest';
|
|
7
|
+
|
|
8
|
+
describe('containsRichTextHTMLMarkup', () => {
|
|
9
|
+
it('distinguishes ordinary plain text from HTML-like input', () => {
|
|
10
|
+
expect(containsRichTextHTMLMarkup('Two lines\nwithout markup')).toBe(false);
|
|
11
|
+
expect(containsRichTextHTMLMarkup('<p>Paragraph</p>')).toBe(true);
|
|
12
|
+
expect(containsRichTextHTMLMarkup('<script>alert(1)</script>')).toBe(false);
|
|
13
|
+
});
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
describe('richTextHTMLToDocument', () => {
|
|
17
|
+
it('preserves plain-text line structure through the canonical converter', () => {
|
|
18
|
+
expect(richTextHTMLToDocument('First line\n\nSecond line')).toEqual({
|
|
19
|
+
type: 'doc',
|
|
20
|
+
version: NOTE_DOC_VERSION,
|
|
21
|
+
content: [
|
|
22
|
+
{ type: 'paragraph', content: [{ type: 'text', text: 'First line' }] },
|
|
23
|
+
{ type: 'paragraph', content: [{ type: 'text', text: 'Second line' }] },
|
|
24
|
+
],
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('maps semantic HTML and decoded entities into rich-text nodes and marks', () => {
|
|
29
|
+
expect(
|
|
30
|
+
richTextHTMLToDocument(
|
|
31
|
+
'<p>Hello <strong>bold & <em>italic</em></strong><br>done</p>',
|
|
32
|
+
),
|
|
33
|
+
).toEqual({
|
|
34
|
+
type: 'doc',
|
|
35
|
+
version: NOTE_DOC_VERSION,
|
|
36
|
+
content: [
|
|
37
|
+
{
|
|
38
|
+
type: 'paragraph',
|
|
39
|
+
content: [
|
|
40
|
+
{ type: 'text', text: 'Hello ' },
|
|
41
|
+
{ type: 'text', text: 'bold & ', marks: [{ type: 'bold' }] },
|
|
42
|
+
{
|
|
43
|
+
type: 'text',
|
|
44
|
+
text: 'italic',
|
|
45
|
+
marks: [{ type: 'bold' }, { type: 'italic' }],
|
|
46
|
+
},
|
|
47
|
+
{ type: 'hardBreak' },
|
|
48
|
+
{ type: 'text', text: 'done' },
|
|
49
|
+
],
|
|
50
|
+
},
|
|
51
|
+
],
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('maps headings, lists, quotes, and dividers into legal block content', () => {
|
|
56
|
+
expect(
|
|
57
|
+
richTextHTMLToDocument(
|
|
58
|
+
'<h4>Heading</h4><ul><li>One</li><li><strong>Two</strong></li></ul><blockquote><p>Quote</p></blockquote><hr>',
|
|
59
|
+
),
|
|
60
|
+
).toEqual({
|
|
61
|
+
type: 'doc',
|
|
62
|
+
version: NOTE_DOC_VERSION,
|
|
63
|
+
content: [
|
|
64
|
+
{
|
|
65
|
+
type: 'heading',
|
|
66
|
+
attrs: { level: 3 },
|
|
67
|
+
content: [{ type: 'text', text: 'Heading' }],
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
type: 'bulletList',
|
|
71
|
+
content: [
|
|
72
|
+
{
|
|
73
|
+
type: 'listItem',
|
|
74
|
+
content: [
|
|
75
|
+
{ type: 'paragraph', content: [{ type: 'text', text: 'One' }] },
|
|
76
|
+
],
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
type: 'listItem',
|
|
80
|
+
content: [
|
|
81
|
+
{
|
|
82
|
+
type: 'paragraph',
|
|
83
|
+
content: [
|
|
84
|
+
{ type: 'text', text: 'Two', marks: [{ type: 'bold' }] },
|
|
85
|
+
],
|
|
86
|
+
},
|
|
87
|
+
],
|
|
88
|
+
},
|
|
89
|
+
],
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
type: 'blockquote',
|
|
93
|
+
content: [
|
|
94
|
+
{ type: 'paragraph', content: [{ type: 'text', text: 'Quote' }] },
|
|
95
|
+
],
|
|
96
|
+
},
|
|
97
|
+
{ type: 'divider' },
|
|
98
|
+
],
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('drops unsafe subtrees and unsafe link destinations', () => {
|
|
103
|
+
const document = richTextHTMLToDocument(
|
|
104
|
+
'<p><a href="javascript:alert(1)" onclick="evil()">Bad</a> <a href="https://example.com" style="color:red">Good</a><script>Hidden</script></p><iframe>Also hidden</iframe>',
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
expect(document).toEqual({
|
|
108
|
+
type: 'doc',
|
|
109
|
+
version: NOTE_DOC_VERSION,
|
|
110
|
+
content: [
|
|
111
|
+
{
|
|
112
|
+
type: 'paragraph',
|
|
113
|
+
content: [
|
|
114
|
+
{ type: 'text', text: 'Bad ' },
|
|
115
|
+
{
|
|
116
|
+
type: 'text',
|
|
117
|
+
text: 'Good',
|
|
118
|
+
marks: [{ type: 'link', attrs: { href: 'https://example.com' } }],
|
|
119
|
+
},
|
|
120
|
+
],
|
|
121
|
+
},
|
|
122
|
+
],
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it('uses HTML parsing rules for malformed nesting and unwraps benign unknown elements', () => {
|
|
127
|
+
expect(
|
|
128
|
+
richTextHTMLToDocument(
|
|
129
|
+
'<section><custom>Wrapped <b>content</b></custom><p>Next',
|
|
130
|
+
),
|
|
131
|
+
).toEqual({
|
|
132
|
+
type: 'doc',
|
|
133
|
+
version: NOTE_DOC_VERSION,
|
|
134
|
+
content: [
|
|
135
|
+
{
|
|
136
|
+
type: 'paragraph',
|
|
137
|
+
content: [
|
|
138
|
+
{ type: 'text', text: 'Wrapped ' },
|
|
139
|
+
{ type: 'text', text: 'content', marks: [{ type: 'bold' }] },
|
|
140
|
+
],
|
|
141
|
+
},
|
|
142
|
+
{ type: 'paragraph', content: [{ type: 'text', text: 'Next' }] },
|
|
143
|
+
],
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it('drops unsafe root elements without splitting surrounding inline content', () => {
|
|
148
|
+
expect(
|
|
149
|
+
richTextHTMLToDocument(
|
|
150
|
+
'<span>Before </span><script>Hidden</script><span>after</span>',
|
|
151
|
+
),
|
|
152
|
+
).toEqual({
|
|
153
|
+
type: 'doc',
|
|
154
|
+
version: NOTE_DOC_VERSION,
|
|
155
|
+
content: [
|
|
156
|
+
{
|
|
157
|
+
type: 'paragraph',
|
|
158
|
+
content: [{ type: 'text', text: 'Before after' }],
|
|
159
|
+
},
|
|
160
|
+
],
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it('preserves preformatted line endings as code-marked hard breaks', () => {
|
|
165
|
+
expect(richTextHTMLToDocument('<pre>line 1\n line 2</pre>')).toEqual({
|
|
166
|
+
type: 'doc',
|
|
167
|
+
version: NOTE_DOC_VERSION,
|
|
168
|
+
content: [
|
|
169
|
+
{
|
|
170
|
+
type: 'paragraph',
|
|
171
|
+
content: [
|
|
172
|
+
{ type: 'text', text: 'line 1', marks: [{ type: 'code' }] },
|
|
173
|
+
{ type: 'hardBreak' },
|
|
174
|
+
{ type: 'text', text: ' line 2', marks: [{ type: 'code' }] },
|
|
175
|
+
],
|
|
176
|
+
},
|
|
177
|
+
],
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it('bounds deeply nested untrusted markup without throwing', () => {
|
|
182
|
+
const nested = `${'<strong>'.repeat(5000)}Text${'</strong>'.repeat(5000)}`;
|
|
183
|
+
expect(() => richTextHTMLToDocument(nested)).not.toThrow();
|
|
184
|
+
});
|
|
185
|
+
});
|