Created
February 20, 2013 14:32
-
-
Save koriroys/4995929 to your computer and use it in GitHub Desktop.
place in your /plugins folder in your jekyll project to use.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Inspiration: Franklin Webber's page_url Jekyll plugin | |
# | |
# Usage: | |
# {% internal_link folder/filename %} | |
# note: do not include filename extension | |
# | |
# Example markdown usage: | |
# [Link to cool page]({% internal_link sessions/ruby_bootstrap %}) | |
# | |
module Jekyll | |
class Page | |
def filename | |
filename_without_extension = File.basename(name, File.extname(name)).strip | |
if @dir.empty? | |
filename_without_extension | |
else | |
File.join(@dir, filename_without_extension) | |
end | |
end | |
def internal_path | |
File.join(@dir, url) | |
end | |
end | |
class InternalLink < Liquid::Tag | |
def initialize(tag_name, filename, tokens) | |
super | |
@filename = filename.strip | |
end | |
def render(context) | |
context.registers[:site].pages.each do |page| | |
if @filename == page.filename || "/#{@filename}" == page.filename | |
return page.internal_path | |
end | |
end | |
'#' | |
end | |
end | |
end | |
Liquid::Template.register_tag('internal_link', Jekyll::InternalLink) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment