-
I need to process custom shortcodes (this is not the only one, there are a few more whose syntax is completely different) coming as a string for example:
For such a task, I am using the react-markdown component and some custom code that I am working on. This is how I am setting up the import React from 'react';
import './App.css';
import ReactMarkdown from "react-markdown";
import remarkGfm from "remark-gfm";
import remarkDirective from "remark-directive";
import remarkFrontmatter from "remark-frontmatter";
import remarkBreaks from "remark-breaks";
import remarkUnwrapImages from "remark-unwrap-images";
import rehypeSlug from "rehype-slug";
import rehypeExternalLinks from "rehype-external-links";
import shortcodePlugin from "./customPlugins/ShortcodePlugin";
function App() {
const str = '# This is some Markdown!\\n\\nHi! I\'m your first Markdown file in **StackEdit**. If you want to learn about StackEdit, you can read me. And this is a\n custom image shortcode ::image{id="https://dummyimage.com/600x400/000/fff" alt="some alt" title="some title"}::\n# another title\n ::image{id="https://dummyimage.com/600x400/000/fff" alt="some alt" title="some title"}::';
return (
<div className="App">
<ReactMarkdown
remarkPlugins={[
remarkGfm,
remarkDirective,
[remarkFrontmatter, ['yaml', 'toml']],
remarkBreaks,
remarkUnwrapImages,
shortcodePlugin
]}
rehypePlugins={[
rehypeSlug,
rehypeExternalLinks
]}
>
{str}
</ReactMarkdown>
</div>
);
}
export default App; Here is the import {visit} from "unist-util-visit";
import processImage from "./Shortcodes/Image";
function shortcodePlugin() {
return (tree: any) => {
visit(tree, (node: any) => {
switch (node.name) {
case 'image':
processImage(node);
break;
}
});
}
}
export default shortcodePlugin; and the const processImage = (node: any) => {
const data = node.data || (node.data = {});
data.hProperties = node.attributes;
node.type = 'containerDirective';
const img = {
type: 'image',
url: node.attributes.id
}
node.children.push(img)
console.log(node)
}
export default processImage; From the above, I would expect two things:
but none of those are happening and I am not sure what I am missing because I have read the docs several times. This is basically what I am getting back. ![]() Can I get some help? What I am missing? PS: I will try to set up a working example later today |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
Hi!
What are shortcodes? Do you mean directives?
How do you mean, “different”?
If you use TS, it’s probably a good idea to use types, instead of
The same with two-colon, leaf, directives:
If you want to use text directives, use a single colon: |
Beta Was this translation helpful? Give feedback.
-
hey @wooorm I have this edge case where the
(which to be honest I am not sure is a valid markdown) and this is the code processing such directive: const processImage = (node: any) => {
const data = node.data || (node.data = {});
data.hProperties = node.attributes;
const imgPath = node.attributes.id;
console.log('imgPath', imgPath);
const img = {
type: 'image',
url: imgPath,
alt: node.attributes.alt
}
node.children.push(img)
}
export default processImage; however Thanks in advance |
Beta Was this translation helpful? Give feedback.
Hi!
What are shortcodes? Do you mean directives?
How do you mean, “different”?
If you use TS, it’s probably a good idea to use types, instead of
any
everywhere.Using types will help you catch bugs.
::x
are leaf directives. They are blocks. Like a heading. Have you tried putting a heading in a markdown paragraph? It doesn’t work:asd # heading? No!
. Instead, you put them on their own line:The same with two-colon, leaf, directives: