~hristoast/hristoast

90a4c77fe258c707271bde696bf223543692f3b3 — Daniil Baturin 2 years ago fed2dda
Fix source link plugin behaviour in the case when target element already has links in it.

The original implementation would look up the first <a> child
rather than the <a> element created for the page source link,
so it would modify the href= attribute of a wrong link
and leave the right link inactive.

Also, remove the default of the repo_base option and do nothing if it's missing.

Signed-off-by: Hristos N. Triantafillou <me@hristos.triantafillou.us>
1 files changed, 13 insertions(+), 13 deletions(-)

M plugins/source-link.lua
M plugins/source-link.lua => plugins/source-link.lua +13 -13
@@ 9,7 9,7 @@ selector = config["selector"]
repo_base = config["repo_base"]

if (not link_text) then
  Log.warning("Missing required option \"link_text\", using default (\"This page's source code\")")
  Log.warning("Missing option \"link_text\", using default (\"This page's source code\")")
  link_text = "This page's source code"
end



@@ 19,19 19,19 @@ if (not selector) then
end

if (not repo_base) then
  Log.warning("Missing required option \"repo_base\", using default (\"https://git.sr.ht/~yourname/your-repo/tree/cool-branch/\")")
  repo_base = "https://git.sr.ht/~yourname/your-repo/tree/cool-branch/"
end

-- Remove trailing slashes
repo_base = Regex.replace(repo_base, "\\/?$", "")
  Log.warning("Missing required option \"repo_base\"")
  -- Nothing to do
else
  -- Remove trailing slashes
  repo_base = Regex.replace(repo_base, "\\/?$", "")

source_link = HTML.select_one(page, selector)
  source_link_container = HTML.select_one(page, selector)

if (source_link) then
   HTML.append_child(source_link, HTML.create_element("a", link_text))
   link = HTML.select_one(source_link, "a")
  if (source_link_container) then
    url = repo_base .. "/" .. page_file
    source_link = HTML.create_element("a", link_text)
    HTML.set_attribute(source_link, "href", url)

   url = repo_base .. "/" .. page_file
   HTML.set_attribute(link, "href", url)
    HTML.append_child(source_link_container, source_link)
  end
end