Commit 8eadb373 authored by Douwe Maan's avatar Douwe Maan

Merge branch 'rs-filter-array' into 'master'

Add FilterArray class to Banzai



See merge request !3053
parents 8ec49e47 c9b11322
module Banzai
class FilterArray < Array
# Insert a value immediately after another value
#
# If the preceding value does not exist, the new value is added to the end
# of the Array.
def insert_after(after_value, value)
i = index(after_value) || length - 1
insert(i + 1, value)
end
# Insert a value immediately before another value
#
# If the succeeding value does not exist, the new value is added to the
# beginning of the Array.
def insert_before(before_value, value)
i = index(before_value) || -1
if i < 0
unshift(value)
else
insert(i, value)
end
end
end
end
......@@ -4,7 +4,7 @@ module Banzai
module Pipeline
class BasePipeline
def self.filters
[]
FilterArray[]
end
def self.transform_context(context)
......
......@@ -2,7 +2,7 @@ module Banzai
module Pipeline
class BroadcastMessagePipeline < DescriptionPipeline
def self.filters
@filters ||= [
@filters ||= FilterArray[
Filter::MarkdownFilter,
Filter::SanitizationFilter,
......
......@@ -10,7 +10,7 @@ module Banzai
end
def self.filters
pipelines.flat_map(&:filters)
FilterArray.new(pipelines.flat_map(&:filters))
end
def self.transform_context(context)
......
......@@ -2,7 +2,7 @@ module Banzai
module Pipeline
class GfmPipeline < BasePipeline
def self.filters
@filters ||= [
@filters ||= FilterArray[
Filter::SyntaxHighlightFilter,
Filter::SanitizationFilter,
......
......@@ -2,7 +2,7 @@ module Banzai
module Pipeline
class PlainMarkdownPipeline < BasePipeline
def self.filters
[
FilterArray[
Filter::MarkdownFilter
]
end
......
......@@ -2,7 +2,7 @@ module Banzai
module Pipeline
class PostProcessPipeline < BasePipeline
def self.filters
[
FilterArray[
Filter::RelativeLinkFilter,
Filter::RedactorFilter
]
......
......@@ -2,7 +2,7 @@ module Banzai
module Pipeline
class ReferenceExtractionPipeline < BasePipeline
def self.filters
[
FilterArray[
Filter::ReferenceGathererFilter
]
end
......
......@@ -2,7 +2,7 @@ module Banzai
module Pipeline
class SingleLinePipeline < GfmPipeline
def self.filters
@filters ||= [
@filters ||= FilterArray[
Filter::SanitizationFilter,
Filter::EmojiFilter,
......
......@@ -4,11 +4,8 @@ module Banzai
module Pipeline
class WikiPipeline < FullPipeline
def self.filters
@filters ||= begin
filters = super
toc = filters.index(Filter::TableOfContentsFilter)
filters.insert(toc + 1, Filter::GollumTagsFilter)
end
@filters ||= super.insert_after(Filter::TableOfContentsFilter,
Filter::GollumTagsFilter)
end
end
end
......
require 'spec_helper'
describe Banzai::FilterArray do
describe '#insert_after' do
it 'inserts an element after a provided element' do
filters = described_class.new(%w(a b c))
filters.insert_after('b', '1')
expect(filters).to eq %w(a b 1 c)
end
it 'inserts an element at the end when the provided element does not exist' do
filters = described_class.new(%w(a b c))
filters.insert_after('d', '1')
expect(filters).to eq %w(a b c 1)
end
end
describe '#insert_before' do
it 'inserts an element before a provided element' do
filters = described_class.new(%w(a b c))
filters.insert_before('b', '1')
expect(filters).to eq %w(a 1 b c)
end
it 'inserts an element at the beginning when the provided element does not exist' do
filters = described_class.new(%w(a b c))
filters.insert_before('d', '1')
expect(filters).to eq %w(1 a b c)
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment