summaryrefslogtreecommitdiffstats
path: root/plugins/fix_urls.lua
blob: 18d059bd3ae9fdfca6ae69ad53d38e4d16ae23b7 (plain)
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
function prefix_urls (links, attr, prefix_url)
  index, link = next(links)

  while index do
    href = HTML.get_attribute(link, attr)

    if href then
      -- remove prefix sometimes introduced by org
      href = Regex.replace(href, "^file://", "")

      -- Check if URL starts with a leading "/"
      if Regex.match(href, "^/") then
        href = Regex.replace(href, "^/*", "")
        href = prefix_url .. href
        HTML.set_attribute(link, attr, href)
      end
    end
    index, link = next(links, index)
  end
end

prefix_url = config["prefix_url"]

if not prefix_url then
  prefix_url = ""
end

if not Regex.match(prefix_url, "(.*)/$") then
  prefix_url = prefix_url .. "/"
end

prefix_urls(HTML.select(page, "a"), "href", prefix_url)
prefix_urls(HTML.select(page, "link"), "href", prefix_url)
prefix_urls(HTML.select(page, "img"), "src", prefix_url)
prefix_urls(HTML.select(page, "script"), "src", prefix_url)