diff --git a/app/assets/javascripts/pretty-text/engines/discourse-markdown-it.js.es6 b/app/assets/javascripts/pretty-text/engines/discourse-markdown-it.js.es6 index ebd2c08f8ab..0b0b0102029 100644 --- a/app/assets/javascripts/pretty-text/engines/discourse-markdown-it.js.es6 +++ b/app/assets/javascripts/pretty-text/engines/discourse-markdown-it.js.es6 @@ -26,11 +26,11 @@ function createHelper(featureName, opts, optionCallbacks, pluginCallbacks, getOp // hack to allow moving of getOptions helper.getOptions = () => getOptions.f(); - helper.registerOptions = function(callback){ + helper.registerOptions = (callback) => { optionCallbacks.push([featureName, callback]); }; - helper.registerPlugin = function(callback){ + helper.registerPlugin = (callback) => { pluginCallbacks.push([featureName, callback]); }; @@ -57,6 +57,10 @@ function setupBlockBBCode(md) { md.block.bbcode_ruler = new Ruler(); } +function setupInlineBBCode(md) { + md.inline.bbcode_ruler = new Ruler(); +} + export function setup(opts, siteSettings, state) { if (opts.setup) { return; @@ -117,6 +121,7 @@ export function setup(opts, siteSettings, state) { }); setupBlockBBCode(opts.engine); + setupInlineBBCode(opts.engine); pluginCallbacks.forEach(([feature, callback])=>{ if (opts.discourse.features[feature]) { 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 ca30631f8b7..8e76449b3f7 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 @@ -46,7 +46,7 @@ export function parseBBCodeTag(src, start, max) { } if (closed) { - length = i; + length = (i-start)+1; let raw = src.slice(start+tag.length+1, i); @@ -168,14 +168,35 @@ function applyBBCode(state, startLine, endLine, silent, md) { // this will prevent lazy continuations from ever going past our end marker state.lineMax = nextLine; - rule.before.call(this, state, info.attrs, md, state.src.slice(initial, initial + info.length + 1)); + if (rule.before) { + rule.before.call(this, state, info.attrs, md, state.src.slice(initial, initial + info.length + 1)); + } + + let wrapTag; + if (rule.wrap) { + let split = rule.wrap.split('.'); + wrapTag = split[0]; + let className = split.slice(1).join(' '); + + 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); - rule.after.call(this, state, lastToken, md); + if (rule.wrap) { + state.push('wrap_bbcode', wrapTag, -1); + } + + if (rule.after) { + rule.after.call(this, state, lastToken, md); + } lastToken = state.tokens[state.tokens.length-1]; @@ -187,8 +208,6 @@ function applyBBCode(state, startLine, endLine, silent, md) { return true; } - - export function setup(helper) { if (!helper.markdownIt) { return; } 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 42d266dde7c..280b60fb8f0 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 @@ -1,13 +1,6 @@ import { parseBBCodeTag } from 'pretty-text/engines/markdown-it/bbcode-block'; -const rules = { - 'b': {tag: 'span', 'class': 'bbcode-b'}, - 'i': {tag: 'span', 'class': 'bbcode-i'}, - 'u': {tag: 'span', 'class': 'bbcode-u'}, - 's': {tag: 'span', 'class': 'bbcode-s'} -}; - -function tokanizeBBCode(state, silent) { +function tokanizeBBCode(state, silent, ruler) { let pos = state.pos; @@ -22,7 +15,17 @@ function tokanizeBBCode(state, silent) { return false; } - const rule = rules[tagInfo.tag]; + let rules = ruler.getRules(); + let rule; + + for (let i=0; i { - md.inline.ruler.push('bbcode-inline', tokanizeBBCode); + const ruler = md.inline.bbcode_ruler; + + md.inline.ruler.push('bbcode-inline', (state,silent) => tokanizeBBCode(state,silent,ruler)); md.inline.ruler2.before('text_collapse', 'bbcode-inline', processBBCode); + + ruler.push('bold', { + tag: 'b', + wrap: 'span.bbcode-b', + }); + + ruler.push('italic', { + tag: 'i', + wrap: 'span.bbcode-i' + }); + + ruler.push('underline', { + tag: 'u', + wrap: 'span.bbcode-u' + }); + + ruler.push('strike', { + tag: 's', + wrap: 'span.bbcode-s' + }); }); }