2019-05-28 21:00:25 -04:00
|
|
|
// add image to array if src has an upload
|
|
|
|
function addImage(uploads, token) {
|
|
|
|
if (token.attrs) {
|
|
|
|
for (let i = 0; i < token.attrs.length; i++) {
|
|
|
|
if (token.attrs[i][1].indexOf("upload://") === 0) {
|
|
|
|
uploads.push([token, i]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function rule(state) {
|
|
|
|
let uploads = [];
|
|
|
|
|
|
|
|
for (let i = 0; i < state.tokens.length; i++) {
|
|
|
|
let blockToken = state.tokens[i];
|
|
|
|
|
|
|
|
if (blockToken.tag === "img" || blockToken.tag === "a") {
|
|
|
|
addImage(uploads, blockToken);
|
|
|
|
}
|
|
|
|
|
2019-11-12 15:32:37 -05:00
|
|
|
if (!blockToken.children) continue;
|
2019-05-28 21:00:25 -04:00
|
|
|
|
|
|
|
for (let j = 0; j < blockToken.children.length; j++) {
|
|
|
|
let token = blockToken.children[j];
|
2019-11-12 15:32:37 -05:00
|
|
|
|
|
|
|
if (token.tag === "img" || token.tag === "a") addImage(uploads, token);
|
2019-05-28 21:00:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uploads.length > 0) {
|
|
|
|
let srcList = uploads.map(([token, srcIndex]) => token.attrs[srcIndex][1]);
|
|
|
|
let lookup = state.md.options.discourse.lookupUploadUrls;
|
|
|
|
let longUrls = (lookup && lookup(srcList)) || {};
|
|
|
|
|
|
|
|
uploads.forEach(([token, srcIndex]) => {
|
|
|
|
let origSrc = token.attrs[srcIndex][1];
|
|
|
|
let mapped = longUrls[origSrc];
|
|
|
|
|
|
|
|
switch (token.tag) {
|
|
|
|
case "img":
|
|
|
|
if (mapped) {
|
|
|
|
token.attrs[srcIndex][1] = mapped.url;
|
2019-06-10 21:15:45 -04:00
|
|
|
token.attrs.push(["data-base62-sha1", mapped.base62_sha1]);
|
2019-05-28 21:00:25 -04:00
|
|
|
} else {
|
|
|
|
token.attrs[srcIndex][1] = state.md.options.discourse.getURL(
|
|
|
|
"/images/transparent.png"
|
|
|
|
);
|
|
|
|
|
|
|
|
token.attrs.push(["data-orig-src", origSrc]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "a":
|
|
|
|
if (mapped) {
|
|
|
|
token.attrs[srcIndex][1] = mapped.short_path;
|
|
|
|
} else {
|
|
|
|
token.attrs[srcIndex][1] = state.md.options.discourse.getURL(
|
|
|
|
"/404"
|
|
|
|
);
|
|
|
|
|
|
|
|
token.attrs.push(["data-orig-href", origSrc]);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setup(helper) {
|
|
|
|
const opts = helper.getOptions();
|
|
|
|
if (opts.previewing) helper.whiteList(["img.resizable"]);
|
2019-06-10 21:15:45 -04:00
|
|
|
|
|
|
|
helper.whiteList([
|
|
|
|
"img[data-orig-src]",
|
|
|
|
"img[data-base62-sha1]",
|
|
|
|
"a[data-orig-href]"
|
|
|
|
]);
|
2019-05-28 21:00:25 -04:00
|
|
|
|
|
|
|
helper.registerPlugin(md => {
|
|
|
|
md.core.ruler.push("upload-protocol", rule);
|
|
|
|
});
|
|
|
|
}
|