Comparing version 4.1.4 to 4.2.0
@@ -23,8 +23,8 @@ import * as unified from 'unified'; | ||
* @typedef {Object} WritrOptions | ||
* @property {string} [openai] - Openai api key (default: undefined) | ||
* @property {RenderOptions} [renderOptions] - Default render options (default: undefined) | ||
* @property {boolean} [throwErrors] - Throw error (default: false) | ||
*/ | ||
type WritrOptions = { | ||
openai?: string; | ||
renderOptions?: RenderOptions; | ||
throwErrors?: boolean; | ||
}; | ||
@@ -41,3 +41,3 @@ /** | ||
* @property {boolean} [mdx] - MDX support (default: true) | ||
* @property {boolean} [caching] - Caching (default: true) | ||
* @property {boolean} [caching] - Caching (default: false) | ||
*/ | ||
@@ -132,2 +132,14 @@ type RenderOptions = { | ||
/** | ||
* Render the markdown content and save it to a file. If the directory doesn't exist it will be created. | ||
* @param {string} filePath The file path to save the rendered markdown content to. | ||
* @param {RenderOptions} [options] the render options. | ||
*/ | ||
renderToFile(filePath: string, options?: RenderOptions): Promise<void>; | ||
/** | ||
* Render the markdown content and save it to a file synchronously. If the directory doesn't exist it will be created. | ||
* @param {string} filePath The file path to save the rendered markdown content to. | ||
* @param {RenderOptions} [options] the render options. | ||
*/ | ||
renderToFileSync(filePath: string, options?: RenderOptions): void; | ||
/** | ||
* Render the markdown content to React. | ||
@@ -134,0 +146,0 @@ * @param {RenderOptions} [options] The render options. |
@@ -61,3 +61,3 @@ // src/writr.ts | ||
_options = { | ||
openai: void 0, | ||
throwErrors: false, | ||
renderOptions: { | ||
@@ -257,2 +257,39 @@ emoji: true, | ||
/** | ||
* Render the markdown content and save it to a file. If the directory doesn't exist it will be created. | ||
* @param {string} filePath The file path to save the rendered markdown content to. | ||
* @param {RenderOptions} [options] the render options. | ||
*/ | ||
async renderToFile(filePath, options) { | ||
try { | ||
const { writeFile, mkdir } = fs.promises; | ||
const directoryPath = dirname(filePath); | ||
const content = await this.render(options); | ||
await mkdir(directoryPath, { recursive: true }); | ||
await writeFile(filePath, content, "utf8"); | ||
} catch (error) { | ||
this.emit("error", error); | ||
if (this._options.throwErrors) { | ||
throw error; | ||
} | ||
} | ||
} | ||
/** | ||
* Render the markdown content and save it to a file synchronously. If the directory doesn't exist it will be created. | ||
* @param {string} filePath The file path to save the rendered markdown content to. | ||
* @param {RenderOptions} [options] the render options. | ||
*/ | ||
renderToFileSync(filePath, options) { | ||
try { | ||
const directoryPath = dirname(filePath); | ||
const content = this.renderSync(options); | ||
fs.mkdirSync(directoryPath, { recursive: true }); | ||
fs.writeFileSync(filePath, content, "utf8"); | ||
} catch (error) { | ||
this.emit("error", error); | ||
if (this._options.throwErrors) { | ||
throw error; | ||
} | ||
} | ||
} | ||
/** | ||
* Render the markdown content to React. | ||
@@ -300,6 +337,13 @@ * @param {RenderOptions} [options] The render options. | ||
async saveToFile(filePath) { | ||
const { writeFile, mkdir } = fs.promises; | ||
const directoryPath = dirname(filePath); | ||
await mkdir(directoryPath, { recursive: true }); | ||
await writeFile(filePath, this._content, "utf8"); | ||
try { | ||
const { writeFile, mkdir } = fs.promises; | ||
const directoryPath = dirname(filePath); | ||
await mkdir(directoryPath, { recursive: true }); | ||
await writeFile(filePath, this._content, "utf8"); | ||
} catch (error) { | ||
this.emit("error", error); | ||
if (this._options.throwErrors) { | ||
throw error; | ||
} | ||
} | ||
} | ||
@@ -312,5 +356,12 @@ /** | ||
saveToFileSync(filePath) { | ||
const directoryPath = dirname(filePath); | ||
fs.mkdirSync(directoryPath, { recursive: true }); | ||
fs.writeFileSync(filePath, this._content, "utf8"); | ||
try { | ||
const directoryPath = dirname(filePath); | ||
fs.mkdirSync(directoryPath, { recursive: true }); | ||
fs.writeFileSync(filePath, this._content, "utf8"); | ||
} catch (error) { | ||
this.emit("error", error); | ||
if (this._options.throwErrors) { | ||
throw error; | ||
} | ||
} | ||
} | ||
@@ -351,4 +402,4 @@ isCacheEnabled(options) { | ||
mergeOptions(current, options) { | ||
if (options.openai) { | ||
current.openai = options.openai; | ||
if (options.throwErrors !== void 0) { | ||
current.throwErrors = options.throwErrors; | ||
} | ||
@@ -355,0 +406,0 @@ if (options.renderOptions) { |
{ | ||
"name": "writr", | ||
"version": "4.1.4", | ||
"version": "4.2.0", | ||
"description": "Markdown Rendering Simplified", | ||
@@ -56,4 +56,4 @@ "type": "module", | ||
"dependencies": { | ||
"cacheable": "^1.8.2", | ||
"hookified": "^1.5.0", | ||
"cacheable": "^1.8.5", | ||
"hookified": "^1.5.1", | ||
"html-react-parser": "^5.1.18", | ||
@@ -77,12 +77,12 @@ "js-yaml": "^4.1.0", | ||
"@types/js-yaml": "^4.0.9", | ||
"@types/node": "^22.8.4", | ||
"@types/node": "^22.10.1", | ||
"@types/react": "^18.3.12", | ||
"@vitest/coverage-v8": "^2.1.4", | ||
"docula": "^0.9.4", | ||
"@vitest/coverage-v8": "^2.1.6", | ||
"docula": "^0.9.5", | ||
"rimraf": "^6.0.1", | ||
"ts-node": "^10.9.2", | ||
"tsup": "^8.3.5", | ||
"typescript": "^5.6.3", | ||
"vitest": "^2.1.4", | ||
"webpack": "^5.95.0", | ||
"typescript": "^5.7.2", | ||
"vitest": "^2.1.6", | ||
"webpack": "^5.96.1", | ||
"xo": "^0.59.3" | ||
@@ -89,0 +89,0 @@ }, |
@@ -25,2 +25,4 @@  | ||
- [`.renderSync(options?: RenderOptions): string`](#rendersyncoptions-renderoptions-string) | ||
- [`.renderToFile(filePath: string, options?: RenderOptions): Promise<void>`](#rendertofilefilepath-string-options-renderoptions-promisevoid) | ||
- [`.renderToFileSync(filePath: string, options?: RenderOptions): void`](#rendertofilesyncfilepath-string-options-renderoptions-void) | ||
- [`.renderReact(options?: RenderOptions, reactOptions?: HTMLReactParserOptions): Promise<React.JSX.Element />`](#renderreactoptions-renderoptions-reactoptions-htmlreactparseroptions-promise-reactjsxelement-) | ||
@@ -85,2 +87,3 @@ - [`.renderReactSync( options?: RenderOptions, reactOptions?: HTMLReactParserOptions): React.JSX.Element`](#renderreactsync-options-renderoptions-reactoptions-htmlreactparseroptions-reactjsxelement) | ||
const writrOptions = { | ||
throwErrors: true, | ||
renderOptions: { | ||
@@ -110,2 +113,3 @@ emoji: true, | ||
const writrOptions = { | ||
throwErrors: true, | ||
renderOptions: { | ||
@@ -155,4 +159,20 @@ emoji: true, | ||
Accessing the default options for this instance of Writr. | ||
Accessing the default options for this instance of Writr. Here is the default settings for `WritrOptions`. | ||
```javascript | ||
{ | ||
throwErrors: false, | ||
renderOptions: { | ||
emoji: true, | ||
toc: false, | ||
slug: false, | ||
highlight: false, | ||
gfm: true, | ||
math: false, | ||
mdx: false, | ||
caching: false, | ||
} | ||
} | ||
``` | ||
## `.frontmatter` | ||
@@ -239,2 +259,22 @@ | ||
## '.renderToFile(filePath: string, options?: RenderOptions): Promise<void>' | ||
Rendering markdown to a file. The options are based on RenderOptions. | ||
```javascript | ||
import { Writr } from 'writr'; | ||
const writr = new Writr(`# Hello World ::-):\n\n This is a test.`); | ||
await writr.renderToFile('path/to/file.html'); | ||
``` | ||
## '.renderToFileSync(filePath: string, options?: RenderOptions): void' | ||
Rendering markdown to a file synchronously. The options are based on RenderOptions. | ||
```javascript | ||
import { Writr } from 'writr'; | ||
const writr = new Writr(`# Hello World ::-):\n\n This is a test.`); | ||
writr.renderToFileSync('path/to/file.html'); | ||
``` | ||
## '.renderReact(options?: RenderOptions, reactOptions?: HTMLReactParserOptions): Promise<React.JSX.Element />' | ||
@@ -241,0 +281,0 @@ |
36506
617
356
Updatedcacheable@^1.8.5
Updatedhookified@^1.5.1