Skip to content

Commit 570b870

Browse files
committed
- Fix inline TOC for Pandoc header slug rules
1 parent 64e4dcd commit 570b870

3 files changed

Lines changed: 17 additions & 5 deletions

File tree

lib/madness/inline_table_of_contents.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,8 @@ def toc_item(line)
3636
level = matches[:level].size - 2
3737

3838
spacer = ' ' * level
39-
slug = text.to_slug
39+
slug = text.to_slug config.renderer
4040

41-
# pandoc removes leading numbers and dots from header slugs, we do the same
42-
slug = slug.remove(/^[\d\-]+/) if config.renderer == 'pandoc'
4341
"#{spacer}- [#{text}](##{slug})"
4442
end
4543

lib/madness/refinements/string_refinements.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,14 @@ def to_href
1111
Addressable::URI.escape self
1212
end
1313

14-
def to_slug
15-
downcase.strip.gsub(/[^[:alnum:]]/, '-').squeeze('-').remove(/(^-|-$)/)
14+
def to_slug(renderer = nil)
15+
result = downcase.strip
16+
17+
if renderer == 'pandoc'
18+
result.remove(/[^a-z0-9 ]/).gsub(' ', '-')
19+
else
20+
result.gsub(/[^[:alnum:]]/, '-').squeeze('-').remove(/(^-|-$)/)
21+
end
1622
end
1723

1824
# This is here so we can have one place that defines how to convert

spec/madness/refinements/string_refinements_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@
2323
expect(subject.to_slug).to eq 'café-müller-in-münchen-123'
2424
end
2525
end
26+
27+
context 'with pandoc renderer' do
28+
subject { '& & Tom & & Jerry & &'}
29+
30+
it 'removes symbols before converting, does not squeeze dashes and leaves trailing and keeps edge dashes' do
31+
expect(subject.to_slug 'pandoc').to eq '--tom---jerry--'
32+
end
33+
end
2634
end
2735

2836
describe '#label' do

0 commit comments

Comments
 (0)