Skip to content

Empty language cannot gain hljs plaintext classes #342

@unphased

Description

@unphased

Hi again.

import { Marked } from 'marked';
import { markedHighlight } from "marked-highlight";
import hljs from 'highlight.js';

const marked = new Marked(
  markedHighlight({
    langPrefix: 'hljs language-',
    highlight(code, lang, info) {
      const language = hljs.getLanguage(lang) ? lang : 'plaintext';
      return hljs.highlight(code, { language }).value;
    }
  })
)

console.log(marked.parse(`
\`\`\`
plaintext
<
&
>
\`\`\`

\`\`\`blah
blah lang
<
&
>
\`\`\``));
<pre><code>plaintext
&lt;
&amp;
&gt;
</code></pre><pre><code class="hljs language-blah">blah lang
&lt;
&amp;
&gt;
</code></pre>
  • Note that if lang is empty, even though it does pass through hljs.highlight() with plaintext lang, the rendered code has no hljs language-plaintext class as expected.

Further notes:

    highlight(code, lang, info) {
      const language = hljs.getLanguage(lang) ? lang : 'blahfakelang';
      const out = hljs.highlight(code, { language });
      console.log('out:', out);
      return out.value;
    }

leads to uncaught throw:

Could not find the language 'blahfakelang', did you forget to load/include a language module?
/Users/slu/stevenlu.net/node_modules/highlight.js/lib/core.js:2107
      throw new Error('Unknown language: "' + languageName + '"');
            ^

Error: Unknown language: "blahfakelang"

essentially, when lang comes in empty, even though it's converted here to plaintext, it won't take, but when a nonempty lang is specified in the payload, then we override it to be plaintext here and it shows up PROPERLY as plaintext, but we cannot configure an empty lang into plaintext as expected via marked-highlight.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions