writr 4.3.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
@@ -366,9 +366,8 @@ console.log(result); // Hello, Universe!
366
366
  For `beforeRender` the data object is a `renderData` object. Here is the interface for `renderData`:
367
367
 
368
368
  ```typescript
369
- export type renderData {
370
- body: string;
371
- content: string;
369
+ export type renderData = {
370
+ body: string
372
371
  options: RenderOptions;
373
372
  }
374
373
  ```
@@ -376,11 +375,43 @@ export type renderData {
376
375
  For `afterRender` the data object is a `resultData` object. Here is the interface for `resultData`:
377
376
 
378
377
  ```typescript
379
- export type resultData {
378
+ export type resultData = {
380
379
  result: string;
381
380
  }
382
381
  ```
383
382
 
383
+ For `saveToFile` the data object is an object with the `filePath` and `content`. Here is the interface for `saveToFileData`:
384
+
385
+ ```typescript
386
+ export type saveToFileData = {
387
+ filePath: string;
388
+ content: string;
389
+ }
390
+ ```
391
+
392
+ This is called when you call `saveToFile`, `saveToFileSync`.
393
+
394
+ For `renderToFile` the data object is an object with the `filePath` and `content`. Here is the interface for `renderToFileData`:
395
+
396
+ ```typescript
397
+ export type renderToFileData = {
398
+ filePath: string;
399
+ content: string;
400
+ }
401
+ ```
402
+
403
+ This is called when you call `renderToFile`, `renderToFileSync`.
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
+
384
415
  # Code of Conduct and Contributing
385
416
  [Code of Conduct](CODE_OF_CONDUCT.md) and [Contributing](CONTRIBUTING.md) guidelines.
386
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
- afterSaveToFile = "afterSaveToFile",
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["afterSaveToFile"] = "afterSaveToFile";
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 {
@@ -290,7 +289,12 @@ ${yamlString}---
290
289
  const directoryPath = dirname(filePath);
291
290
  const content = await this.render(options);
292
291
  await mkdir(directoryPath, { recursive: true });
293
- await writeFile(filePath, content, "utf8");
292
+ const data = {
293
+ filePath,
294
+ content
295
+ };
296
+ await this.hook("renderToFile" /* renderToFile */, data);
297
+ await writeFile(data.filePath, data.content);
294
298
  } catch (error) {
295
299
  this.emit("error", error);
296
300
  if (this._options.throwErrors) {
@@ -308,7 +312,12 @@ ${yamlString}---
308
312
  const directoryPath = dirname(filePath);
309
313
  const content = this.renderSync(options);
310
314
  fs.mkdirSync(directoryPath, { recursive: true });
311
- fs.writeFileSync(filePath, content, "utf8");
315
+ const data = {
316
+ filePath,
317
+ content
318
+ };
319
+ this.hook("renderToFile" /* renderToFile */, data);
320
+ fs.writeFileSync(data.filePath, data.content);
312
321
  } catch (error) {
313
322
  this.emit("error", error);
314
323
  if (this._options.throwErrors) {
@@ -342,8 +351,20 @@ ${yamlString}---
342
351
  * @returns {Promise<void>}
343
352
  */
344
353
  async loadFromFile(filePath) {
345
- const { readFile } = fs.promises;
346
- 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
+ }
347
368
  }
348
369
  /**
349
370
  * Load markdown content from a file synchronously.
@@ -351,7 +372,19 @@ ${yamlString}---
351
372
  * @returns {void}
352
373
  */
353
374
  loadFromFileSync(filePath) {
354
- 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
+ }
355
388
  }
356
389
  /**
357
390
  * Save the markdown content to a file. If the directory doesn't exist it will be created.
@@ -363,7 +396,12 @@ ${yamlString}---
363
396
  const { writeFile, mkdir } = fs.promises;
364
397
  const directoryPath = dirname(filePath);
365
398
  await mkdir(directoryPath, { recursive: true });
366
- await writeFile(filePath, this._content, "utf8");
399
+ const data = {
400
+ filePath,
401
+ content: this._content
402
+ };
403
+ await this.hook("saveToFile" /* saveToFile */, data);
404
+ await writeFile(data.filePath, data.content);
367
405
  } catch (error) {
368
406
  this.emit("error", error);
369
407
  if (this._options.throwErrors) {
@@ -380,7 +418,12 @@ ${yamlString}---
380
418
  try {
381
419
  const directoryPath = dirname(filePath);
382
420
  fs.mkdirSync(directoryPath, { recursive: true });
383
- fs.writeFileSync(filePath, this._content, "utf8");
421
+ const data = {
422
+ filePath,
423
+ content: this._content
424
+ };
425
+ this.hook("saveToFile" /* saveToFile */, data);
426
+ fs.writeFileSync(data.filePath, data.content);
384
427
  } catch (error) {
385
428
  this.emit("error", error);
386
429
  if (this._options.throwErrors) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "writr",
3
- "version": "4.3.0",
3
+ "version": "4.4.1",
4
4
  "description": "Markdown Rendering Simplified",
5
5
  "type": "module",
6
6
  "main": "./dist/writr.js",
@@ -53,17 +53,17 @@
53
53
  "website:serve": "rimraf ./site/README.md ./site/dist && npx docula serve -s ./site -o ./site/dist"
54
54
  },
55
55
  "dependencies": {
56
- "cacheable": "^1.8.7",
57
- "hookified": "^1.6.0",
56
+ "cacheable": "^1.8.8",
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.10.2",
77
- "@types/react": "^19.0.2",
78
- "@vitest/coverage-v8": "^2.1.8",
79
- "docula": "^0.9.6",
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.5",
83
- "typescript": "^5.7.2",
84
- "vitest": "^2.1.8",
85
- "webpack": "^5.97.1",
82
+ "tsup": "^8.4.0",
83
+ "typescript": "^5.7.3",
84
+ "vitest": "^3.0.7",
85
+ "webpack": "^5.98.0",
86
86
  "xo": "^0.60.0"
87
87
  },
88
88
  "xo": {