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