fix(previewer): improve binary mime type check (#3083)

* fix(previewer): improve binary mime type check

Problem: mime type for a ts/js file can either return `text/plain` or
`application/javascript` based on the contents of the file.
Previously, this meant `application/javascript` would be considered
"possibly binary". This, in conjunction with how `vim.filetype.match`
does not give a result for a filename that ends in `.ts`, would lead to
a typescript file taking the path of `check_mime_type` and eventually
`mime_hook`.

Solution: Include `application/javascript` as a non-binary file type
during mime type check.

* [docgen] Update doc/telescope.txt
skip-checks: true

---------

Co-authored-by: Github Actions <actions@github>
(cherry picked from commit fac83a556e)
This commit is contained in:
James Trew
2024-05-20 11:34:02 +02:00
committed by Simon Hauser
parent 679dacb6f4
commit 588e26ff9f
4 changed files with 25 additions and 9 deletions
+17
View File
@@ -212,4 +212,21 @@ utils.set_preview_message = function(bufnr, winid, message, fillchar)
end
end
--- Check if mime type is binary.
--- NOT an exhaustive check, may get false negatives. Ideally should check
--- filetype with `vim.filetype.match` or `filetype_detect` first for filetype
--- info.
---@param mime_type string
---@return boolean
utils.binary_mime_type = function(mime_type)
local type_, subtype = unpack(vim.split(mime_type, "/"))
if vim.tbl_contains({ "text", "inode" }, type_) then
return false
end
if vim.tbl_contains({ "json", "javascript" }, subtype) then
return false
end
return true
end
return utils