rehype-pretty-code icon indicating copy to clipboard operation
rehype-pretty-code copied to clipboard

Syntax highlighting hangs when using a substring pattern with '

Open kpietak opened this issue 11 months ago • 2 comments

I have come across a problem with highlighting sections of source code using a pattern/substring using Nextra.

I am presenting code in Rust that implements lifetimes annotations marked with ' , for example: fn longer<'a>(a : &'a [i32], b : &'a [i32]) -> &'a [i32].

I am trying to highlight the annotation itself ('a) using the code:

    ```rust /'a/
    fn longer_array<'a>(a : &'a [i32], b : &'a [i32]) -> &'a [i32] {
        ...
    }```

This syntax causes the page rendering to crash (even terminating the process causes the node to remain on).

The problem has been previously reported in the Nextra project (https://github.com/shuding/nextra/issues/2792) and was redirected here as a code rendering issue.

kpietak avatar Mar 17 '24 09:03 kpietak

Nextra seems to be using v0.9.11 which is a bit old now as the current version is v0.13.0. In v0.12.1, a bug was fixed with word highlighting that could cause an infinite loop, although I thought it was a regression introduced in v0.12.0 🤔

Can you try to repro it using the latest version of rehype-pretty-code alone?

atomiks avatar Mar 17 '24 09:03 atomiks

I've reproduced the error using the current version (v.0.13.0). Here is the sample code (a modified demo):

import { unified } from "unified";
import remarkParse from "remark-parse";
import remarkRehype from "remark-rehype";
import rehypeStringify from "rehype-stringify";
import rehypePrettyCode from "rehype-pretty-code";
 
async function main() {
  const file = await unified()
    .use(remarkParse)
    .use(remarkRehype)
    .use(rehypePrettyCode, {
      // See Options section below.
    })
    .use(rehypeStringify)
    .process(`\`\`\`rust /'a/
    
    fn longer_array<'a>(a : &'a [i32], b : &'a [i32]) -> &'a [i32] {
        
    }
    `);
 
  console.log(String(file));
}
 
main();

kpietak avatar Mar 17 '24 15:03 kpietak