writr 4.4.0 → 4.4.2

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
@@ -4,13 +4,28 @@
4
4
  [![tests](https://github.com/jaredwray/writr/actions/workflows/tests.yml/badge.svg)](https://github.com/jaredwray/writr/actions/workflows/tests.yml)
5
5
  [![GitHub license](https://img.shields.io/github/license/jaredwray/writr)](https://github.com/jaredwray/writr/blob/master/LICENSE)
6
6
  [![codecov](https://codecov.io/gh/jaredwray/writr/branch/master/graph/badge.svg?token=1YdMesM07X)](https://codecov.io/gh/jaredwray/writr)
7
+ [![jsDelivr](https://data.jsdelivr.com/v1/package/npm/writr/badge)](https://www.jsdelivr.com/package/npm/writr)
7
8
  [![npm](https://img.shields.io/npm/dm/writr)](https://npmjs.com/package/writr)
8
9
  [![npm](https://img.shields.io/npm/v/writr)](https://npmjs.com/package/writr)
9
10
 
11
+ # Features
12
+ * Removes the remark / unified complexity and easy to use.
13
+ * Built in caching 💥 making it render very fast when there isnt a change
14
+ * Frontmatter support built in by default. :tada:
15
+ * Easily Render to `React` or `HTML`.
16
+ * Generates a Table of Contents for your markdown files (remark-toc).
17
+ * Slug generation for your markdown files (rehype-slug).
18
+ * Code Highlighting (rehype-highlight).
19
+ * Math Support (rehype-katex).
20
+ * Markdown to HTML (rehype-stringify).
21
+ * Github Flavor Markdown (remark-gfm).
22
+ * Emoji Support (remark-emoji).
23
+ * MDX Support (remark-mdx).
24
+ * Built in Hooks for adding code to render pipeline.
25
+
10
26
  # Table of Contents
11
- - [Features](#features)
12
- - [ESM and Node Version Support](#esm-and-node-version-support)
13
27
  - [Getting Started](#getting-started)
28
+ - [Running via Browser as an ESM Module](#running-via-browser-as-an-esm-module)
14
29
  - [API](#api)
15
30
  - [`new Writr(arg?: string | WritrOptions, options?: WritrOptions)`](#new-writrarg-string--writroptions-options-writroptions)
16
31
  - [`.content`](#content)
@@ -31,29 +46,10 @@
31
46
  - [`.saveToFile(filePath: string): Promise<void>`](#savetofilefilepath-string-promisevoid)
32
47
  - [`.saveToFileSync(filePath: string): void`](#savetofilesyncfilepath-string-void)
33
48
  - [Hooks](#hooks)
49
+ - [ESM and Node Version Support](#esm-and-node-version-support)
34
50
  - [Code of Conduct and Contributing](#code-of-conduct-and-contributing)
35
51
  - [License](#license)
36
52
 
37
-
38
- # Features
39
- * Removes the remark / unified complexity and easy to use.
40
- * Built in caching 💥 making it render very fast when there isnt a change
41
- * Frontmatter support built in by default. :tada:
42
- * Easily Render to `React` or `HTML`.
43
- * Generates a Table of Contents for your markdown files (remark-toc).
44
- * Slug generation for your markdown files (rehype-slug).
45
- * Code Highlighting (rehype-highlight).
46
- * Math Support (rehype-katex).
47
- * Markdown to HTML (rehype-stringify).
48
- * Github Flavor Markdown (remark-gfm).
49
- * Emoji Support (remark-emoji).
50
- * MDX Support (remark-mdx).
51
- * Built in Hooks for adding code to render pipeline.
52
-
53
- # ESM and Node Version Support
54
-
55
- This package is ESM only and tested on the current lts version and its previous. Please don't open issues for questions regarding CommonJS / ESM or previous Nodejs versions. To learn more about using ESM please read this from `sindresorhus`: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
56
-
57
53
  # Getting Started
58
54
 
59
55
  ```bash
@@ -101,6 +97,33 @@ const writr = new Writr(`# Hello World ::-):\n\n This is a test.`, writrOptions)
101
97
  const html = await writr.render(options); // <h1>Hello World ::-):</h1><p>This is a test.</p>
102
98
  ```
103
99
 
100
+ # Running via Browser as an ESM Module
101
+
102
+ You can also run Writr via the browser. Here is an example of how to do that.
103
+
104
+ ```html
105
+ <!DOCTYPE html>
106
+ <html lang="en">
107
+ <head>
108
+ <meta charset="UTF-8">
109
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
110
+ <title>Writr Example</title>
111
+ <script type="module">
112
+ import {Writr} from 'https://cdn.jsdelivr.net/npm/writr@4.4.1/+esm'
113
+
114
+ const writr = new Writr(`# Hello World ::-):\n\n This is a test.`);
115
+ writr.render().then(html => {
116
+ document.body.innerHTML = html;
117
+ });
118
+ </script>
119
+ </head>
120
+ <body>
121
+ </body>
122
+ </html>
123
+ ```
124
+
125
+ This will render the markdown to HTML and display it in the body of the page.
126
+
104
127
  # API
105
128
 
106
129
  ## `new Writr(arg?: string | WritrOptions, options?: WritrOptions)`
@@ -380,7 +403,7 @@ export type resultData = {
380
403
  }
381
404
  ```
382
405
 
383
- For `beforeSaveToFile` the data object is an object with the `filePath` and `content`. Here is the interface for `saveToFileData`:
406
+ For `saveToFile` the data object is an object with the `filePath` and `content`. Here is the interface for `saveToFileData`:
384
407
 
385
408
  ```typescript
386
409
  export type saveToFileData = {
@@ -391,7 +414,7 @@ export type saveToFileData = {
391
414
 
392
415
  This is called when you call `saveToFile`, `saveToFileSync`.
393
416
 
394
- For `beforeRenderToFile` the data object is an object with the `filePath` and `content`. Here is the interface for `renderToFileData`:
417
+ For `renderToFile` the data object is an object with the `filePath` and `content`. Here is the interface for `renderToFileData`:
395
418
 
396
419
  ```typescript
397
420
  export type renderToFileData = {
@@ -402,6 +425,20 @@ export type renderToFileData = {
402
425
 
403
426
  This is called when you call `renderToFile`, `renderToFileSync`.
404
427
 
428
+ 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`:
429
+
430
+ ```typescript
431
+ export type loadFromFileData = {
432
+ content: string;
433
+ }
434
+ ```
435
+
436
+ This is called when you call `loadFromFile`, `loadFromFileSync`.
437
+
438
+ # ESM and Node Version Support
439
+
440
+ This package is ESM only and tested on the current lts version and its previous. Please don't open issues for questions regarding CommonJS / ESM or previous Nodejs versions. To learn more about using ESM please read this from `sindresorhus`: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
441
+
405
442
  # Code of Conduct and Contributing
406
443
  [Code of Conduct](CODE_OF_CONDUCT.md) and [Contributing](CONTRIBUTING.md) guidelines.
407
444
 
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.2",
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.8",
57
- "hookified": "^1.7.0",
56
+ "cacheable": "^1.8.9",
57
+ "hookified": "^1.8.1",
58
58
  "html-react-parser": "^5.2.2",
59
59
  "js-yaml": "^4.1.0",
60
- "react": "^19.0.0",
61
- "rehype-highlight": "^7.0.1",
60
+ "react": "^19.1.0",
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.14",
77
+ "@types/react": "^19.0.12",
78
+ "@vitest/coverage-v8": "^3.0.9",
79
+ "docula": "^0.11.0",
80
80
  "rimraf": "^6.0.1",
81
81
  "ts-node": "^10.9.2",
82
- "tsup": "^8.3.6",
83
- "typescript": "^5.7.3",
84
- "vitest": "^3.0.4",
85
- "webpack": "^5.97.1",
82
+ "tsup": "^8.4.0",
83
+ "typescript": "^5.8.2",
84
+ "vitest": "^3.0.9",
85
+ "webpack": "^5.98.0",
86
86
  "xo": "^0.60.0"
87
87
  },
88
88
  "xo": {