From 64614b845bcf260fbab194f3933a1a087b2e02f4 Mon Sep 17 00:00:00 2001 From: Rowan Cockett Date: Fri, 17 Feb 2023 15:12:48 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=86=95=20Add=20mystToHtml=20as=20a=20simp?= =?UTF-8?q?le=20wrapper=20function=20(#234)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 📦 Add rehype-stringify --- .changeset/nasty-deers-rescue.md | 5 +++ .changeset/neat-ducks-do.md | 5 +++ package-lock.json | 29 ++++++----------- packages/myst-parser/tests/commonmark.spec.ts | 4 +-- packages/myst-parser/tests/myst.spec.ts | 6 ++-- packages/myst-parser/tests/renderMdast.ts | 29 ----------------- packages/myst-to-html/README.md | 8 +++++ packages/myst-to-html/package.json | 1 + packages/myst-to-html/src/index.ts | 1 + packages/myst-to-html/src/renderMdast.ts | 32 +++++++++++++++++++ packages/myst-to-html/tests/html.spec.ts | 9 ++++++ 11 files changed, 76 insertions(+), 53 deletions(-) create mode 100644 .changeset/nasty-deers-rescue.md create mode 100644 .changeset/neat-ducks-do.md delete mode 100644 packages/myst-parser/tests/renderMdast.ts create mode 100644 packages/myst-to-html/src/renderMdast.ts create mode 100644 packages/myst-to-html/tests/html.spec.ts diff --git a/.changeset/nasty-deers-rescue.md b/.changeset/nasty-deers-rescue.md new file mode 100644 index 000000000..9efa872f3 --- /dev/null +++ b/.changeset/nasty-deers-rescue.md @@ -0,0 +1,5 @@ +--- +'myst-to-html': patch +--- + +Add rehype-stringify as a dependency diff --git a/.changeset/neat-ducks-do.md b/.changeset/neat-ducks-do.md new file mode 100644 index 000000000..5b0714fa7 --- /dev/null +++ b/.changeset/neat-ducks-do.md @@ -0,0 +1,5 @@ +--- +'myst-to-html': patch +--- + +Add mystToHtml utility diff --git a/package-lock.json b/package-lock.json index 59e321e7c..ac67e6045 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3850,7 +3850,6 @@ }, "node_modules/ccount": { "version": "2.0.1", - "dev": true, "license": "MIT", "funding": { "type": "github", @@ -3888,7 +3887,6 @@ }, "node_modules/character-entities-html4": { "version": "2.1.0", - "dev": true, "license": "MIT", "funding": { "type": "github", @@ -6268,7 +6266,6 @@ }, "node_modules/hast-util-to-html": { "version": "8.0.3", - "dev": true, "license": "MIT", "dependencies": { "@types/hast": "^2.0.0", @@ -6366,7 +6363,6 @@ }, "node_modules/html-void-elements": { "version": "2.0.1", - "dev": true, "license": "MIT", "funding": { "type": "github", @@ -10221,8 +10217,8 @@ }, "node_modules/rehype-stringify": { "version": "9.0.3", - "dev": true, - "license": "MIT", + "resolved": "https://registry.npmjs.org/rehype-stringify/-/rehype-stringify-9.0.3.tgz", + "integrity": "sha512-kWiZ1bgyWlgOxpqD5HnxShKAdXtb2IUljn3hQAhySeak6IOQPPt6DeGnsIh4ixm7yKJWzm8TXFuC/lPfcWHJqw==", "dependencies": { "@types/hast": "^2.0.0", "hast-util-to-html": "^8.0.0", @@ -11013,7 +11009,6 @@ }, "node_modules/stringify-entities": { "version": "4.0.3", - "dev": true, "license": "MIT", "dependencies": { "character-entities-html4": "^2.0.0", @@ -11026,7 +11021,6 @@ }, "node_modules/stringify-entities/node_modules/character-entities-legacy": { "version": "3.0.0", - "dev": true, "license": "MIT", "funding": { "type": "github", @@ -13173,6 +13167,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" @@ -16187,8 +16182,7 @@ "dev": true }, "ccount": { - "version": "2.0.1", - "dev": true + "version": "2.0.1" }, "chainsaw": { "version": "0.1.0", @@ -16208,8 +16202,7 @@ "dev": true }, "character-entities-html4": { - "version": "2.1.0", - "dev": true + "version": "2.1.0" }, "chardet": { "version": "0.7.0" @@ -17812,7 +17805,6 @@ }, "hast-util-to-html": { "version": "8.0.3", - "dev": true, "requires": { "@types/hast": "^2.0.0", "ccount": "^2.0.0", @@ -17883,8 +17875,7 @@ "dev": true }, "html-void-elements": { - "version": "2.0.1", - "dev": true + "version": "2.0.1" }, "html-whitespace-sensitive-tag-names": { "version": "2.0.0" @@ -19878,6 +19869,7 @@ "rehype-format": "^4.0.1", "rehype-parse": "^8.0.4", "rehype-remark": "^9.1.2", + "rehype-stringify": "*", "rimraf": "^3.0.2", "ts-jest": "^28.0.7", "ts-loader": "^9.2.8", @@ -20911,7 +20903,8 @@ }, "rehype-stringify": { "version": "9.0.3", - "dev": true, + "resolved": "https://registry.npmjs.org/rehype-stringify/-/rehype-stringify-9.0.3.tgz", + "integrity": "sha512-kWiZ1bgyWlgOxpqD5HnxShKAdXtb2IUljn3hQAhySeak6IOQPPt6DeGnsIh4ixm7yKJWzm8TXFuC/lPfcWHJqw==", "requires": { "@types/hast": "^2.0.0", "hast-util-to-html": "^8.0.0", @@ -21498,15 +21491,13 @@ }, "stringify-entities": { "version": "4.0.3", - "dev": true, "requires": { "character-entities-html4": "^2.0.0", "character-entities-legacy": "^3.0.0" }, "dependencies": { "character-entities-legacy": { - "version": "3.0.0", - "dev": true + "version": "3.0.0" } } }, diff --git a/packages/myst-parser/tests/commonmark.spec.ts b/packages/myst-parser/tests/commonmark.spec.ts index cb2d354ed..6bacf79f7 100644 --- a/packages/myst-parser/tests/commonmark.spec.ts +++ b/packages/myst-parser/tests/commonmark.spec.ts @@ -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; @@ -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-', diff --git a/packages/myst-parser/tests/myst.spec.ts b/packages/myst-parser/tests/myst.spec.ts index 311a126c6..63333b55e 100644 --- a/packages/myst-parser/tests/myst.spec.ts +++ b/packages/myst-parser/tests/myst.spec.ts @@ -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[]; @@ -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-', @@ -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-', diff --git a/packages/myst-parser/tests/renderMdast.ts b/packages/myst-parser/tests/renderMdast.ts deleted file mode 100644 index 842c82156..000000000 --- a/packages/myst-parser/tests/renderMdast.ts +++ /dev/null @@ -1,29 +0,0 @@ -import type { Root } from 'mdast'; -import { State, transform, mystToHast, formatHtml } from 'myst-to-html'; -import rehypeStringify from 'rehype-stringify'; -import { unified } from 'unified'; - -export function renderMdast( - 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(); -} diff --git a/packages/myst-to-html/README.md b/packages/myst-to-html/README.md index 9fc02bd96..5a163fe8e 100644 --- a/packages/myst-to-html/README.md +++ b/packages/myst-to-html/README.md @@ -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')])])); +// '

hello world

' +``` diff --git a/packages/myst-to-html/package.json b/packages/myst-to-html/package.json index fea4abc07..d27abf71e 100644 --- a/packages/myst-to-html/package.json +++ b/packages/myst-to-html/package.json @@ -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" diff --git a/packages/myst-to-html/src/index.ts b/packages/myst-to-html/src/index.ts index a588d0b26..6dc0a6372 100644 --- a/packages/myst-to-html/src/index.ts +++ b/packages/myst-to-html/src/index.ts @@ -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'; diff --git a/packages/myst-to-html/src/renderMdast.ts b/packages/myst-to-html/src/renderMdast.ts new file mode 100644 index 000000000..1b08e4bec --- /dev/null +++ b/packages/myst-to-html/src/renderMdast.ts @@ -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(); +} diff --git a/packages/myst-to-html/tests/html.spec.ts b/packages/myst-to-html/tests/html.spec.ts new file mode 100644 index 000000000..31f880c80 --- /dev/null +++ b/packages/myst-to-html/tests/html.spec.ts @@ -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('

hello world

'); + }); +});