highlightjs / highlight.js

JavaScript syntax highlighter with language auto-detection and zero dependencies.
https://highlightjs.org/
BSD 3-Clause "New" or "Revised" License
23.3k stars 3.52k forks source link

Could not find the language 'HTML, XML' #4033

Closed Antecer closed 2 months ago

Antecer commented 2 months ago

if lang is xml, the language name will result HTML, XML

function test(code, lang) {
    let language = hljs.getLanguage(lang)?.name ?? 'plaintext';
    return hljs.highlight(code, {language}).value;
}

Then i will get this error: image

if i replace HTML, XML to xml, it will work normal:

function test(code, lang) {
    let language = hljs.getLanguage(lang)?.name ?? 'plaintext';
    if (language == 'HTML,xml') language = 'xml';
    return hljs.highlight(code, {language}).value;
}
joshgoebel commented 2 months ago

if lang is xml, the language name will result HTML, XML

This is correct. You are supposed to pass the shortname (ie, xml) to highlight, not the name attribute... the name is only for completness.