~hristoast/hristoast

ref: ed4e35da340817fa5769040737545ec80f6d37ba hristoast/plugins/source-link.lua -rw-r--r-- 1.1 KiB
ed4e35daHristos N. Triantafillou Create and use the source link plugin 1 year, 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
-- Creates a "source link" from a given repo base and injects the link into the page.
--
-- Minimal soupault version: 1.3
-- Author: Hristos N. Triantafillou
-- License: MIT

link_text = config["link_text"]
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\")")
  link_text = "This page's source code"
end

if (not selector) then
  Log.warning("Missing required option \"selector\", using default (\"div#source-link\")")
  selector = "div#source-link"
end

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

-- Remove trailing slashes
repo_base = Regex.replace(repo_base, "\\/?$", "")

source_link = 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")

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