feat(html): use gsub for mimetype lookup

This commit is contained in:
Christian Clason
2025-04-05 12:22:48 +02:00
parent 328ee3db54
commit cbfe8a20c7
6 changed files with 33 additions and 35 deletions

View File

@@ -29,6 +29,7 @@ Language | Tier | Queries | Maintainer
[blade](https://github.com/EmranMR/tree-sitter-blade) | unstable | `HFIJ ` | @calebdw
[blueprint](https://gitlab.com/gabmus/tree-sitter-blueprint) | unstable | `H  J ` | @gabmus
[bp](https://github.com/ambroisie/tree-sitter-bp)[^bp] | unstable | `HFIJL` | @ambroisie
[brightscript](https://github.com/ajdelcimmuto/tree-sitter-brightscript) | unstable | `HFIJ ` | @ajdelcimmuto
[c](https://github.com/tree-sitter/tree-sitter-c) | unstable | `HFIJL` | @amaanq
[c_sharp](https://github.com/tree-sitter/tree-sitter-c-sharp) | unstable | `HF JL` | @amaanq
[caddy](https://github.com/opa-oz/tree-sitter-caddy) | unmaintained | `HFIJ ` | @opa-oz

View File

@@ -153,8 +153,8 @@ return {
},
brightscript = {
install_info = {
revision = '48ce1687125c6dfefcc7a1bef19fa0f0f00426cc',
url = 'https://github.com/ajdelcimmuto/tree-sitter-brightscript',
files = { 'src/parser.c' },
},
maintainers = { '@ajdelcimmuto' },
tier = 2,

View File

@@ -22,7 +22,7 @@ local filetypes = {
haskell_persistent = { 'haskellpersistent' },
idris = { 'idris2' },
janet_simple = { 'janet' },
javascript = { 'javascriptreact', 'ecma', 'jsx', 'js' },
javascript = { 'javascriptreact', 'ecma', 'ecmascript', 'jsx', 'js' },
javascript_glimmer = { 'javascript.glimmer' },
linkerscript = { 'ld' },
latex = { 'tex' },

View File

@@ -39,30 +39,3 @@ end, { force = true })
query.add_predicate('any-kind-eq?', function(match, _, _, pred)
return predicates['kind-eq'](match, pred, true)
end, { force = true })
-- register custom directives
local mimetype_aliases = {
['importmap'] = 'json',
['module'] = 'javascript',
['application/ecmascript'] = 'javascript',
['text/ecmascript'] = 'javascript',
}
---@param match TSQueryMatch
---@param _ string
---@param bufnr integer
---@param pred string[]
---@return boolean|nil
query.add_directive('set-lang-from-mimetype!', function(match, _, bufnr, pred, metadata)
local id = pred[2]
local node = match[id]
local type_attr_value = vim.treesitter.get_node_text(node, bufnr, { metadata = metadata[id] })
local configured = mimetype_aliases[type_attr_value]
if configured then
metadata['injection.language'] = configured
else
local parts = vim.split(type_attr_value, '/', {})
metadata['injection.language'] = parts[#parts]
end
end, { force = true })

View File

@@ -32,16 +32,40 @@
(#not-lua-match? @_no_type_lang "%stype%s*=")
(#set! injection.language "javascript"))
; <script type="mimetype-or-well-known-script-type">
; <script type="foo/bar">
(script_element
(start_tag
(attribute
(attribute_name) @_attr
(#eq? @_attr "type")
(quoted_attribute_value
(attribute_value) @_type)))
(attribute_value) @injection.language)))
(raw_text) @injection.content
(#set-lang-from-mimetype! @_type))
(#gsub! @injection.language "(.+)/(.+)" "%2"))
; <script type="importmap">
((script_element
(start_tag
(attribute
(attribute_name) @_attr
(#eq? @_attr "type")
(quoted_attribute_value
(attribute_value) @_type)))
(raw_text) @injection.content)
(#eq? @_type "importmap")
(#set! injection.language "json"))
; <script type="module">
((script_element
(start_tag
(attribute
(attribute_name) @_attr
(#eq? @_attr "type")
(quoted_attribute_value
(attribute_value) @_type)))
(raw_text) @injection.content)
(#eq? @_type "module")
(#set! injection.language "javascript"))
; <a style="/* css */">
((attribute

View File

@@ -19,6 +19,8 @@
<!-- ^ @javascript -->
<script async defer> const x = 1 </script>
<!-- ^ @javascript -->
<script type="module"> import { foo } from "bar" </script>
<!-- ^ @javascript -->
<script type="text/javascript"> const x = 1 </script>
<!-- ^ @javascript -->
<script type="text/ecmascript"> const x = 1 </script>
@@ -27,8 +29,6 @@
<!-- ^ @javascript -->
<script type="application/javascript"> const x = 1 </script>
<!-- ^ @javascript -->
<script type="module"> import { foo } from "bar" </script>
<!-- ^ @javascript -->
<script defer type="text/javascript"> const x = 1 </script>
<!-- ^ @javascript -->
<script type="text/markdown">## Hello *World*!</script>
@@ -40,7 +40,7 @@
<script type="application/json">{ "true": false }</script>
<!-- ^ @json -->
<script type="importmap">{ "true": false }</script>
<!-- ^ @json -->
<!-- ^ @json -->
<div style="height: 100%">
<!-- ^ @css -->
Test div to test css injections for style attributes