Pass the full CommonMark spec
This commit is contained in:
parent
aa5b8a5749
commit
e1ce47a901
|
@ -213,7 +213,7 @@ export function setup(opts, siteSettings, state) {
|
||||||
html: true,
|
html: true,
|
||||||
breaks: opts.discourse.features.newline,
|
breaks: opts.discourse.features.newline,
|
||||||
xhtmlOut: false,
|
xhtmlOut: false,
|
||||||
linkify: true,
|
linkify: opts.discourse.features.linkify,
|
||||||
typographer: siteSettings.enable_markdown_typographer
|
typographer: siteSettings.enable_markdown_typographer
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,7 @@ function render(tokens, idx, options, env, slf, md) {
|
||||||
|
|
||||||
if (info) {
|
if (info) {
|
||||||
// strip off any additional languages
|
// strip off any additional languages
|
||||||
info = info.split(/\s+/g)[0];
|
info = info.trim().split(/\s+/g)[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
const acceptableCodeClasses = md.options.discourse.acceptableCodeClasses;
|
const acceptableCodeClasses = md.options.discourse.acceptableCodeClasses;
|
||||||
|
@ -23,7 +23,7 @@ function render(tokens, idx, options, env, slf, md) {
|
||||||
|
|
||||||
className = TEXT_CODE_CLASSES.indexOf(info) !== -1 ? 'lang-nohighlight' : 'lang-' + langName;
|
className = TEXT_CODE_CLASSES.indexOf(info) !== -1 ? 'lang-nohighlight' : 'lang-' + langName;
|
||||||
|
|
||||||
return `<pre><code class='${className}'>${escapedContent}</code></pre>\n`;
|
return `<pre><code class="${className}">${escapedContent}</code></pre>\n`;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function setup(helper) {
|
export function setup(helper) {
|
||||||
|
|
|
@ -21,7 +21,8 @@ export function buildOptions(state) {
|
||||||
lookupAvatarByPostNumber,
|
lookupAvatarByPostNumber,
|
||||||
emojiUnicodeReplacer,
|
emojiUnicodeReplacer,
|
||||||
lookupInlineOnebox,
|
lookupInlineOnebox,
|
||||||
previewing
|
previewing,
|
||||||
|
linkify
|
||||||
} = state;
|
} = state;
|
||||||
|
|
||||||
let features = {
|
let features = {
|
||||||
|
@ -33,6 +34,7 @@ export function buildOptions(state) {
|
||||||
'html': true,
|
'html': true,
|
||||||
'category-hashtag': true,
|
'category-hashtag': true,
|
||||||
'onebox': true,
|
'onebox': true,
|
||||||
|
'linkify': linkify !== false,
|
||||||
'newline': !siteSettings.traditional_markdown_linebreaks
|
'newline': !siteSettings.traditional_markdown_linebreaks
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -165,6 +165,7 @@ module PrettyText
|
||||||
__optInput.customEmoji = #{custom_emoji.to_json};
|
__optInput.customEmoji = #{custom_emoji.to_json};
|
||||||
__optInput.emojiUnicodeReplacer = __emojiUnicodeReplacer;
|
__optInput.emojiUnicodeReplacer = __emojiUnicodeReplacer;
|
||||||
__optInput.lookupInlineOnebox = __lookupInlineOnebox;
|
__optInput.lookupInlineOnebox = __lookupInlineOnebox;
|
||||||
|
#{opts[:linkify] == false ? "__optInput.linkify = false;": ""}
|
||||||
JS
|
JS
|
||||||
|
|
||||||
if opts[:topicId]
|
if opts[:topicId]
|
||||||
|
|
|
@ -215,9 +215,12 @@ describe PrettyText do
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'can include code class correctly' do
|
it 'can include code class correctly' do
|
||||||
|
# keep in mind spaces should be trimmed per spec
|
||||||
|
expect(PrettyText.cook("``` ruby the mooby\n`````")).to eq('<pre><code class="lang-ruby"></code></pre>')
|
||||||
expect(PrettyText.cook("```cpp\ncpp\n```")).to match_html("<pre><code class='lang-cpp'>cpp\n</code></pre>")
|
expect(PrettyText.cook("```cpp\ncpp\n```")).to match_html("<pre><code class='lang-cpp'>cpp\n</code></pre>")
|
||||||
expect(PrettyText.cook("```\ncpp\n```")).to match_html("<pre><code class='lang-auto'>cpp\n</code></pre>")
|
expect(PrettyText.cook("```\ncpp\n```")).to match_html("<pre><code class='lang-auto'>cpp\n</code></pre>")
|
||||||
expect(PrettyText.cook("```text\ncpp\n```")).to match_html("<pre><code class='lang-nohighlight'>cpp\n</code></pre>")
|
expect(PrettyText.cook("```text\ncpp\n```")).to match_html("<pre><code class='lang-nohighlight'>cpp\n</code></pre>")
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'indents code correctly' do
|
it 'indents code correctly' do
|
||||||
|
@ -966,69 +969,4 @@ HTML
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# work in progress
|
|
||||||
skip 'passes commonmark spec' do
|
|
||||||
|
|
||||||
SiteSetting.traditional_markdown_linebreaks = true
|
|
||||||
|
|
||||||
html,state,md = nil
|
|
||||||
failed = 0
|
|
||||||
|
|
||||||
File.readlines(Rails.root + 'spec/fixtures/md/spec.txt').each do |line|
|
|
||||||
if line == "```````````````````````````````` example\n"
|
|
||||||
state = :example
|
|
||||||
next
|
|
||||||
end
|
|
||||||
|
|
||||||
if line == "````````````````````````````````\n"
|
|
||||||
md.gsub!('→', "\t")
|
|
||||||
html ||= ''
|
|
||||||
html.gsub!('→', "\t")
|
|
||||||
html.strip!
|
|
||||||
|
|
||||||
# normalize brs
|
|
||||||
html.gsub!('<br />', '<br>')
|
|
||||||
html.gsub!('<hr />', '<hr>')
|
|
||||||
html.gsub!(/<img([^>]+) \/>/, "<img\\1>")
|
|
||||||
|
|
||||||
cooked = PrettyText.markdown(md, sanitize: false)
|
|
||||||
cooked.strip!
|
|
||||||
cooked.gsub!(" class='lang-auto'", '')
|
|
||||||
cooked.gsub!(/<span class="hashtag">(.*)<\/span>/, "\\1")
|
|
||||||
# we don't care about this
|
|
||||||
cooked.gsub!("<blockquote>\n</blockquote>", "<blockquote></blockquote>")
|
|
||||||
|
|
||||||
unless cooked == html
|
|
||||||
failed += 1
|
|
||||||
puts "FAILED SPEC"
|
|
||||||
puts "Expected: "
|
|
||||||
puts html
|
|
||||||
puts "Got: "
|
|
||||||
puts cooked
|
|
||||||
puts "Markdown: "
|
|
||||||
puts md
|
|
||||||
puts
|
|
||||||
end
|
|
||||||
html,state,md = nil
|
|
||||||
next
|
|
||||||
end
|
|
||||||
|
|
||||||
if state == :example && line == ".\n"
|
|
||||||
state = :html
|
|
||||||
next
|
|
||||||
end
|
|
||||||
|
|
||||||
if state == :example
|
|
||||||
md = (md || "") << line
|
|
||||||
end
|
|
||||||
|
|
||||||
if state == :html
|
|
||||||
html = (html || "") << line
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
expect(failed).to eq(0)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,74 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
describe "CommonMark" do
|
||||||
|
it 'passes spec' do
|
||||||
|
|
||||||
|
SiteSetting.traditional_markdown_linebreaks = true
|
||||||
|
SiteSetting.enable_markdown_typographer = false
|
||||||
|
|
||||||
|
html,state,md = nil
|
||||||
|
failed = 0
|
||||||
|
|
||||||
|
File.readlines(Rails.root + 'spec/fixtures/md/spec.txt').each do |line|
|
||||||
|
if line == "```````````````````````````````` example\n"
|
||||||
|
state = :example
|
||||||
|
next
|
||||||
|
end
|
||||||
|
|
||||||
|
if line == "````````````````````````````````\n"
|
||||||
|
md.gsub!('→', "\t")
|
||||||
|
html ||= String.new
|
||||||
|
html.gsub!('→', "\t")
|
||||||
|
html.strip!
|
||||||
|
|
||||||
|
# normalize brs
|
||||||
|
html.gsub!('<br />', '<br>')
|
||||||
|
html.gsub!('<hr />', '<hr>')
|
||||||
|
html.gsub!(/<img([^>]+) \/>/, "<img\\1>")
|
||||||
|
|
||||||
|
cooked = PrettyText.markdown(md, sanitize: false, linkify: false)
|
||||||
|
cooked.strip!
|
||||||
|
cooked.gsub!(" class=\"lang-auto\"", '')
|
||||||
|
cooked.gsub!(/<span class="hashtag">(.*)<\/span>/, "\\1")
|
||||||
|
# we don't care about this
|
||||||
|
cooked.gsub!("<blockquote>\n</blockquote>", "<blockquote></blockquote>")
|
||||||
|
html.gsub!("<blockquote>\n</blockquote>", "<blockquote></blockquote>")
|
||||||
|
html.gsub!("language-ruby", "lang-ruby")
|
||||||
|
# strip out unsupported languages
|
||||||
|
html.gsub!(/ class="language-[;f].*"/, "")
|
||||||
|
|
||||||
|
unless cooked == html
|
||||||
|
failed += 1
|
||||||
|
puts "FAILED SPEC"
|
||||||
|
puts "Expected: "
|
||||||
|
puts html
|
||||||
|
puts "Got: "
|
||||||
|
puts cooked
|
||||||
|
puts "Markdown: "
|
||||||
|
puts md
|
||||||
|
puts
|
||||||
|
end
|
||||||
|
html,state,md = nil
|
||||||
|
next
|
||||||
|
end
|
||||||
|
|
||||||
|
if state == :example && line == ".\n"
|
||||||
|
state = :html
|
||||||
|
next
|
||||||
|
end
|
||||||
|
|
||||||
|
if state == :example
|
||||||
|
md = (md || String.new) << line
|
||||||
|
end
|
||||||
|
|
||||||
|
if state == :html
|
||||||
|
html = (html || String.new) << line
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
expect(failed).to eq(0)
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue