markdown-patch 0.2.1 → 0.3.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/dist/cli.js CHANGED
@@ -1,10 +1,15 @@
1
1
  #!/usr/bin/env node
2
- import { Command } from "commander";
3
- import fs from "fs/promises";
4
- import { getDocumentMap } from "./map.js";
5
- import { printMap } from "./debug.js";
6
- import { applyPatch } from "./patch.js";
7
- import packageJson from "../package.json" assert { type: "json" };
2
+ "use strict";
3
+ var __importDefault = (this && this.__importDefault) || function (mod) {
4
+ return (mod && mod.__esModule) ? mod : { "default": mod };
5
+ };
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ const commander_1 = require("commander");
8
+ const promises_1 = __importDefault(require("fs/promises"));
9
+ const map_js_1 = require("./map.js");
10
+ const debug_js_1 = require("./debug.js");
11
+ const patch_js_1 = require("./patch.js");
12
+ const package_json_1 = __importDefault(require("../package.json"));
8
13
  async function readStdin() {
9
14
  return new Promise((resolve, reject) => {
10
15
  let data = "";
@@ -13,20 +18,20 @@ async function readStdin() {
13
18
  process.stdin.on("error", reject);
14
19
  });
15
20
  }
16
- const program = new Command();
21
+ const program = new commander_1.Command();
17
22
  // Configure the CLI
18
23
  program
19
- .name(Object.keys(packageJson.bin)[0])
20
- .description(packageJson.description)
21
- .version(packageJson.version);
24
+ .name(Object.keys(package_json_1.default.bin)[0])
25
+ .description(package_json_1.default.description)
26
+ .version(package_json_1.default.version);
22
27
  program
23
28
  .command("print-map")
24
29
  .argument("<path>", "filepath to show identified patchable paths for")
25
30
  .argument("[regex]", "limit displayed matches to those matching the supplied regular expression")
26
31
  .action(async (path, regex) => {
27
- const document = await fs.readFile(path, "utf-8");
28
- const documentMap = getDocumentMap(document);
29
- printMap(document, documentMap, regex ? new RegExp(regex) : undefined);
32
+ const document = await promises_1.default.readFile(path, "utf-8");
33
+ const documentMap = (0, map_js_1.getDocumentMap)(document);
34
+ (0, debug_js_1.printMap)(document, documentMap, regex ? new RegExp(regex) : undefined);
30
35
  });
31
36
  program
32
37
  .command("patch")
@@ -40,24 +45,24 @@ program
40
45
  .action(async (operation, targetType, target, documentPath, options) => {
41
46
  let content;
42
47
  if (options.input) {
43
- content = await fs.readFile(options.input, "utf-8");
48
+ content = await promises_1.default.readFile(options.input, "utf-8");
44
49
  }
45
50
  else {
46
51
  content = await readStdin();
47
52
  }
48
- const document = await fs.readFile(documentPath, "utf-8");
53
+ const document = await promises_1.default.readFile(documentPath, "utf-8");
49
54
  const instruction = {
50
55
  operation,
51
56
  targetType,
52
57
  content,
53
58
  target: targetType !== "heading" ? target : target.split(options.delimiter),
54
59
  };
55
- const patchedDocument = applyPatch(document, instruction);
60
+ const patchedDocument = (0, patch_js_1.applyPatch)(document, instruction);
56
61
  if (options.output === "-") {
57
62
  process.stdout.write(patchedDocument);
58
63
  }
59
64
  else {
60
- await fs.writeFile(options.output ? options.output : documentPath, patchedDocument);
65
+ await promises_1.default.writeFile(options.output ? options.output : documentPath, patchedDocument);
61
66
  }
62
67
  });
63
68
  program
@@ -73,7 +78,7 @@ program
73
78
  patchData = await readStdin();
74
79
  }
75
80
  else {
76
- patchData = await fs.readFile(patch, "utf-8");
81
+ patchData = await promises_1.default.readFile(patch, "utf-8");
77
82
  }
78
83
  }
79
84
  catch (e) {
@@ -93,16 +98,16 @@ program
93
98
  console.error("Could not parse patch file as JSON");
94
99
  process.exit(1);
95
100
  }
96
- let document = await fs.readFile(path, "utf-8");
101
+ let document = await promises_1.default.readFile(path, "utf-8");
97
102
  console.log("Document", document);
98
103
  for (const instruction of patchParsed) {
99
- document = applyPatch(document, instruction);
104
+ document = (0, patch_js_1.applyPatch)(document, instruction);
100
105
  }
101
106
  if (options.output === "-") {
102
107
  process.stdout.write(document);
103
108
  }
104
109
  else {
105
- await fs.writeFile(options.output ? options.output : path, document);
110
+ await promises_1.default.writeFile(options.output ? options.output : path, document);
106
111
  }
107
112
  });
108
113
  program
@@ -113,14 +118,14 @@ program
113
118
  .argument("<target>", "Target ('::'-delimited by default for Headings); see `mdpatch print-map <path to document>` for options)")
114
119
  .argument("<documentPath>", "Path to document to query from.")
115
120
  .action(async (targetType, target, documentPath, options) => {
116
- const document = await fs.readFile(documentPath, "utf-8");
117
- const documentMap = getDocumentMap(document);
121
+ const document = await promises_1.default.readFile(documentPath, "utf-8");
122
+ const documentMap = (0, map_js_1.getDocumentMap)(document);
118
123
  const actualTarget = targetType !== "heading"
119
124
  ? target
120
125
  : target.split(options.delimiter).join("\u001f");
121
126
  const value = document.slice(documentMap[targetType][actualTarget].content.start, documentMap[targetType][actualTarget].content.end);
122
127
  if (options.output) {
123
- await fs.writeFile(options.output, value);
128
+ await promises_1.default.writeFile(options.output, value);
124
129
  }
125
130
  else {
126
131
  process.stdout.write(value);
package/dist/constants.js CHANGED
@@ -1,4 +1,7 @@
1
- export const TARGETABLE_BY_ISOLATED_BLOCK_REFERENCE = [
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CAN_INCLUDE_BLOCK_REFERENCE = exports.TARGETABLE_BY_ISOLATED_BLOCK_REFERENCE = void 0;
4
+ exports.TARGETABLE_BY_ISOLATED_BLOCK_REFERENCE = [
2
5
  "code",
3
6
  "heading",
4
7
  "table",
@@ -7,4 +10,4 @@ export const TARGETABLE_BY_ISOLATED_BLOCK_REFERENCE = [
7
10
  "paragraph",
8
11
  "image",
9
12
  ];
10
- export const CAN_INCLUDE_BLOCK_REFERENCE = ["paragraph", "list_item"];
13
+ exports.CAN_INCLUDE_BLOCK_REFERENCE = ["paragraph", "list_item"];
package/dist/debug.js CHANGED
@@ -1,7 +1,13 @@
1
- import chalk from "chalk";
2
- export const printMap = (content, documentMap, regex) => {
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.printMap = void 0;
7
+ const chalk_1 = __importDefault(require("chalk"));
8
+ const printMap = (content, documentMap, regex) => {
3
9
  for (const frontmatterField in documentMap.frontmatter) {
4
- const blockName = `[${chalk.magenta("frontmatter")}] ${chalk.blueBright(frontmatterField)}`;
10
+ const blockName = `[${chalk_1.default.magenta("frontmatter")}] ${chalk_1.default.blueBright(frontmatterField)}`;
5
11
  console.log("\n" + blockName + "\n");
6
12
  console.log(JSON.stringify(documentMap.frontmatter[frontmatterField]));
7
13
  }
@@ -12,9 +18,9 @@ export const printMap = (content, documentMap, regex) => {
12
18
  for (const type in targetablePositions) {
13
19
  for (const positionName in targetablePositions[type]) {
14
20
  const position = targetablePositions[type][positionName];
15
- const blockName = `[${chalk.magenta(type)}] ${positionName
21
+ const blockName = `[${chalk_1.default.magenta(type)}] ${positionName
16
22
  .split("\u001f")
17
- .map((pos) => chalk.blueBright(pos))
23
+ .map((pos) => chalk_1.default.blueBright(pos))
18
24
  .join(",")}`;
19
25
  if (regex && !blockName.match(regex)) {
20
26
  continue;
@@ -24,13 +30,13 @@ export const printMap = (content, documentMap, regex) => {
24
30
  console.log(content
25
31
  .slice(position.content.start - 100, position.content.start)
26
32
  .replaceAll("\n", "\\n\n") +
27
- chalk.black.bgGreen(content
33
+ chalk_1.default.black.bgGreen(content
28
34
  .slice(position.content.start, position.content.end)
29
35
  .replaceAll("\n", "\\n\n")) +
30
36
  content
31
37
  .slice(position.content.end, Math.min(position.content.end + 100, position.marker.start))
32
38
  .replaceAll("\n", "\\n\n") +
33
- chalk.black.bgRed(content
39
+ chalk_1.default.black.bgRed(content
34
40
  .slice(position.marker.start, position.marker.end)
35
41
  .replaceAll("\n", "\\n\n")) +
36
42
  content
@@ -41,13 +47,13 @@ export const printMap = (content, documentMap, regex) => {
41
47
  console.log(content
42
48
  .slice(position.marker.start - 100, position.marker.start)
43
49
  .replaceAll("\n", "\\n\n") +
44
- chalk.black.bgRed(content
50
+ chalk_1.default.black.bgRed(content
45
51
  .slice(position.marker.start, position.marker.end)
46
52
  .replaceAll("\n", "\\n\n")) +
47
53
  content
48
54
  .slice(position.marker.end, Math.min(position.marker.end + 100, position.content.start))
49
55
  .replaceAll("\n", "\\n\n") +
50
- chalk.black.bgGreen(content
56
+ chalk_1.default.black.bgGreen(content
51
57
  .slice(position.content.start, position.content.end)
52
58
  .replaceAll("\n", "\\n\n")) +
53
59
  content
@@ -57,3 +63,4 @@ export const printMap = (content, documentMap, regex) => {
57
63
  }
58
64
  }
59
65
  };
66
+ exports.printMap = printMap;
package/dist/index.d.ts CHANGED
@@ -2,5 +2,6 @@
2
2
  * @module Reference
3
3
  */
4
4
  export { PatchFailureReason, PatchFailed, PatchError, TablePartsNotFound, applyPatch, } from "./patch.js";
5
+ export { getDocumentMap } from "./map.js";
5
6
  export * from "./types.js";
6
7
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EACL,kBAAkB,EAClB,WAAW,EACX,UAAU,EACV,kBAAkB,EAClB,UAAU,GACX,MAAM,YAAY,CAAC;AAEpB,cAAc,YAAY,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EACL,kBAAkB,EAClB,WAAW,EACX,UAAU,EACV,kBAAkB,EAClB,UAAU,GACX,MAAM,YAAY,CAAC;AACpB,OAAO,EACL,cAAc,EACf,MAAM,UAAU,CAAC;AAElB,cAAc,YAAY,CAAC"}
package/dist/index.js CHANGED
@@ -1,5 +1,29 @@
1
+ "use strict";
1
2
  /**
2
3
  * @module Reference
3
4
  */
4
- export { PatchFailureReason, PatchFailed, PatchError, TablePartsNotFound, applyPatch, } from "./patch.js";
5
- export * from "./types.js";
5
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ var desc = Object.getOwnPropertyDescriptor(m, k);
8
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
9
+ desc = { enumerable: true, get: function() { return m[k]; } };
10
+ }
11
+ Object.defineProperty(o, k2, desc);
12
+ }) : (function(o, m, k, k2) {
13
+ if (k2 === undefined) k2 = k;
14
+ o[k2] = m[k];
15
+ }));
16
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
17
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
18
+ };
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ exports.getDocumentMap = exports.applyPatch = exports.TablePartsNotFound = exports.PatchError = exports.PatchFailed = exports.PatchFailureReason = void 0;
21
+ var patch_js_1 = require("./patch.js");
22
+ Object.defineProperty(exports, "PatchFailureReason", { enumerable: true, get: function () { return patch_js_1.PatchFailureReason; } });
23
+ Object.defineProperty(exports, "PatchFailed", { enumerable: true, get: function () { return patch_js_1.PatchFailed; } });
24
+ Object.defineProperty(exports, "PatchError", { enumerable: true, get: function () { return patch_js_1.PatchError; } });
25
+ Object.defineProperty(exports, "TablePartsNotFound", { enumerable: true, get: function () { return patch_js_1.TablePartsNotFound; } });
26
+ Object.defineProperty(exports, "applyPatch", { enumerable: true, get: function () { return patch_js_1.applyPatch; } });
27
+ var map_js_1 = require("./map.js");
28
+ Object.defineProperty(exports, "getDocumentMap", { enumerable: true, get: function () { return map_js_1.getDocumentMap; } });
29
+ __exportStar(require("./types.js"), exports);
package/dist/map.js CHANGED
@@ -1,6 +1,32 @@
1
- import * as marked from "marked";
2
- import { parse as parseYaml } from "yaml";
3
- import { CAN_INCLUDE_BLOCK_REFERENCE, TARGETABLE_BY_ISOLATED_BLOCK_REFERENCE, } from "./constants.js";
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.getDocumentMap = void 0;
27
+ const marked = __importStar(require("marked"));
28
+ const yaml_1 = require("yaml");
29
+ const constants_js_1 = require("./constants.js");
4
30
  function getHeadingPositions(document, tokens, contentOffset) {
5
31
  const positions = {
6
32
  "": {
@@ -90,7 +116,7 @@ function getBlockPositions(document, tokens, contentOffset) {
90
116
  document.slice(endMarker, endMarker + 2) === "\r\n") {
91
117
  endMarker += 2;
92
118
  }
93
- if (CAN_INCLUDE_BLOCK_REFERENCE.includes(token.type) && match) {
119
+ if (constants_js_1.CAN_INCLUDE_BLOCK_REFERENCE.includes(token.type) && match) {
94
120
  const name = match[1];
95
121
  if (!name || match.index === undefined) {
96
122
  return;
@@ -115,7 +141,7 @@ function getBlockPositions(document, tokens, contentOffset) {
115
141
  },
116
142
  };
117
143
  }
118
- if (TARGETABLE_BY_ISOLATED_BLOCK_REFERENCE.includes(token.type)) {
144
+ if (constants_js_1.TARGETABLE_BY_ISOLATED_BLOCK_REFERENCE.includes(token.type)) {
119
145
  lastBlockDetails = {
120
146
  token: token,
121
147
  start: startContent,
@@ -134,7 +160,7 @@ function preProcess(document) {
134
160
  if (match) {
135
161
  const frontmatterText = match[1].trim(); // Captured frontmatter content
136
162
  contentOffset = match[0].length; // Length of the entire frontmatter section including delimiters
137
- frontmatter = parseYaml(frontmatterText);
163
+ frontmatter = (0, yaml_1.parse)(frontmatterText);
138
164
  content = document.slice(contentOffset);
139
165
  }
140
166
  else {
@@ -147,7 +173,7 @@ function preProcess(document) {
147
173
  frontmatter,
148
174
  };
149
175
  }
150
- export const getDocumentMap = (document) => {
176
+ const getDocumentMap = (document) => {
151
177
  const { frontmatter, contentOffset, content } = preProcess(document);
152
178
  const lexer = new marked.Lexer();
153
179
  const tokens = lexer.lex(content);
@@ -160,3 +186,4 @@ export const getDocumentMap = (document) => {
160
186
  lineEnding,
161
187
  };
162
188
  };
189
+ exports.getDocumentMap = getDocumentMap;
package/dist/patch.js CHANGED
@@ -1,9 +1,35 @@
1
- import { getDocumentMap } from "./map.js";
2
- import * as marked from "marked";
3
- import * as yaml from "yaml";
4
- import { ContentType } from "./types.js";
5
- import { isAppendableFrontmatterType, isDictionary, isList, isString, isStringArrayArray, } from "./typeGuards.js";
6
- export var PatchFailureReason;
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.applyPatch = exports.TablePartsNotFound = exports.MergeNotPossible = exports.PatchError = exports.PatchFailed = exports.PatchFailureReason = void 0;
27
+ const map_js_1 = require("./map.js");
28
+ const marked = __importStar(require("marked"));
29
+ const yaml = __importStar(require("yaml"));
30
+ const types_js_1 = require("./types.js");
31
+ const typeGuards_js_1 = require("./typeGuards.js");
32
+ var PatchFailureReason;
7
33
  (function (PatchFailureReason) {
8
34
  PatchFailureReason["InvalidTarget"] = "invalid-target";
9
35
  PatchFailureReason["ContentAlreadyPreexistsInTarget"] = "content-already-preexists-in-target";
@@ -11,8 +37,8 @@ export var PatchFailureReason;
11
37
  PatchFailureReason["ContentTypeInvalid"] = "content-type-invalid";
12
38
  PatchFailureReason["ContentTypeInvalidForTarget"] = "content-type-invalid-for-target";
13
39
  PatchFailureReason["ContentNotMergeable"] = "content-not-mergeable";
14
- })(PatchFailureReason || (PatchFailureReason = {}));
15
- export class PatchFailed extends Error {
40
+ })(PatchFailureReason || (exports.PatchFailureReason = PatchFailureReason = {}));
41
+ class PatchFailed extends Error {
16
42
  constructor(reason, instruction, targetMap) {
17
43
  super();
18
44
  this.reason = reason;
@@ -22,10 +48,13 @@ export class PatchFailed extends Error {
22
48
  Object.setPrototypeOf(this, new.target.prototype);
23
49
  }
24
50
  }
25
- export class PatchError extends Error {
51
+ exports.PatchFailed = PatchFailed;
52
+ class PatchError extends Error {
26
53
  }
27
- export class MergeNotPossible extends Error {
54
+ exports.PatchError = PatchError;
55
+ class MergeNotPossible extends Error {
28
56
  }
57
+ exports.MergeNotPossible = MergeNotPossible;
29
58
  const replaceText = (document, instruction, target) => {
30
59
  return [
31
60
  document.slice(0, target.content.start),
@@ -51,8 +80,9 @@ const appendText = (document, instruction, target) => {
51
80
  document.slice(target.content.end),
52
81
  ].join("");
53
82
  };
54
- export class TablePartsNotFound extends Error {
83
+ class TablePartsNotFound extends Error {
55
84
  }
85
+ exports.TablePartsNotFound = TablePartsNotFound;
56
86
  const _getTableData = (document, target) => {
57
87
  const targetTable = document.slice(target.content.start, target.content.end);
58
88
  const tableToken = marked.lexer(targetTable)[0];
@@ -72,7 +102,7 @@ const replaceTable = (document, instruction, target) => {
72
102
  try {
73
103
  const table = _getTableData(document, target);
74
104
  const tableRows = [table.headerParts];
75
- if (isStringArrayArray(instruction.content)) {
105
+ if ((0, typeGuards_js_1.isStringArrayArray)(instruction.content)) {
76
106
  for (const row of instruction.content) {
77
107
  if (row.length !== table.token.header.length ||
78
108
  typeof row === "string") {
@@ -98,7 +128,7 @@ const prependTable = (document, instruction, target) => {
98
128
  try {
99
129
  const table = _getTableData(document, target);
100
130
  const tableRows = [table.headerParts];
101
- if (isStringArrayArray(instruction.content)) {
131
+ if ((0, typeGuards_js_1.isStringArrayArray)(instruction.content)) {
102
132
  for (const row of instruction.content) {
103
133
  if (row.length !== table.token.header.length ||
104
134
  typeof row === "string") {
@@ -125,7 +155,7 @@ const appendTable = (document, instruction, target) => {
125
155
  try {
126
156
  const table = _getTableData(document, target);
127
157
  const tableRows = [table.headerParts, table.contentParts];
128
- if (isStringArrayArray(instruction.content)) {
158
+ if ((0, typeGuards_js_1.isStringArrayArray)(instruction.content)) {
129
159
  for (const row of instruction.content) {
130
160
  if (row.length !== table.token.header.length ||
131
161
  typeof row === "string") {
@@ -150,33 +180,33 @@ const appendTable = (document, instruction, target) => {
150
180
  const replace = (document, instruction, target) => {
151
181
  const contentType = "contentType" in instruction && instruction.contentType
152
182
  ? instruction.contentType
153
- : ContentType.text;
183
+ : types_js_1.ContentType.text;
154
184
  switch (contentType) {
155
- case ContentType.text:
185
+ case types_js_1.ContentType.text:
156
186
  return replaceText(document, instruction, target);
157
- case ContentType.json:
187
+ case types_js_1.ContentType.json:
158
188
  return replaceTable(document, instruction, target);
159
189
  }
160
190
  };
161
191
  const prepend = (document, instruction, target) => {
162
192
  const contentType = "contentType" in instruction && instruction.contentType
163
193
  ? instruction.contentType
164
- : ContentType.text;
194
+ : types_js_1.ContentType.text;
165
195
  switch (contentType) {
166
- case ContentType.text:
196
+ case types_js_1.ContentType.text:
167
197
  return prependText(document, instruction, target);
168
- case ContentType.json:
198
+ case types_js_1.ContentType.json:
169
199
  return prependTable(document, instruction, target);
170
200
  }
171
201
  };
172
202
  const append = (document, instruction, target) => {
173
203
  const contentType = "contentType" in instruction && instruction.contentType
174
204
  ? instruction.contentType
175
- : ContentType.text;
205
+ : types_js_1.ContentType.text;
176
206
  switch (contentType) {
177
- case ContentType.text:
207
+ case types_js_1.ContentType.text:
178
208
  return appendText(document, instruction, target);
179
- case ContentType.json:
209
+ case types_js_1.ContentType.json:
180
210
  return appendTable(document, instruction, target);
181
211
  }
182
212
  };
@@ -237,13 +267,13 @@ const getTarget = (map, instruction) => {
237
267
  }
238
268
  };
239
269
  function mergeFrontmatterValue(obj1, obj2) {
240
- if (isList(obj1) && isList(obj2)) {
270
+ if ((0, typeGuards_js_1.isList)(obj1) && (0, typeGuards_js_1.isList)(obj2)) {
241
271
  return [...obj1, ...obj2];
242
272
  }
243
- else if (isDictionary(obj1) && isDictionary(obj2)) {
273
+ else if ((0, typeGuards_js_1.isDictionary)(obj1) && (0, typeGuards_js_1.isDictionary)(obj2)) {
244
274
  return { ...obj1, ...obj2 };
245
275
  }
246
- else if (isString(obj1) && isString(obj2)) {
276
+ else if ((0, typeGuards_js_1.isString)(obj1) && (0, typeGuards_js_1.isString)(obj2)) {
247
277
  return obj1 + obj2;
248
278
  }
249
279
  throw new Error(`Cannot merge objects of different types or unsupported types: ${typeof obj1} and ${typeof obj2}`);
@@ -265,8 +295,8 @@ function regenerateDocumentWithFrontmatter(frontmatter, document, map) {
265
295
  * @param instruction The patch to apply.
266
296
  * @returns The patched document
267
297
  */
268
- export const applyPatch = (document, instruction) => {
269
- const map = getDocumentMap(document);
298
+ const applyPatch = (document, instruction) => {
299
+ const map = (0, map_js_1.getDocumentMap)(document);
270
300
  const target = getTarget(map, instruction);
271
301
  if (instruction.targetType === "block" ||
272
302
  instruction.targetType === "heading") {
@@ -300,13 +330,13 @@ export const applyPatch = (document, instruction) => {
300
330
  const frontmatter = { ...map.frontmatter };
301
331
  if (frontmatter[instruction.target] === undefined) {
302
332
  if (instruction.createTargetIfMissing) {
303
- if (isList(instruction.content)) {
333
+ if ((0, typeGuards_js_1.isList)(instruction.content)) {
304
334
  frontmatter[instruction.target] = [];
305
335
  }
306
- else if (isString(instruction.content)) {
336
+ else if ((0, typeGuards_js_1.isString)(instruction.content)) {
307
337
  frontmatter[instruction.target] = "";
308
338
  }
309
- else if (isDictionary(instruction.content)) {
339
+ else if ((0, typeGuards_js_1.isDictionary)(instruction.content)) {
310
340
  frontmatter[instruction.target] = {};
311
341
  }
312
342
  }
@@ -317,13 +347,13 @@ export const applyPatch = (document, instruction) => {
317
347
  try {
318
348
  switch (instruction.operation) {
319
349
  case "append":
320
- if (!isAppendableFrontmatterType(instruction.content)) {
350
+ if (!(0, typeGuards_js_1.isAppendableFrontmatterType)(instruction.content)) {
321
351
  throw new MergeNotPossible();
322
352
  }
323
353
  frontmatter[instruction.target] = mergeFrontmatterValue(frontmatter[instruction.target], instruction.content);
324
354
  break;
325
355
  case "prepend":
326
- if (!isAppendableFrontmatterType(instruction.content)) {
356
+ if (!(0, typeGuards_js_1.isAppendableFrontmatterType)(instruction.content)) {
327
357
  throw new MergeNotPossible();
328
358
  }
329
359
  frontmatter[instruction.target] = mergeFrontmatterValue(instruction.content, frontmatter[instruction.target]);
@@ -341,3 +371,4 @@ export const applyPatch = (document, instruction) => {
341
371
  throw error;
342
372
  }
343
373
  };
374
+ exports.applyPatch = applyPatch;
@@ -1,10 +1,15 @@
1
- import fs from "fs";
2
- import path from "path";
3
- import { getDocumentMap } from "../map";
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const fs_1 = __importDefault(require("fs"));
7
+ const path_1 = __importDefault(require("path"));
8
+ const map_1 = require("../map");
4
9
  describe("map", () => {
5
- const sample = fs.readFileSync(path.join(__dirname, "sample.md"), "utf-8");
10
+ const sample = fs_1.default.readFileSync(path_1.default.join(__dirname, "sample.md"), "utf-8");
6
11
  test("heading", () => {
7
- const actualHeadings = getDocumentMap(sample).heading;
12
+ const actualHeadings = (0, map_1.getDocumentMap)(sample).heading;
8
13
  const expectedHeadings = {
9
14
  "": {
10
15
  content: {
@@ -143,7 +148,7 @@ describe("map", () => {
143
148
  expect(actualHeadings).toEqual(expectedHeadings);
144
149
  });
145
150
  test("block", () => {
146
- const actualBlocks = getDocumentMap(sample).block;
151
+ const actualBlocks = (0, map_1.getDocumentMap)(sample).block;
147
152
  const expectedBlocks = {
148
153
  "2c67a6": {
149
154
  content: {
@@ -201,7 +206,7 @@ describe("map", () => {
201
206
  });
202
207
  describe("frontmatter", () => {
203
208
  test("exists", () => {
204
- const actualFrontmatter = getDocumentMap(sample).frontmatter;
209
+ const actualFrontmatter = (0, map_1.getDocumentMap)(sample).frontmatter;
205
210
  const expectedFrontmatter = {
206
211
  aliases: ["Structured Markdown Patch"],
207
212
  "project-type": "Technical",
@@ -210,14 +215,14 @@ describe("map", () => {
210
215
  expect(expectedFrontmatter).toEqual(actualFrontmatter);
211
216
  });
212
217
  test("does not exist", () => {
213
- const sample = fs.readFileSync(path.join(__dirname, "sample.frontmatter.none.md"), "utf-8");
214
- const actualFrontmatter = getDocumentMap(sample).frontmatter;
218
+ const sample = fs_1.default.readFileSync(path_1.default.join(__dirname, "sample.frontmatter.none.md"), "utf-8");
219
+ const actualFrontmatter = (0, map_1.getDocumentMap)(sample).frontmatter;
215
220
  const expectedFrontmatter = {};
216
221
  expect(expectedFrontmatter).toEqual(actualFrontmatter);
217
222
  });
218
223
  test("does not exist, but starts with hr", () => {
219
- const sample = fs.readFileSync(path.join(__dirname, "sample.frontmatter.nonfrontmatter-hr.md"), "utf-8");
220
- const actualFrontmatter = getDocumentMap(sample).frontmatter;
224
+ const sample = fs_1.default.readFileSync(path_1.default.join(__dirname, "sample.frontmatter.nonfrontmatter-hr.md"), "utf-8");
225
+ const actualFrontmatter = (0, map_1.getDocumentMap)(sample).frontmatter;
221
226
  const expectedFrontmatter = {};
222
227
  expect(expectedFrontmatter).toEqual(actualFrontmatter);
223
228
  });
@@ -1,13 +1,18 @@
1
- import fs from "fs";
2
- import path from "path";
3
- import { applyPatch, PatchFailed } from "../patch";
4
- import { ContentType } from "../types";
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const fs_1 = __importDefault(require("fs"));
7
+ const path_1 = __importDefault(require("path"));
8
+ const patch_1 = require("../patch");
9
+ const types_1 = require("../types");
5
10
  describe("patch", () => {
6
- const sample = fs.readFileSync(path.join(__dirname, "sample.md"), "utf-8");
11
+ const sample = fs_1.default.readFileSync(path_1.default.join(__dirname, "sample.md"), "utf-8");
7
12
  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);
13
+ const inputDocument = fs_1.default.readFileSync(path_1.default.join(__dirname, inputDocumentPath), "utf-8");
14
+ const outputDocument = fs_1.default.readFileSync(path_1.default.join(__dirname, outputDocumentPath), "utf-8");
15
+ expect((0, patch_1.applyPatch)(inputDocument, instruction)).toEqual(outputDocument);
11
16
  };
12
17
  describe("heading", () => {
13
18
  test("prepend", () => {
@@ -117,8 +122,8 @@ describe("patch", () => {
117
122
  // applyIfContentPreexists: false, # default
118
123
  };
119
124
  expect(() => {
120
- applyPatch(sample, instruction);
121
- }).toThrow(PatchFailed);
125
+ (0, patch_1.applyPatch)(sample, instruction);
126
+ }).toThrow(patch_1.PatchFailed);
122
127
  });
123
128
  test("does not preexist at target", () => {
124
129
  const instruction = {
@@ -129,8 +134,8 @@ describe("patch", () => {
129
134
  // applyIfContentPreexists: false, # default
130
135
  };
131
136
  expect(() => {
132
- applyPatch(sample, instruction);
133
- }).not.toThrow(PatchFailed);
137
+ (0, patch_1.applyPatch)(sample, instruction);
138
+ }).not.toThrow(patch_1.PatchFailed);
134
139
  });
135
140
  });
136
141
  });
@@ -145,8 +150,8 @@ describe("patch", () => {
145
150
  applyIfContentPreexists: true,
146
151
  };
147
152
  expect(() => {
148
- applyPatch(sample, instruction);
149
- }).not.toThrow(PatchFailed);
153
+ (0, patch_1.applyPatch)(sample, instruction);
154
+ }).not.toThrow(patch_1.PatchFailed);
150
155
  });
151
156
  });
152
157
  });
@@ -187,7 +192,7 @@ describe("patch", () => {
187
192
  targetType: "block",
188
193
  target: "2c67a6",
189
194
  operation: "prepend",
190
- contentType: ContentType.json,
195
+ contentType: types_1.ContentType.json,
191
196
  content: [
192
197
  ["`something else`", "Some other application", "✅", "✅", "✅"],
193
198
  ],
@@ -199,7 +204,7 @@ describe("patch", () => {
199
204
  targetType: "block",
200
205
  target: "2c67a6",
201
206
  operation: "append",
202
- contentType: ContentType.json,
207
+ contentType: types_1.ContentType.json,
203
208
  content: [
204
209
  ["`something else`", "Some other application", "✅", "✅", "✅"],
205
210
  ],
@@ -211,7 +216,7 @@ describe("patch", () => {
211
216
  targetType: "block",
212
217
  target: "2c67a6",
213
218
  operation: "replace",
214
- contentType: ContentType.json,
219
+ contentType: types_1.ContentType.json,
215
220
  content: [
216
221
  ["`something else`", "Some other application", "✅", "✅", "✅"],
217
222
  ],
@@ -228,31 +233,31 @@ describe("patch", () => {
228
233
  targetType: "frontmatter",
229
234
  target: "array-value",
230
235
  operation: "append",
231
- contentType: ContentType.json,
236
+ contentType: types_1.ContentType.json,
232
237
  content: "OK",
233
238
  };
234
239
  expect(() => {
235
- applyPatch(sample, instruction);
236
- }).toThrow(PatchFailed);
240
+ (0, patch_1.applyPatch)(sample, instruction);
241
+ }).toThrow(patch_1.PatchFailed);
237
242
  });
238
243
  test("invalid type", () => {
239
244
  const instruction = {
240
245
  targetType: "frontmatter",
241
246
  target: "array-value",
242
247
  operation: "append",
243
- contentType: ContentType.json,
248
+ contentType: types_1.ContentType.json,
244
249
  content: 10,
245
250
  };
246
251
  expect(() => {
247
- applyPatch(sample, instruction);
248
- }).toThrow(PatchFailed);
252
+ (0, patch_1.applyPatch)(sample, instruction);
253
+ }).toThrow(patch_1.PatchFailed);
249
254
  });
250
255
  test("list", () => {
251
256
  const instruction = {
252
257
  targetType: "frontmatter",
253
258
  target: "array-value",
254
259
  operation: "append",
255
- contentType: ContentType.json,
260
+ contentType: types_1.ContentType.json,
256
261
  content: ["Beep"],
257
262
  };
258
263
  assertPatchResultsMatch("sample.frontmatter.md", "sample.patch.frontmatter.append.list.md", instruction);
@@ -262,7 +267,7 @@ describe("patch", () => {
262
267
  targetType: "frontmatter",
263
268
  target: "object-value",
264
269
  operation: "append",
265
- contentType: ContentType.json,
270
+ contentType: types_1.ContentType.json,
266
271
  content: { three: "Beep" },
267
272
  };
268
273
  assertPatchResultsMatch("sample.frontmatter.md", "sample.patch.frontmatter.append.dictionary.md", instruction);
@@ -272,7 +277,7 @@ describe("patch", () => {
272
277
  targetType: "frontmatter",
273
278
  target: "string-value",
274
279
  operation: "append",
275
- contentType: ContentType.json,
280
+ contentType: types_1.ContentType.json,
276
281
  content: "Beep",
277
282
  };
278
283
  assertPatchResultsMatch("sample.frontmatter.md", "sample.patch.frontmatter.append.string.md", instruction);
@@ -284,31 +289,31 @@ describe("patch", () => {
284
289
  targetType: "frontmatter",
285
290
  target: "array-value",
286
291
  operation: "prepend",
287
- contentType: ContentType.json,
292
+ contentType: types_1.ContentType.json,
288
293
  content: "OK",
289
294
  };
290
295
  expect(() => {
291
- applyPatch(sample, instruction);
292
- }).toThrow(PatchFailed);
296
+ (0, patch_1.applyPatch)(sample, instruction);
297
+ }).toThrow(patch_1.PatchFailed);
293
298
  });
294
299
  test("invalid type", () => {
295
300
  const instruction = {
296
301
  targetType: "frontmatter",
297
302
  target: "array-value",
298
303
  operation: "prepend",
299
- contentType: ContentType.json,
304
+ contentType: types_1.ContentType.json,
300
305
  content: 10,
301
306
  };
302
307
  expect(() => {
303
- applyPatch(sample, instruction);
304
- }).toThrow(PatchFailed);
308
+ (0, patch_1.applyPatch)(sample, instruction);
309
+ }).toThrow(patch_1.PatchFailed);
305
310
  });
306
311
  test("list", () => {
307
312
  const instruction = {
308
313
  targetType: "frontmatter",
309
314
  target: "array-value",
310
315
  operation: "prepend",
311
- contentType: ContentType.json,
316
+ contentType: types_1.ContentType.json,
312
317
  content: ["Beep"],
313
318
  };
314
319
  assertPatchResultsMatch("sample.frontmatter.md", "sample.patch.frontmatter.prepend.list.md", instruction);
@@ -318,7 +323,7 @@ describe("patch", () => {
318
323
  targetType: "frontmatter",
319
324
  target: "object-value",
320
325
  operation: "prepend",
321
- contentType: ContentType.json,
326
+ contentType: types_1.ContentType.json,
322
327
  content: { three: "Beep" },
323
328
  };
324
329
  assertPatchResultsMatch("sample.frontmatter.md", "sample.patch.frontmatter.prepend.dictionary.md", instruction);
@@ -328,7 +333,7 @@ describe("patch", () => {
328
333
  targetType: "frontmatter",
329
334
  target: "string-value",
330
335
  operation: "prepend",
331
- contentType: ContentType.json,
336
+ contentType: types_1.ContentType.json,
332
337
  content: "Beep",
333
338
  };
334
339
  assertPatchResultsMatch("sample.frontmatter.md", "sample.patch.frontmatter.prepend.string.md", instruction);
@@ -340,7 +345,7 @@ describe("patch", () => {
340
345
  targetType: "frontmatter",
341
346
  target: "array-value",
342
347
  operation: "replace",
343
- contentType: ContentType.json,
348
+ contentType: types_1.ContentType.json,
344
349
  content: ["Replaced"],
345
350
  };
346
351
  assertPatchResultsMatch("sample.frontmatter.md", "sample.patch.frontmatter.replace.list.md", instruction);
@@ -350,7 +355,7 @@ describe("patch", () => {
350
355
  targetType: "frontmatter",
351
356
  target: "object-value",
352
357
  operation: "replace",
353
- contentType: ContentType.json,
358
+ contentType: types_1.ContentType.json,
354
359
  content: {
355
360
  replaced: true,
356
361
  },
@@ -362,7 +367,7 @@ describe("patch", () => {
362
367
  targetType: "frontmatter",
363
368
  target: "string-value",
364
369
  operation: "replace",
365
- contentType: ContentType.json,
370
+ contentType: types_1.ContentType.json,
366
371
  content: "Replaced",
367
372
  };
368
373
  assertPatchResultsMatch("sample.frontmatter.md", "sample.patch.frontmatter.replace.string.md", instruction);
@@ -372,7 +377,7 @@ describe("patch", () => {
372
377
  targetType: "frontmatter",
373
378
  target: "number-value",
374
379
  operation: "replace",
375
- contentType: ContentType.json,
380
+ contentType: types_1.ContentType.json,
376
381
  content: 10,
377
382
  };
378
383
  assertPatchResultsMatch("sample.frontmatter.md", "sample.patch.frontmatter.replace.number.md", instruction);
@@ -382,7 +387,7 @@ describe("patch", () => {
382
387
  targetType: "frontmatter",
383
388
  target: "boolean-value",
384
389
  operation: "replace",
385
- contentType: ContentType.json,
390
+ contentType: types_1.ContentType.json,
386
391
  content: true,
387
392
  };
388
393
  assertPatchResultsMatch("sample.frontmatter.md", "sample.patch.frontmatter.replace.boolean.md", instruction);
@@ -393,7 +398,7 @@ describe("patch", () => {
393
398
  targetType: "frontmatter",
394
399
  target: "new-field",
395
400
  operation: "replace",
396
- contentType: ContentType.json,
401
+ contentType: types_1.ContentType.json,
397
402
  content: ["New Value"],
398
403
  createTargetIfMissing: true,
399
404
  };
@@ -404,7 +409,7 @@ describe("patch", () => {
404
409
  targetType: "frontmatter",
405
410
  target: "new-field",
406
411
  operation: "replace",
407
- contentType: ContentType.json,
412
+ contentType: types_1.ContentType.json,
408
413
  content: {
409
414
  newValue: true,
410
415
  },
@@ -417,7 +422,7 @@ describe("patch", () => {
417
422
  targetType: "frontmatter",
418
423
  target: "new-field",
419
424
  operation: "replace",
420
- contentType: ContentType.json,
425
+ contentType: types_1.ContentType.json,
421
426
  content: "New Value",
422
427
  createTargetIfMissing: true,
423
428
  };
@@ -428,7 +433,7 @@ describe("patch", () => {
428
433
  targetType: "frontmatter",
429
434
  target: "new-field",
430
435
  operation: "replace",
431
- contentType: ContentType.json,
436
+ contentType: types_1.ContentType.json,
432
437
  content: 588600,
433
438
  createTargetIfMissing: true,
434
439
  };
@@ -439,7 +444,7 @@ describe("patch", () => {
439
444
  targetType: "frontmatter",
440
445
  target: "new-field",
441
446
  operation: "replace",
442
- contentType: ContentType.json,
447
+ contentType: types_1.ContentType.json,
443
448
  content: true,
444
449
  createTargetIfMissing: true,
445
450
  };
@@ -1,4 +1,11 @@
1
- export function isStringArrayArray(obj) {
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isStringArrayArray = isStringArrayArray;
4
+ exports.isAppendableFrontmatterType = isAppendableFrontmatterType;
5
+ exports.isString = isString;
6
+ exports.isDictionary = isDictionary;
7
+ exports.isList = isList;
8
+ function isStringArrayArray(obj) {
2
9
  // Check if the object is an array
3
10
  if (!Array.isArray(obj))
4
11
  return false;
@@ -6,15 +13,15 @@ export function isStringArrayArray(obj) {
6
13
  return obj.every((item) => Array.isArray(item) &&
7
14
  item.every((subItem) => typeof subItem === "string"));
8
15
  }
9
- export function isAppendableFrontmatterType(obj) {
16
+ function isAppendableFrontmatterType(obj) {
10
17
  return isString(obj) || isDictionary(obj) || isList(obj);
11
18
  }
12
- export function isString(obj) {
19
+ function isString(obj) {
13
20
  return typeof obj === "string";
14
21
  }
15
- export function isDictionary(obj) {
22
+ function isDictionary(obj) {
16
23
  return typeof obj === "object" && obj !== null && !Array.isArray(obj);
17
24
  }
18
- export function isList(obj) {
25
+ function isList(obj) {
19
26
  return Array.isArray(obj);
20
27
  }
package/dist/types.js CHANGED
@@ -1,4 +1,7 @@
1
- export var ContentType;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ContentType = void 0;
4
+ var ContentType;
2
5
  (function (ContentType) {
3
6
  /**
4
7
  * Content is simple markdown text.
@@ -8,4 +11,4 @@ export var ContentType;
8
11
  * Content is a JSON document
9
12
  */
10
13
  ContentType["json"] = "application/json";
11
- })(ContentType || (ContentType = {}));
14
+ })(ContentType || (exports.ContentType = ContentType = {}));
package/package.json CHANGED
@@ -19,7 +19,7 @@
19
19
  "typescript": "^5.5.4"
20
20
  },
21
21
  "name": "markdown-patch",
22
- "version": "0.2.1",
22
+ "version": "0.3.0",
23
23
  "main": "./dist/index.js",
24
24
  "scripts": {
25
25
  "build": "tsc",