Commit e773de6e authored by Brett Walker's avatar Brett Walker Committed by Robert Speicher

Normalize markdown source during preprocess

to remove UTF8 BOM character
parent 8bd8ea37
---
title: Tolerate UTF8 BOM character during frontmatter rendering
merge_request: 46062
author:
type: fixed
# frozen_string_literal: true
module Banzai
module Filter
class NormalizeSourceFilter < HTML::Pipeline::Filter
UTF8_BOM = "\xEF\xBB\xBF"
def call
# Remove UTF8_BOM from beginning of source text
html.delete_prefix(UTF8_BOM)
end
end
end
end
......@@ -5,6 +5,7 @@ module Banzai
class PreProcessPipeline < BasePipeline
def self.filters
FilterArray[
Filter::NormalizeSourceFilter,
Filter::FrontMatterFilter,
Filter::BlockquoteFenceFilter,
]
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Banzai::Filter::NormalizeSourceFilter do
include FilterSpecHelper
it 'removes the UTF8 BOM from the beginning of the text' do
content = "\xEF\xBB\xBF---"
output = filter(content)
expect(output).to match '---'
end
it 'does not remove those characters from anywhere else in the text' do
content = <<~MD
\xEF\xBB\xBF---
\xEF\xBB\xBF---
MD
output = filter(content)
expect(output).to match "---\n\xEF\xBB\xBF---\n"
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Banzai::Pipeline::PreProcessPipeline do
it 'pre-processes the source text' do
markdown = <<~MD
\xEF\xBB\xBF---
foo: :foo_symbol
bar: :bar_symbol
---
>>>
blockquote
>>>
MD
result = described_class.call(markdown, {})
aggregate_failures do
expect(result[:output]).not_to include "\xEF\xBB\xBF"
expect(result[:output]).not_to include '---'
expect(result[:output]).to include "```yaml\nfoo: :foo_symbol\n"
expect(result[:output]).to include "> blockquote\n"
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