forked from claycarpenter/appsignal-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
appsignal_markdown.rb
118 lines (106 loc) · 3.89 KB
/
appsignal_markdown.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
# From: https://github.com/hashicorp/middleman-hashicorp/blob/master/lib/middleman-hashicorp/redcarpet.rb
require "redcarpet"
require "redcarpet/render_strip"
require "middleman-core"
require "middleman-core/renderers/redcarpet"
require "active_support/core_ext/module/attribute_accessors"
class AppsignalMarkdown < Middleman::Renderers::MiddlemanRedcarpetHTML
# Make a small wrapper module around a/some Padrino formatting helpers
# This is not included in the AppsignalMarkdown class to prevent accidental
# overriding of methods.
module FormatHelpersWrapper
include Padrino::Helpers::FormatHelpers
module_function :strip_tags
end
OPTIONS = {
:autolink => true,
:fenced_code_blocks => true,
:no_intra_emphasis => true,
:strikethrough => true,
:tables => true,
}.freeze
# Initialize with correct config.
# Does not get config from `set :markdown` from `config.rb`
def initialize(options = {})
super(options.merge(OPTIONS))
end
# Parse contents of every paragraph for custom tags and render paragraph.
def paragraph(text)
add_custom_tags("<p>#{text.strip}</p>\n")
end
# Add anchor tags to every heading.
# Create a link from the heading.
#
# Extra logic added:
# - Adds an invisible `span` element that is moved up on the page and acts as
# an anchor. This makes sure the page header doesn't hide the title once
# scrolled to the position on the page.
# - Anchor prefix: Start a heading with a caret symbol to prefix the
# heading's anchor id. `##^prefix My heading` becomes `#prefix-my-heading`.
# - Anchor override: Start a heading with an equals symbol to override the
# heading's anchor id. `##=my-anchor My heading` becomes `#my-anchor`.
# - Strips out any html tags from titles so that they don't get included in
# the generated anchors.
#
# @example
# <!-- Markdown input -->
# ## My heading
# <!-- HTML output -->
# <h2><span class="anchor" id="my-heading"></span><a href="#my-heading">My heading</a></h2>
#
# @example with a anchor prefix
# <!-- Markdown input -->
# ##^my-prefix My heading
# <!-- HTML output -->
# <h2><span class="anchor" id="my-prefix-my-heading"></span><a href="#my-prefix-my-heading">My heading</a></h2>
#
# @example with a anchor override
# <!-- Markdown input -->
# ##=my-anchor My heading
# <!-- HTML output -->
# <h2><span class="anchor" id="my-anchor"></span><a href="#my-anchor">My heading</a></h2>
#
# @example with html in the heading
# <!-- Markdown input -->
# ## My <code>html</code> heading
# <!-- Or -->
# ## My `html` heading
# <!-- HTML output -->
# <h2><span class="anchor" id="my-code-heading"></span><a href="#my-code-heading">My code heading</a></h2>
def header(text, level)
if text =~ /^\^([a-zA-Z0-9\-_]+) /
anchor_prefix = $1
text = text.sub("^#{anchor_prefix} ", "")
anchor = FormatHelpersWrapper.strip_tags(text).parameterize
anchor = "#{anchor_prefix}-#{anchor}" if anchor_prefix
elsif text =~ /^=([a-zA-Z0-9\-_]+) /
anchor = $1
text = text.sub("=#{anchor} ", "")
else
anchor = FormatHelpersWrapper.strip_tags(text).parameterize
end
%(<h%s><span class="anchor" id="%s"></span><a href="#%s">%s</a></h%s>) % [level, anchor, anchor, text, level]
end
private
# Add custom tags to content
def add_custom_tags(text)
map = {
"->" => "notice",
"!>" => "warning"
}
regexp = map.map { |k, _| Regexp.escape(k) }.join("|")
md = text.match(/^<p>(#{regexp})/)
return text unless md
key = md.captures[0]
klass = map[key]
text.gsub!(/#{Regexp.escape(key)}\s+?/, "")
<<-EOH.gsub(/^ {8}/, "")
<div class="custom-wrapper #{klass}">#{text}</div>
EOH
end
end
class AppsignalMarkdownStripDown < Redcarpet::Render::StripDown
def link(link, title, content)
content
end
end