highlightjs / highlight.js

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

[Request] Support namespaced tagged template strings in JavaScript #4001

Closed aral closed 3 months ago

aral commented 4 months ago

Is your request related to a specific problem you're having?

Kitten exposes html and css tagged template strings not from the global namespace but from globalThis.kitten (so as not to pollute the global namespace).

e.g.,

export default () => kitten.html`
  <h1>I’m not highlighted *sadface*.</h1>
`

Currently, highlight.js does not recognise and highlight these as HTML.

The solution you'd prefer / feature you'd like to see added...

I’d love to see the HTML and CSS tagged template string match criteria expanded so they match .html and .css, not just html and css so that, for example

someNamespace.html`…` 

would also be recognised as HTML.

Any alternative solutions you considered...

Leaving a space after the dot works since JavaScript is whitespace agnostic but it looks and feels wrong to have to do that just to make the syntax highlighter happy :)

joshgoebel commented 3 months ago

Can you show me an article or anything that might suggest this is as prolific a "standard" as uppercase class names in JS? We do in several cases highlight idiomatic code (past the syntax itself) - but I'm not certain this yet rises to that same level...

I find this functionality was originally added with:

it's becoming common to write inline HTML and CSS as tagged template literals

I'm not even sure the original support rises to the bar honestly... but this was before my time... Typically our policy is to highlight things that matter syntactically... for example CSS inside a <script> tag in HTML should be highlighted as CSS - it literally is - as understood by every web browser in existence. CSS inside a css`` template string is not syntactically CSS - it's purely a string.

joshgoebel commented 3 months ago

@allejo Any thoughts here? Am I being to string in my thinking?

aral commented 3 months ago

Can you show me an article or anything that might suggest this is as prolific a "standard" as uppercase class names in JS? We do in several cases highlight idiomatic code (past the syntax itself) - but I'm not certain this yet rises to that same level...

I’m not sure about any articles but the TypeScript Language Server (used in VSCode, etc.) correctly handles these.

e.g., screenshot from VSCode:

Screenshot of a compound tagged template HTML string being correctly syntax highlighted in VSCode using the TypeScript Language Server

And ditto for CSS:

Screenshot of a compound tagged template CSS string being correctly syntax highlighted in VSCode using the TypeScript Language Server

joshgoebel commented 3 months ago

Is it matching on anythingatall.html`` - or does it only work for kitten?

aral commented 3 months ago

@joshgoebel On anything at all; I don’t think Microsoft know or care about Kitten ;)