Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Jérome Perrin
gitlab-ce
Commits
bc43ad71
Commit
bc43ad71
authored
Feb 23, 2016
by
Robert Speicher
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Replace Gollum `[[_TOC_]]` tag with result of TableOfContentsFilter
Closes #2494
parent
9f80118e
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
85 additions
and
8 deletions
+85
-8
lib/banzai/filter/gollum_tags_filter.rb
lib/banzai/filter/gollum_tags_filter.rb
+28
-7
lib/banzai/pipeline/wiki_pipeline.rb
lib/banzai/pipeline/wiki_pipeline.rb
+5
-1
spec/lib/banzai/filter/gollum_tags_filter_spec.rb
spec/lib/banzai/filter/gollum_tags_filter_spec.rb
+52
-0
No files found.
lib/banzai/filter/gollum_tags_filter.rb
View file @
bc43ad71
...
...
@@ -50,16 +50,24 @@ module Banzai
# See https://github.com/gollum/gollum/wiki
#
# Rubular: http://rubular.com/r/7dQnE5CUCH
TAGS_PATTERN
=
%r{
\[\[
(.+?)
\]\]
}
TAGS_PATTERN
=
%r{
\[\[
(.+?)
\]\]
}
.
freeze
# Pattern to match allowed image extensions
ALLOWED_IMAGE_EXTENSIONS
=
%r{.+(jpg|png|gif|svg|bmp)
\z
}i
ALLOWED_IMAGE_EXTENSIONS
=
%r{.+(jpg|png|gif|svg|bmp)
\z
}i
.
freeze
def
call
search_text_nodes
(
doc
).
each
do
|
node
|
# A Gollum ToC tag is `[[_TOC_]]`, but due to MarkdownFilter running
# before this one, it will be converted into `[[<em>TOC</em>]]`, so it
# needs special-case handling
if
toc_tag?
(
node
)
next
unless
result
[
:toc
].
present?
process_toc_tag
(
node
)
else
content
=
node
.
content
next
unless
content
.
match
(
TAGS_PATTERN
)
next
unless
content
=~
TAGS_PATTERN
html
=
process_tag
(
$1
)
...
...
@@ -67,12 +75,19 @@ module Banzai
node
.
replace
(
html
)
end
end
end
doc
end
private
# Replace an entire `[[<em>TOC</em>]]` node with the result generated by
# TableOfContentsFilter
def
process_toc_tag
(
node
)
node
.
parent
.
parent
.
replace
(
result
[
:toc
])
end
# Process a single tag into its final HTML form.
#
# tag - The String tag contents (the stuff inside the double brackets).
...
...
@@ -108,6 +123,12 @@ module Banzai
end
end
def
toc_tag?
(
node
)
node
.
content
==
'TOC'
&&
node
.
parent
.
name
==
'em'
&&
node
.
parent
.
parent
.
text
==
'[[TOC]]'
end
def
image?
(
path
)
path
=~
ALLOWED_IMAGE_EXTENSIONS
end
...
...
lib/banzai/pipeline/wiki_pipeline.rb
View file @
bc43ad71
...
...
@@ -4,7 +4,11 @@ module Banzai
module
Pipeline
class
WikiPipeline
<
FullPipeline
def
self
.
filters
super
.
insert
(
1
,
Filter
::
GollumTagsFilter
)
@filters
||=
begin
filters
=
super
toc
=
filters
.
index
(
Filter
::
TableOfContentsFilter
)
filters
.
insert
(
toc
+
1
,
Filter
::
GollumTagsFilter
)
end
end
end
end
...
...
spec/lib/banzai/filter/gollum_tags_filter_spec.rb
View file @
bc43ad71
...
...
@@ -86,4 +86,56 @@ describe Banzai::Filter::GollumTagsFilter, lib: true do
expect
(
doc
.
at_css
(
'a'
)[
'href'
]).
to
eq
'wiki-slug'
end
end
context
'table of contents'
do
let
(
:pipeline
)
{
Banzai
::
Pipeline
[
:wiki
]
}
it
'replaces the tag with the TableOfContentsFilter result'
do
markdown
=
<<-
MD
.
strip_heredoc
[[_TOC_]]
## Header
Foo
MD
result
=
pipeline
.
call
(
markdown
,
project_wiki:
project_wiki
,
project:
project
)
aggregate_failures
do
expect
(
result
[
:output
].
text
).
not_to
include
'[[_TOC_]]'
expect
(
result
[
:output
].
text
).
not_to
include
'[['
expect
(
result
[
:output
].
to_html
).
to
include
(
result
[
:toc
])
end
end
it
'is case-sensitive'
do
markdown
=
<<-
MD
.
strip_heredoc
[[_toc_]]
# Header 1
Foo
MD
output
=
pipeline
.
to_html
(
markdown
,
project_wiki:
project_wiki
,
project:
project
)
expect
(
output
).
to
include
(
'[[<em>toc</em>]]'
)
end
it
'handles an empty pipeline result'
do
# No Markdown headers in this doc, so `result[:toc]` will be empty
markdown
=
<<-
MD
.
strip_heredoc
[[_TOC_]]
Foo
MD
output
=
pipeline
.
to_html
(
markdown
,
project_wiki:
project_wiki
,
project:
project
)
aggregate_failures
do
expect
(
output
).
not_to
include
(
'<ul>'
)
expect
(
output
).
to
include
(
'[[<em>TOC</em>]]'
)
end
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment