Skip to content

Instantly share code, notes, and snippets.

@bradgessler bradgessler/doc.erb.bd
Last active Dec 16, 2015

Embed
What would you like to do?
Sketch for a Tilt/Markdown extension that makes it a little easier to add structure to markdown after its rendered.
# How to tie your shoes
<%= toc.each do |id, text| %>
* [<%= text %>](#<%= id %>)
<% end %>
## Unlace them
Gotta make sure its untied before you tie it!
## Eat a piece of pizza
This is all I know how to do.
## Tie the shoes
I really don't know how to break this down.
## Celebrate!
Yay! Lets party!
ThrowDown.new do
extension '.braddown', '.bd'
# Before this code gets hit, the base markdown renders. Next we scan
# through the doc's H tags and drop an id into each of them based on the
# H tag text.
transform do
doc.find('h1, h2, h3, h4, h5, h6').each { |h| h[:id] = h.text.downcase.gsub(' ', '_') }
end
# Now we're in a spot where we can extract this H tag into some useful data that
# we might want to access from inside the view itself (like a TOC) or from outside
# in something like a pager (maybe we break apart by H1 tags).
extract :toc do
doc.find('h1, h2, h3, h4, h5, h6').map{ |h| [ h[:id], h.text ] }
end
# This is just for fun... but really its useful... running MD images through
# sprockets. This is how its done folks! (Not sure if Sprockets.image_path works,
# but you get the idea)
transform do
doc.find('img[src]').each { |img| img[:src] = Sprockets.image_path(img[:src]) }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.