Skip to content

Commit

Permalink
Merge pull request #251 from jaredwray/adding-in-renderSync
Browse files Browse the repository at this point in the history
adding in renderSync
  • Loading branch information
jaredwray authored Mar 1, 2024
2 parents 8c112be + 626a0ce commit d12ce39
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
18 changes: 8 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,18 +125,16 @@ interface RenderOptions {
}
```

### `.renderTranslation(markdown: string, langCode: string, options?: RenderOptions): Promise<string>`
### `.renderSync(markdown: string, options?: RenderOptions): string`

Rendering markdown to HTML. the options are based on RenderOptions. Which you can access from the Writr instance.


### `.keywords(markdown: string): Promise<string[]>`

AI Generation of Keywords that can be used for SEO on your HTML.

### `.description(markdown: string): Promise<string>`
Rendering markdown to HTML synchronously. the options are based on RenderOptions. Which you can access from the Writr instance. The parameters are the same as the `.render()` function.

AI Generation of a Description that can be used for SEO on your HTML.
```javascript
import { Writr } from 'writr';
const writr = new Writr();
const markdown = `# Hello World ::-):\n\n This is a test.`;
const html = writr.renderSync(markdown); // <h1>Hello World 🙂</h1><p>This is a test.</p>
```

## Code of Conduct and Contributing
[Code of Conduct](CODE_OF_CONDUCT.md) and [Contributing](CONTRIBUTING.md) guidelines.
Expand Down
16 changes: 16 additions & 0 deletions src/writr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,22 @@ class Writr {
}
}

renderSync(markdown: string, options?: RenderOptions): string {
try {
let {engine} = this;
if (options) {
options = {...this._options.renderOptions, ...options};
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
engine = this.createProcessor(options);
}

const file = engine.processSync(markdown);
return String(file);
} catch (error) {
throw new Error(`Failed to render markdown: ${(error as Error).message}`);
}
}

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

Expand Down
26 changes: 26 additions & 0 deletions test/writr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ describe('writr', () => {
const result = await writr.render('# Hello World');
expect(result).toEqual('<h1 id="hello-world">Hello World</h1>');
});
it('should renderSync a simple markdown example', async () => {
const writr = new Writr();
const result = writr.renderSync('# Hello World');
expect(result).toEqual('<h1 id="hello-world">Hello World</h1>');
});
it('should render a simple markdown example with options - slug', async () => {
const writr = new Writr();
const options = {
Expand All @@ -62,6 +67,14 @@ describe('writr', () => {
const result = await writr.render('# Hello World', options);
expect(result).toEqual('<h1>Hello World</h1>');
});
it('should renderSync a simple markdown example with options - emoji', async () => {
const writr = new Writr();
const options = {
emoji: false,
};
const result = writr.renderSync('# Hello World :dog:', options);
expect(result).toEqual('<h1 id="hello-world-dog">Hello World :dog:</h1>');
});
it('should render a simple markdown example with options - emoji', async () => {
const writr = new Writr();
const options = {
Expand Down Expand Up @@ -113,6 +126,19 @@ describe('writr', () => {
expect((error as Error).message).toEqual('Failed to render markdown: Custom Plugin Error: Required configuration missing.');
}
});
it('should throw an error on bad plugin or parsing on renderSync', () => {
const writr = new Writr();
const customPlugin = () => {
throw new Error('Custom Plugin Error: Required configuration missing.');
};

writr.engine.use(customPlugin);
try {
writr.renderSync('# Hello World');
} catch (error) {
expect((error as Error).message).toEqual('Failed to render markdown: Custom Plugin Error: Required configuration missing.');
}
});
it('should be able to do math', async () => {
const writr = new Writr();
const result = await writr.render('$$\n\\frac{1}{2}\n$$');
Expand Down

0 comments on commit d12ce39

Please sign in to comment.