-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #163 from 3rd/zuloo-html-css
add html & css (by @zuloo)
- Loading branch information
Showing
4 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
local document = require("image/utils/document") | ||
return document.create_document_integration({ | ||
name = "css", | ||
-- debug = true, | ||
default_options = { | ||
clear_in_insert_mode = false, | ||
download_remote_images = true, | ||
only_render_image_at_cursor = false, | ||
filetypes = { "css", "sass", "scss" }, | ||
}, | ||
query_buffer_images = function(buffer) | ||
local buf = buffer or vim.api.nvim_get_current_buf() | ||
|
||
local parser = vim.treesitter.get_parser(buf, "css") | ||
local root = parser:parse()[1]:root() | ||
local query = vim.treesitter.query.parse( | ||
"css", | ||
'(call_expression (function_name) @name (#eq? @name "url"))' | ||
) | ||
|
||
local images = {} | ||
|
||
---@diagnostic disable-next-line: missing-parameter | ||
for id, node in query:iter_captures(root, 0) do | ||
local capture = query.captures[id] | ||
|
||
if capture == "name" then | ||
---@diagnostic disable-next-line: unused-local | ||
local start_row, start_col, end_row, end_col = node:range() | ||
local line = vim.api.nvim_buf_get_lines( | ||
buf, | ||
end_row, | ||
end_row + 1, | ||
false | ||
)[1] | ||
|
||
local path = line:sub(start_col):gsub(".*url%([\"'](.-)[\"']%).*$", "%1") | ||
|
||
-- search for path relative to webroot | ||
if path:sub(1,1) == "/" then | ||
path = vim.fs.find(path:sub(2), { | ||
upward = true, | ||
path = vim.fs.dirname(vim.api.nvim_buf_get_name(0)), | ||
})[1] | ||
end | ||
|
||
if path ~= nil then | ||
table.insert(images, { | ||
node = node, | ||
range = { | ||
start_row = start_row, | ||
start_col = start_col, | ||
end_row = end_row, | ||
end_col = end_col, | ||
}, | ||
url = path, | ||
}) | ||
end | ||
end | ||
end | ||
|
||
return images | ||
end | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
local document = require("image/utils/document") | ||
return document.create_document_integration({ | ||
name = "html", | ||
-- debug = true, | ||
default_options = { | ||
clear_in_insert_mode = false, | ||
download_remote_images = true, | ||
only_render_image_at_cursor = false, | ||
filetypes = { "html", "xhtml", "htm" }, | ||
}, | ||
query_buffer_images = function(buffer) | ||
local buf = buffer or vim.api.nvim_get_current_buf() | ||
|
||
local parser = vim.treesitter.get_parser(buf, "html") | ||
local root = parser:parse()[1]:root() | ||
local query = vim.treesitter.query.parse( | ||
"html", | ||
'(attribute (attribute_name) @name (#eq? @name "src")' | ||
.. ' (quoted_attribute_value))' | ||
) | ||
|
||
local images = {} | ||
|
||
---@diagnostic disable-next-line: missing-parameter | ||
for id, node in query:iter_captures(root, 0) do | ||
local capture = query.captures[id] | ||
|
||
if capture == "name" then | ||
---@diagnostic disable-next-line: unused-local | ||
local start_row, start_col, end_row, end_col = node:range() | ||
local line = vim.api.nvim_buf_get_lines( | ||
buf, | ||
end_row, | ||
end_row + 1, | ||
false | ||
)[1] | ||
|
||
local path = line:sub(start_col):gsub(".*src=[\"'](.-)[\"'].*$", "%1") | ||
|
||
-- search for path relative to webroot | ||
if path:sub(1,1) == "/" then | ||
path = vim.fs.find(path:sub(2), { | ||
upward = true, | ||
path = vim.fs.dirname(vim.api.nvim_buf_get_name(0)), | ||
})[1] | ||
end | ||
|
||
if path ~= nil then | ||
table.insert(images, { | ||
node = node, | ||
range = { | ||
start_row = start_row, | ||
start_col = start_col, | ||
end_row = end_row, | ||
end_col = end_col, | ||
}, | ||
url = path, | ||
}) | ||
end | ||
end | ||
end | ||
|
||
return images | ||
end | ||
}) |