~hristoast/hristoast

cc79fd0660d1b2bef89bacea34589f6c20fe1029 — Hristos N. Triantafillou 2 years ago 3025265
Use the updated section link highlight plugin
1 files changed, 18 insertions(+), 17 deletions(-)

M plugins/section-link-highlight.lua
M plugins/section-link-highlight.lua => plugins/section-link-highlight.lua +18 -17
@@ 6,9 6,9 @@
-- Sample configuration:
-- [plugins.active-link-hightlight]
--   active_link_class = "active"
--   selector = "nav"
--   nav_menu_selector = "nav"
--
-- Minimal soupault version: 1.3
-- Minimal soupault version: 1.6
-- Author: Daniil Baturin
-- License: MIT



@@ 17,35 17,36 @@ active_link_class = config["active_link_class"]
nav_menu_selector = config["selector"]

if (not active_link_class) then
  Log.warning("Missing required option \"active_link_class\", using default (\"active\")")
  Log.warning("active_link_class option is not set, using default (\"active\")")
  active_link_class = "active"
end

if (not nav_menu_selector) then
  Log.warning("Missing required option \"selector\", using default (\"nav\")")
  Log.warning("nav_menu_selector option is not set, using default (\"nav\")")
  nav_menu_selector = "nav"
end

menu = HTML.select_one(page, nav_menu_selector)
links = HTML.select(menu, "a")

-- For those who want to try it on Windows
page_file = Regex.replace_all(page_file, "\\\\", "/")

index, link = next(links)
while index do
  href = strlower(HTML.get_attribute(link, "href"))
  -- Don't highlight the main page
  -- That would also highlight all paths since they all have / in them
  if href ~= "/" then
    if Regex.match(page_file, Regex.replace_all(href, "/", "")) then
      HTML.add_class(link, active_link_class)
    end
  end

  -- Special handling for the blog, since it's at "/"
  if (href == "/") and (page_file == "site/index.html") then
     HTML.add_class(link, active_link_class)
  -- Remove leading and trailing slashes
  href = Regex.replace_all(href, "(\\/?$|^\\/)", "")
  page_url = Regex.replace_all(page_url, "(\\/?$|^\\/)", "")

  -- Normalize slashes
  href = Regex.replace_all(href, "\\/+", "\\/")

  -- Edge case: the / link that becomes "" after normalization
  -- Anything would match the empty string and higlight all links,
  -- so we handle this case explicitly
  if ((page_url == "") and (href == ""))
    or ((href ~= "") and Regex.match(page_url, "^" .. href))
  then
    HTML.add_class(link, active_link_class)
  end

  index, link = next(links, index)