From 03c0e0725f6bb39ef14d80c56ce481e9cd1aac8d Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Tue, 26 Nov 2024 09:01:01 +0000 Subject: [PATCH 1/4] wip: add tests to schema --- package-lock.json | 45 ++++++++++++++++++++++ packages/myst-to-html/package.json | 7 +++- packages/myst-to-html/src/schema.ts | 2 + packages/myst-to-html/tests/schema.spec.ts | 16 ++++++++ 4 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 packages/myst-to-html/tests/schema.spec.ts diff --git a/package-lock.json b/package-lock.json index 17f7bf5ca..5b188ab63 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16016,6 +16016,51 @@ "unist-util-remove": "^3.1.0", "unist-util-select": "^4.0.3", "unist-util-visit": "^4.1.0" + }, + "devDependencies": { + "hastscript": "^7.0.0" + } + }, + "packages/myst-to-html/node_modules/@types/hast": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/@types/hast/-/hast-3.0.4.tgz", + "integrity": "sha512-WPs+bbQw5aCj+x6laNGWLH3wviHtoCv/P3+otBhbOhJgG8qtpdAMlTCxLtsTWA7LH1Oh/bFCHsBn0TPS5m30EQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/unist": "*" + } + }, + "packages/myst-to-html/node_modules/hast-util-parse-selector": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/hast-util-parse-selector/-/hast-util-parse-selector-4.0.0.tgz", + "integrity": "sha512-wkQCkSYoOGCRKERFWcxMVMOcYE2K1AaNLU8DXS9arxnLOUEWbOXKXiJUNzEpqZ3JOKpnha3jkFrumEjVliDe7A==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/hast": "^3.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, + "packages/myst-to-html/node_modules/hastscript": { + "version": "9.0.0", + "resolved": "https://registry.npmjs.org/hastscript/-/hastscript-9.0.0.tgz", + "integrity": "sha512-jzaLBGavEDKHrc5EfFImKN7nZKKBdSLIdGvCwDZ9TfzbF2ffXiov8CKE445L2Z1Ek2t/m4SKQ2j6Ipv7NyUolw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/hast": "^3.0.0", + "comma-separated-tokens": "^2.0.0", + "hast-util-parse-selector": "^4.0.0", + "property-information": "^6.0.0", + "space-separated-tokens": "^2.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" } }, "packages/myst-to-html/node_modules/unist-builder": { diff --git a/packages/myst-to-html/package.json b/packages/myst-to-html/package.json index 0431684d7..6813db180 100644 --- a/packages/myst-to-html/package.json +++ b/packages/myst-to-html/package.json @@ -51,9 +51,12 @@ "rehype-stringify": "^9.0.3", "unified": "^10.1.2", "unist-builder": "^3.0.0", + "unist-util-find-after": "^4.0.0", "unist-util-remove": "^3.1.0", "unist-util-select": "^4.0.3", - "unist-util-visit": "^4.1.0", - "unist-util-find-after": "^4.0.0" + "unist-util-visit": "^4.1.0" + }, + "devDependencies": { + "hastscript": "^7.0.0" } } diff --git a/packages/myst-to-html/src/schema.ts b/packages/myst-to-html/src/schema.ts index db0164b4f..aa1823523 100644 --- a/packages/myst-to-html/src/schema.ts +++ b/packages/myst-to-html/src/schema.ts @@ -161,6 +161,7 @@ const mdast: Handler = (h, node) => h(node, 'div', { id: node.id }); const mermaid: Handler = (h, node) => h(node, 'div', { class: 'margin' }); const myst: Handler = (h, node) => h(node, 'div', { class: 'margin' }); const output: Handler = (h, node) => h(node, 'div', { class: 'output' }); +const keyboard: Handler = (h, node) => h(node, 'kbd', all(h, node)); export const mystToHast: Plugin<[Options?], string, GenericParent> = (opts) => (tree: GenericParent) => { @@ -202,6 +203,7 @@ export const mystToHast: Plugin<[Options?], string, GenericParent> = mermaid, myst, output, + keyboard, ...opts?.handlers, }, }); diff --git a/packages/myst-to-html/tests/schema.spec.ts b/packages/myst-to-html/tests/schema.spec.ts new file mode 100644 index 000000000..c9d3b4498 --- /dev/null +++ b/packages/myst-to-html/tests/schema.spec.ts @@ -0,0 +1,16 @@ +import { describe, expect, it } from 'vitest'; +import { mystToHast } from '../src/schema'; +import { u } from 'unist-builder'; +import { h } from 'hastscript'; + +// @ts-expect-error this +const toHast: (node: any) => any = mystToHast(); + +describe('mystToHast', () => { + it('Converts a keyboard node to kbd', () => { + const hast = toHast( + u('root', [u('paragraph', [u('keyboard', [u('text', { value: 'Ctrl' })])])]), + ); + expect(hast).toStrictEqual(h(null, [h('p', [h('kbd', 'Ctrl')])])); + }); +}); From d1f40b3884bbacce24a99a819be54efac2d1770a Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Tue, 26 Nov 2024 09:02:26 +0000 Subject: [PATCH 2/4] chore: run prettier --- packages/myst-to-html/src/schema.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/myst-to-html/src/schema.ts b/packages/myst-to-html/src/schema.ts index aa1823523..438dcf379 100644 --- a/packages/myst-to-html/src/schema.ts +++ b/packages/myst-to-html/src/schema.ts @@ -203,7 +203,7 @@ export const mystToHast: Plugin<[Options?], string, GenericParent> = mermaid, myst, output, - keyboard, + keyboard, ...opts?.handlers, }, }); From b92038b18506e83e09751defd8633a069bb7f7e5 Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Tue, 26 Nov 2024 11:42:21 +0000 Subject: [PATCH 3/4] chore: drop ts-expect-error --- packages/myst-to-html/tests/schema.spec.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/myst-to-html/tests/schema.spec.ts b/packages/myst-to-html/tests/schema.spec.ts index c9d3b4498..5649fc4de 100644 --- a/packages/myst-to-html/tests/schema.spec.ts +++ b/packages/myst-to-html/tests/schema.spec.ts @@ -3,7 +3,6 @@ import { mystToHast } from '../src/schema'; import { u } from 'unist-builder'; import { h } from 'hastscript'; -// @ts-expect-error this const toHast: (node: any) => any = mystToHast(); describe('mystToHast', () => { From 58fe94a8210c6df117fd2f6e7bf42fe7567ce4f4 Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Tue, 26 Nov 2024 11:43:10 +0000 Subject: [PATCH 4/4] chore: add changeset --- .changeset/cold-elephants-vanish.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/cold-elephants-vanish.md diff --git a/.changeset/cold-elephants-vanish.md b/.changeset/cold-elephants-vanish.md new file mode 100644 index 000000000..a2122d64b --- /dev/null +++ b/.changeset/cold-elephants-vanish.md @@ -0,0 +1,5 @@ +--- +"myst-to-html": patch +--- + +Support keyboard nodes in HTML export