From 44fb2a2833d5ce644aeaaee32b20c034cf781915 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 20 Jul 2017 15:33:36 -0400 Subject: [PATCH] DEV: support multiple capture groups for text post process --- .../category-hashtag.js.es6 | 8 ++-- .../discourse-markdown/mentions.js.es6 | 6 +-- .../text-post-process.js.es6 | 37 ++++++++++++++++--- spec/components/pretty_text_spec.rb | 2 - 4 files changed, 39 insertions(+), 14 deletions(-) diff --git a/app/assets/javascripts/pretty-text/engines/discourse-markdown/category-hashtag.js.es6 b/app/assets/javascripts/pretty-text/engines/discourse-markdown/category-hashtag.js.es6 index 60279ee778b..488a528211e 100644 --- a/app/assets/javascripts/pretty-text/engines/discourse-markdown/category-hashtag.js.es6 +++ b/app/assets/javascripts/pretty-text/engines/discourse-markdown/category-hashtag.js.es6 @@ -1,6 +1,6 @@ -function addHashtag(buffer, match, state) { +function addHashtag(buffer, matches, state) { const options = state.md.options.discourse; - const slug = match.slice(1); + const slug = matches[1]; const categoryHashtagLookup = options.categoryHashtagLookup; const result = categoryHashtagLookup && categoryHashtagLookup(slug); @@ -34,7 +34,7 @@ function addHashtag(buffer, match, state) { buffer.push(token); token = new state.Token('text', '', 0); - token.content = match; + token.content = matches[0]; buffer.push(token); token = new state.Token('span_close', 'span', -1); @@ -46,7 +46,7 @@ export function setup(helper) { helper.registerPlugin(md=>{ const rule = { - matcher: /#[\w-:]{1,101}/, + matcher: /#([\w-:]{1,101})/, onMatch: addHashtag }; diff --git a/app/assets/javascripts/pretty-text/engines/discourse-markdown/mentions.js.es6 b/app/assets/javascripts/pretty-text/engines/discourse-markdown/mentions.js.es6 index 3a8947fef34..54a5284372b 100644 --- a/app/assets/javascripts/pretty-text/engines/discourse-markdown/mentions.js.es6 +++ b/app/assets/javascripts/pretty-text/engines/discourse-markdown/mentions.js.es6 @@ -1,5 +1,5 @@ -function addMention(buffer, match, state) { - let username = match.slice(1); +function addMention(buffer, matches, state) { + let username = matches[1] || matches[2]; let mentionLookup = state.md.options.discourse.mentionLookup; let getURL = state.md.options.discourse.getURL; @@ -39,7 +39,7 @@ export function setup(helper) { helper.registerPlugin(md => { const rule = { - matcher: /@\w[\w.-]{0,58}\w|@\w/, + matcher: /@(\w[\w.-]{0,58}\w)|@(\w)/, onMatch: addMention }; diff --git a/app/assets/javascripts/pretty-text/engines/discourse-markdown/text-post-process.js.es6 b/app/assets/javascripts/pretty-text/engines/discourse-markdown/text-post-process.js.es6 index d189bb60796..38bea8abf18 100644 --- a/app/assets/javascripts/pretty-text/engines/discourse-markdown/text-post-process.js.es6 +++ b/app/assets/javascripts/pretty-text/engines/discourse-markdown/text-post-process.js.es6 @@ -12,18 +12,45 @@ export class TextPostProcessRuler { getMatcher() { if (this.matcher) { return this.matcher; } - this.matcher = new RegExp(this.rules.map((r) => - "(" + r.rule.matcher.toString().slice(1,-1) + ")" - ).join('|'), 'g'); + this.matcherIndex = []; + let rules = this.rules.map((r) => + "(" + r.rule.matcher.toString().slice(1,-1) + ")" + ); + + let i; + let regexString = ""; + let last = 1; + + // this code is a bit tricky, our matcher may have multiple capture groups + // we want to dynamically determine how many + for(i=0; i0) { + regexString = regexString + '|'; + } + regexString = regexString + rules[i]; + + let regex = new RegExp(regexString + '|(x)'); + last = 'x'.match(regex).length - 1; + } + + this.matcher = new RegExp(rules.join('|'), 'g'); return this.matcher; } applyRule(buffer, match, state) { let i; for(i=0; i