Add FilterArray class to Banzai
This commit is contained in:
parent
6aa50165b0
commit
c9b1132217
11 changed files with 76 additions and 13 deletions
27
lib/banzai/filter_array.rb
Normal file
27
lib/banzai/filter_array.rb
Normal file
|
@ -0,0 +1,27 @@
|
|||
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
|
||||
|
|
39
spec/lib/banzai/filter_array_spec.rb
Normal file
39
spec/lib/banzai/filter_array_spec.rb
Normal file
|
@ -0,0 +1,39 @@
|
|||
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
|
Loading…
Reference in a new issue