-
Notifications
You must be signed in to change notification settings - Fork 32
/
config.rb
99 lines (75 loc) · 2.24 KB
/
config.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
require 'bundler'
Bundler.require
# https://stackoverflow.com/a/71994319
require 'uri'
def URI.escape(url)
url
end
set :markdown_engine, :redcarpet
set :markdown, layout_engine: :erb,
fenced_code_blocks: true,
lax_html_blocks: true,
with_toc_data: true
activate :syntax
activate :blog do |blog|
blog.prefix = "blog"
end
activate :directory_indexes
set :images_dir, 'images'
page "/docs/*", :layout => "docs"
page "/blog/*", :layout => :blog
configure :build do
activate :minify_css
end
helpers do
def body_for(resource)
html = resource.render layout: nil
Nokogiri::HTML::DocumentFragment.parse html
end
def javascript_include_tag(name, options = {})
options.merge!(onload: Opal::Sprockets.load_asset(name) + ";console.log('loaded #{name}')")
super(sprockets_path(name + ".js") || name, options)
end
def stylesheet_link_tag(name, options = {})
super(sprockets_path(name + ".css") || name, options)
end
def sprockets_path(name)
assets_path = Dir["#{__dir__}/source/assets/.*.json"].first
assets = File.exist?(assets_path) ? JSON.parse(File.read(assets_path)) : {}
asset = assets.dig("assets", name)
asset ? "/assets/#{asset}" : nil
end
def table_of_contents(resource)
body = body_for resource
content = []
in_list = false
headings = body.css('h2,h3')
headings.each_with_index do |h, idx|
subheading = h.name == 'h3'
if subheading and !in_list
in_list = true
content << %Q{\n<ul class="subchapter">}
elsif !subheading and in_list
in_list = false
content << "\n</ul>"
elsif subheading
in_list = true
end
li_class = subheading ? 'toc-list-subchapter' : 'toc-list-chapter'
content << %Q{\n<li class="#{li_class}">}
content << content_tag(:a, h.text, href: '#' + h[:id].to_s)
unless headings[idx + 1] && headings[idx + 1].name == 'h3'
content << '</li>'
end
end
content << "\n</ul></li>" if in_list
%Q{<ul class="nav opal-sidebar">#{content.join}</ul>}
end
end
ignore '*.sass'
ignore '*.scss'
activate(:external_pipeline,
name: :sprockets,
command: "bin/build-sprockets #{'--watch' unless build?}",
source: "assets/",
)