main
 1-- tag-pills.lua — Transform org heading tags into styled clickable pills
 2--
 3-- Before: <span class="tag"><span class="bun">bun</span>&nbsp;<span class="til">til</span></span>
 4-- After:  <span class="tags"><a class="tag" href="/flux/tags/bun.html">bun</a><a class="tag" href="/flux/tags/til.html">til</a></span>
 5
 6tag_containers = HTML.select(page, "span.tag")
 7i = 1
 8while tag_containers[i] do
 9  container = tag_containers[i]
10
11  -- Collect tag names from child <span> elements (skip text nodes)
12  children = HTML.select(container, "span")
13  tag_names = {}
14  count = 0
15  j = 1
16  while children[j] do
17    tag_name = HTML.inner_text(children[j])
18    cleaned = Regex.replace_all(tag_name, "\\s+", "")
19    if cleaned ~= "" then
20      count = count + 1
21      tag_names[count] = cleaned
22    end
23    j = j + 1
24  end
25
26  -- Replace container content with linked pills
27  HTML.delete_content(container)
28  HTML.set_attribute(container, "class", "tags")
29
30  j = 1
31  while j <= count do
32    pill = HTML.create_element("a", tag_names[j])
33    HTML.set_attribute(pill, "class", "tag")
34    HTML.set_attribute(pill, "href", "/flux/tags/" .. tag_names[j] .. ".html")
35    HTML.append_child(container, pill)
36    j = j + 1
37  end
38
39  i = i + 1
40end
41
42-- Also strip the &nbsp; before the tags container in headings
43-- Org inserts &nbsp;&nbsp;&nbsp; between heading text and tags
44headings = HTML.select(page, "h2")
45j = 1
46while headings[j] do
47  h = headings[j]
48  raw = HTML.inner_html(h)
49  -- Remove nbsp sequences before the tags span
50  cleaned = Regex.replace_all(raw, "&nbsp;", "")
51  if cleaned ~= raw then
52    HTML.replace_content(h, HTML.parse(cleaned))
53  end
54  j = j + 1
55end