Skip to content
This repository was archived by the owner on Jan 21, 2022. It is now read-only.

Commit 6df1a13

Browse files
Support single or double quotes in anchor tags for quicklinks plugin.
1 parent 5031311 commit 6df1a13

File tree

3 files changed

+43
-27
lines changed

3 files changed

+43
-27
lines changed

config.rb

+4
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
# end
4949

5050
require 'navigation'
51+
require 'quicklinks'
5152

5253
set :markdown_engine, :redcarpet
5354
set :markdown, :layout_engine => :erb,
@@ -91,6 +92,9 @@
9192
# For navigation breadcrumbs
9293
activate :navigation
9394

95+
# For generated intra-page links
96+
activate :quicklinks
97+
9498
helpers do
9599
class Middleman::Sitemap::Page
96100
def banner_url

navigation.rb

-27
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
# mostly from https://github.com/multiscan/middleman-navigation but modified slightly
22
module Navigation
3-
4-
require 'redcarpet'
5-
63
class << self
74
def registered(app)
85
app.helpers HelperMethods
@@ -62,30 +59,6 @@ def children_nav(options={})
6259
return content_tag :ul, menu_content, options
6360
end
6461

65-
def quick_links()
66-
links = []
67-
page_src = File.read(current_page.source_file)
68-
sections = page_src.scan /\n\#{2,3}[^#]+\#{2,3}\n/
69-
70-
markdown = ''
71-
72-
sections.each do |s|
73-
74-
next if s.match(/id='(.+)'/).nil? or s.match(/<\/a>([^#.]+)\#{2,3}/).nil?
75-
76-
anchor_name = s.match(/id='(.+)'/)[1]
77-
title = s.match(/<\/a>([^#.]+)\#{2,3}/)[1].strip!
78-
indent = (s.count('#') / 2) - 2
79-
80-
markdown << ' ' * indent
81-
markdown << "* [#{title}](##{anchor_name})\n"
82-
83-
end
84-
85-
md = Redcarpet::Markdown.new(Redcarpet::Render::HTML)
86-
md.render(markdown)
87-
end
88-
8962
# create an <ul> list with links to all the parent pages down to the root
9063
def trail_nav() # removed sep
9164
p = current_page

quicklinks.rb

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module QuickLinks
2+
require 'redcarpet'
3+
4+
class << self
5+
def registered(app)
6+
app.helpers HelperMethods
7+
end
8+
9+
alias :included :registered
10+
end
11+
12+
module HelperMethods
13+
def quick_links()
14+
links = []
15+
page_src = File.read(current_page.source_file)
16+
sections = page_src.scan /\n\#{2,3}[^#]+\#{2,3}\n/
17+
18+
markdown = ''
19+
20+
sections.each do |s|
21+
22+
next if s.match(/id=['"](.+)['"]/).nil? or s.match(/<\/a>([^#.]+)\#{2,3}/).nil?
23+
24+
anchor_name = s.match(/id=['"](.+)['"]/)[1]
25+
title = s.match(/<\/a>([^#.]+)\#{2,3}/)[1].strip!
26+
indent = (s.count('#') / 2) - 2
27+
28+
markdown << ' ' * indent
29+
markdown << "* [#{title}](##{anchor_name})\n"
30+
31+
end
32+
33+
md = Redcarpet::Markdown.new(Redcarpet::Render::HTML)
34+
md.render(markdown)
35+
end
36+
end
37+
end
38+
39+
::Middleman::Extensions.register(:quicklinks, QuickLinks)

0 commit comments

Comments
 (0)