Tag name and attributes are not taken into account when using Hastscript #1275
-
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Found a solution by adding helper function: import type { PhrasingContent, Paragraph, Root, Code } from 'mdast'
import type { Properties, Result } from 'hastscript'
import type { Transformer, Plugin } from 'unified'
import type { Parent, Node } from 'unist'
import { visit } from 'unist-util-visit'
import { h as _h } from 'hastscript'
let h = (
el: string,
attrs: Properties = {},
children: unknown[] = [],
): Paragraph => {
let { properties, tagName } = _h(el, attrs)
return {
data: { hProperties: properties, hName: tagName },
children: children as PhrasingContent[],
type: 'paragraph',
}
}
export let remarkCopyCode: Plugin<[], Root> =
(): Transformer<Root> => (tree: Node) => {
visit(tree, 'code', (node: Code, index: number, parent: Parent) => {
let { lang } = node
let html = h(
'div',
{
class: 'copy-code-wrapper',
},
[
h(
'button',
{
class: 'copy-code-button',
'data-lang': lang,
},
[
{
value: 'Copy',
type: 'text',
},
],
),
node as unknown as Result,
],
)
parent.children[index] = html
})
} |
Beta Was this translation helpful? Give feedback.
-
@azat-io an issue I'm seeing, you are mixing the HTML and Markdown syntax tree types. If you want to work on the HTML, write a Rehype plugin and work with the HTML AST (hast). https://unifiedjs.com/learn/ remark plugins should only have markdown constructs (mdast), if you want to specify different HTML types for markdown constructs use |
Beta Was this translation helpful? Give feedback.
@azat-io an issue I'm seeing, you are mixing the HTML and Markdown syntax tree types.
If you want to work on the HTML, write a Rehype plugin and work with the HTML AST (hast). https://unifiedjs.com/learn/
Also some of the previous community ideas in https://github.com/orgs/mdx-js/discussions/1948
remark plugins should only have markdown constructs (mdast), if you want to specify different HTML types for markdown constructs use
node.data.hName
as documented in https://github.com/syntax-tree/mdast-util-to-hast#fields-on-nodesThat said, a rehype plugin sound like a better for what you are describing.