Skip to content

Commit

Permalink
Fixed type error in remark-img-to-jsx.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
karuppusamy-d committed Mar 24, 2023
1 parent 2b7a2d3 commit ff4837f
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/lib/remark-img-to-jsx.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { visit } from "unist-util-visit";
import sizeOf from "image-size";
import fs from "fs";

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Node = import("unist").Node & { children: any };
import { Node, Parent } from "unist";

const remarkImgToJsx = () => {
return (tree: Node) => {
return (tree: Parent<Node>) => {
visit(
tree,
// only visit p tags that contain an img element
(node: Node) =>
(node) =>
node.type === "paragraph" &&
node.children.some((n: Node) => n.type === "image"),
(node as Parent).children.some((n: Node) => n.type === "image"),
(node) => {
const imageNode = node.children.find((n: Node) => n.type === "image");
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const imageNode = (node as Parent<any>).children.find(
(n: Node) => n.type === "image"
);

// only local files
if (fs.existsSync(`${process.cwd()}/public${imageNode.url}`)) {
Expand All @@ -40,7 +41,7 @@ const remarkImgToJsx = () => {

// Change node type from p to div to avoid nesting error
node.type = "div";
node.children = [imageNode];
(node as Parent).children = [imageNode];
}
}
);
Expand Down

1 comment on commit ff4837f

@vercel
Copy link

@vercel vercel bot commented on ff4837f Mar 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.