Skip to content

Commit

Permalink
Merge pull request #250 from jaredwray/fixing-index.js-reference-and-…
Browse files Browse the repository at this point in the history
…updating-options

fixing index.js reference and updating options
  • Loading branch information
jaredwray authored Mar 1, 2024
2 parents 5433e63 + 8efd9c5 commit 8c112be
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 42 deletions.
25 changes: 13 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,22 @@ const description = await writr.description(markdown); // 'Hello World Test'

### `new Writr(options?: WritrOptions)`

```js
interface WritrOptions {
openai?: string; // openai api key (default: undefined)
emoji?: boolean; // emoji support (default: true)
toc?: boolean; // table of contents generation (default: true)
slug?: boolean; // slug generation (default: true)
highlight?: boolean; // code highlighting (default: true)
gfm?: boolean; // github flavor markdown (default: true)
}
```

You can access the `WritrOptions` from the instance of Writr.
You can access the `WritrOptions` from the instance of Writr. Here is an example of WritrOptions.

```javascript
import { Writr, WritrOptions } from 'writr';
const writrOptions = {
openai: 'your-api-key', // openai api key (default: undefined)
renderOptions: {
emoji: true,
toc: true,
slug: true,
highlight: true,
gfm: true,
math: true
}
};
const writr = new Writr(writrOptions);
```

### `.engine`
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"version": "3.0.1",
"description": "Markdown Rendering Simplified",
"type": "module",
"exports": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": "./dist/writr.js",
"types": "./dist/writr.d.ts",
"repository": "https://github.com/jaredwray/writr.git",
"author": "Jared Wray <me@jaredwray.com>",
"engines": {
Expand Down
30 changes: 16 additions & 14 deletions src/writr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ import remarkEmoji from 'remark-emoji';

type WritrOptions = {
openai?: string; // Openai api key (default: undefined)
emoji?: boolean; // Emoji support (default: true)
toc?: boolean; // Table of contents generation (default: true)
slug?: boolean; // Slug generation (default: true)
highlight?: boolean; // Code highlighting (default: true)
gfm?: boolean; // Github flavor markdown (default: true)
renderOptions?: RenderOptions; // Default render options (default: undefined)
};

type RenderOptions = {
Expand All @@ -25,6 +21,7 @@ type RenderOptions = {
slug?: boolean; // Slug generation (default: true)
highlight?: boolean; // Code highlighting (default: true)
gfm?: boolean; // Github flavor markdown (default: true)
math?: boolean; // Math support (default: true)
};

class Writr {
Expand All @@ -42,18 +39,23 @@ class Writr {

private readonly _options: WritrOptions = {
openai: undefined,
emoji: true,
toc: true,
slug: true,
highlight: true,
gfm: true,
renderOptions: {
emoji: true,
toc: true,
slug: true,
highlight: true,
gfm: true,
math: true,
},
};

constructor(options?: WritrOptions) {
if (options) {
this._options = {...this._options, ...options};
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
this.engine = this.createProcessor(this._options);
if (this._options.renderOptions) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
this.engine = this.createProcessor(this._options.renderOptions);
}
}
}

Expand All @@ -65,7 +67,7 @@ class Writr {
try {
let {engine} = this;
if (options) {
options = {...this._options, ...options};
options = {...this._options.renderOptions, ...options};
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
engine = this.createProcessor(options);
}
Expand All @@ -77,7 +79,7 @@ class Writr {
}
}

private createProcessor(options: RenderOptions | WritrOptions): any {
private createProcessor(options: RenderOptions): any {
const processor = unified().use(remarkParse);

if (options.gfm) {
Expand Down
38 changes: 24 additions & 14 deletions test/writr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,44 @@ describe('writr', () => {

it('should be able to set options', () => {
const options = {
emoji: false,
toc: false,
slug: false,
highlight: false,
gfm: false,
renderOptions: {
toc: false,
slug: false,