Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Sublime Text treat <script type="text/html"> as HTML

I've been doing a lot of work with Knockout templates lately, and I've been using Sublime to do it. one thing that I've noticed though is that when using a template, which needs to be defined in a block like this:

<script type="text/html"></script>

it treats the contents as Javascript, which means I'm missing out on a lot of HTML tools which I have installed. I'd like to make it treat that content as HTML instead of Javascript; is there any setting which I could use to do this?

like image 927
moberemk Avatar asked Jun 17 '13 21:06

moberemk


2 Answers

I managed to find the answer thanks to iamntz here; the trick is simple. For Sublime Text 3:

  1. Open up Packages within your install directory, then find HTML.sublime-package and open it in 7zip (or your favorite archive tool)
  2. Find HTML.tmLanguage and open it for editing
  3. Find this line:

    <string>(?:^\s+)?(&lt;)((?i:script))\b(?![^&gt;]*/&gt;)</string>
    

    and replace it with this one:

    <string>(?:^\s+)?(&lt;)((?i:script))\b(?!([^&gt;]*text/html[^&gt;]*|[^&gt;]*/>))</string>
    

Nice and easy; the text/html in that second snippet can be replaced with any template type, and it will now be read as HTML by Sublime. This fix will also work with any HTML packages you have installed.

like image 121
moberemk Avatar answered Oct 01 '22 16:10

moberemk


This doesn't appear to be necessary any longer for Sublime Text 3 build 3103. Just make sure your script tag's type attribute begins with "text/" and doesn't end in "javascript" and it should handle HTML correctly now.

EDIT:

This has become a problem again with Sublime Text 3 build 3176. The fix is to modify the HTML package again but with this change in HTML.sublime-syntax:

         - script-javascript
         - tag-generic-attribute-meta
         - tag-generic-attribute-value
-    - match: (?i)(?=text/html(?!{{unquoted_attribute_value}})|'text/html'|"text/html")
+    - match: (?i)(?=text/html(?!{{unquoted_attribute_value}})|'text/html'|"text/html"|"text/x-template")
       set:
         - script-html
         - tag-generic-attribute-meta

Replace "x-template" with whatever type you are using for your script tag templates.

like image 38
David K. Hess Avatar answered Oct 01 '22 15:10

David K. Hess