Skip to content

Commit

Permalink
Merge pull request #158 from neo451/dev
Browse files Browse the repository at this point in the history
fix(ui): resolve links earlier for images
  • Loading branch information
neo451 authored Feb 22, 2025
2 parents bfd42ef + bd513db commit 0390a13
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 66 deletions.
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 @@ -82,6 +82,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 @@ -169,7 +170,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 @@ -245,7 +246,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.

0 comments on commit 0390a13

Please sign in to comment.