Skip to content

Commit f666aa9

Browse files
committed
Make anchor text configurable
1 parent 0b6f0c2 commit f666aa9

2 files changed

Lines changed: 15 additions & 4 deletions

File tree

src/markd/options.cr

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ require "uri"
33
module Markd
44
# Markdown rendering options.
55
class Options
6-
property time, gfm, toc
6+
property time, gfm
7+
8+
# if `true', show a anchor text to the beginning of the head tag
9+
# You can specify a String as the anchor text to this option too.
10+
property toc : String | Bool
711

812
# If `true`:
913
# - straight quotes will be made curly

src/markd/renderers/html_renderer.cr

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,17 @@ module Markd
1111

1212
def heading(node : Node, entering : Bool) : Nil
1313
tag_name = HEADINGS[node.data["level"].as(Int32) - 1]
14+
toc = @options.toc
15+
1416
if entering
1517
newline
1618
tag(tag_name, attrs(node))
17-
toc(node) if @options.toc
19+
case toc
20+
when String
21+
toc(node, toc)
22+
when true
23+
toc(node)
24+
end
1825
else
1926
tag(tag_name, end_tag: true)
2027
newline
@@ -308,7 +315,7 @@ module Markd
308315
url.match(Rule::UNSAFE_PROTOCOL) && !url.match(Rule::UNSAFE_DATA_PROTOCOL)
309316
end
310317

311-
private def toc(node : Node)
318+
private def toc(node : Node, anchor_text = "§")
312319
return unless node.type.heading?
313320

314321
title = {% if compare_versions(Crystal::VERSION, "1.2.0") < 0 %}
@@ -317,7 +324,7 @@ module Markd
317324
URI.encode_path(node.first_child.text)
318325
{% end %}
319326

320-
@output_io << %(<a id="anchor-) << title << %(" class="anchor" href="#anchor-) << title << %(">) << "§" << %( </a>)
327+
@output_io << %(<a id="anchor-) << title << %(" class="anchor" href="#anchor-) << title << %(">) << anchor_text << %( </a>)
321328
@last_output = ">"
322329
end
323330

0 commit comments

Comments
 (0)