rehype-pretty / rehype-pretty-code

Beautiful code blocks for Markdown or MDX.

Home Page:https://rehype-pretty.pages.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Type Error with rehypePrettyCode in Contentlayer Configuration

onurhan1337 opened this issue · comments

I'm encountering a type error when trying to configure the rehypePrettyCode plugin in my Contentlayer configuration. The onVisitLine, onVisitHighlightedLine, and onVisitHighlightedWord functions are expected to return a value of type Transformer<any, any> or void, but they are currently returning a value of type Transformer<Root, Root> or void.

Here's the relevant part of my configuration:

rehypePlugins: [
  rehypeSlug,
  [
    rehypePrettyCode,
    {
      theme: "poimandres",
      onVisitLine(node: RehypeNode) {
        // Prevent lines from collapsing in `display: grid` mode, and allow empty
        // lines to be copy/pasted
        if (node.children.length === 0) {
          node.children = [{ type: "text", value: " " }];
        }
      },
      onVisitHighlightedLine(node: RehypeNode) {
        node.properties.className.push("line--highlighted");
      },
      onVisitHighlightedWord(node: RehypeNode) {
        node.properties.className = ["word--highlighted"];
      },
    },
    rehypeAutolinkHeadings,
    {
      properties: {
        className: ["subheading-anchor"],
        ariaLabel: "Link to section",
      },
    },
  ],
],

In addition to the above, here's my package.json:

{
  "dependencies": {
    "contentlayer": "^0.3.4",
    "rehype-pretty-code": "^0.12.0",
    "rehype-slug": "^6.0.0",
    "rehype-autolink-headings": "^7.1.0",
  }
}

expected to return a value of type Transformer<any, any> or void, but they are currently returning a value of type Transformer<Root, Root> or void.

I'm not sure I understand why that would error, if it accepts any generics then it should work?

expected to return a value of type Transformer<any, any> or void, but they are currently returning a value of type Transformer<Root, Root> or void.

I'm not sure I understand why that would error, if it accepts any generics then it should work?

Actually this code part works fine, after updating all my packages I encountered this weird error in the contentlayer.config.ts file. I can't figure out why it's not working.

ss

It might be due to contentlayer using unified@10 while this library uses unified@11 since v0.11.0. I also experienced problems with MDX v2 due to the --shiki css variables, while MDX v3 worked.

v0.10.2 was the last version using unified@10 so that may work instead (or if it works at runtime, just ignore the error.)