writr 4.4.0 → 4.4.1

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 CHANGED
@@ -380,7 +380,7 @@ export type resultData = {
380
380
  }
381
381
  ```
382
382
 
383
- For `beforeSaveToFile` the data object is an object with the `filePath` and `content`. Here is the interface for `saveToFileData`:
383
+ For `saveToFile` the data object is an object with the `filePath` and `content`. Here is the interface for `saveToFileData`:
384
384
 
385
385
  ```typescript
386
386
  export type saveToFileData = {
@@ -391,7 +391,7 @@ export type saveToFileData = {
391
391
 
392
392
  This is called when you call `saveToFile`, `saveToFileSync`.
393
393
 
394
- For `beforeRenderToFile` the data object is an object with the `filePath` and `content`. Here is the interface for `renderToFileData`:
394
+ For `renderToFile` the data object is an object with the `filePath` and `content`. Here is the interface for `renderToFileData`:
395
395
 
396
396
  ```typescript
397
397
  export type renderToFileData = {
@@ -402,6 +402,16 @@ export type renderToFileData = {
402
402
 
403
403
  This is called when you call `renderToFile`, `renderToFileSync`.
404
404
 
405
+ For `loadFromFile` the data object is an object with `content` so you can change before it is set on `writr.content`. Here is the interface for `loadFromFileData`:
406
+
407
+ ```typescript
408
+ export type loadFromFileData = {
409
+ content: string;
410
+ }
411
+ ```
412
+
413
+ This is called when you call `loadFromFile`, `loadFromFileSync`.
414
+
405
415
  # Code of Conduct and Contributing
406
416
  [Code of Conduct](CODE_OF_CONDUCT.md) and [Contributing](CONTRIBUTING.md) guidelines.
407
417
 
package/dist/writr.d.ts CHANGED
@@ -52,10 +52,9 @@ type RenderOptions = {
52
52
  declare enum WritrHooks {
53
53
  beforeRender = "beforeRender",
54
54
  afterRender = "afterRender",
55
- beforeSaveToFile = "beforeSaveToFile",
56
- beforeRenderToFile = "beforeRenderToFile",
57
- beforeLoadFromFile = "beforeLoadFromFile",
58
- afterLoadFromFile = "afterLoadFromFile"
55
+ saveToFile = "saveToFile",
56
+ renderToFile = "renderToFile",
57
+ loadFromFile = "loadFromFile"
59
58
  }
60
59
  declare class Writr extends Hookified {
61
60
  engine: unified.Processor<mdast.Root, mdast.Root, hast.Root, hast.Root, string>;
package/dist/writr.js CHANGED
@@ -57,10 +57,9 @@ var WritrCache = class {
57
57
  var WritrHooks = /* @__PURE__ */ ((WritrHooks2) => {
58
58
  WritrHooks2["beforeRender"] = "beforeRender";
59
59
  WritrHooks2["afterRender"] = "afterRender";
60
- WritrHooks2["beforeSaveToFile"] = "beforeSaveToFile";
61
- WritrHooks2["beforeRenderToFile"] = "beforeRenderToFile";
62
- WritrHooks2["beforeLoadFromFile"] = "beforeLoadFromFile";
63
- WritrHooks2["afterLoadFromFile"] = "afterLoadFromFile";
60
+ WritrHooks2["saveToFile"] = "saveToFile";
61
+ WritrHooks2["renderToFile"] = "renderToFile";
62
+ WritrHooks2["loadFromFile"] = "loadFromFile";
64
63
  return WritrHooks2;
65
64
  })(WritrHooks || {});
66
65
  var Writr = class extends Hookified {
@@ -294,7 +293,7 @@ ${yamlString}---
294
293
  filePath,
295
294
  content
296
295
  };
297
- await this.hook("beforeRenderToFile" /* beforeRenderToFile */, data);
296
+ await this.hook("renderToFile" /* renderToFile */, data);
298
297
  await writeFile(data.filePath, data.content);
299
298
  } catch (error) {
300
299
  this.emit("error", error);
@@ -317,7 +316,7 @@ ${yamlString}---
317
316
  filePath,
318
317
  content
319
318
  };
320
- this.hook("beforeRenderToFile" /* beforeRenderToFile */, data);
319
+ this.hook("renderToFile" /* renderToFile */, data);
321
320
  fs.writeFileSync(data.filePath, data.content);
322
321
  } catch (error) {
323
322
  this.emit("error", error);
@@ -352,8 +351,20 @@ ${yamlString}---
352
351
  * @returns {Promise<void>}
353
352
  */
354
353
  async loadFromFile(filePath) {
355
- const { readFile } = fs.promises;
356
- this._content = await readFile(filePath, "utf8");
354
+ try {
355
+ const { readFile } = fs.promises;
356
+ const data = {
357
+ content: ""
358
+ };
359
+ data.content = await readFile(filePath, "utf8");
360
+ await this.hook("loadFromFile" /* loadFromFile */, data);
361
+ this._content = data.content;
362
+ } catch (error) {
363
+ this.emit("error", error);
364
+ if (this._options.throwErrors) {
365
+ throw error;
366
+ }
367
+ }
357
368
  }
358
369
  /**
359
370
  * Load markdown content from a file synchronously.
@@ -361,7 +372,19 @@ ${yamlString}---
361
372
  * @returns {void}
362
373
  */
363
374
  loadFromFileSync(filePath) {
364
- this._content = fs.readFileSync(filePath, "utf8");
375
+ try {
376
+ const data = {
377
+ content: ""
378
+ };
379
+ data.content = fs.readFileSync(filePath, "utf8");
380
+ this.hook("loadFromFile" /* loadFromFile */, data);
381
+ this._content = data.content;
382
+ } catch (error) {
383
+ this.emit("error", error);
384
+ if (this._options.throwErrors) {
385
+ throw error;
386
+ }
387
+ }
365
388
  }
366
389
  /**
367
390
  * Save the markdown content to a file. If the directory doesn't exist it will be created.
@@ -377,7 +400,7 @@ ${yamlString}---
377
400
  filePath,
378
401
  content: this._content
379
402
  };
380
- await this.hook("beforeSaveToFile" /* beforeSaveToFile */, data);
403
+ await this.hook("saveToFile" /* saveToFile */, data);
381
404
  await writeFile(data.filePath, data.content);
382
405
  } catch (error) {
383
406
  this.emit("error", error);
@@ -399,7 +422,7 @@ ${yamlString}---
399
422
  filePath,
400
423
  content: this._content
401
424
  };
402
- this.hook("beforeSaveToFile" /* beforeSaveToFile */, data);
425
+ this.hook("saveToFile" /* saveToFile */, data);
403
426
  fs.writeFileSync(data.filePath, data.content);
404
427
  } catch (error) {
405
428
  this.emit("error", error);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "writr",
3
- "version": "4.4.0",
3
+ "version": "4.4.1",
4
4
  "description": "Markdown Rendering Simplified",
5
5
  "type": "module",
6
6
  "main": "./dist/writr.js",
@@ -54,16 +54,16 @@
54
54
  },
55
55
  "dependencies": {
56
56
  "cacheable": "^1.8.8",
57
- "hookified": "^1.7.0",
57
+ "hookified": "^1.7.1",
58
58
  "html-react-parser": "^5.2.2",
59
59
  "js-yaml": "^4.1.0",
60
60
  "react": "^19.0.0",
61
- "rehype-highlight": "^7.0.1",
61
+ "rehype-highlight": "^7.0.2",
62
62
  "rehype-katex": "^7.0.1",
63
63
  "rehype-slug": "^6.0.0",
64
64
  "rehype-stringify": "^10.0.1",
65
65
  "remark-emoji": "^5.0.1",
66
- "remark-gfm": "^4.0.0",
66
+ "remark-gfm": "^4.0.1",
67
67
  "remark-math": "^6.0.0",
68
68
  "remark-mdx": "^3.1.0",
69
69
  "remark-parse": "^11.0.0",
@@ -73,16 +73,16 @@
73
73
  },
74
74
  "devDependencies": {
75
75
  "@types/js-yaml": "^4.0.9",
76
- "@types/node": "^22.12.0",
77
- "@types/react": "^19.0.8",
78
- "@vitest/coverage-v8": "^3.0.4",
79
- "docula": "^0.10.0",
76
+ "@types/node": "^22.13.5",
77
+ "@types/react": "^19.0.10",
78
+ "@vitest/coverage-v8": "^3.0.7",
79
+ "docula": "^0.10.1",
80
80
  "rimraf": "^6.0.1",
81
81
  "ts-node": "^10.9.2",
82
- "tsup": "^8.3.6",
82
+ "tsup": "^8.4.0",
83
83
  "typescript": "^5.7.3",
84
- "vitest": "^3.0.4",
85
- "webpack": "^5.97.1",
84
+ "vitest": "^3.0.7",
85
+ "webpack": "^5.98.0",
86
86
  "xo": "^0.60.0"
87
87
  },
88
88
  "xo": {