forked from claycarpenter/appsignal-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.rb
210 lines (187 loc) · 5.62 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
require "lib/appsignal_markdown"
# TODO: Fixed in middleman 4.3.x. Once it is released we upgrade and remove
# this silencer.
Haml::TempleEngine.disable_option_validator!
DOCS_ROOT = File.expand_path(File.dirname(__FILE__))
GITHUB_ROOT = "https://github.com/appsignal/appsignal-docs/tree/main"
Time.zone = "Amsterdam"
set :protocol, "https://"
set :host, "docs.appsignal.com"
set :layout, :article
set :markdown_engine, :redcarpet
set :markdown, AppsignalMarkdown::OPTIONS.merge(:renderer => AppsignalMarkdown)
set :haml, :attr_wrapper => %(")
set :css_dir, "assets/stylesheets"
set :js_dir, "assets/javascripts"
set :images_dir, "assets/images"
activate :syntax,
:line_numbers => true,
:css_class => "code-block"
activate :external_pipeline,
name: :webpack,
command: build? ? "yarn build" : "yarn dev",
source: ".tmp/dist",
latency: 1
helpers do
def description_of_page(page)
if page.data.description
page.data.description
else
auto_description(page)
end
end
# Return and around 200 character description of the given file.
# It ensures the description is complete lines.
def auto_description(page)
source = page.file_descriptor.read
# Remove YAML frontmatter
source = source.gsub(/^(---\s*\n.*?\n?)^(---\s*$\n?)/m, "")
renderer = Redcarpet::Markdown.new(AppsignalMarkdownStripDown)
content = renderer.render(source)
description = []
content.split("\n").each do |line|
description << line
if description.sum(&:length) > 200
break # Keep the description short
end
end
description.join(" ")
end
def canonical_url(path)
path = path.sub("index.html", "")
path = "/#{path}" unless path.start_with?("/")
"#{config[:protocol]}#{config[:host]}#{path}"
end
def title
if current_page.data.title
current_page.data.title.gsub(/<[^>]*>/, "").tap do |title|
unless current_page.data.title_no_brand
title << " | #{site_name}"
end
end
else
site_name
end
end
def site_name
"AppSignal documentation"
end
def section_active?(section_prefix)
current_url = current_page.url
if section_prefix == "/"
current_url == section_prefix
else
current_url.start_with?(section_prefix)
end
end
def page_active?(path)
path == current_page.url
end
def link_with_active(*args, &block)
if block_given?
path, options = args
else
name, path, options = args
end
options ||= {}
options[:class] = options[:class].to_s
options[:class] += " active" if path == current_page.url
if options.delete(:parent_active) && current_page.url.start_with?(path.sub(".html", "/"))
options[:class] += " active"
end
new_args =
if block_given?
[path, options]
else
[name, path, options]
end
link_to(
*new_args,
&block
)
end
def edit_link
page_path = current_page.source_file
link_to(
"Create a pull request",
page_path.gsub(DOCS_ROOT, GITHUB_ROOT),
:class => "c-button c-button--sm c-button--gray ml-2"
)
end
def markdown(content)
# Set the markdown renderer scope to current view object, so that it can
# call view helpers, instead of erroring on a `nil` scope object.
AppsignalMarkdown.scope = self if AppsignalMarkdown.scope.nil?
# https://github.com/hashicorp/middleman-hashicorp/blob/14c705614b2f97b5a78903f17b904f767c1fdbe2/lib/middleman-hashicorp/redcarpet.rb
Redcarpet::Markdown.new(AppsignalMarkdown, AppsignalMarkdown::OPTIONS).render(content)
end
def inline_markdown(content)
markdown(content).sub(/^<p>(.*)<\/p>$/, "\\1")
end
def option_value(key, option, integration)
option[integration][key] || option[key]
end
def link_for_option(option, integration)
config_key = option_value(:config_key, option, integration)
if config_key
link_to config_key, "#option-#{config_key}"
else
env_key = option_value(:env_key, option, integration)
link_to env_key, "#option-#{env_key.downcase}"
end
end
def options_for(integration)
integration = integration.to_s
data[:config_options][:options].select do |option|
option.key? integration
end
end
def option_type_format(option)
type = option[:type]
case type
when Array
type.map do |t|
option_type_value_format(t)
end.join(" / ")
when Hash
if type.key? "array"
option_type_value_format("array", type[:array])
else
option_type_value_format("list", type[:list])
end
else
option_type_value_format(type)
end
end
def option_type_value_format(type, sub_types = [])
case type
when "bool"
"Boolean (<code>true</code> / <code>false</code>)"
when "array"
sub_types_label = sub_types.map(&:humanize).join(", ")
type_label = sub_types.length > 1 ? "types" : "type"
content_tag :code, "Array<#{sub_types_label}>",
:title => "Array with values of #{type_label} #{sub_types_label}"
when "list"
sub_types_label = sub_types.map(&:humanize).join(", ")
type_label = sub_types.length > 1 ? "types" : "type"
content_tag :code, "list(#{sub_types_label})",
:title => "List with values of #{type_label} #{sub_types_label}"
when NilClass
"Error: Missing type!"
else
content_tag :code, type.humanize
end
end
def option_default_value(option)
default = option[:default_value]
case default
when Hash
inline_markdown default[:markdown]
when nil # null in the YAML file
"nil (This is unset by default)"
else
content_tag :code, default.to_s
end
end
end