forked from NodeBB/nodebb-plugin-glossary
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrary.js
More file actions
126 lines (110 loc) · 3.46 KB
/
library.js
File metadata and controls
126 lines (110 loc) · 3.46 KB
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
'use strict';
const controllers = require('./lib/controllers');
const db = require.main.require('./src/database');
const cache = require.main.require('./src/cache');
const utils = require.main.require('./src/utils');
const routeHelpers = require.main.require('./src/routes/helpers');
const meta = require.main.require('./src/meta');
const pubsub = require.main.require('./src/pubsub');
const socketAdminPlugins = require.main.require('./src/socket.io/admin/plugins');
const plugin = module.exports;
let settings;
plugin.init = async (params) => {
const { router } = params;
await loadSettings();
pubsub.on(`action:settings.set.glossary`, async () => {
await loadSettings();
});
socketAdminPlugins.glossary = {};
socketAdminPlugins.glossary.empty = async function () {
const ids = await db.getSortedSetRange(`settings:glossary:sorted-list:keywords`, 0, -1);
await db.deleteAll(ids.map(id => `settings:glossary:sorted-list:keywords:${id}`));
await db.delete(`settings:glossary:sorted-list:keywords`);
cache.del(`settings:glossary`);
await loadSettings();
};
routeHelpers.setupPageRoute(router, '/glossary', [], controllers.renderGlossary);
routeHelpers.setupAdminPageRoute(router, '/admin/plugins/glossary', [], controllers.renderAdminPage);
};
async function loadSettings() {
settings = await meta.settings.get('glossary');
if (!settings.hasOwnProperty('icon')) {
settings.icon = 'fa-info';
await meta.settings.setOne('glossary', 'icon', 'fa-info');
}
plugin.generateRegexes(settings);
}
plugin.generateRegexes = function (settings) {
if (Array.isArray(settings.keywords)) {
let options = '';
if (settings.singleMatch !== 'on') {
options += 'g';
}
if (settings.caseSensitive !== 'on') {
options += 'i';
}
settings.keywords.forEach((keyword) => {
if (keyword && keyword.name) {
const escaped = utils.escapeRegexChars(keyword.name);
keyword.nameRegex = new RegExp(`\\b(${escaped})\\b(?=[^>]*<)`, options);
}
});
}
};
plugin.filterParsePost = async (hookData) => {
if (hookData.postData && hookData.postData.content && settings.enabled !== 'off') {
plugin.parsePost(hookData.postData, settings);
}
return hookData;
};
plugin.parsePost = function (postData, settings) {
if (Array.isArray(settings.keywords)) {
let iconHtml = '';
if (settings.icon) {
iconHtml = ` <i class="fa ${settings.icon} fa-sm text-muted"></i>`;
}
settings.keywords.forEach((keyword) => {
postData.content = postData.content.replace(
keyword.nameRegex,
(match, p1) => `<span class="glossary-wrapper" title="${keyword.description}" data-bs-toggle="tooltip" data-bs-placement="top"><span class="glossary-word">${p1}</span>${iconHtml}</span>`,
);
});
}
};
plugin.filterTeasersConfigureStripTags = function (hookData) {
hookData.tags = hookData.tags.filter(tag => tag !== 'span');
return hookData;
};
plugin.defineWidgetAreas = async function (areas) {
areas = areas.concat([
{
name: '[[glossary:widgets.header]]',
template: 'glossary.tpl',
location: 'header',
},
{
name: '[[glossary:widgets.left]]',
template: 'glossary.tpl',
location: 'left',
},
{
name: '[[glossary:widgets.right]]',
template: 'glossary.tpl',
location: 'right',
},
{
name: '[[glossary:widgets.footer]]',
template: 'glossary.tpl',
location: 'footer',
},
]);
return areas;
};
plugin.addAdminNavigation = (header) => {
header.plugins.push({
route: '/plugins/glossary',
icon: 'fa-info',
name: '[[glossary:title]]',
});
return header;
};