Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio Code: How to automatically format HTML contained in template literals?

In my Javascript code I have many template literals that contain HTML code. These blocks are correctly highlighted via es6-string-html. However, they are not formatted automatically, which means the following block will be left as is:

let html = `
      <div class="wrapper">
<div>Hello, world!
     </div>
                 </div>
      `;

How can I enable auto-formatting for these strings as well?

like image 683
Edgar Derby Avatar asked Mar 25 '26 20:03

Edgar Derby


1 Answers

When Prettier detects a tagged template in JavaScript with a tag named html, it will format it. You can read more about this here and for highlighted you can use what you suggested es6-string-html

/**
 * html wrapper is needed for prettier formatting
 */
const html = String.raw;

const template = html`
  <div>
    <p>hello world</p>
    <p>hello world</p>
  </div>
`;
like image 171
Chris Avatar answered Mar 27 '26 08:03

Chris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!