From de50d8cd35c924d9939a15ce1033606b2b48e8f8 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 29 Jun 2017 13:59:40 -0400 Subject: [PATCH] Support for [email] [url] [img] bbcodes --- .../engines/markdown-it/bbcode-inline.js.es6 | 108 +++++++++++++++--- spec/components/pretty_text_spec.rb | 25 ++++ 2 files changed, 116 insertions(+), 17 deletions(-) diff --git a/app/assets/javascripts/pretty-text/engines/markdown-it/bbcode-inline.js.es6 b/app/assets/javascripts/pretty-text/engines/markdown-it/bbcode-inline.js.es6 index 280b60fb8f0..b8d6c2d10fc 100644 --- a/app/assets/javascripts/pretty-text/engines/markdown-it/bbcode-inline.js.es6 +++ b/app/assets/javascripts/pretty-text/engines/markdown-it/bbcode-inline.js.es6 @@ -16,9 +16,9 @@ function tokanizeBBCode(state, silent, ruler) { } let rules = ruler.getRules(); - let rule; + let rule, i; - for (let i=0; i tokanizeBBCode(state,silent,ruler)); md.inline.ruler2.before('text_collapse', 'bbcode-inline', processBBCode); + ruler.push('url', { + tag: 'url', + replace: function(state, tagInfo, content) { + let token; + + token = state.push('link_open', 'a', 1); + token.attrs = [['href', content], ['data-bbcode', 'true']]; + + token = state.push('text', '', 0); + token.content = content; + + token = state.push('link_close', 'a', -1); + return true; + } + }); + + ruler.push('email', { + tag: 'email', + replace: function(state, tagInfo, content) { + let token; + + token = state.push('link_open', 'a', 1); + token.attrs = [['href', 'mailto:' + content], ['data-bbcode', 'true']]; + + token = state.push('text', '', 0); + token.content = content; + + token = state.push('link_close', 'a', -1); + return true; + } + }); + + ruler.push('image', { + tag: 'img', + replace: function(state, tagInfo, content) { + let token = state.push('image', 'img', 0); + token.attrs = [['src', content],['alt','']]; + token.children = []; + return true; + } + }); + ruler.push('bold', { tag: 'b', wrap: 'span.bbcode-b', diff --git a/spec/components/pretty_text_spec.rb b/spec/components/pretty_text_spec.rb index 8e4e959b676..e71bfe7b693 100644 --- a/spec/components/pretty_text_spec.rb +++ b/spec/components/pretty_text_spec.rb @@ -759,6 +759,31 @@ HTML expect(cook("[quote=\"EvilTrout, post:2, topic:#{topic.id}\"]\nddd\n[/quote]", topic_id: 1)).to eq(n(expected)) end + + it "supports img bbcode" do + cooked = PrettyText.cook "[img]http://www.image/test.png[/img]" + html = "

" + expect(cooked).to eq(html) + end + + it "provides safety for img bbcode" do + cooked = PrettyText.cook "[img]http://aaa.com[/img]" + html = '

' + expect(cooked).to eq(html) + end + + it "supports email bbcode" do + cooked = PrettyText.cook "[email]sam@sam.com[/email]" + html = '

sam@sam.com

' + expect(cooked).to eq(html) + end + + it "supports url bbcode" do + cooked = PrettyText.cook "[url]http://sam.com[/url]" + html = '

http://sam.com

' + expect(cooked).to eq(html) + end + end end