From d941ed90d6c12a8bf0c8b922076cb6eca8114816 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 29 Jun 2017 16:04:10 -0400 Subject: [PATCH] support for bbcode [code] blocks --- .../engines/markdown-it/bbcode-block.js.es6 | 71 ++++++++++++------- .../engines/markdown-it/bbcode-inline.js.es6 | 10 +++ spec/components/pretty_text_spec.rb | 12 ++++ 3 files changed, 67 insertions(+), 26 deletions(-) diff --git a/app/assets/javascripts/pretty-text/engines/markdown-it/bbcode-block.js.es6 b/app/assets/javascripts/pretty-text/engines/markdown-it/bbcode-block.js.es6 index 3205e7cc2e0..952fe9426a9 100644 --- a/app/assets/javascripts/pretty-text/engines/markdown-it/bbcode-block.js.es6 +++ b/app/assets/javascripts/pretty-text/engines/markdown-it/bbcode-block.js.es6 @@ -200,38 +200,44 @@ function applyBBCode(state, startLine, endLine, silent, md) { // this will prevent lazy continuations from ever going past our end marker state.lineMax = nextLine; - if (rule.before) { - rule.before.call(this, state, info.attrs, md, state.src.slice(initial, initial + info.length + 1)); - } + if (rule.replace) { + let content = state.src.slice(state.bMarks[startLine+1], state.eMarks[nextLine-1]); + if (!rule.replace.call(this, state, info, content)) { + return false; + } + } else { - let wrapTag; - if (rule.wrap) { - let split = rule.wrap.split('.'); - wrapTag = split[0]; - let className = split.slice(1).join(' '); + if (rule.before) { + rule.before.call(this, state, info.attrs, md, state.src.slice(initial, initial + info.length + 1)); + } - let token = state.push('wrap_bbcode', wrapTag, 1); + let wrapTag; + if (rule.wrap) { + let split = rule.wrap.split('.'); + wrapTag = split[0]; + let className = split.slice(1).join(' '); - if (className) { - token.attrs = [['class', className]]; + let token = state.push('wrap_bbcode', wrapTag, 1); + + if (className) { + token.attrs = [['class', className]]; + } + } + + let lastToken = state.tokens[state.tokens.length-1]; + lastToken.map = [ startLine, nextLine ]; + + state.md.block.tokenize(state, startLine + 1, nextLine); + + if (rule.wrap) { + state.push('wrap_bbcode', wrapTag, -1); + } + + if (rule.after) { + rule.after.call(this, state, lastToken, md, state.src.slice(start-2, start + closeTag.length - 1)); } } - let lastToken = state.tokens[state.tokens.length-1]; - lastToken.map = [ startLine, nextLine ]; - - state.md.block.tokenize(state, startLine + 1, nextLine); - - if (rule.wrap) { - state.push('wrap_bbcode', wrapTag, -1); - } - - if (rule.after) { - rule.after.call(this, state, lastToken, md, state.src.slice(start-2, start + closeTag.length - 1)); - } - - lastToken = state.tokens[state.tokens.length-1]; - state.parentType = old_parent; state.lineMax = old_line_max; state.line = nextLine+1; @@ -242,7 +248,20 @@ function applyBBCode(state, startLine, endLine, silent, md) { export function setup(helper) { if (!helper.markdownIt) { return; } + helper.registerPlugin(md => { + const ruler = md.block.bbcode_ruler; + + ruler.push('code', { + tag: 'code', + replace: function(state, tagInfo, content) { + let token; + token = state.push('fence', 'code', 0); + token.content = content; + return true; + } + }); + isWhiteSpace = md.utils.isWhiteSpace; md.block.ruler.after('fence', 'bbcode', (state, startLine, endLine, silent)=> { return applyBBCode(state, startLine, endLine, silent, md); 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 b8d6c2d10fc..f435a2ae1c1 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 @@ -150,6 +150,16 @@ export function setup(helper) { md.inline.ruler.push('bbcode-inline', (state,silent) => tokanizeBBCode(state,silent,ruler)); md.inline.ruler2.before('text_collapse', 'bbcode-inline', processBBCode); + ruler.push('code', { + tag: 'code', + replace: function(state, tagInfo, content) { + let token; + token = state.push('code_inline', 'code', 0); + token.content = content; + return true; + } + }); + ruler.push('url', { tag: 'url', replace: function(state, tagInfo, content) { diff --git a/spec/components/pretty_text_spec.rb b/spec/components/pretty_text_spec.rb index e71bfe7b693..dea7d69ccc3 100644 --- a/spec/components/pretty_text_spec.rb +++ b/spec/components/pretty_text_spec.rb @@ -784,6 +784,18 @@ HTML expect(cooked).to eq(html) end + it "supports inline code bbcode" do + cooked = PrettyText.cook "Testing [code]codified **stuff** and `more` stuff[/code]" + html = "

Testing codified **stuff** and `more` stuff

" + expect(cooked).to eq(html) + end + + it "supports block code bbcode" do + cooked = PrettyText.cook "[code]\ncodified\n\n\n **stuff** and `more` stuff\n[/code]" + html = "
codified\n\n\n  **stuff** and `more` stuff
" + expect(cooked).to eq(html) + end + end end