sloane.sh/lib/filters/sidenotes.rb

30 lines
856 B
Ruby
Raw Permalink Normal View History

2025-01-03 15:20:36 -05:00
Nanoc::Filter.define(:sidenotes) do |content, params|
@footnotes = {}
# First pass: collect all footnote definitions
content.scan(/\[\^([^\]]+)\]:\s*(.+?)($|\n\n)/) do |ref, text, _|
@footnotes[ref] = text.strip
end
# Second pass: replace footnote references with sidenote markup
result = content.gsub(/\[\^([^\]]+)\](?!:)/) do |match|
ref = $1
next match unless @footnotes[ref] # Skip if no matching footnote definition
<<~HTML
<label for="sn-#{ref}"
class="margin-toggle sidenote-number">
</label>
<input type="checkbox"
id="sn-#{ref}"
class="margin-toggle"/>
<span class="sidenote">
#{@footnotes[ref]}
</span>
HTML
end
# Finally, remove the original footnote definitions
result.gsub(/\[\^([^\]]+)\]:\s*(.+?)($|\n\n)/, '')
end