Skip to content

Instantly share code, notes, and snippets.

@Balder1840
Last active May 27, 2022 05:49
Show Gist options
  • Save Balder1840/2ce0b73c37e1abbde4cad8b4d4623546 to your computer and use it in GitHub Desktop.
Save Balder1840/2ce0b73c37e1abbde4cad8b4d4623546 to your computer and use it in GitHub Desktop.
Gollum with authentication and NiceTOC, as well as a collapsed sidebar
# To register this macro into your Gollum wiki, I recommend adding it to your
# existing `--config` file. If you don't have a configuration, you can create a
# new one at the root of your wiki and then start Gollum with:
#
# --config your-config-file.rb
#
# This macro provides a list of links to all of the pages in your wiki (except
# for the current page). It can handle any amount of nested subdirectories.
module Gollum
class Macro
class NiceTOC < Gollum::Macro
def render
return if @wiki.pages.size.zero?
@nest_dir=[]
@current_page_dir = ''
if is_sidebar
@current_page_dir = Pathname(@page.parent_page.path).dirname.to_s
@current_page_dir += '/'
end
content_tag("ul") {
wiki.map { |name, page|
@nest_dir.clear
list_item(name, page)
}.compact.join
}
end
private
def is_sidebar()
@page.path.start_with?('_Sidebar')
end
def add(subtree, filename_parts, gollum_page)
head, *tail = filename_parts
subtree[head] ||= {}
if tail.empty?
subtree[head] = gollum_page
else
add(subtree[head], tail, gollum_page)
end
subtree
end
def content_tag(html_tag_name, options = {}, &block)
tag = [html_tag_name]
[:class, :href].each do |option|
tag << "#{option.to_s}=\"#{options[option]}\"" if options[option]
end
content = ""
content += "<#{tag.compact.join(" ")}>"
content += (yield block).to_s.gsub(/\n/, "")
content += "</#{html_tag_name}>"
content
end
def list_item(name, gollum_page)
if gollum_page.is_a?(::Gollum::Page)
return if @page.url_path == gollum_page.url_path
_current_page_class = is_sidebar && @page.parent_page.url_path == gollum_page.url_path ? "current-page" : ""
content_tag("li", class: _current_page_class ) {
content_tag("a", href:"/"+ gollum_page.url_path) {
gollum_page.metadata_title || gollum_page.name
}
}
else
_dir_class = ''
_side_bar_dir_title = ""
if is_sidebar
@nest_dir << name
_nest_dir_str = @nest_dir.join('/') + "/"
_dir_class = "sidebar-dir "
_side_bar_dir_title = "sidebar-dir-title"
if !@current_page_dir.start_with?(_nest_dir_str)
_dir_class += " collapsed "
end
end
content_tag("li", class: _dir_class) {
content_tag("span", class: _side_bar_dir_title) {
content_tag("span") { name }
} +
content_tag("ul") {
_temp = gollum_page.map { |nom, page| list_item(nom, page) }.join
if is_sidebar
@nest_dir.pop
end
_temp
}
}
end
end
def wiki
@wiki.pages.reduce({}) { |tree, gollum_page|
add(
tree,
Pathname(gollum_page.path).each_filename.to_a,
gollum_page
)
}
end
end
end
end
wiki_options = {
# universal_toc: true,
# index_page: "Index",
mathjax: true,
css: true,
js: true,
sidebar: :left
# local_time: true,
show_local_time: true,
# static: true
}
Precious::App.set(:environment, :production)
#Precious::App.set(:default_markup, :markdown) # set your favorite markup language
Precious::App.set(:wiki_options, wiki_options)
#!/usr/bin/env ruby
require 'rubygems'
require 'gollum/auth' # Don't forget to load the gem!
require 'gollum/app'
module Gollum
class Macro
class NiceTOC < Gollum::Macro
def render
return if @wiki.pages.size.zero?
@nest_dir=[]
@current_page_dir = ''
if is_sidebar
@current_page_dir = Pathname(@page.parent_page.path).dirname.to_s
@current_page_dir += '/'
end
content_tag("ul") {
wiki.map { |name, page|
@nest_dir.clear
list_item(name, page)
}.compact.join
}
end
private
def is_sidebar()
@page.path.start_with?('_Sidebar')
end
def add(subtree, filename_parts, gollum_page)
head, *tail = filename_parts
subtree[head] ||= {}
if tail.empty?
subtree[head] = gollum_page
else
add(subtree[head], tail, gollum_page)
end
subtree
end
def content_tag(html_tag_name, options = {}, &block)
tag = [html_tag_name]
[:class, :href].each do |option|
tag << "#{option.to_s}=\"#{options[option]}\"" if options[option]
end
content = ""
content += "<#{tag.compact.join(" ")}>"
content += (yield block).to_s.gsub(/\n/, "")
content += "</#{html_tag_name}>"
content
end
def list_item(name, gollum_page)
if gollum_page.is_a?(::Gollum::Page)
return if @page.url_path == gollum_page.url_path
_current_page_class = is_sidebar && @page.parent_page.url_path == gollum_page.url_path ? "current-page" : ""
content_tag("li", class: _current_page_class ) {
content_tag("a", href:"/"+ gollum_page.url_path) {
gollum_page.metadata_title || gollum_page.name
}
}
else
_dir_class = ''
_side_bar_dir_title = ""
if is_sidebar
@nest_dir << name
_nest_dir_str = @nest_dir.join('/') + "/"
_dir_class = "sidebar-dir "
_side_bar_dir_title = "sidebar-dir-title"
if !@current_page_dir.start_with?(_nest_dir_str)
_dir_class += " collapsed "
end
end
content_tag("li", class: _dir_class) {
content_tag("span", class: _side_bar_dir_title) {
content_tag("span") { name }
} +
content_tag("ul") {
_temp = gollum_page.map { |nom, page| list_item(nom, page) }.join
if is_sidebar
@nest_dir.pop
end
_temp
}
}
end
end
def wiki
@wiki.pages.reduce({}) { |tree, gollum_page|
add(
tree,
Pathname(gollum_page.path).each_filename.to_a,
gollum_page
)
}
end
end
end
end
users = YAML.load %q{
---
- username: rick
password: asdf754&1129-@lUZw
name: Rick Sanchez
email: [email protected]
- username: morty
password_digest: 5994471abb01112afcc18159f6cc74b4f511b99806da59b3caf5a9c173cacfc5
name: Morty Smith
email: [email protected]
}
# Allow unauthenticated users to read the wiki (disabled by default).
options = { allow_unauthenticated_readonly: true }
# Allow only authenticated users to change the wiki.
# (NOTE: This must be loaded *before* Precious::App!)
use Gollum::Auth, users, options
wiki_options = {
# universal_toc: true,
# index_page: "Index",
mathjax: true,
css: true,
js: true,
sidebar: :left
# local_time: true,
show_local_time: true,
# static: true
}
# gollum_path = File.expand_path(File.join(File.dirname(__FILE__), 'wiki.git')) # CHANGE THIS TO POINT TO YOUR OWN WIKI REPO
Precious::App.set(:gollum_path, '/home/pi/wiki/.git')
Precious::App.set(:environment, :production)
#Precious::App.set(:environment, ENV.fetch('RACK_ENV', :production).to_sym)
#Precious::App.set(:default_markup, :markdown) # set your favorite markup language
Precious::App.set(:wiki_options, wiki_options)
run Precious::App
.main-content > .markdown-body {
padding-top: 0;
padding-left: 10px;
}
.main-content > .markdown-body > p[dir='auto']:first-child:empty {
margin-bottom: 0 !important;
}
#sidebar-content ul {
list-style: none;
}
#sidebar-content ul ul {
padding-left: 15px;
}
#sidebar-content .current-page a {
text-decoration: underline;
color: #0366d6;
}
#sidebar-content li.sidebar-dir.collapsed ul {
display:none;
}
#sidebar-content li.sidebar-dir .sidebar-dir-title {
cursor: pointer;
position: relative;
}
#sidebar-content li.sidebar-dir .sidebar-dir-title:before {
content: '\25BC'; /*Black Down-pointing Triangle ▼*/
position: absolute;
display: inline-block;
left: -16px;
font-size: 14px;
font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif,"Apple Color Emoji"
}
#sidebar-content li.sidebar-dir.collapsed .sidebar-dir-title:before {
content: '\25B6'; /*Black Right-pointing Triangle ▶*/
}
.task-list-item-checkbox {
margin-right: 3px;
vertical-align: middle;
}
a.reversefootnote[role='doc-backlink'] {
font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif,"Apple Color Emoji"
}
$(document).ready(function () {
$("#sidebar-content li.sidebar-dir > span.sidebar-dir-title").click(function(){
$(this).parent().toggleClass("collapsed")
})
})
# To register this macro into your Gollum wiki, I recommend adding it to your
# existing `--config` file. If you don't have a configuration, you can create a
# new one at the root of your wiki and then start Gollum with:
#
# --config your-config-file.rb
#
# This macro provides a list of links to all of the pages in your wiki (except
# for the current page). It can handle any amount of nested subdirectories.
module Gollum
class Macro
class NiceTOC < Gollum::Macro
def render
return if @wiki.pages.size.zero?
content_tag("ul") {
wiki.map { |name, page| list_item(name, page) }.compact.join
}
end
private
def add(subtree, filename_parts, gollum_page)
head, *tail = filename_parts
subtree[head] ||= {}
if tail.empty?
subtree[head] = gollum_page
else
add(subtree[head], tail, gollum_page)
end
subtree
end
def content_tag(html_tag_name, options = {}, &block)
tag = [html_tag_name]
[:class, :href].each do |option|
tag << "#{option.to_s}=\"#{options[option]}\"" if options[option]
end
content = ""
content += "<#{tag.compact.join(" ")}>"
content += (yield block).to_s.gsub(/\n/, "")
content += "</#{html_tag_name}>"
content
end
def list_item(name, gollum_page)
if gollum_page.is_a?(::Gollum::Page)
return if @page.url_path == gollum_page.url_path
content_tag("li") {
content_tag("a", href: gollum_page.url_path) {
gollum_page.metadata_title || gollum_page.name
}
}
else
content_tag("li") {
content_tag("span") { name } +
content_tag("ul") {
gollum_page.map { |nom, page| list_item(nom, page) }.join
}
}
end
end
def wiki
@wiki.pages.reduce({}) { |tree, gollum_page|
add(
tree,
Pathname(gollum_page.path).each_filename.to_a,
gollum_page
)
}
end
end
end
end
@Balder1840
Copy link
Author

Create _Sidebar.md and put <<NiceTOC()>> in it

@Balder1840
Copy link
Author

Balder1840 commented May 18, 2022

  • Option 1, without Auth:
    gollum --config config.rb

  • Option 2, with Auth:
    rackup config.ru --host 0.0.0.0 --port 4567, it can with -d to run in background

remember to put custom.js & custom.css in the git and commit.

@Balder1840
Copy link
Author

refers to the github repo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment