writr 4.1.4 → 4.2.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/README.md CHANGED
@@ -22,6 +22,8 @@
22
22
  - [`.engine`](#engine)
23
23
  - [`.render(options?: RenderOptions): Promise<string>`](#renderoptions-renderoptions-promisestring)
24
24
  - [`.renderSync(options?: RenderOptions): string`](#rendersyncoptions-renderoptions-string)
25
+ - [`.renderToFile(filePath: string, options?: RenderOptions): Promise<void>`](#rendertofilefilepath-string-options-renderoptions-promisevoid)
26
+ - [`.renderToFileSync(filePath: string, options?: RenderOptions): void`](#rendertofilesyncfilepath-string-options-renderoptions-void)
25
27
  - [`.renderReact(options?: RenderOptions, reactOptions?: HTMLReactParserOptions): Promise<React.JSX.Element />`](#renderreactoptions-renderoptions-reactoptions-htmlreactparseroptions-promise-reactjsxelement-)
26
28
  - [`.renderReactSync( options?: RenderOptions, reactOptions?: HTMLReactParserOptions): React.JSX.Element`](#renderreactsync-options-renderoptions-reactoptions-htmlreactparseroptions-reactjsxelement)
27
29
  - [`.loadFromFile(filePath: string): Promise<void>`](#loadfromfilefilepath-string-promisevoid)
@@ -81,6 +83,7 @@ An example passing in the options also via the constructor:
81
83
  ```javascript
82
84
  import { Writr, WritrOptions } from 'writr';
83
85
  const writrOptions = {
86
+ throwErrors: true,
84
87
  renderOptions: {
85
88
  emoji: true,
86
89
  toc: true,
@@ -105,6 +108,7 @@ By default the constructor takes in a markdown `string` or `WritrOptions` in the
105
108
  ```javascript
106
109
  import { Writr, WritrOptions } from 'writr';
107
110
  const writrOptions = {
111
+ throwErrors: true,
108
112
  renderOptions: {
109
113
  emoji: true,
110
114
  toc: true,
@@ -149,7 +153,23 @@ console.log(writr.body); // '# Hello World ::-):\n\n This is a test.'
149
153
 
150
154
  ## `.options`
151
155
 
152
- Accessing the default options for this instance of Writr.
156
+ Accessing the default options for this instance of Writr. Here is the default settings for `WritrOptions`.
157
+
158
+ ```javascript
159
+ {
160
+ throwErrors: false,
161
+ renderOptions: {
162
+ emoji: true,
163
+ toc: false,
164
+ slug: false,
165
+ highlight: false,
166
+ gfm: true,
167
+ math: false,
168
+ mdx: false,
169
+ caching: false,
170
+ }
171
+ }
172
+ ```
153
173
 
154
174
  ## `.frontmatter`
155
175
 
@@ -232,6 +252,26 @@ const writr = new Writr(`# Hello World ::-):\n\n This is a test.`);
232
252
  const html = writr.renderSync(); // <h1>Hello World 🙂</h1><p>This is a test.</p>
233
253
  ```
234
254
 
255
+ ## '.renderToFile(filePath: string, options?: RenderOptions): Promise<void>'
256
+
257
+ Rendering markdown to a file. The options are based on RenderOptions.
258
+
259
+ ```javascript
260
+ import { Writr } from 'writr';
261
+ const writr = new Writr(`# Hello World ::-):\n\n This is a test.`);
262
+ await writr.renderToFile('path/to/file.html');
263
+ ```
264
+
265
+ ## '.renderToFileSync(filePath: string, options?: RenderOptions): void'
266
+
267
+ Rendering markdown to a file synchronously. The options are based on RenderOptions.
268
+
269
+ ```javascript
270
+ import { Writr } from 'writr';
271
+ const writr = new Writr(`# Hello World ::-):\n\n This is a test.`);
272
+ writr.renderToFileSync('path/to/file.html');
273
+ ```
274
+
235
275
  ## '.renderReact(options?: RenderOptions, reactOptions?: HTMLReactParserOptions): Promise<React.JSX.Element />'
236
276
 
237
277
  Rendering markdown to React. The options are based on RenderOptions and now HTMLReactParserOptions from `html-react-parser`.
package/dist/writr.d.ts CHANGED
@@ -20,12 +20,12 @@ declare class WritrCache {
20
20
  /**
21
21
  * Writr options.
22
22
  * @typedef {Object} WritrOptions
23
- * @property {string} [openai] - Openai api key (default: undefined)
24
23
  * @property {RenderOptions} [renderOptions] - Default render options (default: undefined)
24
+ * @property {boolean} [throwErrors] - Throw error (default: false)
25
25
  */
26
26
  type WritrOptions = {
27
- openai?: string;
28
27
  renderOptions?: RenderOptions;
28
+ throwErrors?: boolean;
29
29
  };
30
30
  /**
31
31
  * Render options.
@@ -37,7 +37,7 @@ type WritrOptions = {
37
37
  * @property {boolean} [gfm] - Github flavor markdown (default: true)
38
38
  * @property {boolean} [math] - Math support (default: true)
39
39
  * @property {boolean} [mdx] - MDX support (default: true)
40
- * @property {boolean} [caching] - Caching (default: true)
40
+ * @property {boolean} [caching] - Caching (default: false)
41
41
  */
42
42
  type RenderOptions = {
43
43
  emoji?: boolean;
@@ -126,6 +126,18 @@ declare class Writr extends Hookified {
126
126
  * @returns {string} The rendered HTML content.
127
127
  */
128
128
  renderSync(options?: RenderOptions): string;
129
+ /**
130
+ * Render the markdown content and save it to a file. If the directory doesn't exist it will be created.
131
+ * @param {string} filePath The file path to save the rendered markdown content to.
132
+ * @param {RenderOptions} [options] the render options.
133
+ */
134
+ renderToFile(filePath: string, options?: RenderOptions): Promise<void>;
135
+ /**
136
+ * Render the markdown content and save it to a file synchronously. If the directory doesn't exist it will be created.
137
+ * @param {string} filePath The file path to save the rendered markdown content to.
138
+ * @param {RenderOptions} [options] the render options.
139
+ */
140
+ renderToFileSync(filePath: string, options?: RenderOptions): void;
129
141
  /**
130
142
  * Render the markdown content to React.
131
143
  * @param {RenderOptions} [options] The render options.
package/dist/writr.js CHANGED
@@ -58,7 +58,7 @@ var Writr = class extends Hookified {
58
58
  engine = unified().use(remarkParse).use(remarkGfm).use(remarkToc).use(remarkEmoji).use(remarkRehype).use(rehypeSlug).use(remarkMath).use(rehypeKatex).use(rehypeHighlight).use(remarkMDX).use(rehypeStringify);
59
59
  // Stringify HTML
60
60
  _options = {
61
- openai: void 0,
61
+ throwErrors: false,
62
62
  renderOptions: {
63
63
  emoji: true,
64
64
  toc: true,
@@ -252,6 +252,43 @@ ${yamlString}---
252
252
  throw new Error(`Failed to render markdown: ${error.message}`);
253
253
  }
254
254
  }
255
+ /**
256
+ * Render the markdown content and save it to a file. If the directory doesn't exist it will be created.
257
+ * @param {string} filePath The file path to save the rendered markdown content to.
258
+ * @param {RenderOptions} [options] the render options.
259
+ */
260
+ async renderToFile(filePath, options) {
261
+ try {
262
+ const { writeFile, mkdir } = fs.promises;
263
+ const directoryPath = dirname(filePath);
264
+ const content = await this.render(options);
265
+ await mkdir(directoryPath, { recursive: true });
266
+ await writeFile(filePath, content, "utf8");
267
+ } catch (error) {
268
+ this.emit("error", error);
269
+ if (this._options.throwErrors) {
270
+ throw error;
271
+ }
272
+ }
273
+ }
274
+ /**
275
+ * Render the markdown content and save it to a file synchronously. If the directory doesn't exist it will be created.
276
+ * @param {string} filePath The file path to save the rendered markdown content to.
277
+ * @param {RenderOptions} [options] the render options.
278
+ */
279
+ renderToFileSync(filePath, options) {
280
+ try {
281
+ const directoryPath = dirname(filePath);
282
+ const content = this.renderSync(options);
283
+ fs.mkdirSync(directoryPath, { recursive: true });
284
+ fs.writeFileSync(filePath, content, "utf8");
285
+ } catch (error) {
286
+ this.emit("error", error);
287
+ if (this._options.throwErrors) {
288
+ throw error;
289
+ }
290
+ }
291
+ }
255
292
  /**
256
293
  * Render the markdown content to React.
257
294
  * @param {RenderOptions} [options] The render options.
@@ -295,10 +332,17 @@ ${yamlString}---
295
332
  * @returns {Promise<void>}
296
333
  */
297
334
  async saveToFile(filePath) {
298
- const { writeFile, mkdir } = fs.promises;
299
- const directoryPath = dirname(filePath);
300
- await mkdir(directoryPath, { recursive: true });
301
- await writeFile(filePath, this._content, "utf8");
335
+ try {
336
+ const { writeFile, mkdir } = fs.promises;
337
+ const directoryPath = dirname(filePath);
338
+ await mkdir(directoryPath, { recursive: true });
339
+ await writeFile(filePath, this._content, "utf8");
340
+ } catch (error) {
341
+ this.emit("error", error);
342
+ if (this._options.throwErrors) {
343
+ throw error;
344
+ }
345
+ }
302
346
  }
303
347
  /**
304
348
  * Save the markdown content to a file synchronously. If the directory doesn't exist it will be created.
@@ -306,9 +350,16 @@ ${yamlString}---
306
350
  * @returns {void}
307
351
  */
308
352
  saveToFileSync(filePath) {
309
- const directoryPath = dirname(filePath);
310
- fs.mkdirSync(directoryPath, { recursive: true });
311
- fs.writeFileSync(filePath, this._content, "utf8");
353
+ try {
354
+ const directoryPath = dirname(filePath);
355
+ fs.mkdirSync(directoryPath, { recursive: true });
356
+ fs.writeFileSync(filePath, this._content, "utf8");
357
+ } catch (error) {
358
+ this.emit("error", error);
359
+ if (this._options.throwErrors) {
360
+ throw error;
361
+ }
362
+ }
312
363
  }
313
364
  isCacheEnabled(options) {
314
365
  if (options?.caching !== void 0) {
@@ -344,8 +395,8 @@ ${yamlString}---
344
395
  return processor;
345
396
  }
346
397
  mergeOptions(current, options) {
347
- if (options.openai) {
348
- current.openai = options.openai;
398
+ if (options.throwErrors !== void 0) {
399
+ current.throwErrors = options.throwErrors;
349
400
  }
350
401
  if (options.renderOptions) {
351
402
  current.renderOptions ??= {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "writr",
3
- "version": "4.1.4",
3
+ "version": "4.2.0",
4
4
  "description": "Markdown Rendering Simplified",
5
5
  "type": "module",
6
6
  "main": "./dist/writr.js",
@@ -53,8 +53,8 @@
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.2",
57
- "hookified": "^1.5.0",
56
+ "cacheable": "^1.8.5",
57
+ "hookified": "^1.5.1",
58
58
  "html-react-parser": "^5.1.18",
59
59
  "js-yaml": "^4.1.0",
60
60
  "react": "^18.3.1",
@@ -73,16 +73,16 @@
73
73
  },
74
74
  "devDependencies": {
75
75
  "@types/js-yaml": "^4.0.9",
76
- "@types/node": "^22.8.4",
76
+ "@types/node": "^22.10.1",
77
77
  "@types/react": "^18.3.12",
78
- "@vitest/coverage-v8": "^2.1.4",
79
- "docula": "^0.9.4",
78
+ "@vitest/coverage-v8": "^2.1.6",
79
+ "docula": "^0.9.5",
80
80
  "rimraf": "^6.0.1",
81
81
  "ts-node": "^10.9.2",
82
82
  "tsup": "^8.3.5",
83
- "typescript": "^5.6.3",
84
- "vitest": "^2.1.4",
85
- "webpack": "^5.95.0",
83
+ "typescript": "^5.7.2",
84
+ "vitest": "^2.1.6",
85
+ "webpack": "^5.96.1",
86
86
  "xo": "^0.59.3"
87
87
  },
88
88
  "xo": {