From aa5b8a57492d1d761282b5dcab8f391ac36934aa Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Fri, 21 Jul 2017 13:12:30 -0400 Subject: [PATCH] REFACTOR: Merge onebox and inline onebox code paths --- app/assets/javascripts/markdown-it-bundle.js | 1 - .../discourse-markdown/inline-onebox.js.es6 | 68 -------------- .../engines/discourse-markdown/onebox.js.es6 | 94 ++++++++++++------- 3 files changed, 59 insertions(+), 104 deletions(-) delete mode 100644 app/assets/javascripts/pretty-text/engines/discourse-markdown/inline-onebox.js.es6 diff --git a/app/assets/javascripts/markdown-it-bundle.js b/app/assets/javascripts/markdown-it-bundle.js index 4ac2c201c8c..81c05f719fd 100644 --- a/app/assets/javascripts/markdown-it-bundle.js +++ b/app/assets/javascripts/markdown-it-bundle.js @@ -4,7 +4,6 @@ //= require ./pretty-text/engines/discourse-markdown/quotes //= require ./pretty-text/engines/discourse-markdown/emoji //= require ./pretty-text/engines/discourse-markdown/onebox -//= require ./pretty-text/engines/discourse-markdown/inline-onebox //= require ./pretty-text/engines/discourse-markdown/bbcode-block //= require ./pretty-text/engines/discourse-markdown/bbcode-inline //= require ./pretty-text/engines/discourse-markdown/code diff --git a/app/assets/javascripts/pretty-text/engines/discourse-markdown/inline-onebox.js.es6 b/app/assets/javascripts/pretty-text/engines/discourse-markdown/inline-onebox.js.es6 deleted file mode 100644 index 4e7cbc6d91c..00000000000 --- a/app/assets/javascripts/pretty-text/engines/discourse-markdown/inline-onebox.js.es6 +++ /dev/null @@ -1,68 +0,0 @@ -import { cachedInlineOnebox } from 'pretty-text/inline-oneboxer'; - -function applyInlineOnebox(state, silent) { - if (silent || !state.tokens) { - return; - } - - for (let i=1; i children.length-3) { - continue; - } - - let text = children[j+1]; - let close = children[j+2]; - - // check attrs only include a href - let attrs = child.attrs; - if (!attrs || attrs.length !== 1 || attrs[0][0] !== "href") { - continue; - } - - let href = attrs[0][1]; - if (!/^http|^\/\//i.test(href)) { - continue; - } - - // we already know text matches cause it is an auto link - if (!close || close.type !== "link_close") { - continue; - } - - // link must be the same as the href - if (!text || text.content !== href) { - continue; - } - - // check for href - let onebox = cachedInlineOnebox(href); - - let options = state.md.options.discourse; - if (options.lookupInlineOnebox) { - onebox = options.lookupInlineOnebox(href); - } - - if (onebox) { - text.content = onebox.title; - } else if (state.md.options.discourse.previewing) { - attrs.push(["class", "inline-onebox-loading"]); - } - } - } - } - } -} - -export function setup(helper) { - helper.registerPlugin(md => { - md.core.ruler.after('onebox', 'inline-onebox', applyInlineOnebox); - }); -} diff --git a/app/assets/javascripts/pretty-text/engines/discourse-markdown/onebox.js.es6 b/app/assets/javascripts/pretty-text/engines/discourse-markdown/onebox.js.es6 index 47368b5daf2..eff7ee4ae2c 100644 --- a/app/assets/javascripts/pretty-text/engines/discourse-markdown/onebox.js.es6 +++ b/app/assets/javascripts/pretty-text/engines/discourse-markdown/onebox.js.es6 @@ -1,53 +1,61 @@ import { lookupCache } from 'pretty-text/oneboxer'; +import { cachedInlineOnebox } from 'pretty-text/inline-oneboxer'; + +const ONEBOX = 1; +const INLINE = 2; function applyOnebox(state, silent) { - if (silent || !state.tokens || state.tokens.length < 3) { + if (silent || !state.tokens) { return; } - let i; - for(i=1;i children.length-3) { continue; + } + + if (j === 0 && token.leading_space) { + mode = INLINE; } else if (j > 0) { - - let prevSibling = token.children[j-1]; - + let prevSibling = children[j-1]; if (prevSibling.tag !== 'br' || prevSibling.leading_space) { - continue; + mode = INLINE; } } // look ahead for soft or hard break - let text = token.children[j+1]; - let close = token.children[j+2]; - let lookahead = token.children[j+3]; + let text = children[j+1]; + let close = children[j+2]; + let lookahead = children[j+3]; if (lookahead && lookahead.tag !== 'br') { - continue; + mode = INLINE; } // check attrs only include a href - let attrs = child["attrs"]; + let attrs = child.attrs; if (!attrs || attrs.length !== 1 || attrs[0][0] !== "href") { continue; } + let href = attrs[0][1]; + // edge case ... what if this is not http or protocoless? - if (!/^http|^\/\//i.test(attrs[0][1])) { + if (!/^http|^\/\//i.test(href)) { continue; } @@ -56,28 +64,44 @@ function applyOnebox(state, silent) { continue; } - // we already determined earlier that 0 0 was href - let cached = lookupCache(attrs[0][1]); + if (mode === ONEBOX) { + // we already determined earlier that 0 0 was href + let cached = lookupCache(attrs[0][1]); - if (cached) { - // replace link with 2 blank text nodes and inline html for onebox - child.type = 'html_raw'; - child.content = cached; - child.inline = true; + if (cached) { + // replace link with 2 blank text nodes and inline html for onebox + child.type = 'html_raw'; + child.content = cached; + child.inline = true; - text.type = 'html_raw'; - text.content = ''; - text.inline = true; + text.type = 'html_raw'; + text.content = ''; + text.inline = true; - close.type = 'html_raw'; - close.content = ''; - close.inline = true; + close.type = 'html_raw'; + close.content = ''; + close.inline = true; - } else { - // decorate... - attrs.push(["class", "onebox"]); - attrs.push(["target", "_blank"]); + } else { + // decorate... + attrs.push(["class", "onebox"]); + attrs.push(["target", "_blank"]); + } + } else if (mode === INLINE) { + let onebox = cachedInlineOnebox(href); + + let options = state.md.options.discourse; + if (options.lookupInlineOnebox) { + onebox = options.lookupInlineOnebox(href); + } + + if (onebox) { + text.content = onebox.title; + } else if (state.md.options.discourse.previewing) { + attrs.push(["class", "inline-onebox-loading"]); + } } + } } }