Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Lazy footnotes for Markdown, based on TidBits lazy link style
#!/usr/bin/env ruby
# encoding: utf-8
Marked 2 preprocessor - "Lazy" footnotes.
Allows use of `[^]` or `†` footnote references
where the next [^]: or †: note defines the text of the footnote.
† - Option-T
^ - Shift-6
Footnotes are given random prefixes to avoid duplication of ids within
a page.
Lorem ipsum dolor sit amet[^], consectetur adipisicing elit, sed do
eiusmod tempor† incididunt ut labore et dolore magna aliqua. Ut enim
ad minim veniam, quis nostrud exercitation[^] ullamco laboris nisi ut
aliquip ex ea commodo consequat.
[^]: This footnote will replace the first `[^]` found in the text.
†: This one will replace the first `†`. The syntaxes are auto-detected
and interchangeable.
[^]: This one replaces the second instance.
Inspired by [tidbits][^] and Carl Johnson†
# Marked sends the Markdown text to STDIN when calling the script.
if RUBY_VERSION.to_f > 1.8
input ='UTF-8')
input =
# random prefix and footnote counter
rand = rand(100000) + 100000
counter = 0
def replace_caret(input, prefix, counter)
input.sub(/(\[)\^(\].*?\[)\^\]:/m) {
def replace_cross(input, prefix, counter)
input.sub(/([^†]+)†(.*?)†:/m) {
while input =~ /(\[\^\].*?\[\^\]:|†.*?†:)/m
idx_caret = input.index(/\[\^\].*?\[\^\]:/m) || false
idx_cross = input.index(/†.*?†:/m) || false
break if !idx_caret && !idx_cross
if ((idx_caret && idx_cross && idx_caret < idx_cross) || (idx_caret && !idx_cross))
input = replace_caret(input, rand, counter += 1)
elsif (idx_cross)
input = replace_cross(input, rand, counter += 1)
print input
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment