summaryrefslogtreecommitdiffstats
path: root/plugins/footnote-postprocess.lua
blob: 5daaa6598b97b646402b8276a12653fe7de76bc0 (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
36
37
38
39
40
41
42
43
44
45
46
47
48
-- Turn `markdown-it-footnote` output into Tufte-compatible sidenotes.

footnotes = HTML.select(page, ".footnote-ref")

index, footnote = next(footnotes)

while index do
  ahref = HTML.select_one(footnote, "a")
  href = HTML.get_attribute(ahref, "href")
  href = Regex.replace(href, "^\#", "")
  footnote_contents = HTML.select_one(page, "li#" .. href)
  HTML.delete(HTML.select_one(footnote_contents, ".footnote-backref"))

  paragraphs = HTML.select(footnote_contents, "p")
  i, p = next(paragraphs)

  while i do
    HTML.set_tag_name(p, "span")
    HTML.add_class(p, "footnote-p")
    i, p = next(paragraphs, i)
  end

  footnote_contents = HTML.inner_html(footnote_contents)

  label = HTML.create_element("label")
  HTML.add_class(label, "margin-toggle")
  HTML.add_class(label, "sidenote-number")
  HTML.set_attribute(label, "for", href)
  HTML.insert_after(footnote, label)

  input = HTML.create_element("input")
  HTML.add_class(input, "margin-toggle")
  HTML.set_attribute(input, "type", "checkbox")
  HTML.set_attribute(input, "id", href)
  HTML.insert_after(label, input)

  contents_node = HTML.create_element("span")
  HTML.add_class(contents_node, "note")
  HTML.add_class(contents_node, "sidenote")
  HTML.append_child(contents_node, HTML.parse(footnote_contents))
  HTML.insert_after(input, contents_node)

  HTML.delete(footnote)
  index, footnote = next(footnotes, index)
end

HTML.delete(HTML.select_one(page, "hr.footnotes-sep"))
HTML.delete(HTML.select_one(page, "section.footnotes"))