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 ec072ff5f47..3205e7cc2e0 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 @@ -227,7 +227,7 @@ function applyBBCode(state, startLine, endLine, silent, md) { } if (rule.after) { - rule.after.call(this, state, lastToken, md); + rule.after.call(this, state, lastToken, md, state.src.slice(start-2, start + closeTag.length - 1)); } lastToken = state.tokens[state.tokens.length-1]; diff --git a/plugins/poll/assets/javascripts/lib/discourse-markdown/poll.js.es6 b/plugins/poll/assets/javascripts/lib/discourse-markdown/poll.js.es6 index 9eefd12ec32..b7b41648e1e 100644 --- a/plugins/poll/assets/javascripts/lib/discourse-markdown/poll.js.es6 +++ b/plugins/poll/assets/javascripts/lib/discourse-markdown/poll.js.es6 @@ -44,8 +44,18 @@ function getHelpText(count, min, max) { function replaceToken(tokens, target, list) { let pos = tokens.indexOf(target); + let level = tokens[pos].level; + tokens.splice(pos, 1, ...list); list[0].map = target.map; + + // resequence levels + for(;pos 0) { level++; } + } } // analyzes the block to that we have poll options @@ -105,6 +115,9 @@ const rule = { after: function(state, openToken, md, raw) { let items = getListItems(state.tokens, openToken); + if (!items) { + return invalidPoll(state, raw); + } const attrs = openToken.bbcode_attrs; @@ -190,6 +203,10 @@ const rule = { replaceToken(state.tokens, openToken, header); + // we got to correct the level on the state + // we just resequenced + state.level = state.tokens[state.tokens.length-1].level; + state.push('poll_close', 'div', -1); token = state.push('poll_open', 'div', 1); diff --git a/plugins/poll/spec/lib/pretty_text_spec.rb b/plugins/poll/spec/lib/pretty_text_spec.rb index e4538713d96..417a2989f4f 100644 --- a/plugins/poll/spec/lib/pretty_text_spec.rb +++ b/plugins/poll/spec/lib/pretty_text_spec.rb @@ -12,6 +12,27 @@ describe PrettyText do SiteSetting.enable_experimental_markdown_it = true end + it 'can properly bake 2 polls' do + md = <<~MD + this is a test + + - i am a list + + [poll] + 1. test 1 + 2. test 2 + [/poll] + + [poll name=poll2] + 1. test 1 + 2. test 2 + [/poll] + MD + + cooked = PrettyText.cook(md) + expect(cooked.scan('class="poll"').length).to eq(2) + end + it 'works correctly for new vs old engine with trivial cases' do md = <<~MD [poll]