question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Highlight HTML, CSS, Markdown, GraphQL in JS tagged template literals

See original GitHub issue

Motivation

Prism doesn’t highlight supported languages when they are in a tagged template literal with common tags.

Screen Shot 2019-06-07 at 11 25 17

Description

Support for common tagged template literals such as:

// HTML
html`...`

// CSS
css`...`

// React Components CSS
styled.foo``
Component.extend``
styled.foo.attr({})``
Component.extend.attr({)``
styled(Component).attr({})``
css.global``
css.resolve``

// Markdown
md`...`
markdown`...`

// GraphQL
gql`...`
graphql`...`
graphql.experimental`...`

// SQL
sql`SELECT * FROM users WHERE id = ${id}`

To allow this highlighting behavior without adding a tag call, one option is to also support comments in the place of tags:

/* HTML */`...`
/* CSS */`...`

There are also other non-template literal syntaxes which are potentially desired:

// Angular Components HTML + CSS
@Component({
  template: `<div>...</div>`,
  styles: [`h1 { color: blue; }`]
})

// styled-jsx CSS
<style jsx>{`div{color:red}`}</style>

Prettier has some AST tests for these things which could probably be repurposed here (or at least, could inspire a solution):

HTML tagged template literals:

CSS tagged template literals:

Markdown tagged template literals:

GraphQL tagged template literals:


Edit: It seems like #1714 has part of (all of) the styled-components implementations, and maybe this could be repurposed to achieve all the other languages.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:12 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
RunDevelopmentcommented, Jun 8, 2019

I would vote to add the eventual change with all languages as part of the main default JS language, since tagged template strings with different languages are common in places other than React / JSX / other frameworks. source

While it’s true that these tagged literals are quite common for frameworks, they are not part of the JS language spec and therefore should be an optional opt-in feature IMO. A more technical reason as to why handling tagged template strings with different languages in the main JS language is not optimal are dependencies. JS will have to require all included languages meaning that to highlight a small snippet of JS code (which might not even contain templates) you have to (down-)load all these languages. While I could tolerate CSS and Markup being required, I don’t see why JS has to require GraphQL, Markdown, or other languages we’ll include later.

My preferred solution here is a new language which handles all languages embedded in JS templates. I’m thinking of something along the lines of JS Templates. This makes the whole thing opt-in and keeps main JS lang light.

1reaction
RunDevelopmentcommented, Jun 7, 2019

Great suggestion!

There is one thing I want to add to the list of non-tagged literals:

div.innerHTML = `
    <em>Foo</em>
`;

(Even GitHub highlights this)


To allow this highlighting behavior without adding a tag call, one option is to also support comments in the place of tags:

/* HTML */`...`
/* CSS */`...`

I really don’t like the idea of Prism specific syntax for the sole purpose of highlighting. I get why this is desirable but no other editor (at least not that I’m aware of) supports this and adjusting your code specifically for Prism is not something which should have to do.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Enhanced strings with tagged template literals
Dive into how tagged template literals let us build enhanced strings! The template literal is a nice and useful addition to the JavaScript...
Read more >
Awesome Tagged Template Literals - stereobooster
Detailed CSS IntelliSense while working in styled strings. vscode-graphql - Syntax highlighting (type, query, mutation, interface, union, enum, ...
Read more >
Awesome Tagged Template Literals | by stereobooster | Medium
this function can parse CSS and return it as a JS object; or it can insert it in the DOM and return the...
Read more >
HTML & CSS Syntax highlighting - The Observable Forum
It would make HTML and CSS notebooks as legible as Javascript current is. ... enter HTML highlighting with the html tagged template literal, ......
Read more >
Comment tagged templates - Visual Studio Marketplace
Adds basic syntax highlighting for JavaScript and TypeScript tagged template strings using language identifier comments: const cssString ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found