Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ui): resolve links earlier for images #158

Merged
merged 1 commit into from
Feb 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions lua/feed/db/local.lua
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,20 @@ end
---@param k any
---@return function | feed.entry
function M:__index(k)
if not k then
return
end
local ms = rawget(M, k)
if ms then
return ms
else
local r = mem[k]
if not r then
r = Path.load(self.dir / "object" / k)
mem[k] = r
end
return r
end
local r = mem[k]
if not r then
r = Path.load(self.dir / "object" / k)
mem[k] = r
end
return r
end

---@param id string
Expand Down
2 changes: 1 addition & 1 deletion lua/feed/fetch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ function M.update_feed_co(url, opts)
entry.content = nil
local id = vim.fn.sha256(entry.link)
local fp = tostring(db.dir / "data" / id)
ut.save_file(fp, content)
db[id] = entry
ut.save_file(fp, content)
end

feeds[url] = feeds[url] or {}
Expand Down
6 changes: 4 additions & 2 deletions lua/feed/server/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,13 @@ return {
local state = require("feed.ui.state")
local Config = require("feed.config")

local entries = db:filter(state.query)
if not state.entries then
state.entries = db:filter(Config.search.default_query)
end

router:get("/", function(req, res)
local acc = {}
for _, id in ipairs(entries) do
for _, id in ipairs(state.entries) do
acc[#acc + 1] = router:render("entry", {
id = id,
title = db[id].title,
Expand Down
5 changes: 3 additions & 2 deletions lua/feed/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ local function image_attach(buf)
end

local body_transforms = {
require("feed.utils").resolve_urls,
require("feed.utils").remove_urls,
-- TODO: get rid of html headers and stuff
-- TODO: allow user
Expand Down Expand Up @@ -167,7 +168,7 @@ M.show_index = function()
wo = Config.options.index.wo,
bo = Config.options.index.bo,
keys = Config.keys.index,
zindex = 8,
zindex = 3,
})
end

Expand Down Expand Up @@ -243,7 +244,7 @@ local function show_entry(ctx)
keys = Config.keys.entry,
ft = "markdown",
zen = Config.zen.enabled,
zindex = 10,
zindex = 5,
})

if ctx.link then
Expand Down
19 changes: 19 additions & 0 deletions lua/feed/utils/url.lua
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,25 @@ M.remove_urls = function(body, id)
return body
end

---@param body string
---@param id string
---@return string
M.resolve_urls = function(body, id)
local base = require("feed.db")[id].link
local text_n_links = M.get_urls(body)
for _, v in ipairs(text_n_links) do
local link = v[2]
local resolved = link
if not M.looks_like_url(link) then
resolved = M.url_resolve(base, link)
end
if resolved and M.looks_like_url(resolved) and resolved ~= base then
body = string.gsub(body, escape_pattern(link), "(" .. resolved .. ")")
end
end
return body
end

---@param url string
---@param base string
M.resolve_and_open = function(url, base)
Expand Down
55 changes: 0 additions & 55 deletions tests/_test_ui.lua

This file was deleted.

Loading