3

I am using TypeScript with Sublime 3. How can I add HTML highlight in template attribute: [NOTE: I am already using Microsoft TypeScript Package]

Look how its not highlighted now:

enter image description here

2 Answers 2

8

Here's a quick fix that still makes use of your installed TypeScript package and its existing syntax highlight definition:

  1. Open a TypeScript file (with your installed TypeScript syntax highlighting)

  2. Select Tools > Developer > New Syntax from Typescript.tmLanguage to create a new syntax definition file based on the existing one

  3. Find the template context (ctrl+f for string.template.ts) and add an include for 'scope:text.html.basic' to the push as indicated in the commented line below:

    %YAML 1.2
    ---
    # http://www.sublimetext.com/docs/3/syntax.html
    name: TypeScript + HTML  # <-- renaming is optional
    
      # ...
    
      template:
        - match: "([_$[:alpha:]][_$[:alnum:]]*)?(`)"
          captures:
            1: entity.name.function.tagged-template.ts
            2: punctuation.definition.string.template.begin.ts
          push:
            - meta_scope: string.template.ts
            - match: "`"
              captures:
                0: punctuation.definition.string.template.end.ts
              pop: true
            - include: template-substitution-element
            - include: string-character-escape
            - include: 'scope:text.html.basic'  # <-- !! only add this line !!
      template-substitution-element:
    
      # ...
    

    optional step:
    Change the name property at the beginning of the file to something like TypeScript + HTML to easily find and select it in the Syntax list later

  4. Save the file with a .sublime-syntax file-ending

  5. Restart Sublime Text and apply your new syntax highlighting to a typescript file:

    Screenshot of HTML syntax highlighting inside Angular template string within a TypeScript file

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

2 Comments

View > Syntax > Open all with current extension as > TypeScript + HTML :)
Any suggestions on getting this working with the inline css styles angular has as well?
3

You can read here how to achieve this:

https://forum.sublimetext.com/t/javascript-es6-template-literals-syntax-for-html/18242

Reproduced here:

Open Tools > Developer > New Syntax

Add:

%YAML1.2
---
# See http://www.sublimetext.com/docs/3/syntax.html
name: JavaScript NG
file_extensions:
  - js
  - ng.js
scope: source.js.ng
contexts:
  main:
    - match: ""
      push: scope:source.js
      with_prototype:
      - match: '`'
        push:
          - meta_content_scope: text.html.basic.embedded.js
          - include: 'scope:text.html.basic'
          - match: '`'
          pop: true

and save it has JavaScript-NG.sublime-syntax

There is also an open github issue on this:

https://github.com/sublimehq/Packages/issues/179

3 Comments

I came across this article before posting the question. it did not do any highlighting. When it finishes loading the syntax everything is the same. I tried relaunching sublime but nothing happened.
I got it working after choosing 'Javascript NG' from view -> syntax -> 'Javascript NG'
I created a file name JavaScript-NG.sublime-syntax, pasted the content provided in this answer, saved it to packages>user. the restart sublime. But I couldn't activate the syntax in sublime.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.