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>
This commit is contained in:
James Trew
2024-05-02 21:34:49 -04:00
committed by GitHub
parent 486a6489c4
commit fac83a556e
4 changed files with 48 additions and 7 deletions

View File

@@ -222,4 +222,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