markdown-patch 0.1.2 → 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.
package/dist/cli.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+ export {};
3
+ //# sourceMappingURL=cli.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":""}
@@ -0,0 +1,7 @@
1
+ export declare const TARGETABLE_BY_ISOLATED_BLOCK_REFERENCE: string[];
2
+ export declare const CAN_INCLUDE_BLOCK_REFERENCE: string[];
3
+ export declare enum ContentType {
4
+ text = "text/plain",
5
+ tableRows = "application/vnd.markdown-patch.table-rows+json"
6
+ }
7
+ //# sourceMappingURL=constants.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,sCAAsC,UAQlD,CAAC;AAEF,eAAO,MAAM,2BAA2B,UAA6B,CAAC;AAEtE,oBAAY,WAAW;IACrB,IAAI,eAAe;IACnB,SAAS,mDAAmD;CAC7D"}
package/dist/constants.js CHANGED
@@ -8,3 +8,8 @@ export const TARGETABLE_BY_ISOLATED_BLOCK_REFERENCE = [
8
8
  "image",
9
9
  ];
10
10
  export const CAN_INCLUDE_BLOCK_REFERENCE = ["paragraph", "list_item"];
11
+ export var ContentType;
12
+ (function (ContentType) {
13
+ ContentType["text"] = "text/plain";
14
+ ContentType["tableRows"] = "application/vnd.markdown-patch.table-rows+json";
15
+ })(ContentType || (ContentType = {}));
@@ -0,0 +1,3 @@
1
+ import { DocumentMap } from "./types.js";
2
+ export declare const printMap: (content: string, documentMap: DocumentMap, regex: RegExp | undefined) => void;
3
+ //# sourceMappingURL=debug.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"debug.d.ts","sourceRoot":"","sources":["../src/debug.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAEzC,eAAO,MAAM,QAAQ,YACV,MAAM,eACF,WAAW,SACjB,MAAM,GAAG,SAAS,KACxB,IAmEF,CAAC"}
@@ -0,0 +1,3 @@
1
+ export { PatchFailureReason, PatchFailed, PatchError, TablePartsNotFound, applyPatch, } from "./patch.js";
2
+ export * from "./types.js";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kBAAkB,EAClB,WAAW,EACX,UAAU,EACV,kBAAkB,EAClB,UAAU,GACX,MAAM,YAAY,CAAC;AAEpB,cAAc,YAAY,CAAC"}
package/dist/index.js CHANGED
@@ -1 +1,2 @@
1
+ export { PatchFailureReason, PatchFailed, PatchError, TablePartsNotFound, applyPatch, } from "./patch.js";
1
2
  export * from "./types.js";
package/dist/map.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import { DocumentMap } from "./types.js";
2
+ export declare const getDocumentMap: (document: string) => DocumentMap;
3
+ //# sourceMappingURL=map.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"map.d.ts","sourceRoot":"","sources":["../src/map.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,WAAW,EAGZ,MAAM,YAAY,CAAC;AAyLpB,eAAO,MAAM,cAAc,aAAc,MAAM,KAAG,WAQjD,CAAC"}
@@ -0,0 +1,19 @@
1
+ import { DocumentMapMarkerContentPair, PatchInstruction } from "./types.js";
2
+ export declare enum PatchFailureReason {
3
+ InvalidTarget = "invalid-target",
4
+ ContentAlreadyPreexistsInTarget = "content-already-preexists-in-target",
5
+ TableContentIncorrectColumnCount = "table-content-incorrect-column-count",
6
+ RequestedBlockTypeBehaviorUnavailable = "requested-block-type-behavior-unavailable"
7
+ }
8
+ export declare class PatchFailed extends Error {
9
+ reason: PatchFailureReason;
10
+ instruction: PatchInstruction;
11
+ targetMap: DocumentMapMarkerContentPair | null;
12
+ constructor(reason: PatchFailureReason, instruction: PatchInstruction, targetMap: DocumentMapMarkerContentPair | null);
13
+ }
14
+ export declare class PatchError extends Error {
15
+ }
16
+ export declare class TablePartsNotFound extends Error {
17
+ }
18
+ export declare const applyPatch: (document: string, instruction: PatchInstruction) => string;
19
+ //# sourceMappingURL=patch.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"patch.d.ts","sourceRoot":"","sources":["../src/patch.ts"],"names":[],"mappings":"AAEA,OAAO,EAIL,4BAA4B,EAE5B,gBAAgB,EAEjB,MAAM,YAAY,CAAC;AAGpB,oBAAY,kBAAkB;IAC5B,aAAa,mBAAmB;IAChC,+BAA+B,wCAAwC;IACvE,gCAAgC,yCAAyC;IACzE,qCAAqC,8CAA8C;CACpF;AAED,qBAAa,WAAY,SAAQ,KAAK;IAC7B,MAAM,EAAE,kBAAkB,CAAC;IAC3B,WAAW,EAAE,gBAAgB,CAAC;IAC9B,SAAS,EAAE,4BAA4B,GAAG,IAAI,CAAC;gBAGpD,MAAM,EAAE,kBAAkB,EAC1B,WAAW,EAAE,gBAAgB,EAC7B,SAAS,EAAE,4BAA4B,GAAG,IAAI;CAUjD;AAED,qBAAa,UAAW,SAAQ,KAAK;CAAG;AA0CxC,qBAAa,kBAAmB,SAAQ,KAAK;CAAG;AAmNhD,eAAO,MAAM,UAAU,aACX,MAAM,eACH,gBAAgB,KAC5B,MA8BF,CAAC"}
package/dist/patch.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { getDocumentMap } from "./map.js";
2
2
  import * as marked from "marked";
3
+ import { ContentType } from "./constants.js";
3
4
  export var PatchFailureReason;
4
5
  (function (PatchFailureReason) {
5
6
  PatchFailureReason["InvalidTarget"] = "invalid-target";
@@ -123,38 +124,35 @@ const appendTable = (document, instruction, target) => {
123
124
  }
124
125
  };
125
126
  const replace = (document, instruction, target) => {
126
- const targetBlockTypeBehavior = "targetBlockTypeBehavior" in instruction &&
127
- instruction.targetBlockTypeBehavior
128
- ? instruction.targetBlockTypeBehavior
129
- : "text";
130
- switch (targetBlockTypeBehavior) {
131
- case "text":
127
+ const contentType = "contentType" in instruction && instruction.contentType
128
+ ? instruction.contentType
129
+ : ContentType.text;
130
+ switch (contentType) {
131
+ case ContentType.text:
132
132
  return replaceText(document, instruction, target);
133
- case "table":
133
+ case ContentType.tableRows:
134
134
  return replaceTable(document, instruction, target);
135
135
  }
136
136
  };
137
137
  const prepend = (document, instruction, target) => {
138
- const targetBlockTypeBehavior = "targetBlockTypeBehavior" in instruction &&
139
- instruction.targetBlockTypeBehavior
140
- ? instruction.targetBlockTypeBehavior
141
- : "text";
142
- switch (targetBlockTypeBehavior) {
143
- case "text":
138
+ const contentType = "contentType" in instruction && instruction.contentType
139
+ ? instruction.contentType
140
+ : ContentType.text;
141
+ switch (contentType) {
142
+ case ContentType.text:
144
143
  return prependText(document, instruction, target);
145
- case "table":
144
+ case ContentType.tableRows:
146
145
  return prependTable(document, instruction, target);
147
146
  }
148
147
  };
149
148
  const append = (document, instruction, target) => {
150
- const targetBlockTypeBehavior = "targetBlockTypeBehavior" in instruction &&
151
- instruction.targetBlockTypeBehavior
152
- ? instruction.targetBlockTypeBehavior
153
- : "text";
154
- switch (targetBlockTypeBehavior) {
155
- case "text":
149
+ const contentType = "contentType" in instruction && instruction.contentType
150
+ ? instruction.contentType
151
+ : ContentType.text;
152
+ switch (contentType) {
153
+ case ContentType.text:
156
154
  return appendText(document, instruction, target);
157
- case "table":
155
+ case ContentType.tableRows:
158
156
  return appendTable(document, instruction, target);
159
157
  }
160
158
  };
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=map.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"map.test.d.ts","sourceRoot":"","sources":["../../src/tests/map.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=patch.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"patch.test.d.ts","sourceRoot":"","sources":["../../src/tests/patch.test.ts"],"names":[],"mappings":""}
@@ -1,6 +1,7 @@
1
1
  import fs from "fs";
2
2
  import path from "path";
3
3
  import { applyPatch, PatchFailed } from "../patch";
4
+ import { ContentType } from "../constants";
4
5
  describe("patch", () => {
5
6
  const sample = fs.readFileSync(path.join(__dirname, "sample.md"), "utf-8");
6
7
  describe("heading", () => {
@@ -178,9 +179,9 @@ describe("patch", () => {
178
179
  const expected = fs.readFileSync(path.join(__dirname, "sample.patch.block.targetBlockTypeBehavior.table.prepend.md"), "utf-8");
179
180
  const instruction = {
180
181
  targetType: "block",
181
- targetBlockTypeBehavior: "table",
182
182
  target: "2c67a6",
183
183
  operation: "prepend",
184
+ contentType: ContentType.tableRows,
184
185
  content: [
185
186
  ["`something else`", "Some other application", "✅", "✅", "✅"],
186
187
  ],
@@ -192,9 +193,9 @@ describe("patch", () => {
192
193
  const expected = fs.readFileSync(path.join(__dirname, "sample.patch.block.targetBlockTypeBehavior.table.append.md"), "utf-8");
193
194
  const instruction = {
194
195
  targetType: "block",
195
- targetBlockTypeBehavior: "table",
196
196
  target: "2c67a6",
197
197
  operation: "append",
198
+ contentType: ContentType.tableRows,
198
199
  content: [
199
200
  ["`something else`", "Some other application", "✅", "✅", "✅"],
200
201
  ],
@@ -206,9 +207,9 @@ describe("patch", () => {
206
207
  const expected = fs.readFileSync(path.join(__dirname, "sample.patch.block.targetBlockTypeBehavior.table.replace.md"), "utf-8");
207
208
  const instruction = {
208
209
  targetType: "block",
209
- targetBlockTypeBehavior: "table",
210
210
  target: "2c67a6",
211
211
  operation: "replace",
212
+ contentType: ContentType.tableRows,
212
213
  content: [
213
214
  ["`something else`", "Some other application", "✅", "✅", "✅"],
214
215
  ],
@@ -0,0 +1,95 @@
1
+ import { ContentType } from "./constants";
2
+ export interface DocumentRange {
3
+ start: number;
4
+ end: number;
5
+ }
6
+ export interface DocumentMapMarkerContentPair {
7
+ marker: DocumentRange;
8
+ content: DocumentRange;
9
+ }
10
+ export interface HeadingMarkerContentPair extends DocumentMapMarkerContentPair {
11
+ level: number;
12
+ }
13
+ export interface DocumentMap {
14
+ heading: Record<string, HeadingMarkerContentPair>;
15
+ block: Record<string, DocumentMapMarkerContentPair>;
16
+ }
17
+ export type PatchTargetType = "heading" | "block";
18
+ export type PatchOperation = "replace" | "prepend" | "append";
19
+ export interface BasePatchInstructionTarget {
20
+ targetType: PatchTargetType;
21
+ target: any;
22
+ }
23
+ export interface BasePatchInstructionOperation {
24
+ operation: string;
25
+ }
26
+ export interface BaseHeadingPatchInstruction extends BasePatchInstructionTarget {
27
+ targetType: "heading";
28
+ target: string[] | null;
29
+ }
30
+ export interface BaseBlockPatchInstruction extends BasePatchInstructionTarget {
31
+ targetType: "block";
32
+ target: string;
33
+ }
34
+ export interface NonExtendingPatchInstruction extends BasePatchInstructionOperation {
35
+ }
36
+ export interface ExtendingPatchInstruction extends BasePatchInstructionOperation {
37
+ /** Trim whitepsace from target before joining with content
38
+ *
39
+ * - For `prepend`: Trims content from the beginning of
40
+ * the target content.
41
+ * - For `append`: Trims content from the end of the target
42
+ * content. Your content should probably end in a newline
43
+ * in this case, or the trailing heading will no longer
44
+ * be the start of its own line
45
+ */
46
+ trimTargetWhitespace?: boolean;
47
+ /** Apply patch even if content already exists at target
48
+ *
49
+ * By default, we will fail to apply a patch if the supplied
50
+ * content is found anywhere in your target content. If you
51
+ * would instead like the patch to occur, regardless of whether
52
+ * it appears the content is already there, you can set
53
+ * `applyIfContentPreexists` to `true`.
54
+ */
55
+ applyIfContentPreexists?: boolean;
56
+ }
57
+ export interface StringContent {
58
+ contentType?: ContentType.text;
59
+ content: string;
60
+ }
61
+ export interface TableRowsContent {
62
+ contentType: ContentType.tableRows;
63
+ content: string[][];
64
+ }
65
+ export interface PrependHeadingPatchInstruction extends ExtendingPatchInstruction, BaseHeadingPatchInstruction, StringContent {
66
+ operation: "prepend";
67
+ }
68
+ export interface AppendHeadingPatchInstruction extends ExtendingPatchInstruction, BaseHeadingPatchInstruction, StringContent {
69
+ operation: "append";
70
+ }
71
+ export interface ReplaceHeadingPatchInstruction extends NonExtendingPatchInstruction, BaseHeadingPatchInstruction, StringContent {
72
+ operation: "replace";
73
+ }
74
+ export interface PrependBlockPatchInstruction extends ExtendingPatchInstruction, BaseBlockPatchInstruction, StringContent {
75
+ operation: "prepend";
76
+ }
77
+ export interface AppendBlockPatchInstruction extends ExtendingPatchInstruction, BaseBlockPatchInstruction, StringContent {
78
+ operation: "append";
79
+ }
80
+ export interface ReplaceBlockPatchInstruction extends NonExtendingPatchInstruction, BaseBlockPatchInstruction, StringContent {
81
+ operation: "replace";
82
+ }
83
+ export interface PrependTableRowsBlockPatchInstruction extends ExtendingPatchInstruction, BaseBlockPatchInstruction, TableRowsContent {
84
+ operation: "prepend";
85
+ }
86
+ export interface AppendTableRowsBlockPatchInstruction extends ExtendingPatchInstruction, BaseBlockPatchInstruction, TableRowsContent {
87
+ operation: "append";
88
+ }
89
+ export interface ReplaceTableRowsBlockPatchInstruction extends NonExtendingPatchInstruction, BaseBlockPatchInstruction, TableRowsContent {
90
+ operation: "replace";
91
+ }
92
+ export type HeadingPatchInstruction = PrependHeadingPatchInstruction | AppendHeadingPatchInstruction | ReplaceHeadingPatchInstruction;
93
+ export type BlockPatchInstruction = PrependBlockPatchInstruction | AppendBlockPatchInstruction | ReplaceBlockPatchInstruction | PrependTableRowsBlockPatchInstruction | AppendTableRowsBlockPatchInstruction | ReplaceTableRowsBlockPatchInstruction;
94
+ export type PatchInstruction = HeadingPatchInstruction | BlockPatchInstruction;
95
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE1C,MAAM,WAAW,aAAa;IAC5B,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,M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,CAAC"}
package/package.json CHANGED
@@ -15,11 +15,11 @@
15
15
  "typescript": "^5.5.4"
16
16
  },
17
17
  "name": "markdown-patch",
18
- "version": "0.1.2",
18
+ "version": "0.1.3",
19
19
  "main": "./dist/index.js",
20
20
  "scripts": {
21
21
  "build": "npx tsc",
22
- "test": "node node_modules/.bin/jest"
22
+ "test": "npx jest"
23
23
  },
24
24
  "bin": {
25
25
  "mdpatch": "./dist/cli.js"
@@ -32,5 +32,20 @@
32
32
  "files": [
33
33
  "dist/",
34
34
  "README.md"
35
- ]
35
+ ],
36
+ "jest": {
37
+ "preset": "ts-jest",
38
+ "testEnvironment": "node",
39
+ "testPathIgnorePatterns": [
40
+ "/node_modules/",
41
+ "/dist/"
42
+ ],
43
+ "transform": {
44
+ "^.+\\.ts$": ["ts-jest", {"useESM": true}]
45
+ },
46
+ "extensionsToTreatAsEsm": [".ts"],
47
+ "moduleNameMapper": {
48
+ "^(\\.{1,2}/.*)\\.js$": "$1"
49
+ }
50
+ }
36
51
  }