From 98e03b04b50624110572558c49acaa520f28d7ed Mon Sep 17 00:00:00 2001 From: Sam <sam.saffron@gmail.com> Date: Tue, 11 Jul 2017 16:48:25 -0400 Subject: [PATCH] Don't depend on imports for md extensions --- .../engines/discourse-markdown-it.js.es6 | 9 +++ .../markdown-it/category-hashtag.js.es6 | 4 +- .../engines/markdown-it/emoji.js.es6 | 3 +- .../engines/markdown-it/helpers.js.es6 | 60 ------------------- lib/pretty_text.rb | 4 ++ lib/pretty_text/shims.js | 2 +- 6 files changed, 16 insertions(+), 66 deletions(-) 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 4029171cef3..ac80029cfdb 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 @@ -143,11 +143,16 @@ function setupImageDimensions(md) { md.renderer.rules.image = renderImage; } +let Helpers; + export function setup(opts, siteSettings, state) { if (opts.setup) { return; } + // we got to require this late cause bundle is not loaded in pretty-text + Helpers = Helpers || requirejs('pretty-text/engines/markdown-it/helpers'); + opts.markdownIt = true; let optionCallbacks = []; @@ -190,6 +195,10 @@ export function setup(opts, siteSettings, state) { delete opts[entry]; }); + copy.helpers = { + textReplace: Helpers.textReplace + }; + opts.discourse = copy; getOptions.f = () => opts.discourse; diff --git a/app/assets/javascripts/pretty-text/engines/markdown-it/category-hashtag.js.es6 b/app/assets/javascripts/pretty-text/engines/markdown-it/category-hashtag.js.es6 index b2c1e8ab44c..79d57002a68 100644 --- a/app/assets/javascripts/pretty-text/engines/markdown-it/category-hashtag.js.es6 +++ b/app/assets/javascripts/pretty-text/engines/markdown-it/category-hashtag.js.es6 @@ -1,5 +1,3 @@ -import { textReplace } from 'pretty-text/engines/markdown-it/helpers'; - function addHashtag(buffer, matches, state) { const options = state.md.options.discourse; const [hashtag, slug] = matches; @@ -99,7 +97,7 @@ export function setup(helper) { helper.registerPlugin(md=>{ - md.core.ruler.push('category-hashtag', state => textReplace( + md.core.ruler.push('category-hashtag', state => md.options.discourse.helpers.textReplace( state, applyHashtag, true /* skip all links */ )); }); diff --git a/app/assets/javascripts/pretty-text/engines/markdown-it/emoji.js.es6 b/app/assets/javascripts/pretty-text/engines/markdown-it/emoji.js.es6 index dd85d24f34a..0e4eed203bf 100644 --- a/app/assets/javascripts/pretty-text/engines/markdown-it/emoji.js.es6 +++ b/app/assets/javascripts/pretty-text/engines/markdown-it/emoji.js.es6 @@ -1,6 +1,5 @@ import { buildEmojiUrl, isCustomEmoji } from 'pretty-text/emoji'; import { translations } from 'pretty-text/emoji/data'; -import { textReplace } from 'pretty-text/engines/markdown-it/helpers'; const MAX_NAME_LENGTH = 60; @@ -240,7 +239,7 @@ export function setup(helper) { }); helper.registerPlugin((md)=>{ - md.core.ruler.push('emoji', state => textReplace( + md.core.ruler.push('emoji', state => md.options.discourse.helpers.textReplace( state, (c,s)=>applyEmoji(c,s,md.options.discourse.emojiUnicodeReplacer)) ); }); diff --git a/app/assets/javascripts/pretty-text/engines/markdown-it/helpers.js.es6 b/app/assets/javascripts/pretty-text/engines/markdown-it/helpers.js.es6 index b016b5a86c4..c689d5c633b 100644 --- a/app/assets/javascripts/pretty-text/engines/markdown-it/helpers.js.es6 +++ b/app/assets/javascripts/pretty-text/engines/markdown-it/helpers.js.es6 @@ -13,66 +13,6 @@ export default null; // emitter: emitter // }); -export function inlineRegexRule(md, options) { - - const start = options.start.charCodeAt(0); - const maxLength = (options.maxLength || 500) + 1; - - return function(state, silent) { - const pos = state.pos; - - if (state.src.charCodeAt(pos) !== start || silent) { - return false; - } - - // test prev - if (pos > 0) { - let prev = state.src.charCodeAt(pos-1); - if (!md.utils.isWhiteSpace(prev) && !md.utils.isPunctChar(String.fromCharCode(prev))) { - return false; - } - } - - // skip if in a link - if (options.skipInLink && state.tokens) { - let i; - for(i=state.tokens.length-1;i>=0;i--) { - let token = state.tokens[i]; - let type = token.type; - if (type === 'link_open' || (type === 'html_inline' && token.content.substr(0,2).toLowerCase() === "<a")) { - return false; - } - if (type.block || type === 'link_close' || (type === 'html_inline' && token.content.substr(0,4).toLowerCase() === "</a>")) { - break; - } - } - } - - const substr = state.src.slice(pos, Math.min(pos + maxLength,state.posMax)); - - const matches = options.matcher.exec(substr); - if (!matches) { - return false; - } - - // got to test trailing boundary - const finalPos = pos+matches[0].length; - if (finalPos < state.posMax) { - const trailing = state.src.charCodeAt(finalPos); - if (!md.utils.isSpace(trailing) && !md.utils.isPunctChar(String.fromCharCode(trailing))) { - return false; - } - } - - if (options.emitter(matches, state)) { - state.pos = Math.min(state.posMax, finalPos); - return true; - } - - return false; - - }; -} // based off https://github.com/markdown-it/markdown-it-emoji/blob/master/dist/markdown-it-emoji.js // diff --git a/lib/pretty_text.rb b/lib/pretty_text.rb index 3231301f53a..2b9b3489b78 100644 --- a/lib/pretty_text.rb +++ b/lib/pretty_text.rb @@ -81,6 +81,10 @@ module PrettyText ctx_load(ctx, "vendor/assets/javascripts/lodash.js") ctx_load_manifest(ctx, "pretty-text-bundle.js") + if SiteSetting.enable_experimental_markdown_it + ctx_load_manifest(ctx, "markdown-it-bundle.js") + end + root_path = "#{Rails.root}/app/assets/javascripts/" apply_es6_file(ctx, root_path, "discourse/lib/utilities") diff --git a/lib/pretty_text/shims.js b/lib/pretty_text/shims.js index afb1a199a37..b3efe6cdf54 100644 --- a/lib/pretty_text/shims.js +++ b/lib/pretty_text/shims.js @@ -8,7 +8,7 @@ __emojiUnicodeReplacer = null; __setUnicode = function(replacements) { require('pretty-text/engines/discourse-markdown/emoji').setUnicodeReplacements(replacements); - + let unicodeRegexp = new RegExp(Object.keys(replacements).sort().reverse().join("|"), "g"); __emojiUnicodeReplacer = function(text) {