-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary.js
146 lines (116 loc) · 3.94 KB
/
library.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
'use strict';
const XRegExp = require('xregexp');
const meta = require.main.require('./src/meta');
const slugify = require.main.require('./src/slugify');
const socketPlugins = require.main.require('./src/socket.io/plugins');
const utils = require.main.require('./src/utils');
const controllers = require('./lib/controllers');
const utility = require('./lib/utility');
const plugin = module.exports;
socketPlugins.linkMentions = {};
const PLUGIN_HASH = 'link-mentions';
const SYMBOL = '|';
const REGEX = XRegExp(`(?:^|\\s|\\>|;)(\\${SYMBOL}[\\p{L}\\d\\-_.]+)`, 'g');
const isLatinMention = /@[\w\d\-_.]+$/;
plugin.init = async (params) => {
const { router } = params;
const routeHelpers = require.main.require('./src/routes/helpers');
routeHelpers.setupAdminPageRoute(router, '/admin/plugins/link-mentions', controllers.renderAdminPage);
};
plugin.addAdminNavigation = function (header, callback) {
header.plugins.push({
route: '/plugins/link-mentions',
icon: 'fa-tint',
name: 'Link Mentions',
});
callback(null, header);
};
plugin.validateSettings = function (data) {
const { plugin, settings } = data;
if (PLUGIN_HASH !== plugin) {
return data;
}
if (!Array.isArray(settings['mention-list'])) {
settings['mention-list'] = [];
}
const keywords = settings['mention-list'].map(pair => pair.keyword);
data.settings = {
'mention-list': settings['mention-list']
.filter((pair, idx) => pair.keyword.trim() &&
pair.link.trim() &&
utils.isAbsoluteUrl(pair.link) &&
idx === keywords.indexOf(pair.keyword))
.map((pair) => {
pair.keyword = utils.stripHTMLTags(pair.keyword.trim());
pair.link = utils.stripHTMLTags(pair.link.trim());
return pair;
}),
};
return data;
};
socketPlugins.linkMentions.keywordSearch = async function (socket, data) {
const query = data.query || '';
const settings = await meta.settings.get(PLUGIN_HASH);
if (!settings || !Array.isArray(settings['mention-list'])) {
return [];
}
return settings['mention-list'].map(pair => pair.keyword).filter(k => k.startsWith(query));
};
plugin.parsePost = async function (data) {
if (!data || !data.postData || !data.postData.content) {
return data;
}
data.postData.content = await plugin.parseRaw(data.postData.content);
return data;
};
plugin.parseRaw = async function (content) {
let matches = [];
let splitContent = utility.split(content, false, false, true);
splitContent.forEach(function (cleanedContent, i) {
// skip code/blockquote
if ((i % 2) === 0) {
matches = matches.concat(cleanedContent.match(REGEX) || []);
}
});
if (!matches.length) {
return content;
}
// Filter duplicates, clean up cruft
matches = matches.filter((cur, idx) => idx === matches.indexOf(cur))
.map(function (match) {
const atIndex = match.indexOf(SYMBOL);
return atIndex !== 0 ? match.slice(atIndex) : match;
});
const keyword2Pair = await getKeyword2PairTable();
matches.forEach((match) => {
match = removePunctuationSuffix(match.slice(1));
const pair = keyword2Pair[slugify(match)];
if (!pair) {
return;
}
const matchRegex = isLatinMention.test(match) ?
new RegExp(`(?:^|\\s|>|;)\\${SYMBOL}${match}\\b`, 'g') :
new RegExp(`(?:^|\\s|>|;)\\${SYMBOL}${match}`, 'g');
splitContent = splitContent.map((c, i) => {
if ((i % 2) === 1) {
return c;
}
return c.replace(matchRegex, function (inlineMatch) {
// Adding leftover bits
const plain = inlineMatch.slice(0, inlineMatch.indexOf(SYMBOL));
return `${plain}<a class="plugin-link-mentions-keyword plugin-mentions-a" href="${pair.link}">${pair.keyword}</a>`;
});
});
});
return splitContent.join('');
};
async function getKeyword2PairTable() {
const settings = await meta.settings.get(PLUGIN_HASH);
if (!settings || !Array.isArray(settings['mention-list'])) {
return {};
}
return Object.fromEntries(settings['mention-list'].map(pair => [slugify(pair.keyword), { ...pair }]));
}
function removePunctuationSuffix(string) {
return string.replace(/[!?.]*$/, '');
}