21

Is there any Vs Code extension to syntax-highlight HTML inside JavaScript strings?

Specifically I am writing web components

const html = content => `
  <div>
    <table>
      ${content}
    </table>
  </div>
`;

class BaseTable extends HTMLElement {
  constructor() {
    super();
  }

  set content(value) {
    const template = document.createElement('template');
    template.innerHTML = style + html(value);

    this.attachShadow({ mode: 'open' });
    this.shadowRoot.appendChild(template.content.cloneNode(true));
  }
}

I want string inside html constant variable to be highlighted

2
  • Just FWIW, that isn't a string, it's a template literal. (Untagged template literals result in strings, though. Tagged ones may not.) But I'd expect anything up-to-date that syntax highlights HTML in strings would also handle template literals. Commented Feb 13, 2020 at 12:03
  • 4
    For future readers: you can use the plugin es6-string-html to highlight HTML template strings prefixed by a comment: /* html */`<h1>Hello world</h1>`. There are es6-string-* plugins for other languages too, like CSS and Markdown. Commented Apr 20, 2021 at 2:29

1 Answer 1

11

If you use lit-html, There's a plugin in vs code called lit-plugin for syntax highlighting. Syntax is,

const markup = content => html`
  <div>
    <table>
      ${content}
    </table>
  </div>
`;

screenshot

Sign up to request clarification or add additional context in comments.

6 Comments

Its actually solution to my problem but there is error that html is not defined
did you import html from lit-html ?
With this lit-html plugin installed; you don't need to load lit-html WHEN you define your own html Tagged template: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
The plugin will also syntax highlight literals that use the built in String.raw e.g. const markup = String.raw`<div><p>Hi</p></div>`;
If you aren't using Lit library, the solution to work with the "lit-html" extension is what @lamplightdev said, use String.raw.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.