51 lines
1.3 KiB
JavaScript
51 lines
1.3 KiB
JavaScript
import Controller from "@ember/controller";
|
|
import ModalFunctionality from "discourse/mixins/modal-functionality";
|
|
import discourseComputed from "discourse-common/utils/decorators";
|
|
import { equal } from "@ember/object/computed";
|
|
|
|
export default Controller.extend(ModalFunctionality, {
|
|
isReplace: equal("model.nameKey", "replace"),
|
|
isTag: equal("model.nameKey", "tag"),
|
|
isLink: equal("model.nameKey", "link"),
|
|
|
|
@discourseComputed(
|
|
"value",
|
|
"model.compiledRegularExpression",
|
|
"model.words",
|
|
"isReplace",
|
|
"isTag",
|
|
"isLink"
|
|
)
|
|
matches(value, regexpString, words, isReplace, isTag, isLink) {
|
|
if (!value || !regexpString) {
|
|
return;
|
|
}
|
|
|
|
const regexp = new RegExp(regexpString, "ig");
|
|
const matches = value.match(regexp) || [];
|
|
|
|
if (isReplace || isLink) {
|
|
return matches.map((match) => ({
|
|
match,
|
|
replacement: words.find((word) =>
|
|
new RegExp(word.regexp, "ig").test(match)
|
|
).replacement,
|
|
}));
|
|
} else if (isTag) {
|
|
return matches.map((match) => {
|
|
const tags = new Set();
|
|
|
|
words.forEach((word) => {
|
|
if (new RegExp(word.regexp, "ig").test(match)) {
|
|
word.replacement.split(",").forEach((tag) => tags.add(tag));
|
|
}
|
|
});
|
|
|
|
return { match, tags: Array.from(tags) };
|
|
});
|
|
}
|
|
|
|
return matches;
|
|
},
|
|
});
|