code-hike / codehike

Build rich content websites with Markdown and React
https://codehike.org
MIT License
4.74k stars 147 forks source link

Option to leave code with unsupported language untouched #210

Closed MarkusWendorf closed 2 years ago

MarkusWendorf commented 2 years ago

Hello, and thanks again for all the work.

I have a private rehype plugin that relies on code blocks with a specific language to be set (think bla-bla-blub, more like a private api). It also relies on the fact that these code blocks are not manipulated by remark plugins (expecting a specific node/tree structure).

Right now a warning is displayed for unsupported languages, but the transform is happening regardless (with lang=text).

I would like to have the option to disable this behaviour and not do any transformations on unsupported languages.

Another option would be to not highlight unsupported languages and tell the user to use lang=text.

https://github.com/code-hike/codehike/blob/3e61df61aa5ffa833093694e11bae1c523297e51/packages/mdx/src/highlighter/index.tsx#L60-L64

pomber commented 2 years ago

Does your remark plugin change the code node into another type of node? If that's the case, if you put your plugin before code-hike's, code-hike should ignore that node.

MarkusWendorf commented 2 years ago

@pomber it's a rehype plugin, so it's always after codehike I guess.

Regardless, I think it would be better to not transform unsupported languages and change the warning, what do you think?

 console.warn( 
   "[Code Hike warning]", 
   `${lang} is not a valid language. You can use language "text" instead, but you will lose syntax highlighting.` 
 ) 
pomber commented 2 years ago

I was thinking of another approach (that isn't a breaking change). Adding a ignoreLanguages config. Something like:

  remarkPlugins: [
    [
      remarkCodeHike,
      {
        theme: someTheme,
        lineNumbers: true,
        ignoreLanguages: ["", "mermaid"]
      },
    ],
  ],

That should also work with mermaid.

Does that work for you?

MarkusWendorf commented 2 years ago

Yes that would work.

pomber commented 2 years ago

Changed the name from the example from ignoreLanguages to skipLanguages

MarkusWendorf commented 2 years ago

@pomber tested the pre-release 0.6.2-next.1. Works as intended, thank you.

github-actions[bot] commented 2 years ago

:rocket: Issue was released in v0.6.2 :rocket: