babel / babel-sublime

Syntax definitions for ES6 JavaScript with React JSX extensions.
https://babeljs.io/
MIT License
3.26k stars 170 forks source link

feat: Syntax Highlighting for HTML Template Strings #138

Closed sparty02 closed 9 years ago

sparty02 commented 9 years ago

Given HTML content in an ES6 template string, e.g.:

let template = `
    <div class="row">
        <div class="col-md-4">
            <p>Hello there!</p>
        </div>
    </div>
`

....syntax highlight the HTML as if it was stand-alone in a .html file.

sparty02 commented 9 years ago

Interestingly, by accident, I found out that it syntax highlights correctly with a single quote ', but not with the template literal backtick ```.

i.e., the following syntax highlights as it would if this markup was in a standalone .html file.

template: '
    <input class="form-control" value="name"">
'
zertosh commented 9 years ago

There is also no way to know that the contents of a template string is html. I made a custom version of the syntax that matched css in a template string when called with __CSS:

const foo = __CSS(`/*css string*/`);

but sublime kept crashing, so I stopped using it.

Interestingly, by accident, I found out that it syntax highlights correctly with a single quote

what you're seeing is plain ol' JSX matching. single quotes are only matched for one line. so the first line is a string start, the 2nd is JSX, and the 3rd is another string start.

sparty02 commented 9 years ago

There is also no way to know that the contents of a template string is html.

Agreed. However, thinking out loud (with the caveat that I am naive on the internals of babel-sublime), would it make sense to "evaluate" the contents of a template literal, maybe running it through the JSX matching, and highlighting it as such if the JSX matcher didn't choke on it?

zertosh commented 9 years ago

Since you can interpolate expressions in template strings, it wouldn't make sense to try to detect the contents as any particular type of source. Besides, you're opening a can of worms when you start matching some strings but not others. Check the issues, people get really emotional about their colors :stuck_out_tongue:

sparty02 commented 9 years ago

Check the issues, people get really emotional about their colors

That's why I'm coming up with crazy ideas... :smile: anyway, thanks for humoring me... :wink:

zertosh commented 9 years ago

It's not crazy hehe - I tried it myself with css. If it didn't crash ST3, I would've published instructions on how to make your own version that matched whatever function call with a template string as css/html.

OliverJAsh commented 9 years ago

This seems to work when using https://github.com/Benvie/JavaScriptNext.tmLanguage?

thedamon commented 9 years ago

@OliverJAsh that's strange.. I came to this package after I wasn't getting that behaviour with JavaScriptNext.tmLanguage

felipearosemena commented 8 years ago

I know this is closed, but would like to +1 this request.

Wouldn't it be possible to parse the content between backticks as html and then the expressions inside ${} as js?

It seems to me that would make sense, after all aren't template literals supposed to make easier to work with HTML within JS? Proper syntax highlighting would be a huge bonus for this.

DylanPiercey commented 7 years ago

Currently the atom babel syntax plugin allows for this but only if the tagged template function is called html - like so:

const it = html`
  <div>hi</div>
`

Also note that GitHub does the same.

tamer1an commented 7 years ago

devtool

56.0.2924.87 (64-bit)