markdown-patch 0.1.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/README.md +111 -0
  2. package/dist/cli.d.ts +3 -0
  3. package/dist/cli.d.ts.map +1 -0
  4. package/dist/cli.js +108 -0
  5. package/dist/constants.d.ts +7 -0
  6. package/dist/constants.d.ts.map +1 -0
  7. package/dist/constants.js +15 -0
  8. package/dist/debug.d.ts +3 -0
  9. package/dist/debug.d.ts.map +1 -0
  10. package/dist/debug.js +50 -0
  11. package/dist/index.d.ts +3 -0
  12. package/dist/index.d.ts.map +1 -0
  13. package/dist/index.js +2 -0
  14. package/dist/map.d.ts +3 -0
  15. package/dist/map.d.ts.map +1 -0
  16. package/dist/map.js +144 -0
  17. package/dist/patch.d.ts +19 -0
  18. package/dist/patch.d.ts.map +1 -0
  19. package/dist/patch.js +189 -0
  20. package/dist/tests/map.test.d.ts +2 -0
  21. package/dist/tests/map.test.d.ts.map +1 -0
  22. package/dist/tests/map.test.js +202 -0
  23. package/dist/tests/patch.test.d.ts +2 -0
  24. package/dist/tests/patch.test.d.ts.map +1 -0
  25. package/dist/tests/patch.test.js +223 -0
  26. package/dist/types.d.ts +95 -0
  27. package/dist/types.d.ts.map +1 -0
  28. package/dist/types.js +1 -0
  29. package/package.json +23 -3
  30. package/.tool-versions +0 -1
  31. package/.vscode/launch.json +0 -21
  32. package/document.md +0 -11
  33. package/document.mdpatch.json +0 -8
  34. package/jest.config.ts +0 -9
  35. package/src/cli.ts +0 -88
  36. package/src/constants.ts +0 -11
  37. package/src/debug.ts +0 -75
  38. package/src/index.ts +0 -9
  39. package/src/map.ts +0 -200
  40. package/src/patch.ts +0 -326
  41. package/src/tests/map.test.ts +0 -212
  42. package/src/tests/patch.test.ts +0 -297
  43. package/src/tests/sample.md +0 -81
  44. package/src/tests/sample.patch.block.append.md +0 -82
  45. package/src/tests/sample.patch.block.prepend.md +0 -82
  46. package/src/tests/sample.patch.block.replace.md +0 -81
  47. package/src/tests/sample.patch.block.targetBlockTypeBehavior.table.append.md +0 -82
  48. package/src/tests/sample.patch.block.targetBlockTypeBehavior.table.prepend.md +0 -82
  49. package/src/tests/sample.patch.block.targetBlockTypeBehavior.table.replace.md +0 -77
  50. package/src/tests/sample.patch.heading.append.md +0 -82
  51. package/src/tests/sample.patch.heading.document.append.md +0 -82
  52. package/src/tests/sample.patch.heading.document.prepend.md +0 -82
  53. package/src/tests/sample.patch.heading.prepend.md +0 -82
  54. package/src/tests/sample.patch.heading.replace.md +0 -81
  55. package/src/tests/sample.patch.heading.trimTargetWhitespace.append.md +0 -80
  56. package/src/tests/sample.patch.heading.trimTargetWhitespace.prepend.md +0 -80
  57. package/src/types.ts +0 -155
  58. package/tsconfig.json +0 -18
package/src/patch.ts DELETED
@@ -1,326 +0,0 @@
1
- import { getDocumentMap } from "./map.js";
2
- import * as marked from "marked";
3
- import {
4
- AppendTableRowsBlockPatchInstruction,
5
- PrependTableRowsBlockPatchInstruction,
6
- DocumentMap,
7
- DocumentMapMarkerContentPair,
8
- ExtendingPatchInstruction,
9
- PatchInstruction,
10
- ReplaceTableRowsBlockPatchInstruction,
11
- } from "./types.js";
12
-
13
- export enum PatchFailureReason {
14
- InvalidTarget = "invalid-target",
15
- ContentAlreadyPreexistsInTarget = "content-already-preexists-in-target",
16
- TableContentIncorrectColumnCount = "table-content-incorrect-column-count",
17
- RequestedBlockTypeBehaviorUnavailable = "requested-block-type-behavior-unavailable",
18
- }
19
-
20
- export class PatchFailed extends Error {
21
- public reason: PatchFailureReason;
22
- public instruction: PatchInstruction;
23
- public targetMap: DocumentMapMarkerContentPair | null;
24
-
25
- constructor(
26
- reason: PatchFailureReason,
27
- instruction: PatchInstruction,
28
- targetMap: DocumentMapMarkerContentPair | null
29
- ) {
30
- super();
31
- this.reason = reason;
32
- this.instruction = instruction;
33
- this.targetMap = targetMap;
34
- this.name = "PatchFailed";
35
-
36
- Object.setPrototypeOf(this, new.target.prototype);
37
- }
38
- }
39
-
40
- export class PatchError extends Error {}
41
-
42
- const replaceText = (
43
- document: string,
44
- instruction: PatchInstruction,
45
- target: DocumentMapMarkerContentPair
46
- ): string => {
47
- return [
48
- document.slice(0, target.content.start),
49
- instruction.content,
50
- document.slice(target.content.end),
51
- ].join("");
52
- };
53
-
54
- const prependText = (
55
- document: string,
56
- instruction: ExtendingPatchInstruction & PatchInstruction,
57
- target: DocumentMapMarkerContentPair
58
- ): string => {
59
- return [
60
- document.slice(0, target.content.start),
61
- instruction.content,
62
- instruction.trimTargetWhitespace
63
- ? document.slice(target.content.start).trimStart()
64
- : document.slice(target.content.start),
65
- ].join("");
66
- };
67
-
68
- const appendText = (
69
- document: string,
70
- instruction: ExtendingPatchInstruction & PatchInstruction,
71
- target: DocumentMapMarkerContentPair
72
- ): string => {
73
- return [
74
- instruction.trimTargetWhitespace
75
- ? document.slice(0, target.content.end).trimEnd()
76
- : document.slice(0, target.content.end),
77
- instruction.content,
78
- document.slice(target.content.end),
79
- ].join("");
80
- };
81
-
82
- export class TablePartsNotFound extends Error {}
83
-
84
- const _getTableData = (
85
- document: string,
86
- target: DocumentMapMarkerContentPair
87
- ): {
88
- token: marked.Tokens.Table;
89
- lineEnding: string;
90
- headerParts: string;
91
- contentParts: string;
92
- } => {
93
- const targetTable = document.slice(target.content.start, target.content.end);
94
- const tableToken = marked.lexer(targetTable)[0];
95
- const match = /^(.*?)(?:\r?\n)(.*?)(\r?\n)/.exec(targetTable);
96
- if (!(tableToken.type === "table") || !match) {
97
- throw new TablePartsNotFound();
98
- }
99
-
100
- const lineEnding = match[3];
101
- return {
102
- token: tableToken as marked.Tokens.Table,
103
- lineEnding: match[3],
104
- headerParts: match[1] + lineEnding + match[2] + lineEnding,
105
- contentParts: targetTable.slice(match[0].length),
106
- };
107
- };
108
-
109
- const replaceTable = (
110
- document: string,
111
- instruction: ReplaceTableRowsBlockPatchInstruction,
112
- target: DocumentMapMarkerContentPair
113
- ): string => {
114
- try {
115
- const table = _getTableData(document, target);
116
- const tableRows: string[] = [table.headerParts];
117
- for (const row of instruction.content) {
118
- if (row.length !== table.token.header.length || typeof row === "string") {
119
- throw new PatchFailed(
120
- PatchFailureReason.TableContentIncorrectColumnCount,
121
- instruction,
122
- target
123
- );
124
- }
125
-
126
- tableRows.push("| " + row.join(" | ") + " |" + table.lineEnding);
127
- }
128
-
129
- return [
130
- document.slice(0, target.content.start),
131
- tableRows.join(""),
132
- document.slice(target.content.end),
133
- ].join("");
134
- } catch (TablePartsNotFound) {
135
- throw new PatchFailed(
136
- PatchFailureReason.RequestedBlockTypeBehaviorUnavailable,
137
- instruction,
138
- target
139
- );
140
- }
141
- };
142
-
143
- const prependTable = (
144
- document: string,
145
- instruction: PrependTableRowsBlockPatchInstruction,
146
- target: DocumentMapMarkerContentPair
147
- ): string => {
148
- try {
149
- const table = _getTableData(document, target);
150
- const tableRows: string[] = [table.headerParts];
151
- for (const row of instruction.content) {
152
- if (row.length !== table.token.header.length || typeof row === "string") {
153
- throw new PatchFailed(
154
- PatchFailureReason.TableContentIncorrectColumnCount,
155
- instruction,
156
- target
157
- );
158
- }
159
-
160
- tableRows.push("| " + row.join(" | ") + " |" + table.lineEnding);
161
- }
162
-
163
- tableRows.push(table.contentParts);
164
-
165
- return [
166
- document.slice(0, target.content.start),
167
- tableRows.join(""),
168
- document.slice(target.content.end),
169
- ].join("");
170
- } catch (TablePartsNotFound) {
171
- throw new PatchFailed(
172
- PatchFailureReason.RequestedBlockTypeBehaviorUnavailable,
173
- instruction,
174
- target
175
- );
176
- }
177
- };
178
-
179
- const appendTable = (
180
- document: string,
181
- instruction: AppendTableRowsBlockPatchInstruction,
182
- target: DocumentMapMarkerContentPair
183
- ): string => {
184
- try {
185
- const table = _getTableData(document, target);
186
- const tableRows: string[] = [table.headerParts, table.contentParts];
187
- for (const row of instruction.content) {
188
- if (row.length !== table.token.header.length || typeof row === "string") {
189
- throw new PatchFailed(
190
- PatchFailureReason.TableContentIncorrectColumnCount,
191
- instruction,
192
- target
193
- );
194
- }
195
-
196
- tableRows.push("| " + row.join(" | ") + " |" + table.lineEnding);
197
- }
198
-
199
- return [
200
- document.slice(0, target.content.start),
201
- tableRows.join(""),
202
- document.slice(target.content.end),
203
- ].join("");
204
- } catch (TablePartsNotFound) {
205
- throw new PatchFailed(
206
- PatchFailureReason.RequestedBlockTypeBehaviorUnavailable,
207
- instruction,
208
- target
209
- );
210
- }
211
- };
212
-
213
- const replace = (
214
- document: string,
215
- instruction: PatchInstruction,
216
- target: DocumentMapMarkerContentPair
217
- ): string => {
218
- const targetBlockTypeBehavior =
219
- "targetBlockTypeBehavior" in instruction
220
- ? instruction.targetBlockTypeBehavior
221
- : "text";
222
-
223
- switch (targetBlockTypeBehavior) {
224
- case "text":
225
- return replaceText(document, instruction, target);
226
- case "table":
227
- return replaceTable(
228
- document,
229
- instruction as ReplaceTableRowsBlockPatchInstruction,
230
- target
231
- );
232
- }
233
- };
234
-
235
- const prepend = (
236
- document: string,
237
- instruction: ExtendingPatchInstruction & PatchInstruction,
238
- target: DocumentMapMarkerContentPair
239
- ): string => {
240
- const targetBlockTypeBehavior =
241
- "targetBlockTypeBehavior" in instruction
242
- ? instruction.targetBlockTypeBehavior
243
- : "text";
244
-
245
- switch (targetBlockTypeBehavior) {
246
- case "text":
247
- return prependText(document, instruction, target);
248
- case "table":
249
- return prependTable(
250
- document,
251
- instruction as PrependTableRowsBlockPatchInstruction,
252
- target
253
- );
254
- }
255
- };
256
-
257
- const append = (
258
- document: string,
259
- instruction: ExtendingPatchInstruction & PatchInstruction,
260
- target: DocumentMapMarkerContentPair
261
- ): string => {
262
- const targetBlockTypeBehavior =
263
- "targetBlockTypeBehavior" in instruction
264
- ? instruction.targetBlockTypeBehavior
265
- : "text";
266
-
267
- switch (targetBlockTypeBehavior) {
268
- case "text":
269
- return appendText(document, instruction, target);
270
- case "table":
271
- return appendTable(
272
- document,
273
- instruction as AppendTableRowsBlockPatchInstruction,
274
- target
275
- );
276
- }
277
- };
278
-
279
- const getTarget = (
280
- map: DocumentMap,
281
- instruction: PatchInstruction
282
- ): DocumentMapMarkerContentPair | undefined => {
283
- switch (instruction.targetType) {
284
- case "heading":
285
- return map.heading[
286
- instruction.target ? instruction.target.join("\u001f") : ""
287
- ];
288
- case "block":
289
- return map.block[instruction.target];
290
- }
291
- };
292
-
293
- export const applyPatch = (
294
- document: string,
295
- instruction: PatchInstruction
296
- ): string => {
297
- const map = getDocumentMap(document);
298
- const target = getTarget(map, instruction);
299
-
300
- if (!target) {
301
- throw new PatchFailed(PatchFailureReason.InvalidTarget, instruction, null);
302
- }
303
-
304
- if (
305
- (!("applyIfContentPreexists" in instruction) ||
306
- !instruction.applyIfContentPreexists) &&
307
- typeof instruction.content === "string" &&
308
- document
309
- .slice(target.content.start, target.content.end)
310
- .includes(instruction.content.trim())
311
- ) {
312
- throw new PatchFailed(
313
- PatchFailureReason.ContentAlreadyPreexistsInTarget,
314
- instruction,
315
- target
316
- );
317
- }
318
- switch (instruction.operation) {
319
- case "append":
320
- return append(document, instruction, target);
321
- case "prepend":
322
- return prepend(document, instruction, target);
323
- case "replace":
324
- return replace(document, instruction, target);
325
- }
326
- };
@@ -1,212 +0,0 @@
1
- import fs from "fs";
2
- import path from "path";
3
-
4
- import { getDocumentMap } from "../map";
5
- import { DocumentMapMarkerContentPair } from "../types";
6
-
7
- describe("map", () => {
8
- const sample = fs.readFileSync(path.join(__dirname, "sample.md"), "utf-8");
9
-
10
- test("heading", () => {
11
- const actualHeadings = getDocumentMap(sample).heading;
12
-
13
- const expectedHeadings = {
14
- "": {
15
- content: {
16
- start: 130,
17
- end: 6988,
18
- },
19
- marker: {
20
- start: 0,
21
- end: 0,
22
- },
23
- level: 0,
24
- },
25
- Overview: {
26
- content: {
27
- start: 142,
28
- end: 430,
29
- },
30
- marker: {
31
- start: 131,
32
- end: 142,
33
- },
34
- level: 1,
35
- },
36
- Problems: {
37
- content: {
38
- start: 441,
39
- end: 1468,
40
- },
41
- marker: {
42
- start: 430,
43
- end: 441,
44
- },
45
- level: 1,
46
- },
47
- Actions: {
48
- content: {
49
- start: 1478,
50
- end: 3182,
51
- },
52
- marker: {
53
- start: 1468,
54
- end: 1478,
55
- },
56
- level: 1,
57
- },
58
- Headers: {
59
- content: {
60
- start: 3192,
61
- end: 4282,
62
- },
63
- marker: {
64
- start: 3182,
65
- end: 3192,
66
- },
67
- level: 1,
68
- },
69
- "Page Targets": {
70
- content: {
71
- start: 4297,
72
- end: 6988,
73
- },
74
- marker: {
75
- start: 4282,
76
- end: 4297,
77
- },
78
- level: 1,
79
- },
80
- "Page Targets\u001fHeading": {
81
- content: {
82
- start: 4309,
83
- end: 5251,
84
- },
85
- marker: {
86
- start: 4298,
87
- end: 4309,
88
- },
89
- level: 2,
90
- },
91
- "Page Targets\u001fBlock": {
92
- content: {
93
- start: 5260,
94
- end: 6122,
95
- },
96
- marker: {
97
- start: 5251,
98
- end: 5260,
99
- },
100
- level: 2,
101
- },
102
- "Page Targets\u001fBlock\u001fUse Cases": {
103
- content: {
104
- start: 5778,
105
- end: 6122,
106
- },
107
- marker: {
108
- start: 5764,
109
- end: 5778,
110
- },
111
- level: 3,
112
- },
113
- "Page Targets\u001fFrontmatter Field": {
114
- content: {
115
- start: 6143,
116
- end: 6690,
117
- },
118
- marker: {
119
- start: 6122,
120
- end: 6143,
121
- },
122
- level: 2,
123
- },
124
- "Page Targets\u001fFrontmatter Field\u001fUse Cases": {
125
- content: {
126
- start: 6510,
127
- end: 6690,
128
- },
129
- marker: {
130
- start: 6496,
131
- end: 6510,
132
- },
133
- level: 3,
134
- },
135
- "Page Targets\u001fDocument Properties (Exploratory)": {
136
- content: {
137
- start: 6727,
138
- end: 6988,
139
- },
140
- marker: {
141
- start: 6690,
142
- end: 6727,
143
- },
144
- level: 2,
145
- },
146
- };
147
-
148
- //console.log(JSON.stringify(actualHeadings, undefined, 4));
149
-
150
- expect(actualHeadings).toEqual(expectedHeadings);
151
- });
152
-
153
- test("block", () => {
154
- const actualBlocks = getDocumentMap(sample).block;
155
- const expectedBlocks: Record<string, DocumentMapMarkerContentPair> = {
156
- "2c67a6": {
157
- content: {
158
- start: 1478,
159
- end: 3172,
160
- },
161
- marker: {
162
- start: 3173,
163
- end: 3181,
164
- },
165
- },
166
- "1d6271": {
167
- content: {
168
- start: 3192,
169
- end: 4272,
170
- },
171
- marker: {
172
- start: 4273,
173
- end: 4281,
174
- },
175
- },
176
- bfec1f: {
177
- content: {
178
- start: 4310,
179
- end: 4606,
180
- },
181
- marker: {
182
- start: 4607,
183
- end: 4615,
184
- },
185
- },
186
- "259a73": {
187
- content: {
188
- start: 6570,
189
- end: 6633,
190
- },
191
- marker: {
192
- start: 6633,
193
- end: 6642,
194
- },
195
- },
196
- e6068e: {
197
- content: {
198
- start: 6642,
199
- end: 6681,
200
- },
201
- marker: {
202
- start: 6681,
203
- end: 6690,
204
- },
205
- },
206
- };
207
-
208
- //console.log(JSON.stringify(actualBlocks, undefined, 4));
209
-
210
- expect(actualBlocks).toEqual(expectedBlocks);
211
- });
212
- });