Initial commit
This commit is contained in:
82
_plugins/breadcrumbs.rb
Normal file
82
_plugins/breadcrumbs.rb
Normal file
@@ -0,0 +1,82 @@
|
||||
require_relative 'drops/breadcrumb_item.rb'
|
||||
|
||||
module Jekyll
|
||||
module Breadcrumbs
|
||||
@@config = {}
|
||||
@@siteAddress = ""
|
||||
@@sideAddresses = {}
|
||||
|
||||
def self.clearAddressCache
|
||||
@@sideAddresses = {}
|
||||
end
|
||||
|
||||
def self.loadAddressCache(site)
|
||||
clearAddressCache
|
||||
site.documents.each { |page| addAddressItem(page.url, page['crumbtitle'] || page['title'] || '') } # collection files including posts
|
||||
site.pages.each { |page| addAddressItem(page.url, page['crumbtitle'] || page['title'] || '') } # pages
|
||||
site.posts.docs.each { |page| addAddressItem(page.url, page['crumbtitle'] || page['title'] || '') } # posts
|
||||
end
|
||||
|
||||
def self.addAddressItem(url, title)
|
||||
key = createAddressCacheKey(url)
|
||||
@@sideAddresses[key] = {:url => url, :title => title}
|
||||
end
|
||||
|
||||
def self.findAddressItem(path)
|
||||
key = createAddressCacheKey(path)
|
||||
@@sideAddresses[key] if key
|
||||
end
|
||||
|
||||
def self.createAddressCacheKey(path)
|
||||
path.chomp("/").empty? ? "/" : path.chomp("/")
|
||||
end
|
||||
|
||||
def self.buildSideBreadcrumbs(side, payload)
|
||||
payload["breadcrumbs"] = []
|
||||
return if side.url == @@siteAddress && root_hide === true
|
||||
|
||||
drop = Jekyll::Drops::BreadcrumbItem
|
||||
position = 0
|
||||
|
||||
path = side.url.chomp("/").split(/(?=\/)/)
|
||||
-1.upto(path.size - 1) do |int|
|
||||
joined_path = int == -1 ? "" : path[0..int].join
|
||||
item = findAddressItem(joined_path)
|
||||
if item
|
||||
position += 1
|
||||
item[:position] = position
|
||||
item[:root_image] = root_image
|
||||
payload["breadcrumbs"] << drop.new(item)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Config
|
||||
def self.loadConfig(site)
|
||||
config = site.config["breadcrumbs"] || {"root" => {"hide" => false, "image" => false}}
|
||||
root = config["root"]
|
||||
@@config[:root_hide] = root["hide"] || false
|
||||
@@config[:root_image] = root["image"] || false
|
||||
|
||||
@@siteAddress = site.config["baseurl"] || "/"
|
||||
@@siteAddress = "/" if @@siteAddress.empty?
|
||||
end
|
||||
|
||||
def self.root_hide
|
||||
@@config[:root_hide]
|
||||
end
|
||||
|
||||
def self.root_image
|
||||
@@config[:root_image]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Jekyll::Hooks.register :site, :pre_render do |site, payload|
|
||||
Jekyll::Breadcrumbs::loadConfig(site)
|
||||
Jekyll::Breadcrumbs::loadAddressCache(site)
|
||||
end
|
||||
|
||||
Jekyll::Hooks.register [:pages, :documents], :pre_render do |side, payload|
|
||||
Jekyll::Breadcrumbs::buildSideBreadcrumbs(side, payload)
|
||||
end
|
||||
29
_plugins/drops/breadcrumb_item.rb
Normal file
29
_plugins/drops/breadcrumb_item.rb
Normal file
@@ -0,0 +1,29 @@
|
||||
module Jekyll
|
||||
module Drops
|
||||
class BreadcrumbItem < Liquid::Drop
|
||||
extend Forwardable
|
||||
|
||||
def initialize(side)
|
||||
@side = side
|
||||
end
|
||||
|
||||
def position
|
||||
@side[:position]
|
||||
end
|
||||
|
||||
def title
|
||||
@side[:title]
|
||||
end
|
||||
|
||||
def url
|
||||
@side[:url]
|
||||
end
|
||||
|
||||
def rootimage
|
||||
@side[:root_image]
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
9
_plugins/file_filters.rb
Normal file
9
_plugins/file_filters.rb
Normal file
@@ -0,0 +1,9 @@
|
||||
module Jekyll
|
||||
module FileFilters
|
||||
def file_mtime(input_url)
|
||||
File.mtime(input_url)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Liquid::Template.register_filter(Jekyll::FileFilters)
|
||||
81
_plugins/relative_path_filters.rb
Normal file
81
_plugins/relative_path_filters.rb
Normal file
@@ -0,0 +1,81 @@
|
||||
module Jekyll
|
||||
module RelativePathFilter
|
||||
def to_relative_path(input_url)
|
||||
site = @context.registers[:site]
|
||||
page = @context.registers[:page]
|
||||
|
||||
relative_filter_config = site.config["RelativePathFilter"] || {}
|
||||
enable_this_filter = relative_filter_config["enabled"]
|
||||
|
||||
unless enable_this_filter
|
||||
# Use Jekyll's to_relative_path for absolute paths, leave relative paths as-is
|
||||
if input_url.start_with?('/')
|
||||
return relative_url(input_url)
|
||||
else
|
||||
input_url
|
||||
end
|
||||
end
|
||||
|
||||
# Use our custom logic for all URLs
|
||||
|
||||
# Check if this is an index page
|
||||
is_index_page = page['name'].start_with?('index.')
|
||||
from_path = page['url']
|
||||
|
||||
# If it's an page file and Page URL end with slash (pretty permalink),
|
||||
# Make the path to include 'index.html' for correct relative path calculation
|
||||
if is_index_page && from_path.end_with?('/')
|
||||
from_path = from_path + 'index.html'
|
||||
end
|
||||
|
||||
# If the input_url is a directory (ends with '/'), append 'index.html' too
|
||||
if input_url.end_with?('/')
|
||||
input_url = input_url + 'index.html'
|
||||
end
|
||||
|
||||
if input_url.start_with?('/')
|
||||
calculate_relative_path(from_path, input_url)
|
||||
else
|
||||
input_url
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def calculate_relative_path(from_path, to_path)
|
||||
# Normalize the from_path
|
||||
from_path = "/" + from_path unless from_path.start_with?('/')
|
||||
|
||||
# Handle directory indexes (trailing slashes)
|
||||
if from_path.end_with?('/')
|
||||
from_dir = from_path
|
||||
else
|
||||
from_dir = File.dirname(from_path)
|
||||
end
|
||||
|
||||
# Ensure from_dir ends with '/' for consistent processing
|
||||
from_dir += '/' unless from_dir.end_with?('/')
|
||||
|
||||
# Count directory levels
|
||||
if from_dir == '/' || from_dir == './'
|
||||
up_levels = 0
|
||||
else
|
||||
clean_dir = from_dir.gsub(/^\//, '').gsub(/\/$/, '')
|
||||
up_levels = clean_dir.split('/').size
|
||||
end
|
||||
|
||||
# Remove leading slash from target
|
||||
to_path = to_path[1..-1] if to_path.start_with?('/')
|
||||
|
||||
# Build the path
|
||||
if up_levels == 0
|
||||
to_path
|
||||
else
|
||||
'../' * up_levels + to_path
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Liquid::Template.register_filter(Jekyll::RelativePathFilter)
|
||||
9
_plugins/string_filters.rb
Normal file
9
_plugins/string_filters.rb
Normal file
@@ -0,0 +1,9 @@
|
||||
module Jekyll
|
||||
module StringFilter
|
||||
def endswith(text, query)
|
||||
return text.end_with? query
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Liquid::Template.register_filter(Jekyll::StringFilter)
|
||||
Reference in New Issue
Block a user