2017-07-14 08:27:28 -04:00
|
|
|
import { parseBBCodeTag } from "pretty-text/engines/discourse-markdown/bbcode-block";
|
2017-06-08 18:02:30 -04:00
|
|
|
|
2020-07-27 05:22:57 -04:00
|
|
|
function tokenizeBBCode(state, silent, ruler) {
|
2017-06-08 18:02:30 -04:00
|
|
|
let pos = state.pos;
|
|
|
|
|
|
|
|
// 91 = [
|
|
|
|
if (silent || state.src.charCodeAt(pos) !== 91) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const tagInfo = parseBBCodeTag(state.src, pos, state.posMax);
|
|
|
|
|
|
|
|
if (!tagInfo) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-29 13:59:40 -04:00
|
|
|
let rule, i;
|
2017-06-23 11:36:45 -04:00
|
|
|
|
2017-06-30 15:19:07 -04:00
|
|
|
let ruleInfo = ruler.getRuleForTag(tagInfo.tag);
|
|
|
|
if (!ruleInfo) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
rule = ruleInfo.rule;
|
2017-06-23 11:36:45 -04:00
|
|
|
|
2017-06-29 13:59:40 -04:00
|
|
|
if (rule.replace) {
|
|
|
|
// special handling for replace
|
|
|
|
// we pass raw contents to callback so we simply need to greedy match to end tag
|
|
|
|
if (tagInfo.closing) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-06-08 18:02:30 -04:00
|
|
|
|
2017-06-29 13:59:40 -04:00
|
|
|
let closeTag = "[/" + tagInfo.tag + "]";
|
|
|
|
let found = false;
|
2017-06-08 18:02:30 -04:00
|
|
|
|
2017-06-29 13:59:40 -04:00
|
|
|
for (
|
|
|
|
i = state.pos + tagInfo.length;
|
|
|
|
i <= state.posMax - closeTag.length;
|
|
|
|
i++
|
|
|
|
) {
|
|
|
|
if (
|
|
|
|
state.src.charCodeAt(pos) === 91 &&
|
|
|
|
state.src.slice(i, i + closeTag.length).toLowerCase() === closeTag
|
2018-06-15 11:03:24 -04:00
|
|
|
) {
|
2017-06-29 13:59:40 -04:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let content = state.src.slice(state.pos + tagInfo.length, i);
|
2017-06-08 18:02:30 -04:00
|
|
|
|
2017-06-29 13:59:40 -04:00
|
|
|
if (rule.replace(state, tagInfo, content)) {
|
|
|
|
state.pos = i + closeTag.length;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tagInfo.rule = rule;
|
|
|
|
|
|
|
|
let token = state.push("text", "", 0);
|
|
|
|
token.content = state.src.slice(pos, pos + tagInfo.length);
|
2017-07-14 08:27:28 -04:00
|
|
|
token.meta = "bbcode";
|
2017-06-29 13:59:40 -04:00
|
|
|
|
|
|
|
state.delimiters.push({
|
|
|
|
bbInfo: tagInfo,
|
|
|
|
marker: "bb" + tagInfo.tag,
|
|
|
|
open: !tagInfo.closing,
|
|
|
|
close: !!tagInfo.closing,
|
|
|
|
token: state.tokens.length - 1,
|
|
|
|
level: state.level,
|
|
|
|
end: -1,
|
|
|
|
jump: 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
state.pos = pos + tagInfo.length;
|
|
|
|
return true;
|
|
|
|
}
|
2017-06-08 18:02:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function processBBCode(state, silent) {
|
|
|
|
let i,
|
|
|
|
startDelim,
|
|
|
|
endDelim,
|
|
|
|
tagInfo,
|
|
|
|
delimiters = state.delimiters,
|
|
|
|
max = delimiters.length;
|
|
|
|
|
|
|
|
if (silent) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < max - 1; i++) {
|
|
|
|
startDelim = delimiters[i];
|
|
|
|
tagInfo = startDelim.bbInfo;
|
|
|
|
|
|
|
|
if (!tagInfo) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (startDelim.end === -1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
endDelim = delimiters[startDelim.end];
|
|
|
|
|
2017-06-29 16:43:57 -04:00
|
|
|
let tag, className;
|
|
|
|
|
2022-06-20 09:25:13 -04:00
|
|
|
const startToken = state.tokens[startDelim.token];
|
|
|
|
const endToken = state.tokens[endDelim.token];
|
|
|
|
|
2017-06-29 16:43:57 -04:00
|
|
|
if (typeof tagInfo.rule.wrap === "function") {
|
2017-07-14 08:27:28 -04:00
|
|
|
let content = "";
|
|
|
|
for (let j = startDelim.token + 1; j < endDelim.token; j++) {
|
|
|
|
let inner = state.tokens[j];
|
|
|
|
if (inner.type === "text" && inner.meta !== "bbcode") {
|
|
|
|
content += inner.content;
|
|
|
|
}
|
2017-06-29 16:43:57 -04:00
|
|
|
}
|
2022-06-20 09:25:13 -04:00
|
|
|
tagInfo.rule.wrap(startToken, endToken, tagInfo, content, state);
|
2017-07-14 08:27:28 -04:00
|
|
|
continue;
|
2017-06-29 16:43:57 -04:00
|
|
|
} else {
|
|
|
|
let split = tagInfo.rule.wrap.split(".");
|
|
|
|
tag = split[0];
|
|
|
|
className = split.slice(1).join(" ");
|
|
|
|
}
|
2017-06-23 11:36:45 -04:00
|
|
|
|
2022-06-20 09:25:13 -04:00
|
|
|
startToken.type = "bbcode_" + tagInfo.tag + "_open";
|
|
|
|
startToken.tag = tag;
|
2017-06-23 11:36:45 -04:00
|
|
|
if (className) {
|
2022-06-20 09:25:13 -04:00
|
|
|
startToken.attrs = [["class", className]];
|
2017-06-23 11:36:45 -04:00
|
|
|
}
|
2022-06-20 09:25:13 -04:00
|
|
|
startToken.nesting = 1;
|
|
|
|
startToken.markup = startToken.content;
|
|
|
|
startToken.content = "";
|
|
|
|
|
|
|
|
endToken.type = "bbcode_" + tagInfo.tag + "_close";
|
|
|
|
endToken.tag = tag;
|
|
|
|
endToken.nesting = -1;
|
|
|
|
endToken.markup = startToken.content;
|
|
|
|
endToken.content = "";
|
2017-06-08 18:02:30 -04:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setup(helper) {
|
2020-10-27 22:22:06 -04:00
|
|
|
helper.allowList([
|
2017-06-08 18:02:30 -04:00
|
|
|
"span.bbcode-b",
|
|
|
|
"span.bbcode-i",
|
|
|
|
"span.bbcode-u",
|
|
|
|
"span.bbcode-s",
|
|
|
|
]);
|
|
|
|
|
|
|
|
helper.registerOptions((opts) => {
|
|
|
|
opts.features["bbcode-inline"] = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
helper.registerPlugin((md) => {
|
2017-07-17 16:21:47 -04:00
|
|
|
const ruler = md.inline.bbcode.ruler;
|
2017-06-23 11:36:45 -04:00
|
|
|
|
|
|
|
md.inline.ruler.push("bbcode-inline", (state, silent) =>
|
2020-07-27 05:22:57 -04:00
|
|
|
tokenizeBBCode(state, silent, ruler)
|
2017-06-23 11:36:45 -04:00
|
|
|
);
|
2022-06-20 09:25:13 -04:00
|
|
|
md.inline.ruler2.before("fragments_join", "bbcode-inline", processBBCode);
|
2017-06-23 11:36:45 -04:00
|
|
|
|
2017-06-29 16:04:10 -04:00
|
|
|
ruler.push("code", {
|
|
|
|
tag: "code",
|
2021-11-13 08:01:55 -05:00
|
|
|
replace(state, tagInfo, content) {
|
2017-06-29 16:04:10 -04:00
|
|
|
let token;
|
|
|
|
token = state.push("code_inline", "code", 0);
|
|
|
|
token.content = content;
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-06-20 09:25:13 -04:00
|
|
|
const simpleUrlRegex = /^https?:\/\//;
|
2017-06-29 13:59:40 -04:00
|
|
|
ruler.push("url", {
|
|
|
|
tag: "url",
|
2022-06-20 09:25:13 -04:00
|
|
|
wrap(startToken, endToken, tagInfo, content, state) {
|
2017-07-14 08:27:28 -04:00
|
|
|
const url = (tagInfo.attrs["_default"] || content).trim();
|
2022-06-20 09:25:13 -04:00
|
|
|
let linkifyFound = false;
|
|
|
|
|
|
|
|
if (state.md.options.linkify) {
|
|
|
|
const tokens = state.tokens;
|
|
|
|
const startIndex = tokens.indexOf(startToken);
|
|
|
|
const endIndex = tokens.indexOf(endToken);
|
|
|
|
|
|
|
|
// reuse existing tokens from linkify if they exist
|
|
|
|
for (let index = startIndex + 1; index < endIndex; index++) {
|
|
|
|
const token = tokens[index];
|
|
|
|
|
|
|
|
if (
|
|
|
|
token.markup === "linkify" &&
|
|
|
|
token.info === "auto" &&
|
|
|
|
token.type === "link_open"
|
|
|
|
) {
|
|
|
|
linkifyFound = true;
|
|
|
|
token.attrs.push(["data-bbcode", "true"]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-14 08:27:28 -04:00
|
|
|
|
2022-06-20 09:25:13 -04:00
|
|
|
if (!linkifyFound && simpleUrlRegex.test(url)) {
|
2017-07-14 08:27:28 -04:00
|
|
|
startToken.type = "link_open";
|
|
|
|
startToken.tag = "a";
|
2019-11-15 10:34:26 -05:00
|
|
|
startToken.attrs = [
|
|
|
|
["href", url],
|
|
|
|
["data-bbcode", "true"],
|
|
|
|
];
|
2017-07-14 08:27:28 -04:00
|
|
|
startToken.content = "";
|
|
|
|
startToken.nesting = 1;
|
|
|
|
|
|
|
|
endToken.type = "link_close";
|
|
|
|
endToken.tag = "a";
|
|
|
|
endToken.content = "";
|
|
|
|
endToken.nesting = -1;
|
|
|
|
} else {
|
|
|
|
// just strip the bbcode tag
|
|
|
|
endToken.content = "";
|
|
|
|
startToken.content = "";
|
|
|
|
|
|
|
|
// edge case, we don't want this detected as a onebox if auto linked
|
|
|
|
// this ensures it is not stripped
|
|
|
|
startToken.type = "html_inline";
|
|
|
|
}
|
2017-07-12 18:10:07 -04:00
|
|
|
|
2017-07-14 08:27:28 -04:00
|
|
|
return false;
|
2017-06-29 13:59:40 -04:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
ruler.push("email", {
|
|
|
|
tag: "email",
|
2021-11-13 08:01:55 -05:00
|
|
|
replace(state, tagInfo, content) {
|
2017-06-29 13:59:40 -04:00
|
|
|
let token;
|
2022-06-20 09:25:13 -04:00
|
|
|
const email = tagInfo.attrs["_default"] || content;
|
2017-06-29 13:59:40 -04:00
|
|
|
|
|
|
|
token = state.push("link_open", "a", 1);
|
2019-11-15 10:34:26 -05:00
|
|
|
token.attrs = [
|
|
|
|
["href", "mailto:" + email],
|
|
|
|
["data-bbcode", "true"],
|
|
|
|
];
|
2017-06-29 13:59:40 -04:00
|
|
|
|
|
|
|
token = state.push("text", "", 0);
|
|
|
|
token.content = content;
|
|
|
|
|
2020-05-08 03:19:48 -04:00
|
|
|
state.push("link_close", "a", -1);
|
2017-06-29 13:59:40 -04:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
ruler.push("image", {
|
|
|
|
tag: "img",
|
2021-11-13 08:01:55 -05:00
|
|
|
replace(state, tagInfo, content) {
|
2017-06-29 13:59:40 -04:00
|
|
|
let token = state.push("image", "img", 0);
|
2019-11-15 10:34:26 -05:00
|
|
|
token.attrs = [
|
|
|
|
["src", content],
|
|
|
|
["alt", ""],
|
|
|
|
];
|
2017-06-29 13:59:40 -04:00
|
|
|
token.children = [];
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2017-06-23 11:36:45 -04:00
|
|
|
ruler.push("bold", {
|
|
|
|
tag: "b",
|
|
|
|
wrap: "span.bbcode-b",
|
|
|
|
});
|
|
|
|
|
|
|
|
ruler.push("italic", {
|
|
|
|
tag: "i",
|
|
|
|
wrap: "span.bbcode-i",
|
|
|
|
});
|
|
|
|
|
|
|
|
ruler.push("underline", {
|
|
|
|
tag: "u",
|
|
|
|
wrap: "span.bbcode-u",
|
|
|
|
});
|
|
|
|
|
|
|
|
ruler.push("strike", {
|
|
|
|
tag: "s",
|
|
|
|
wrap: "span.bbcode-s",
|
|
|
|
});
|
2017-06-08 18:02:30 -04:00
|
|
|
});
|
|
|
|
}
|