面包屑链接在收集项中正常工作。但是,当我在主要父集合索引页面上时,得到以下信息:
当我已经在父级集合永久链接上时,作为父级获得了重复的网址。
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
我希望主要收藏的永久链接只是首页/页面标题。这是我定义集合的方式:
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
# Collections
collections:
practices:
output: true
permalink: /legal-practice/:title:output_ext
defense:
output: true
permalink: /legal-practice/criminal-defense/:title:output_ext
attorney:
output: true
permalink: /attorney/:title:output_ext
collections_dir: collections
此永久链接是嵌套的,并且是第一个集合中的一项。