markdown-patch 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -107
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +21 -0
- package/dist/constants.d.ts +3 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/debug.d.ts +3 -0
- package/dist/debug.d.ts.map +1 -0
- package/dist/debug.js +12 -3
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/map.d.ts +3 -0
- package/dist/map.d.ts.map +1 -0
- package/dist/map.js +47 -29
- package/dist/patch.d.ts +30 -0
- package/dist/patch.d.ts.map +1 -0
- package/dist/patch.js +205 -55
- package/dist/tests/map.test.d.ts +2 -0
- package/dist/tests/map.test.d.ts.map +1 -0
- package/dist/tests/map.test.js +23 -0
- package/dist/tests/patch.test.d.ts +2 -0
- package/dist/tests/patch.test.d.ts.map +1 -0
- package/dist/tests/patch.test.js +271 -42
- package/dist/typeGuards.d.ts +7 -0
- package/dist/typeGuards.d.ts.map +1 -0
- package/dist/typeGuards.js +20 -0
- package/dist/types.d.ts +199 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +11 -1
- package/package.json +33 -5
package/dist/tests/patch.test.js
CHANGED
|
@@ -1,64 +1,60 @@
|
|
|
1
1
|
import fs from "fs";
|
|
2
2
|
import path from "path";
|
|
3
3
|
import { applyPatch, PatchFailed } from "../patch";
|
|
4
|
+
import { ContentType } from "../types";
|
|
4
5
|
describe("patch", () => {
|
|
5
6
|
const sample = fs.readFileSync(path.join(__dirname, "sample.md"), "utf-8");
|
|
7
|
+
const assertPatchResultsMatch = (inputDocumentPath, outputDocumentPath, instruction) => {
|
|
8
|
+
const inputDocument = fs.readFileSync(path.join(__dirname, inputDocumentPath), "utf-8");
|
|
9
|
+
const outputDocument = fs.readFileSync(path.join(__dirname, outputDocumentPath), "utf-8");
|
|
10
|
+
expect(applyPatch(inputDocument, instruction)).toEqual(outputDocument);
|
|
11
|
+
};
|
|
6
12
|
describe("heading", () => {
|
|
7
13
|
test("prepend", () => {
|
|
8
|
-
const expected = fs.readFileSync(path.join(__dirname, "sample.patch.heading.prepend.md"), "utf-8");
|
|
9
14
|
const instruction = {
|
|
10
15
|
targetType: "heading",
|
|
11
16
|
target: ["Overview"],
|
|
12
17
|
operation: "prepend",
|
|
13
18
|
content: "Beep Boop\n",
|
|
14
19
|
};
|
|
15
|
-
|
|
16
|
-
expect(actualResult).toEqual(expected);
|
|
20
|
+
assertPatchResultsMatch("sample.md", "sample.patch.heading.prepend.md", instruction);
|
|
17
21
|
});
|
|
18
22
|
test("append", () => {
|
|
19
|
-
const expected = fs.readFileSync(path.join(__dirname, "sample.patch.heading.append.md"), "utf-8");
|
|
20
23
|
const instruction = {
|
|
21
24
|
targetType: "heading",
|
|
22
25
|
target: ["Overview"],
|
|
23
26
|
operation: "append",
|
|
24
27
|
content: "Beep Boop\n",
|
|
25
28
|
};
|
|
26
|
-
|
|
27
|
-
expect(actualResult).toEqual(expected);
|
|
29
|
+
assertPatchResultsMatch("sample.md", "sample.patch.heading.append.md", instruction);
|
|
28
30
|
});
|
|
29
31
|
test("replace", () => {
|
|
30
|
-
const expected = fs.readFileSync(path.join(__dirname, "sample.patch.heading.replace.md"), "utf-8");
|
|
31
32
|
const instruction = {
|
|
32
33
|
targetType: "heading",
|
|
33
34
|
target: ["Overview"],
|
|
34
35
|
operation: "replace",
|
|
35
36
|
content: "Beep Boop\n",
|
|
36
37
|
};
|
|
37
|
-
|
|
38
|
-
expect(actualResult).toEqual(expected);
|
|
38
|
+
assertPatchResultsMatch("sample.md", "sample.patch.heading.replace.md", instruction);
|
|
39
39
|
});
|
|
40
40
|
describe("document", () => {
|
|
41
41
|
test("prepend", () => {
|
|
42
|
-
const expected = fs.readFileSync(path.join(__dirname, "sample.patch.heading.document.prepend.md"), "utf-8");
|
|
43
42
|
const instruction = {
|
|
44
43
|
targetType: "heading",
|
|
45
44
|
target: null,
|
|
46
45
|
operation: "prepend",
|
|
47
46
|
content: "Beep Boop\n",
|
|
48
47
|
};
|
|
49
|
-
|
|
50
|
-
expect(actualResult).toEqual(expected);
|
|
48
|
+
assertPatchResultsMatch("sample.md", "sample.patch.heading.document.prepend.md", instruction);
|
|
51
49
|
});
|
|
52
50
|
test("append", () => {
|
|
53
|
-
const expected = fs.readFileSync(path.join(__dirname, "sample.patch.heading.document.append.md"), "utf-8");
|
|
54
51
|
const instruction = {
|
|
55
52
|
targetType: "heading",
|
|
56
53
|
target: null,
|
|
57
54
|
operation: "append",
|
|
58
55
|
content: "Beep Boop\n",
|
|
59
56
|
};
|
|
60
|
-
|
|
61
|
-
expect(actualResult).toEqual(expected);
|
|
57
|
+
assertPatchResultsMatch("sample.md", "sample.patch.heading.document.append.md", instruction);
|
|
62
58
|
});
|
|
63
59
|
});
|
|
64
60
|
});
|
|
@@ -66,7 +62,6 @@ describe("patch", () => {
|
|
|
66
62
|
describe("trimTargetWhitespace", () => {
|
|
67
63
|
describe("heading", () => {
|
|
68
64
|
test("prepend", () => {
|
|
69
|
-
const expected = fs.readFileSync(path.join(__dirname, "sample.patch.heading.trimTargetWhitespace.prepend.md"), "utf-8");
|
|
70
65
|
const instruction = {
|
|
71
66
|
targetType: "heading",
|
|
72
67
|
target: ["Page Targets", "Document Properties (Exploratory)"],
|
|
@@ -74,11 +69,9 @@ describe("patch", () => {
|
|
|
74
69
|
content: "Beep Boop",
|
|
75
70
|
trimTargetWhitespace: true,
|
|
76
71
|
};
|
|
77
|
-
|
|
78
|
-
expect(actualResult).toEqual(expected);
|
|
72
|
+
assertPatchResultsMatch("sample.md", "sample.patch.heading.trimTargetWhitespace.prepend.md", instruction);
|
|
79
73
|
});
|
|
80
74
|
test("append", () => {
|
|
81
|
-
const expected = fs.readFileSync(path.join(__dirname, "sample.patch.heading.trimTargetWhitespace.append.md"), "utf-8");
|
|
82
75
|
const instruction = {
|
|
83
76
|
targetType: "heading",
|
|
84
77
|
target: ["Problems"],
|
|
@@ -86,11 +79,32 @@ describe("patch", () => {
|
|
|
86
79
|
content: "Beep Boop\n",
|
|
87
80
|
trimTargetWhitespace: true,
|
|
88
81
|
};
|
|
89
|
-
|
|
90
|
-
expect(actualResult).toEqual(expected);
|
|
82
|
+
assertPatchResultsMatch("sample.md", "sample.patch.heading.trimTargetWhitespace.append.md", instruction);
|
|
91
83
|
});
|
|
92
84
|
});
|
|
93
85
|
});
|
|
86
|
+
describe("createTargetIfMissing", () => {
|
|
87
|
+
test("nested", () => {
|
|
88
|
+
const instruction = {
|
|
89
|
+
targetType: "heading",
|
|
90
|
+
target: ["Page Targets", "Block", "Test"],
|
|
91
|
+
operation: "replace",
|
|
92
|
+
content: "Beep Boop\n",
|
|
93
|
+
createTargetIfMissing: true,
|
|
94
|
+
};
|
|
95
|
+
assertPatchResultsMatch("sample.md", "sample.patch.heading.createIfMissing.nested.md", instruction);
|
|
96
|
+
});
|
|
97
|
+
test("root", () => {
|
|
98
|
+
const instruction = {
|
|
99
|
+
targetType: "heading",
|
|
100
|
+
target: ["Alpha", "Beta", "Test"],
|
|
101
|
+
operation: "replace",
|
|
102
|
+
content: "Beep Boop\n",
|
|
103
|
+
createTargetIfMissing: true,
|
|
104
|
+
};
|
|
105
|
+
assertPatchResultsMatch("sample.md", "sample.patch.heading.createIfMissing.root.md", instruction);
|
|
106
|
+
});
|
|
107
|
+
});
|
|
94
108
|
describe("applyIfContentPreexists", () => {
|
|
95
109
|
describe("disabled (default)", () => {
|
|
96
110
|
describe("heading", () => {
|
|
@@ -140,81 +154,296 @@ describe("patch", () => {
|
|
|
140
154
|
});
|
|
141
155
|
describe("block", () => {
|
|
142
156
|
test("prepend", () => {
|
|
143
|
-
const expected = fs.readFileSync(path.join(__dirname, "sample.patch.block.prepend.md"), "utf-8");
|
|
144
157
|
const instruction = {
|
|
145
158
|
targetType: "block",
|
|
146
159
|
target: "e6068e",
|
|
147
160
|
operation: "prepend",
|
|
148
161
|
content: "- OK\n",
|
|
149
162
|
};
|
|
150
|
-
|
|
151
|
-
expect(actualResult).toEqual(expected);
|
|
163
|
+
assertPatchResultsMatch("sample.md", "sample.patch.block.prepend.md", instruction);
|
|
152
164
|
});
|
|
153
165
|
test("append", () => {
|
|
154
|
-
const expected = fs.readFileSync(path.join(__dirname, "sample.patch.block.append.md"), "utf-8");
|
|
155
166
|
const instruction = {
|
|
156
167
|
targetType: "block",
|
|
157
168
|
target: "e6068e",
|
|
158
169
|
operation: "append",
|
|
159
170
|
content: "\n- OK",
|
|
160
171
|
};
|
|
161
|
-
|
|
162
|
-
expect(actualResult).toEqual(expected);
|
|
172
|
+
assertPatchResultsMatch("sample.md", "sample.patch.block.append.md", instruction);
|
|
163
173
|
});
|
|
164
174
|
test("replace", () => {
|
|
165
|
-
const expected = fs.readFileSync(path.join(__dirname, "sample.patch.block.replace.md"), "utf-8");
|
|
166
175
|
const instruction = {
|
|
167
176
|
targetType: "block",
|
|
168
177
|
target: "259a73",
|
|
169
178
|
operation: "replace",
|
|
170
179
|
content: "- OK",
|
|
171
180
|
};
|
|
172
|
-
|
|
173
|
-
expect(actualResult).toEqual(expected);
|
|
181
|
+
assertPatchResultsMatch("sample.md", "sample.patch.block.replace.md", instruction);
|
|
174
182
|
});
|
|
175
183
|
describe("tagetBlockTypeBehavior", () => {
|
|
176
184
|
describe("table", () => {
|
|
177
185
|
test("prepend", () => {
|
|
178
|
-
const expected = fs.readFileSync(path.join(__dirname, "sample.patch.block.targetBlockTypeBehavior.table.prepend.md"), "utf-8");
|
|
179
186
|
const instruction = {
|
|
180
187
|
targetType: "block",
|
|
181
|
-
targetBlockTypeBehavior: "table",
|
|
182
188
|
target: "2c67a6",
|
|
183
189
|
operation: "prepend",
|
|
190
|
+
contentType: ContentType.json,
|
|
184
191
|
content: [
|
|
185
192
|
["`something else`", "Some other application", "✅", "✅", "✅"],
|
|
186
193
|
],
|
|
187
194
|
};
|
|
188
|
-
|
|
189
|
-
expect(actualResult).toEqual(expected);
|
|
195
|
+
assertPatchResultsMatch("sample.md", "sample.patch.block.targetBlockTypeBehavior.table.prepend.md", instruction);
|
|
190
196
|
});
|
|
191
197
|
test("append", () => {
|
|
192
|
-
const expected = fs.readFileSync(path.join(__dirname, "sample.patch.block.targetBlockTypeBehavior.table.append.md"), "utf-8");
|
|
193
198
|
const instruction = {
|
|
194
199
|
targetType: "block",
|
|
195
|
-
targetBlockTypeBehavior: "table",
|
|
196
200
|
target: "2c67a6",
|
|
197
201
|
operation: "append",
|
|
202
|
+
contentType: ContentType.json,
|
|
198
203
|
content: [
|
|
199
204
|
["`something else`", "Some other application", "✅", "✅", "✅"],
|
|
200
205
|
],
|
|
201
206
|
};
|
|
202
|
-
|
|
203
|
-
expect(actualResult).toEqual(expected);
|
|
207
|
+
assertPatchResultsMatch("sample.md", "sample.patch.block.targetBlockTypeBehavior.table.append.md", instruction);
|
|
204
208
|
});
|
|
205
209
|
test("replace", () => {
|
|
206
|
-
const expected = fs.readFileSync(path.join(__dirname, "sample.patch.block.targetBlockTypeBehavior.table.replace.md"), "utf-8");
|
|
207
210
|
const instruction = {
|
|
208
211
|
targetType: "block",
|
|
209
|
-
targetBlockTypeBehavior: "table",
|
|
210
212
|
target: "2c67a6",
|
|
211
213
|
operation: "replace",
|
|
214
|
+
contentType: ContentType.json,
|
|
212
215
|
content: [
|
|
213
216
|
["`something else`", "Some other application", "✅", "✅", "✅"],
|
|
214
217
|
],
|
|
215
218
|
};
|
|
216
|
-
|
|
217
|
-
|
|
219
|
+
assertPatchResultsMatch("sample.md", "sample.patch.block.targetBlockTypeBehavior.table.replace.md", instruction);
|
|
220
|
+
});
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
describe("frontmatter", () => {
|
|
225
|
+
describe("append", () => {
|
|
226
|
+
test("mismatched types", () => {
|
|
227
|
+
const instruction = {
|
|
228
|
+
targetType: "frontmatter",
|
|
229
|
+
target: "array-value",
|
|
230
|
+
operation: "append",
|
|
231
|
+
contentType: ContentType.json,
|
|
232
|
+
content: "OK",
|
|
233
|
+
};
|
|
234
|
+
expect(() => {
|
|
235
|
+
applyPatch(sample, instruction);
|
|
236
|
+
}).toThrow(PatchFailed);
|
|
237
|
+
});
|
|
238
|
+
test("invalid type", () => {
|
|
239
|
+
const instruction = {
|
|
240
|
+
targetType: "frontmatter",
|
|
241
|
+
target: "array-value",
|
|
242
|
+
operation: "append",
|
|
243
|
+
contentType: ContentType.json,
|
|
244
|
+
content: 10,
|
|
245
|
+
};
|
|
246
|
+
expect(() => {
|
|
247
|
+
applyPatch(sample, instruction);
|
|
248
|
+
}).toThrow(PatchFailed);
|
|
249
|
+
});
|
|
250
|
+
test("list", () => {
|
|
251
|
+
const instruction = {
|
|
252
|
+
targetType: "frontmatter",
|
|
253
|
+
target: "array-value",
|
|
254
|
+
operation: "append",
|
|
255
|
+
contentType: ContentType.json,
|
|
256
|
+
content: ["Beep"],
|
|
257
|
+
};
|
|
258
|
+
assertPatchResultsMatch("sample.frontmatter.md", "sample.patch.frontmatter.append.list.md", instruction);
|
|
259
|
+
});
|
|
260
|
+
test("dictionary", () => {
|
|
261
|
+
const instruction = {
|
|
262
|
+
targetType: "frontmatter",
|
|
263
|
+
target: "object-value",
|
|
264
|
+
operation: "append",
|
|
265
|
+
contentType: ContentType.json,
|
|
266
|
+
content: { three: "Beep" },
|
|
267
|
+
};
|
|
268
|
+
assertPatchResultsMatch("sample.frontmatter.md", "sample.patch.frontmatter.append.dictionary.md", instruction);
|
|
269
|
+
});
|
|
270
|
+
test("string", () => {
|
|
271
|
+
const instruction = {
|
|
272
|
+
targetType: "frontmatter",
|
|
273
|
+
target: "string-value",
|
|
274
|
+
operation: "append",
|
|
275
|
+
contentType: ContentType.json,
|
|
276
|
+
content: "Beep",
|
|
277
|
+
};
|
|
278
|
+
assertPatchResultsMatch("sample.frontmatter.md", "sample.patch.frontmatter.append.string.md", instruction);
|
|
279
|
+
});
|
|
280
|
+
});
|
|
281
|
+
describe("prepend", () => {
|
|
282
|
+
test("mismatched types", () => {
|
|
283
|
+
const instruction = {
|
|
284
|
+
targetType: "frontmatter",
|
|
285
|
+
target: "array-value",
|
|
286
|
+
operation: "prepend",
|
|
287
|
+
contentType: ContentType.json,
|
|
288
|
+
content: "OK",
|
|
289
|
+
};
|
|
290
|
+
expect(() => {
|
|
291
|
+
applyPatch(sample, instruction);
|
|
292
|
+
}).toThrow(PatchFailed);
|
|
293
|
+
});
|
|
294
|
+
test("invalid type", () => {
|
|
295
|
+
const instruction = {
|
|
296
|
+
targetType: "frontmatter",
|
|
297
|
+
target: "array-value",
|
|
298
|
+
operation: "prepend",
|
|
299
|
+
contentType: ContentType.json,
|
|
300
|
+
content: 10,
|
|
301
|
+
};
|
|
302
|
+
expect(() => {
|
|
303
|
+
applyPatch(sample, instruction);
|
|
304
|
+
}).toThrow(PatchFailed);
|
|
305
|
+
});
|
|
306
|
+
test("list", () => {
|
|
307
|
+
const instruction = {
|
|
308
|
+
targetType: "frontmatter",
|
|
309
|
+
target: "array-value",
|
|
310
|
+
operation: "prepend",
|
|
311
|
+
contentType: ContentType.json,
|
|
312
|
+
content: ["Beep"],
|
|
313
|
+
};
|
|
314
|
+
assertPatchResultsMatch("sample.frontmatter.md", "sample.patch.frontmatter.prepend.list.md", instruction);
|
|
315
|
+
});
|
|
316
|
+
test("dictionary", () => {
|
|
317
|
+
const instruction = {
|
|
318
|
+
targetType: "frontmatter",
|
|
319
|
+
target: "object-value",
|
|
320
|
+
operation: "prepend",
|
|
321
|
+
contentType: ContentType.json,
|
|
322
|
+
content: { three: "Beep" },
|
|
323
|
+
};
|
|
324
|
+
assertPatchResultsMatch("sample.frontmatter.md", "sample.patch.frontmatter.prepend.dictionary.md", instruction);
|
|
325
|
+
});
|
|
326
|
+
test("string", () => {
|
|
327
|
+
const instruction = {
|
|
328
|
+
targetType: "frontmatter",
|
|
329
|
+
target: "string-value",
|
|
330
|
+
operation: "prepend",
|
|
331
|
+
contentType: ContentType.json,
|
|
332
|
+
content: "Beep",
|
|
333
|
+
};
|
|
334
|
+
assertPatchResultsMatch("sample.frontmatter.md", "sample.patch.frontmatter.prepend.string.md", instruction);
|
|
335
|
+
});
|
|
336
|
+
});
|
|
337
|
+
describe("replace", () => {
|
|
338
|
+
test("list", () => {
|
|
339
|
+
const instruction = {
|
|
340
|
+
targetType: "frontmatter",
|
|
341
|
+
target: "array-value",
|
|
342
|
+
operation: "replace",
|
|
343
|
+
contentType: ContentType.json,
|
|
344
|
+
content: ["Replaced"],
|
|
345
|
+
};
|
|
346
|
+
assertPatchResultsMatch("sample.frontmatter.md", "sample.patch.frontmatter.replace.list.md", instruction);
|
|
347
|
+
});
|
|
348
|
+
test("dictionary", () => {
|
|
349
|
+
const instruction = {
|
|
350
|
+
targetType: "frontmatter",
|
|
351
|
+
target: "object-value",
|
|
352
|
+
operation: "replace",
|
|
353
|
+
contentType: ContentType.json,
|
|
354
|
+
content: {
|
|
355
|
+
replaced: true,
|
|
356
|
+
},
|
|
357
|
+
};
|
|
358
|
+
assertPatchResultsMatch("sample.frontmatter.md", "sample.patch.frontmatter.replace.dictionary.md", instruction);
|
|
359
|
+
});
|
|
360
|
+
test("string", () => {
|
|
361
|
+
const instruction = {
|
|
362
|
+
targetType: "frontmatter",
|
|
363
|
+
target: "string-value",
|
|
364
|
+
operation: "replace",
|
|
365
|
+
contentType: ContentType.json,
|
|
366
|
+
content: "Replaced",
|
|
367
|
+
};
|
|
368
|
+
assertPatchResultsMatch("sample.frontmatter.md", "sample.patch.frontmatter.replace.string.md", instruction);
|
|
369
|
+
});
|
|
370
|
+
test("number", () => {
|
|
371
|
+
const instruction = {
|
|
372
|
+
targetType: "frontmatter",
|
|
373
|
+
target: "number-value",
|
|
374
|
+
operation: "replace",
|
|
375
|
+
contentType: ContentType.json,
|
|
376
|
+
content: 10,
|
|
377
|
+
};
|
|
378
|
+
assertPatchResultsMatch("sample.frontmatter.md", "sample.patch.frontmatter.replace.number.md", instruction);
|
|
379
|
+
});
|
|
380
|
+
test("boolean", () => {
|
|
381
|
+
const instruction = {
|
|
382
|
+
targetType: "frontmatter",
|
|
383
|
+
target: "boolean-value",
|
|
384
|
+
operation: "replace",
|
|
385
|
+
contentType: ContentType.json,
|
|
386
|
+
content: true,
|
|
387
|
+
};
|
|
388
|
+
assertPatchResultsMatch("sample.frontmatter.md", "sample.patch.frontmatter.replace.boolean.md", instruction);
|
|
389
|
+
});
|
|
390
|
+
describe("createTargetIfMissing", () => {
|
|
391
|
+
test("list", () => {
|
|
392
|
+
const instruction = {
|
|
393
|
+
targetType: "frontmatter",
|
|
394
|
+
target: "new-field",
|
|
395
|
+
operation: "replace",
|
|
396
|
+
contentType: ContentType.json,
|
|
397
|
+
content: ["New Value"],
|
|
398
|
+
createTargetIfMissing: true,
|
|
399
|
+
};
|
|
400
|
+
assertPatchResultsMatch("sample.frontmatter.md", "sample.patch.frontmatter.createTargetIfMissing.list.md", instruction);
|
|
401
|
+
});
|
|
402
|
+
test("dictionary", () => {
|
|
403
|
+
const instruction = {
|
|
404
|
+
targetType: "frontmatter",
|
|
405
|
+
target: "new-field",
|
|
406
|
+
operation: "replace",
|
|
407
|
+
contentType: ContentType.json,
|
|
408
|
+
content: {
|
|
409
|
+
newValue: true,
|
|
410
|
+
},
|
|
411
|
+
createTargetIfMissing: true,
|
|
412
|
+
};
|
|
413
|
+
assertPatchResultsMatch("sample.frontmatter.md", "sample.patch.frontmatter.createTargetIfMissing.dictionary.md", instruction);
|
|
414
|
+
});
|
|
415
|
+
test("string", () => {
|
|
416
|
+
const instruction = {
|
|
417
|
+
targetType: "frontmatter",
|
|
418
|
+
target: "new-field",
|
|
419
|
+
operation: "replace",
|
|
420
|
+
contentType: ContentType.json,
|
|
421
|
+
content: "New Value",
|
|
422
|
+
createTargetIfMissing: true,
|
|
423
|
+
};
|
|
424
|
+
assertPatchResultsMatch("sample.frontmatter.md", "sample.patch.frontmatter.createTargetIfMissing.string.md", instruction);
|
|
425
|
+
});
|
|
426
|
+
test("number", () => {
|
|
427
|
+
const instruction = {
|
|
428
|
+
targetType: "frontmatter",
|
|
429
|
+
target: "new-field",
|
|
430
|
+
operation: "replace",
|
|
431
|
+
contentType: ContentType.json,
|
|
432
|
+
content: 588600,
|
|
433
|
+
createTargetIfMissing: true,
|
|
434
|
+
};
|
|
435
|
+
assertPatchResultsMatch("sample.frontmatter.md", "sample.patch.frontmatter.createTargetIfMissing.number.md", instruction);
|
|
436
|
+
});
|
|
437
|
+
test("boolean", () => {
|
|
438
|
+
const instruction = {
|
|
439
|
+
targetType: "frontmatter",
|
|
440
|
+
target: "new-field",
|
|
441
|
+
operation: "replace",
|
|
442
|
+
contentType: ContentType.json,
|
|
443
|
+
content: true,
|
|
444
|
+
createTargetIfMissing: true,
|
|
445
|
+
};
|
|
446
|
+
assertPatchResultsMatch("sample.frontmatter.md", "sample.patch.frontmatter.createTargetIfMissing.boolean.md", instruction);
|
|
218
447
|
});
|
|
219
448
|
});
|
|
220
449
|
});
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { AppendableFrontmatterType } from "./types";
|
|
2
|
+
export declare function isStringArrayArray(obj: unknown): obj is string[][];
|
|
3
|
+
export declare function isAppendableFrontmatterType(obj: unknown): obj is AppendableFrontmatterType;
|
|
4
|
+
export declare function isString(obj: unknown): obj is string;
|
|
5
|
+
export declare function isDictionary(obj: unknown): obj is Record<string, unknown>;
|
|
6
|
+
export declare function isList(obj: unknown): obj is Array<unknown>;
|
|
7
|
+
//# sourceMappingURL=typeGuards.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"typeGuards.d.ts","sourceRoot":"","sources":["../src/typeGuards.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,EAAE,MAAM,SAAS,CAAC;AAEpD,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,OAAO,GAAG,GAAG,IAAI,MAAM,EAAE,EAAE,CAUlE;AACD,wBAAgB,2BAA2B,CACzC,GAAG,EAAE,OAAO,GACX,GAAG,IAAI,yBAAyB,CAElC;AAED,wBAAgB,QAAQ,CAAC,GAAG,EAAE,OAAO,GAAG,GAAG,IAAI,MAAM,CAEpD;AAED,wBAAgB,YAAY,CAAC,GAAG,EAAE,OAAO,GAAG,GAAG,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAEzE;AAED,wBAAgB,MAAM,CAAC,GAAG,EAAE,OAAO,GAAG,GAAG,IAAI,KAAK,CAAC,OAAO,CAAC,CAE1D"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export function isStringArrayArray(obj) {
|
|
2
|
+
// Check if the object is an array
|
|
3
|
+
if (!Array.isArray(obj))
|
|
4
|
+
return false;
|
|
5
|
+
// Check if every element is an array of strings
|
|
6
|
+
return obj.every((item) => Array.isArray(item) &&
|
|
7
|
+
item.every((subItem) => typeof subItem === "string"));
|
|
8
|
+
}
|
|
9
|
+
export function isAppendableFrontmatterType(obj) {
|
|
10
|
+
return isString(obj) || isDictionary(obj) || isList(obj);
|
|
11
|
+
}
|
|
12
|
+
export function isString(obj) {
|
|
13
|
+
return typeof obj === "string";
|
|
14
|
+
}
|
|
15
|
+
export function isDictionary(obj) {
|
|
16
|
+
return typeof obj === "object" && obj !== null && !Array.isArray(obj);
|
|
17
|
+
}
|
|
18
|
+
export function isList(obj) {
|
|
19
|
+
return Array.isArray(obj);
|
|
20
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
export interface DocumentRange {
|
|
2
|
+
start: number;
|
|
3
|
+
end: number;
|
|
4
|
+
}
|
|
5
|
+
export interface DocumentMapMarkerContentPair {
|
|
6
|
+
marker: DocumentRange;
|
|
7
|
+
content: DocumentRange;
|
|
8
|
+
}
|
|
9
|
+
export interface HeadingMarkerContentPair extends DocumentMapMarkerContentPair {
|
|
10
|
+
level: number;
|
|
11
|
+
}
|
|
12
|
+
export interface DocumentMap {
|
|
13
|
+
heading: Record<string, HeadingMarkerContentPair>;
|
|
14
|
+
block: Record<string, DocumentMapMarkerContentPair>;
|
|
15
|
+
frontmatter: Record<string, any>;
|
|
16
|
+
contentOffset: number;
|
|
17
|
+
lineEnding: string;
|
|
18
|
+
}
|
|
19
|
+
export type PatchTargetType = "heading" | "block" | "frontmatter";
|
|
20
|
+
export type PatchOperation = "replace" | "prepend" | "append";
|
|
21
|
+
export interface BasePatchInstructionTarget {
|
|
22
|
+
targetType: PatchTargetType;
|
|
23
|
+
target: any;
|
|
24
|
+
createTargetIfMissing?: boolean;
|
|
25
|
+
}
|
|
26
|
+
export interface BasePatchInstructionOperation {
|
|
27
|
+
operation: string;
|
|
28
|
+
}
|
|
29
|
+
export interface BaseHeadingPatchInstruction extends BasePatchInstructionTarget {
|
|
30
|
+
targetType: "heading";
|
|
31
|
+
target: string[] | null;
|
|
32
|
+
}
|
|
33
|
+
export interface BaseFrontmatterPatchInstruction extends BasePatchInstructionTarget {
|
|
34
|
+
targetType: "frontmatter";
|
|
35
|
+
target: string;
|
|
36
|
+
}
|
|
37
|
+
export interface BaseBlockPatchInstruction extends BasePatchInstructionTarget {
|
|
38
|
+
targetType: "block";
|
|
39
|
+
target: string;
|
|
40
|
+
}
|
|
41
|
+
export interface NonExtendingPatchInstruction extends BasePatchInstructionOperation {
|
|
42
|
+
}
|
|
43
|
+
export interface TextExtendingPatchInstruction extends BasePatchInstructionOperation {
|
|
44
|
+
/** Trim whitepsace from target before joining with content
|
|
45
|
+
*
|
|
46
|
+
* - For `prepend`: Trims content from the beginning of
|
|
47
|
+
* the target content.
|
|
48
|
+
* - For `append`: Trims content from the end of the target
|
|
49
|
+
* content. Your content should probably end in a newline
|
|
50
|
+
* in this case, or the trailing heading will no longer
|
|
51
|
+
* be the start of its own line
|
|
52
|
+
*/
|
|
53
|
+
trimTargetWhitespace?: boolean;
|
|
54
|
+
/** Apply patch even if content already exists at target
|
|
55
|
+
*
|
|
56
|
+
* By default, we will fail to apply a patch if the supplied
|
|
57
|
+
* content is found anywhere in your target content. If you
|
|
58
|
+
* would instead like the patch to occur, regardless of whether
|
|
59
|
+
* it appears the content is already there, you can set
|
|
60
|
+
* `applyIfContentPreexists` to `true`.
|
|
61
|
+
*/
|
|
62
|
+
applyIfContentPreexists?: boolean;
|
|
63
|
+
}
|
|
64
|
+
export interface StringContent {
|
|
65
|
+
contentType?: ContentType.text;
|
|
66
|
+
content: string;
|
|
67
|
+
}
|
|
68
|
+
export interface JsonContent {
|
|
69
|
+
contentType: ContentType.json;
|
|
70
|
+
content: unknown;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Prepend content to content existing under a heading
|
|
74
|
+
*
|
|
75
|
+
* @category Patch Instructions
|
|
76
|
+
*/
|
|
77
|
+
export interface PrependHeadingPatchInstruction extends TextExtendingPatchInstruction, BaseHeadingPatchInstruction, StringContent {
|
|
78
|
+
operation: "prepend";
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Append content to content existing under a heading
|
|
82
|
+
*
|
|
83
|
+
* @category Patch Instructions
|
|
84
|
+
*/
|
|
85
|
+
export interface AppendHeadingPatchInstruction extends TextExtendingPatchInstruction, BaseHeadingPatchInstruction, StringContent {
|
|
86
|
+
operation: "append";
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Replace content under a heading
|
|
90
|
+
*
|
|
91
|
+
* @category Patch Instructions
|
|
92
|
+
*/
|
|
93
|
+
export interface ReplaceHeadingPatchInstruction extends NonExtendingPatchInstruction, BaseHeadingPatchInstruction, StringContent {
|
|
94
|
+
operation: "replace";
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Prepend content to a block referenced by a block reference
|
|
98
|
+
*
|
|
99
|
+
* @category Patch Instructions
|
|
100
|
+
*/
|
|
101
|
+
export interface PrependBlockPatchInstruction extends TextExtendingPatchInstruction, BaseBlockPatchInstruction, StringContent {
|
|
102
|
+
operation: "prepend";
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Append content to a block referenced by a block reference
|
|
106
|
+
*
|
|
107
|
+
* @category Patch Instructions
|
|
108
|
+
*/
|
|
109
|
+
export interface AppendBlockPatchInstruction extends TextExtendingPatchInstruction, BaseBlockPatchInstruction, StringContent {
|
|
110
|
+
operation: "append";
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Replace content of block referenced by a block reference.
|
|
114
|
+
*
|
|
115
|
+
* @category Patch Instructions
|
|
116
|
+
*/
|
|
117
|
+
export interface ReplaceBlockPatchInstruction extends NonExtendingPatchInstruction, BaseBlockPatchInstruction, StringContent {
|
|
118
|
+
operation: "replace";
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Prepend rows to a table referenced by a block reference.
|
|
122
|
+
*
|
|
123
|
+
* @category Patch Instructions
|
|
124
|
+
*/
|
|
125
|
+
export interface PrependTableRowsBlockPatchInstruction extends TextExtendingPatchInstruction, BaseBlockPatchInstruction, JsonContent {
|
|
126
|
+
operation: "prepend";
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Patch Instruction for appending rows to a table
|
|
130
|
+
* referenced by a block reference.
|
|
131
|
+
*
|
|
132
|
+
* @category Patch Instructions
|
|
133
|
+
*/
|
|
134
|
+
export interface AppendTableRowsBlockPatchInstruction extends TextExtendingPatchInstruction, BaseBlockPatchInstruction, JsonContent {
|
|
135
|
+
operation: "append";
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Patch Instruction for replacing all rows of a table
|
|
139
|
+
* referenced by a block reference.
|
|
140
|
+
*
|
|
141
|
+
* @category Patch Instructions
|
|
142
|
+
*/
|
|
143
|
+
export interface ReplaceTableRowsBlockPatchInstruction extends NonExtendingPatchInstruction, BaseBlockPatchInstruction, JsonContent {
|
|
144
|
+
operation: "replace";
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Prepend content to a frontmatter field
|
|
148
|
+
*
|
|
149
|
+
* @category Patch Instructions
|
|
150
|
+
*/
|
|
151
|
+
export interface PrependFrontmatterPatchInstruction extends NonExtendingPatchInstruction, BaseFrontmatterPatchInstruction, JsonContent {
|
|
152
|
+
operation: "prepend";
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Append content to a frontmatter field
|
|
156
|
+
*
|
|
157
|
+
* @category Patch Instructions
|
|
158
|
+
*/
|
|
159
|
+
export interface AppendFrontmatterPatchInstruction extends NonExtendingPatchInstruction, BaseFrontmatterPatchInstruction, JsonContent {
|
|
160
|
+
operation: "append";
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Replace content of frontmatter field
|
|
164
|
+
*
|
|
165
|
+
* @category Patch Instructions
|
|
166
|
+
*/
|
|
167
|
+
export interface ReplaceFrontmatterPatchInstruction extends NonExtendingPatchInstruction, BaseFrontmatterPatchInstruction, JsonContent {
|
|
168
|
+
operation: "replace";
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Patch Instruction for Headings
|
|
172
|
+
*/
|
|
173
|
+
export type HeadingPatchInstruction = PrependHeadingPatchInstruction | AppendHeadingPatchInstruction | ReplaceHeadingPatchInstruction;
|
|
174
|
+
/**
|
|
175
|
+
* Patch Instruction for Block References
|
|
176
|
+
*/
|
|
177
|
+
export type BlockPatchInstruction = PrependBlockPatchInstruction | AppendBlockPatchInstruction | ReplaceBlockPatchInstruction | PrependTableRowsBlockPatchInstruction | AppendTableRowsBlockPatchInstruction | ReplaceTableRowsBlockPatchInstruction;
|
|
178
|
+
export type FrontmatterPatchInstruction = PrependFrontmatterPatchInstruction | AppendFrontmatterPatchInstruction | ReplaceFrontmatterPatchInstruction;
|
|
179
|
+
/**
|
|
180
|
+
* Patch Instruction
|
|
181
|
+
*/
|
|
182
|
+
export type PatchInstruction = HeadingPatchInstruction | BlockPatchInstruction | FrontmatterPatchInstruction;
|
|
183
|
+
export declare enum ContentType {
|
|
184
|
+
/**
|
|
185
|
+
* Content is simple text.
|
|
186
|
+
*/
|
|
187
|
+
text = "text/plain",
|
|
188
|
+
/**
|
|
189
|
+
* Content is a JSON document
|
|
190
|
+
*/
|
|
191
|
+
json = "application/json"
|
|
192
|
+
}
|
|
193
|
+
export interface PreprocessedDocument {
|
|
194
|
+
frontmatter: Record<string, any>;
|
|
195
|
+
contentOffset: number;
|
|
196
|
+
content: string;
|
|
197
|
+
}
|
|
198
|
+
export type AppendableFrontmatterType = string | Array<unknown> | Record<string, unknown>;
|
|
199
|
+
//# sourceMappingURL=types.d.ts.map
|