2016-06-14 14:31:51 -04:00
|
|
|
import { lookupCache } from 'pretty-text/oneboxer';
|
2017-07-21 13:12:30 -04:00
|
|
|
import { cachedInlineOnebox } from 'pretty-text/inline-oneboxer';
|
|
|
|
|
|
|
|
const ONEBOX = 1;
|
|
|
|
const INLINE = 2;
|
2016-06-14 14:31:51 -04:00
|
|
|
|
2017-08-02 17:27:41 -04:00
|
|
|
function isTopLevel(href) {
|
|
|
|
let split = href.split(/https?:\/\/[^\/]+[\/?]/i);
|
|
|
|
let hasExtra = split && split[1] && split[1].length > 0;
|
|
|
|
return !hasExtra;
|
|
|
|
}
|
|
|
|
|
2017-07-14 08:27:28 -04:00
|
|
|
function applyOnebox(state, silent) {
|
2017-07-21 13:12:30 -04:00
|
|
|
if (silent || !state.tokens) {
|
2017-07-14 08:27:28 -04:00
|
|
|
return;
|
|
|
|
}
|
2016-06-14 14:31:51 -04:00
|
|
|
|
2017-07-21 13:12:30 -04:00
|
|
|
for(let i=1;i<state.tokens.length;i++) {
|
2017-07-14 08:27:28 -04:00
|
|
|
let token = state.tokens[i];
|
|
|
|
let prev = state.tokens[i-1];
|
2017-08-02 16:10:08 -04:00
|
|
|
let mode = prev.type === "paragraph_open" && prev.level === 0 ? ONEBOX : INLINE;
|
2016-06-14 14:31:51 -04:00
|
|
|
|
2017-08-02 16:10:08 -04:00
|
|
|
if (token.type === "inline") {
|
2017-07-21 13:12:30 -04:00
|
|
|
|
|
|
|
let children = token.children;
|
|
|
|
for(let j=0;j<children.length-2;j++){
|
|
|
|
let child = children[j];
|
2016-06-14 14:31:51 -04:00
|
|
|
|
2017-07-14 08:27:28 -04:00
|
|
|
if (child.type === "link_open" && child.markup === 'linkify' && child.info === 'auto') {
|
2016-06-14 14:31:51 -04:00
|
|
|
|
2017-07-21 13:12:30 -04:00
|
|
|
if (j > children.length-3) {
|
2017-07-14 08:27:28 -04:00
|
|
|
continue;
|
2017-07-21 13:12:30 -04:00
|
|
|
}
|
2017-06-08 18:02:30 -04:00
|
|
|
|
2017-07-21 13:12:30 -04:00
|
|
|
if (j === 0 && token.leading_space) {
|
|
|
|
mode = INLINE;
|
|
|
|
} else if (j > 0) {
|
|
|
|
let prevSibling = children[j-1];
|
2017-07-14 08:27:28 -04:00
|
|
|
if (prevSibling.tag !== 'br' || prevSibling.leading_space) {
|
2017-07-21 13:12:30 -04:00
|
|
|
mode = INLINE;
|
2017-07-14 08:27:28 -04:00
|
|
|
}
|
|
|
|
}
|
2017-06-08 18:02:30 -04:00
|
|
|
|
2017-07-14 08:27:28 -04:00
|
|
|
// look ahead for soft or hard break
|
2017-07-21 13:12:30 -04:00
|
|
|
let text = children[j+1];
|
|
|
|
let close = children[j+2];
|
|
|
|
let lookahead = children[j+3];
|
2016-06-14 14:31:51 -04:00
|
|
|
|
2017-07-14 08:27:28 -04:00
|
|
|
if (lookahead && lookahead.tag !== 'br') {
|
2017-07-21 13:12:30 -04:00
|
|
|
mode = INLINE;
|
2017-07-14 08:27:28 -04:00
|
|
|
}
|
2016-06-14 14:31:51 -04:00
|
|
|
|
2017-07-14 08:27:28 -04:00
|
|
|
// check attrs only include a href
|
2017-07-21 13:12:30 -04:00
|
|
|
let attrs = child.attrs;
|
2016-06-14 14:31:51 -04:00
|
|
|
|
2017-07-14 08:27:28 -04:00
|
|
|
if (!attrs || attrs.length !== 1 || attrs[0][0] !== "href") {
|
|
|
|
continue;
|
|
|
|
}
|
2017-07-12 18:10:07 -04:00
|
|
|
|
2017-07-21 13:12:30 -04:00
|
|
|
let href = attrs[0][1];
|
|
|
|
|
2017-07-14 08:27:28 -04:00
|
|
|
// edge case ... what if this is not http or protocoless?
|
2017-07-21 13:12:30 -04:00
|
|
|
if (!/^http|^\/\//i.test(href)) {
|
2017-07-14 08:27:28 -04:00
|
|
|
continue;
|
|
|
|
}
|
2016-06-14 14:31:51 -04:00
|
|
|
|
2017-07-14 08:27:28 -04:00
|
|
|
// we already know text matches cause it is an auto link
|
|
|
|
if (!close || close.type !== "link_close") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-07-21 13:12:30 -04:00
|
|
|
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;
|
|
|
|
|
|
|
|
text.type = 'html_raw';
|
|
|
|
text.content = '';
|
|
|
|
text.inline = true;
|
2016-06-14 14:31:51 -04:00
|
|
|
|
2017-07-21 13:12:30 -04:00
|
|
|
close.type = 'html_raw';
|
|
|
|
close.content = '';
|
|
|
|
close.inline = true;
|
2016-06-14 14:31:51 -04:00
|
|
|
|
2017-07-21 13:12:30 -04:00
|
|
|
} else {
|
|
|
|
// decorate...
|
|
|
|
attrs.push(["class", "onebox"]);
|
|
|
|
attrs.push(["target", "_blank"]);
|
|
|
|
}
|
|
|
|
} else if (mode === INLINE) {
|
2016-06-14 14:31:51 -04:00
|
|
|
|
2017-08-02 17:27:41 -04:00
|
|
|
if (!isTopLevel(href)) {
|
|
|
|
let onebox = cachedInlineOnebox(href);
|
|
|
|
|
|
|
|
let options = state.md.options.discourse;
|
|
|
|
if (options.lookupInlineOnebox) {
|
|
|
|
onebox = options.lookupInlineOnebox(href);
|
|
|
|
}
|
2016-06-14 14:31:51 -04:00
|
|
|
|
2017-08-02 17:27:41 -04:00
|
|
|
if (onebox) {
|
|
|
|
text.content = onebox.title;
|
|
|
|
} else if (state.md.options.discourse.previewing) {
|
|
|
|
attrs.push(["class", "inline-onebox-loading"]);
|
|
|
|
}
|
2017-07-21 13:12:30 -04:00
|
|
|
}
|
2017-07-14 08:27:28 -04:00
|
|
|
}
|
2017-07-21 13:12:30 -04:00
|
|
|
|
2017-07-14 08:27:28 -04:00
|
|
|
}
|
2016-06-14 14:31:51 -04:00
|
|
|
}
|
|
|
|
}
|
2017-07-14 08:27:28 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function setup(helper) {
|
|
|
|
helper.registerPlugin(md => {
|
|
|
|
md.core.ruler.after('linkify', 'onebox', applyOnebox);
|
2016-06-14 14:31:51 -04:00
|
|
|
});
|
|
|
|
}
|