Skip to content

Commit

Permalink
#️⃣ Fix HTML tags in inline contexts (#1404)
Browse files Browse the repository at this point in the history
  • Loading branch information
agoose77 authored Jul 23, 2024
1 parent 848b6d8 commit 19e96e2
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/modern-wombats-boil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'myst-transforms': patch
---

Fix conversion of hast fragments to mdast fragments
2 changes: 1 addition & 1 deletion packages/myst-transforms/src/html.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ describe('Test reconstructHtmlTransform', () => {
htmlTransform(mdast);
expect(mdast).toEqual({
type: 'root',
children: [{ type: 'paragraph', children: [{ type: 'image', url: 'test.mp4' }] }],
children: [{ type: 'image', url: 'test.mp4' }],
});
});
test('more open tags than close tags', async () => {
Expand Down
5 changes: 4 additions & 1 deletion packages/myst-transforms/src/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ const defaultHtmlToMdastOptions: Record<keyof HtmlTransformOptions, any> = {
sub(h: H, node: any) {
return h(node, 'subscript', all(h, node));
},
kbd(h: H, node: any) {
return h(node, 'keyboard', all(h, node));
},
cite(h: H, node: any) {
const attrs = addClassAndIdentifier(node);
return attrs.label ? h(node, 'cite', attrs, all(h, node)) : all(h, node);
Expand Down Expand Up @@ -173,7 +176,7 @@ export function htmlTransform(tree: GenericParent, opts?: HtmlTransformOptions)
n.tagName = '_brKeep';
});
}
const mdast = unified().use(rehypeRemark, { handlers }).runSync(hast);
const mdast = unified().use(rehypeRemark, { handlers, document: false }).runSync(hast);
node.type = 'htmlParsed';
node.children = mdast.children as Parent[];
visit(node, (n: any) => delete n.position);
Expand Down
41 changes: 36 additions & 5 deletions packages/myst-transforms/tests/html.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,9 @@ cases:
after:
type: root
children:
- type: paragraph
children:
- type: image
url: example.jpg
title: example
- type: image
url: example.jpg
title: example
- title: table
before:
type: root
Expand Down Expand Up @@ -132,3 +130,36 @@ cases:
after:
type: root
children: []

- title: inline elements
before:
type: root
children:
- type: list
children:
- type: listItem
children:
- type: html
value: <code>foo</code>
after:
type: root
children:
- type: list
children:
- type: listItem
children:
- type: inlineCode
value: foo
- title: kbd tag
before:
type: root
children:
- type: html
value: <kbd>Shift</kbd>
after:
type: root
children:
- type: keyboard
children:
- type: text
value: Shift

0 comments on commit 19e96e2

Please sign in to comment.