Skip to content

Commit

Permalink
🆕 Add mystToHtml as a simple wrapper function (#234)
Browse files Browse the repository at this point in the history
* 📦 Add rehype-stringify
  • Loading branch information
rowanc1 authored Feb 17, 2023
1 parent a9913fc commit 64614b8
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 53 deletions.
5 changes: 5 additions & 0 deletions .changeset/nasty-deers-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'myst-to-html': patch
---

Add rehype-stringify as a dependency
5 changes: 5 additions & 0 deletions .changeset/neat-ducks-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'myst-to-html': patch
---

Add mystToHtml utility
29 changes: 10 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/myst-parser/tests/commonmark.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import fs from 'fs';
import path from 'path';
import { createTokenizer, mystParse } from '../src';
import { renderMdast } from './renderMdast';
import { mystToHtml } from 'myst-to-html';

type Spec = {
section: string;
Expand Down Expand Up @@ -81,7 +81,7 @@ describe('Common Mark Spec with unified', () => {
frontmatter: false, // Frontmatter screws with some tests!
},
});
const output = renderMdast(tree, {
const output = mystToHtml(tree, {
formatHtml: false,
hast: {
clobberPrefix: 'm-',
Expand Down
6 changes: 3 additions & 3 deletions packages/myst-parser/tests/myst.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import yaml from 'js-yaml';
import { visit } from 'unist-util-visit';
import type { Root } from 'mdast';
import { mystParse } from '../src';
import { renderMdast } from './renderMdast';
import { mystToHtml } from 'myst-to-html';

type TestFile = {
cases: TestCase[];
Expand Down Expand Up @@ -112,7 +112,7 @@ describe('Testing mdast --> html conversions', () => {
test.each(htmlCases)('%s', (name, { html, mdast }) => {
if (html) {
if (name.includes('cmark_spec_0.30')) {
const output = renderMdast(mdast, {
const output = mystToHtml(mdast, {
formatHtml: false,
hast: {
clobberPrefix: 'm-',
Expand All @@ -138,7 +138,7 @@ describe('Testing mdast --> html conversions', () => {

expect(normalize(o)).toEqual(normalize(i));
} else {
const newHTML = renderMdast(mdast, {
const newHTML = mystToHtml(mdast, {
formatHtml: true,
hast: {
clobberPrefix: 'm-',
Expand Down
29 changes: 0 additions & 29 deletions packages/myst-parser/tests/renderMdast.ts

This file was deleted.

8 changes: 8 additions & 0 deletions packages/myst-to-html/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# myst-to-html

Convert a MyST AST to HTML.

```typescript
import { u } from 'unist-builder';
import { mystToHtml } from 'myst-to-html';

const html = mystToHtml(u('root', [u('paragraph', [u('text', 'hello world')])]));
// '<p>hello world</p>'
```
1 change: 1 addition & 0 deletions packages/myst-to-html/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"rehype-format": "^4.0.1",
"rehype-parse": "^8.0.4",
"rehype-remark": "^9.1.2",
"rehype-stringify": "^9.0.3",
"unified": "^10.1.2",
"unist-builder": "^3.0.1",
"unist-util-find-after": "^4.0.0"
Expand Down
1 change: 1 addition & 0 deletions packages/myst-to-html/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export { addMathRenderers, renderMath } from './renderer';
export { mystToHast } from './schema';
export { State } from './state';
export { transform } from './transforms';
export { mystToHtml } from './renderMdast';
32 changes: 32 additions & 0 deletions packages/myst-to-html/src/renderMdast.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { Root } from 'mdast';
import rehypeStringify from 'rehype-stringify';
import { unified } from 'unified';
import { formatHtml } from './format';
import { mystToHast } from './schema';
import { State } from './state';
import { transform } from './transforms';

export function mystToHtml(
tree: Root,
opts?: {
formatHtml?: boolean;
hast?: {
clobberPrefix?: 'm-';
allowDangerousHtml?: boolean;
};
stringifyHtml?: {
closeSelfClosing?: boolean;
allowDangerousHtml?: boolean;
};
},
) {
const state = new State();
const pipe = unified()
.use(transform, state)
.use(mystToHast, opts?.hast)
.use(formatHtml, opts?.formatHtml)
.use(rehypeStringify, opts?.stringifyHtml);
const result = pipe.runSync(tree);
const html = pipe.stringify(result);
return html.trim();
}
9 changes: 9 additions & 0 deletions packages/myst-to-html/tests/html.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { u } from 'unist-builder';
import { mystToHtml } from '../src';

describe('mystToHtml', () => {
it('Converts a tag schema to a string', () => {
const html = mystToHtml(u('root', [u('paragraph', [u('text', 'hello world')])]));
expect(html).toBe('<p>hello world</p>');
});
});

0 comments on commit 64614b8

Please sign in to comment.