2021-05-28 00:20:26 +08:00
|
|
|
const MAX_MATCHES = 100;
|
|
|
|
|
2021-02-25 20:00:58 +08:00
|
|
|
function isLinkOpen(str) {
|
|
|
|
return /^<a[>\s]/i.test(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
function isLinkClose(str) {
|
|
|
|
return /^<\/a\s*>/i.test(str);
|
|
|
|
}
|
|
|
|
|
2021-05-18 17:09:47 +08:00
|
|
|
function findAllMatches(text, matchers) {
|
2021-02-25 20:00:58 +08:00
|
|
|
const matches = [];
|
|
|
|
|
2021-05-18 17:09:47 +08:00
|
|
|
let count = 0;
|
|
|
|
|
|
|
|
matchers.forEach((matcher) => {
|
|
|
|
let match;
|
|
|
|
while (
|
|
|
|
(match = matcher.pattern.exec(text)) !== null &&
|
2021-05-28 00:20:26 +08:00
|
|
|
count++ < MAX_MATCHES
|
2021-05-18 17:09:47 +08:00
|
|
|
) {
|
|
|
|
matches.push({
|
2021-06-18 23:54:06 +08:00
|
|
|
index: match.index + match[0].indexOf(match[1]),
|
|
|
|
text: match[1],
|
2021-05-18 17:09:47 +08:00
|
|
|
replacement: matcher.replacement,
|
2021-06-02 13:36:49 +08:00
|
|
|
link: matcher.link,
|
2021-05-18 17:09:47 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2021-02-25 20:00:58 +08:00
|
|
|
|
|
|
|
return matches.sort((a, b) => a.index - b.index);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setup(helper) {
|
|
|
|
helper.registerPlugin((md) => {
|
2021-06-02 13:36:49 +08:00
|
|
|
const matchers = [];
|
|
|
|
|
|
|
|
if (md.options.discourse.watchedWordsReplace) {
|
|
|
|
Object.entries(md.options.discourse.watchedWordsReplace).map(
|
|
|
|
([word, replacement]) => {
|
|
|
|
matchers.push({
|
|
|
|
pattern: new RegExp(word, "gi"),
|
|
|
|
replacement,
|
|
|
|
link: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
2021-02-25 20:00:58 +08:00
|
|
|
}
|
|
|
|
|
2021-06-02 13:36:49 +08:00
|
|
|
if (md.options.discourse.watchedWordsLink) {
|
|
|
|
Object.entries(md.options.discourse.watchedWordsLink).map(
|
|
|
|
([word, replacement]) => {
|
|
|
|
matchers.push({
|
|
|
|
pattern: new RegExp(word, "gi"),
|
|
|
|
replacement,
|
|
|
|
link: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (matchers.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
2021-02-25 20:00:58 +08:00
|
|
|
|
|
|
|
const cache = {};
|
|
|
|
|
2021-06-02 13:36:49 +08:00
|
|
|
md.core.ruler.push("watched-words", (state) => {
|
2021-02-25 20:00:58 +08:00
|
|
|
for (let j = 0, l = state.tokens.length; j < l; j++) {
|
|
|
|
if (state.tokens[j].type !== "inline") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let tokens = state.tokens[j].children;
|
|
|
|
|
|
|
|
let htmlLinkLevel = 0;
|
|
|
|
|
|
|
|
// We scan from the end, to keep position when new tags added.
|
|
|
|
// Use reversed logic in links start/end match
|
|
|
|
for (let i = tokens.length - 1; i >= 0; i--) {
|
|
|
|
const currentToken = tokens[i];
|
|
|
|
|
|
|
|
// Skip content of markdown links
|
|
|
|
if (currentToken.type === "link_close") {
|
|
|
|
i--;
|
|
|
|
while (
|
|
|
|
tokens[i].level !== currentToken.level &&
|
|
|
|
tokens[i].type !== "link_open"
|
|
|
|
) {
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip content of html tag links
|
|
|
|
if (currentToken.type === "html_inline") {
|
|
|
|
if (isLinkOpen(currentToken.content) && htmlLinkLevel > 0) {
|
|
|
|
htmlLinkLevel--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isLinkClose(currentToken.content)) {
|
|
|
|
htmlLinkLevel++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (currentToken.type === "text") {
|
|
|
|
const text = currentToken.content;
|
|
|
|
const matches = (cache[text] =
|
2021-05-18 17:09:47 +08:00
|
|
|
cache[text] || findAllMatches(text, matchers));
|
2021-02-25 20:00:58 +08:00
|
|
|
|
|
|
|
// Now split string to nodes
|
|
|
|
const nodes = [];
|
|
|
|
let level = currentToken.level;
|
|
|
|
let lastPos = 0;
|
|
|
|
|
|
|
|
let token;
|
|
|
|
for (let ln = 0; ln < matches.length; ln++) {
|
|
|
|
if (matches[ln].index < lastPos) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-09-09 17:03:59 +08:00
|
|
|
if (
|
|
|
|
matches[ln].index > 0 &&
|
|
|
|
(text[matches[ln].index - 1] === "@" ||
|
|
|
|
text[matches[ln].index - 1] === "#")
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-02-25 20:00:58 +08:00
|
|
|
if (matches[ln].index > lastPos) {
|
|
|
|
token = new state.Token("text", "", 0);
|
|
|
|
token.content = text.slice(lastPos, matches[ln].index);
|
|
|
|
token.level = level;
|
|
|
|
nodes.push(token);
|
|
|
|
}
|
|
|
|
|
2021-06-02 13:36:49 +08:00
|
|
|
if (matches[ln].link) {
|
|
|
|
const url = state.md.normalizeLink(matches[ln].replacement);
|
|
|
|
if (htmlLinkLevel === 0 && state.md.validateLink(url)) {
|
|
|
|
token = new state.Token("link_open", "a", 1);
|
|
|
|
token.attrs = [["href", url]];
|
|
|
|
token.level = level++;
|
|
|
|
token.markup = "linkify";
|
|
|
|
token.info = "auto";
|
|
|
|
nodes.push(token);
|
|
|
|
|
|
|
|
token = new state.Token("text", "", 0);
|
|
|
|
token.content = matches[ln].text;
|
|
|
|
token.level = level;
|
|
|
|
nodes.push(token);
|
|
|
|
|
|
|
|
token = new state.Token("link_close", "a", -1);
|
|
|
|
token.level = --level;
|
|
|
|
token.markup = "linkify";
|
|
|
|
token.info = "auto";
|
|
|
|
nodes.push(token);
|
|
|
|
}
|
2021-02-25 20:00:58 +08:00
|
|
|
} else {
|
|
|
|
token = new state.Token("text", "", 0);
|
|
|
|
token.content = matches[ln].replacement;
|
|
|
|
token.level = level;
|
|
|
|
nodes.push(token);
|
|
|
|
}
|
|
|
|
|
|
|
|
lastPos = matches[ln].index + matches[ln].text.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lastPos < text.length) {
|
|
|
|
token = new state.Token("text", "", 0);
|
|
|
|
token.content = text.slice(lastPos);
|
|
|
|
token.level = level;
|
|
|
|
nodes.push(token);
|
|
|
|
}
|
|
|
|
|
|
|
|
// replace current node
|
|
|
|
state.tokens[j].children = tokens = md.utils.arrayReplaceAt(
|
|
|
|
tokens,
|
|
|
|
i,
|
|
|
|
nodes
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|