-
I have a custom plugin where I am getting a node and I would like to add an const processPageInclude = (node: any, productId?: string) => {
const data = node.data || (node.data = {})
data.hProperties = node.attributes;
node.children.unshift({
type: 'containerDirective',
children: [{
type: 'html',
value: '<div>This is some HTML</div>div>'
}]
});
console.log(node)
}
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 31 replies
-
Welcome @reypm! A few thoughts.
|
Beta Was this translation helpful? Give feedback.
-
Doing as follows makes the example to work: const myCustomPlugin = (node: any) => {
const markdownSource = `
# heading
* list
* items
`;
return markdownSource;
};
export default myCustomPlugin;
Long story short: I have a bunch of custom shortcodes for example: // this will add data to the node and later using `components` it renders some HTML code
:::::email-card{mailto:some@some.com}Click to email something
:::::
// this will fetch the content from external sources and will return plain text and/or markdown
// is a way to reuse content
:::::
include{id=some-page}
:::::
// this will add data to the node and later using `components` it renders some HTML code
:::::phone-card{phoneNumber:1111111111}Click to make a call
:::::
// this will fetch the content from external sources and will return plain-text and/or markdown
// is a way to reuse content
:::::
include{id=some-page-1}
::::: the original idea with |
Beta Was this translation helpful? Give feedback.
-
hey @wooorm / @ChristianMurphy I am still unable to get this working properly :| so here is all I have done: /**
* Gives directives a tag name, so they will transfer into the HTML AST (hast)
*/
function remarkDirectiveCustomize(options: any) {
return (tree: MdastRoot) =>
visit(tree, "containerDirective", (node) => {
node.data ??= {};
node.data.hName = "container-directive";
node.data.hProperties = {
type: node.name,
attributes: node.attributes,
product: options[0]
};
});
}
/**
* Find the directive HTML tags and replace them with markdown content
*/
function rehypeRenderDirective() {
return (tree: HastRoot) =>
visit(
tree,
{ type: "element", tagName: "container-directive" },
(node) => {
node.tagName = "div";
if (node.properties.type === "my-include") {
node.children = unified()
.use(remarkRehype)
.use(rehypeSanitize)
.runSync(
unified().use(remarkParse).parse(processPageInclude(node))
.children as ElementContent[]
);
}
}
);
}
return (
<ReactMarkdown
remarkPlugins={[
remarkGfm,
remarkDirective,
[remarkFrontmatter, ["yaml", "toml"]],
remarkBreaks,
[remarkMermaid],
shortcodePlugin,
[remarkDirectiveCustomize, [metaData?.id]],
]}
rehypePlugins={[rehypeExternalLinks, rehypeSlug, rehypeRenderDirective]}
remarkRehypeOptions={{ allowDangerousHtml: true }}
>
{data}
</ReactMarkdown>
); But I end up with this error:
But let's say the Promise is not returning any data which is not the case then I did manage to create a runnable example showing the same behavior, can you take a look and see what I am doing wrong? |
Beta Was this translation helpful? Give feedback.
Promise support, from components, in React is very much WIP. For something like remarkjs/react-markdown#680 to land, that needs to happen first.
You can do two things:
a) create your own async component. You can use the
components
here to map custom elements (saymy-directive
to your fancy async component, and do async work there, not on the AST, but at render.b)
react-markdown
is a tiny wrapper around other projects, look at the code, investigate thesyntax-tree
org, you can use those projects yourself, in your own async wrapper. Control the caches and stuff yourself manually