Skip to content

Commit

Permalink
Merge pull request #392 from gjtorikian/no-node
Browse files Browse the repository at this point in the history
Allow pipeline to run without node filters
  • Loading branch information
gjtorikian authored Jan 8, 2024
2 parents 52498c3 + 64c896f commit d3f3137
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
52 changes: 33 additions & 19 deletions lib/html_pipeline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ class << self
def initialize(text_filters: [], convert_filter: nil, sanitization_config: SanitizationFilter::DEFAULT_CONFIG, node_filters: [], default_context: {}, result_class: Hash)
raise ArgumentError, "default_context cannot be nil" if default_context.nil?

@text_filters = text_filters.flatten.freeze
@text_filters = text_filters.flatten.freeze || []
validate_filters(@text_filters, HTMLPipeline::TextFilter)

@node_filters = node_filters.flatten.freeze
@node_filters = node_filters.flatten.freeze || []
validate_filters(@node_filters, HTMLPipeline::NodeFilter)

@convert_filter = convert_filter
Expand Down Expand Up @@ -151,33 +151,46 @@ def call(text, context: {}, result: {})
context = context.freeze
result ||= {}

payload = default_payload({
text_filters: @text_filters.map(&:name),
context: context,
result: result,
})
instrument("call_text_filters.html_pipeline", payload) do
result[:output] =
@text_filters.inject(text) do |doc, filter|
perform_filter(filter, doc, context: context, result: result)
end
if @text_filters.any?
payload = default_payload({
text_filters: @text_filters.map(&:name),
context: context,
result: result,
})
instrument("call_text_filters.html_pipeline", payload) do
result[:output] =
@text_filters.inject(text) do |doc, filter|
perform_filter(filter, doc, context: context, result: result)
end
end
end

text = result[:output]
text = result[:output] || text

html = @convert_filter.call(text) unless @convert_filter.nil?
html = if @convert_filter.nil?
text
else
instrument("call_convert_filter.html_pipeline", payload) do
html = @convert_filter.call(text)
end
end

unless @node_filters.empty?
payload = default_payload({
node_filters: @node_filters.map { |f| f.class.name },
context: context,
result: result,
})
instrument("call_node_filters.html_pipeline", payload) do
result[:output] = Selma::Rewriter.new(sanitizer: @sanitization_config, handlers: @node_filters).rewrite(html)
html = result[:output]
payload = default_payload({
node_filters: @node_filters.map { |f| f.class.name },
context: context,
result: result,
})
end
end

instrument("html_pipeline.sanitization", payload) do
result[:output] = Selma::Rewriter.new(sanitizer: @sanitization_config, handlers: @node_filters).rewrite(html)
end

result = result.merge(@node_filters.collect(&:result).reduce({}, :merge))
@node_filters.each(&:reset!)

Expand All @@ -195,6 +208,7 @@ def perform_filter(filter, doc, context: {}, result: {})
context: context,
result: result,
})

instrument("call_filter.html_pipeline", payload) do
filter.call(doc, context: context, result: result)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/html_pipeline/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

class HTMLPipeline
VERSION = "3.0.1"
VERSION = "3.0.2"
end
7 changes: 7 additions & 0 deletions test/html_pipeline/convert_filter/markdown_filter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,11 @@ def test_not_convert_newlines_in_lists
@gfm.call("* foo\n* bar", context: { markdown: options }),
)
end

def test_works_without_node_filters
markdown = "1. Foo\n2. Bar"
result = HTMLPipeline.new(convert_filter: HTMLPipeline::ConvertFilter::MarkdownFilter.new).call(markdown)[:output]

assert_equal("<ol>\n<li>Foo</li>\n<li>Bar</li>\n</ol>", result)
end
end

0 comments on commit d3f3137

Please sign in to comment.