wooorm / refractor

Lightweight, robust, elegant virtual syntax highlighting using Prism
MIT License
722 stars 33 forks source link

Weird behavior on 3.x, and not on 4.x #59

Closed jangxyz closed 2 years ago

jangxyz commented 2 years ago

On trying to use @mapbox/rehype-prism with svelte, I came along with the issue of rendering bunch of [object Object] literals printed.

renders-strangely (The @html directive let's your render html without escaping any angle brackets)

It even happens without applying imported rehypePrism plugin to the processor. Just import rehypePrism from '@mapbox/rehype-prism' somehow "stringifies" the objects. Commenting out the import statement as well shows the original content as expected.

renders-correctly (Nothing special, just some C++ code working with OpenCV)

Digging deeper, I found that

The languages arduino and chaiscript registers and extends refractorCpp, so my bet is on that one (these are the only two langauges that requires cpp.js). However the file contains mostly parameters, so I'm stuck.

I would've start looking into the prism code or svelte, but found out for some reason it works okay with refractor^4. I wonder if there is an explanation here -- and hopefully don't have to start digging again. I can't find much information about the difference between 3.x and 4.x, except for the 4.0.0 release note. My guess is that using ESM imports and not somehow interferes with the global scope, but cannot figure out what the actual difference is.

The project seems to keep both major versions, so I'm confused.

jangxyz commented 2 years ago

As a side note story, I started out with rehype-highlight, but found out highlight.js does not support line numbers, but Prism does.

If I had found out that refractor does not support prism plugins, I wouldn't have came across this route. My bad. 😂 Maybe I should try rehype-prism-plus, or start creating a transformer myself.

Thought the problem itself is worth to mention, though.

wooorm commented 2 years ago

Hey!

I’m closing this because it’s not yet a reproducible problem here. My recommendation is to raise a PR with mapbox/rehype-prism folks to update. There are tons of different ways to highlight code. You can choose which one works for you.