30 lines
856 B
Ruby
30 lines
856 B
Ruby
|
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
|