How to type a custom plugin on project? #747
-
Continuing with migrating the portfolio to TypeScript, we got a new problem. This is the custom plugin import { Node } from "unist";
import visit from "unist-util-visit";
const allowedFiletypes: string[] = ["avi", "mp4", "mov", "mkv"];
interface ImageNode extends Node {
url: string;
}
function video() {
function transformer(tree: Node) {
visit(tree, "image", (node: ImageNode) => {
const { url } = node;
const fileType: string | undefined = url.split(".").pop();
if (fileType && allowedFiletypes.includes(fileType)) {
node.data = node.data || {};
node.data.hName = "video";
// TODO: If images already returns a src, next line is not required.
// node.data.hProperties = { src: node.url };
}
});
}
return transformer;
}
module.exports = video; To import in: import * as remarkVideo from "presentation/lib/remarkvideo";
----
const _mapProps = (props: ReactMarkdownOptions): ReactMarkdownOptions => ({
...props,
remarkPlugins: [
// RemarkMathPlugin,
// RemarkHighlightPlugin,
unwrapImages,
remarkVideo,
],
rehypePlugins: [
rehypeRaw,
[rehypeSanitize, merge(gh, { attributes: { code: ["className"] } })],
],
components: {
...props.components,
// math: ({ value }) => <BlockMath>{value}</BlockMath>,
// inlineMath: ({ value }) => <InlineMath>{value}</InlineMath>,
code: CodeBlock,
img: ImageBlock,
video: VideoBlock,
},
}); And it gives us: Type 'typeof import("/home/luisalaguna/Projects/portfolio/frontend2.0/src/presentation/lib/remarkvideo")' is not assignable to type 'Pluggable<any[], Settings>'.
Type 'typeof import("/home/luisalaguna/Projects/portfolio/frontend2.0/src/presentation/lib/remarkvideo")' is not assignable to type '[Plugin<any[], Setti If we use Thank you. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
@SalahAdDin can you share a repeatable example? https://codesandbox.io/s/youthful-hofstadter-qtojl?file=/src/App.tsx A number of things to check for:
Also note, that mdast has typings for the image node which can be used to simplify the typings. |
Beta Was this translation helpful? Give feedback.
@SalahAdDin can you share a repeatable example?
Trying what you shared in a simplified codesandbox, the typing appears to work.
https://codesandbox.io/s/youthful-hofstadter-qtojl?file=/src/App.tsx
A number of things to check for:
Also note, that mdast has typings for the image node which can be used to simpli…