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

@@ -3,6 +3,7 @@ local utils = require "telescope.utils"
local Path = require "plenary.path"
local from_entry = require "telescope.from_entry"
local Previewer = require "telescope.previewers.previewer"
local putil = require "telescope.previewers.utils"
local defaulter = utils.make_default_callable
@@ -65,9 +66,8 @@ local cat_maker = function(filename, _, start, _)
end
if 1 == vim.fn.executable "file" then
local output = utils.get_os_command_output { "file", "--mime-type", "-b", filename }
local mime_type = vim.split(output[1], "/")[1]
if mime_type ~= "text" then
local mime_type = utils.get_os_command_output({ "file", "--mime-type", "-b", filename })[1]
if putil.binary_mime_type(mime_type) then
return { "echo", "Binary file found. These files cannot be displayed!" }
end
end