Fermain / -mollify

9 stars 9 forks source link

CodeBlock without specified language leads to 500 error #215

Open jaaneh opened 8 months ago

jaaneh commented 8 months ago

Having upgraded to v.0.3 canary, we are seeing 500 errors on any page that had a code block without specifying a language.

Is ok: ```js console.log("Hello, world"); ```

Is not ok: ``` console.log("Hello, world"); ```

Sometimes code blocks are used for plain text, and I therefore suggest adding a default language to the codeBlockPlugin. I think defaulting to txt is perfectly fine.

Related file: codeBlockPlugin.js

Suggested fix:

export default function codeBlockPlugin() {
  let codeBlockExists = false;
  return (tree) => {
    visit(tree, 'code', (node) => {
      codeBlockExists = true;
      node.type = 'html';
+     node.lang = node.lang || 'txt';
      node.value = `
        <Components.CodeBlock 
          class="my-2" 
      language="${node.lang}" 
      code={${JSON.stringify(node.value)}} 
      buttonLabel="Copy" 
      lineNumbers={true}
    />`;
    });
  };
}