Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature request] Syntax highlighting in <script type="text/babel"> for HTML support #291

Closed
Kristinita opened this issue Nov 18, 2016 · 12 comments

Comments

@Kristinita
Copy link

Summary

It would be nice, if in JavaScript (Babel) syntax will be added support syntax highlighting for Babel in tag <script></script> for HTML.

Expected behavior

In separate file.

In separate file

Actual behavior

In HTML.

In HTML

Steps to reproduce

In open HTML file I have script:

<script type="text/babel">
  let arr = ["hello", 2]; // let

  let [str, times] = arr; //

  alert( str.repeat(times) );
</script>

Script not highlight for me. I get scope for each place of this script: source.js meta.jsx.js I get each time.

Environment

Sublime Text:
Build 3126
Syntax:
JavaScript (Babel) into Babel plugin
Color scheme:
Monokai Phoenix into Babel plugin

Thanks.

@nwoltman
Copy link

You might have better luck opening an issue in the sublimehq/Packages repo since it should be the Sublime HTML syntax that includes this feature.

@zertosh
Copy link
Member

zertosh commented Nov 28, 2016

Can't do anything about this. What the HTML grammar decides to scope as embedded javascript is outside babel-sublime.

@zertosh zertosh closed this as completed Nov 28, 2016
@alopatin
Copy link

I was having this issue too, this is how I fixed it (on MacOS):

  1. Copy Sublime Text.app/Contents/MacOS/Packages/HTML.sublime-package to somewhere.
  2. Rename the file to HTML.sublime-package.zip, then unzip it.
  3. Copy the contained HTML.sublime-syntax file to ~/Library/Application Support/Sublime Text 3/Packages/User/.
  4. Grep for javascript, change it to javascript|babel.
  5. Close and reopen your original HTML file to see the updated syntax highlighting!

For convenience, here is a gist with the modified HTML.sublime-syntax file that you can just copy to ~/Library/Application Support/Sublime Text 3/Packages/User/.

@stanbers
Copy link

@alopatin , when I copy the HTML.sublime-syntax file to that path you mentioned above, my sublime still not work ,did I miss anything ? do you have any suggestions ?

@asdutoit
Copy link

@stanbers , I assume you came right. But just for the sake of anybody else who may be experiencing the same issue, rather save the file, HTML.sublime-syntax, in ~/Library/Application Support/Sublime Text 3/Packages/HTML/ . Ensure Sublime Text 3 is closed before you start.
Then re-open Sublime and test.

@animex
Copy link

animex commented Jul 7, 2018

@alopatin Thanks. Did the same with Windows! The only difference is the line is different where you need to change in HTMl.sublime-syntax:

javascript_mime_type: |-
  (?ix)(?:
    # https://mimesniff.spec.whatwg.org/#javascript-mime-type
    (?:application|text)/(?:x-)?(?:java|ecma)script
    | text/javascript1\.[0-5]
    | text/babel
    | text/jscript
    | text/livescript
)

@chavab1
Copy link

chavab1 commented Nov 1, 2018

I finally got it to work half-way using @alopatin 's help. However, it still doesn't work entirely. Some of the declarations are all yellow after entering some JSX code. Do I have to set a setting somewhere??
Any idea how I can get it to work??
capture

Thanx

@dtg5
Copy link

dtg5 commented Nov 4, 2018

I have the same problem as @chavab1.
For me it looks like this:
image

EDIT:
Fixed by adding this code to the main: block in HTML.sublime-syntax:

- match: '(<)((?i:script))\b(?![^>]*/>)(?![^>]*(?i:type.?=.?text/((?!babel).*)))'
  captures:
    0: meta.tag.script.begin.html
    1: punctuation.definition.tag.begin.html
    2: entity.name.tag.script.html
  push:
    - match: (?i)(</)(script)(>)
      captures:
        1: punctuation.definition.tag.begin.html
        2: entity.name.tag.script.html
        3: punctuation.definition.tag.end.html
      pop: true
    - include: tag-stuff
    - match: '>'
      push: "Packages/Babel/JavaScript (Babel).sublime-syntax"
      with_prototype:
         - match: (?i)(?=</script)
           pop: true

Doesn't seem very elegant, but it got the job done I guess. My HTML looks like this now:

image

@chavab1
Copy link

chavab1 commented Nov 5, 2018

@dtg5 I actually figured out that it was the ending / that was causing the code to look that way. After I figured that out I did research and found that other people had started complaining about it as well and there is even a thread dedicated to it. It seems like support for JSX is still off.
They suggest to use this syntax. It does work after installing this package.

However, after enabling this new syntax I found that the PHP syntax doesn't work anymore. I researched that and the developers just became aware of it. They suggest to disable the default PHP syntax and use their own PHP syntax. They said it's just a temporary fix and they are currently working on a permanent solution.

After going through all that and then seeing your solution I'm kind of tempted to just use your solution. It's definitely much easier. Here's how my HTML looks now.

capture

@woojoo666
Copy link

still isn't working for me, even with the solution from @dtg5. I know the syntax file is being loaded because it's parsing the 'text/babel' tag, but it's parsing it as plain javascript. Can anybody provide the sublime-syntax file that is working for them (Windows, Sublime Text 3)? Thanks

img

@chavab1
Copy link

chavab1 commented May 20, 2019

It looks like you're still having the problem with the '/' sign.
As I stated in my previous response, I got it to work by using this syntax. However, it conflicted with my PHP syntax so I had to use a different PHP syntax as well.

Another solution was making revisions to the main HTML syntax as @dtg5 discovered.

I've attached the file that I'm using with @dtg5's fix and it's working. I'm using the babel package as well.

HTML.zip

@woojoo666
Copy link

@chavab1 oh nice, that HTML sublime syntax file works for me, thanks! And I'll have to check out the Naomi syntax package as well, seems like a good alternative instead of manually overriding my HTML syntax file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants