mirror of
https://github.com/discourse/discourse.git
synced 2024-11-25 09:42:07 +08:00
Added code block normalization routing for import
This commit is contained in:
parent
544e7b999e
commit
05ca1e6e46
2
Gemfile
2
Gemfile
|
@ -199,6 +199,8 @@ gem 'fast_blank' #, github: "SamSaffron/fast_blank"
|
|||
# this provides a very efficient lru cache
|
||||
gem 'lru_redux'
|
||||
|
||||
gem 'htmlentities', require: false
|
||||
|
||||
# IMPORTANT: mini profiler monkey patches, so it better be required last
|
||||
# If you want to amend mini profiler to do the monkey patches in the railstie
|
||||
# we are open to it. by deferring require to the initializer we can configure discourse installs without it
|
||||
|
|
|
@ -130,6 +130,7 @@ GEM
|
|||
highline (1.6.20)
|
||||
hike (1.2.3)
|
||||
hiredis (0.5.1)
|
||||
htmlentities (4.3.2)
|
||||
i18n (0.6.9)
|
||||
image_optim (0.9.1)
|
||||
exifr (~> 1.1.3)
|
||||
|
@ -420,6 +421,7 @@ DEPENDENCIES
|
|||
handlebars-source (= 1.3.0)
|
||||
highline
|
||||
hiredis
|
||||
htmlentities
|
||||
image_optim (= 0.9.1)
|
||||
librarian (>= 0.0.25)
|
||||
listen (= 0.7.3)
|
||||
|
|
12
lib/import/normalize.rb
Normal file
12
lib/import/normalize.rb
Normal file
|
@ -0,0 +1,12 @@
|
|||
# markdown normalizer to be used by importers
|
||||
#
|
||||
#
|
||||
require 'htmlentities'
|
||||
module Import::Normalize
|
||||
def self.normalize_code_blocks(code, lang=nil)
|
||||
coder = HTMLEntities.new
|
||||
code.gsub(/<pre>\s*<code>\n?(.*?)\n?<\/code>\s*<\/pre>/m) {
|
||||
"\n```#{lang}\n#{coder.decode($1)}\n```\n"
|
||||
}
|
||||
end
|
||||
end
|
21
spec/components/import/normalize_spec.rb
Normal file
21
spec/components/import/normalize_spec.rb
Normal file
|
@ -0,0 +1,21 @@
|
|||
require "spec_helper"
|
||||
require_dependency "import/normalize"
|
||||
|
||||
describe Import::Normalize do
|
||||
describe "#normalize_code_blocks" do
|
||||
it "normalizes 2 code blocks correctly" do
|
||||
markdown = <<MD
|
||||
|
||||
<pre>
|
||||
<code>
|
||||
I am a te "
|
||||
</code></pre>
|
||||
test
|
||||
<pre><code>this is a ""</code></pre>
|
||||
MD
|
||||
expected = " \n \n```\n I am a te \"\n \n```\n\n test \n \n```\nthis is a \"\"\n```\n\n"
|
||||
Import::Normalize.normalize_code_blocks(markdown).should == expected
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user