/
index.js
489 lines (415 loc) · 15.1 KB
/
index.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
'use strict';
const MarkdownIt = require('markdown-it');
const fs = require('fs');
const path = require('path');
const probe = require('probe-image-size');
const nconf = require.main.require('nconf');
const winston = require.main.require('winston');
const meta = require.main.require('./src/meta');
const posts = require.main.require('./src/posts');
const translator = require.main.require('./src/translator');
const plugins = require.main.require('./src/plugins');
const cacheCreate = require.main.require('./src/cacheCreate');
const SocketPlugins = require.main.require('./src/socket.io/plugins');
SocketPlugins.markdown = require('./websockets');
let parser;
let app;
const Markdown = {
config: {},
_externalImageCache: undefined,
_externalImageFailures: new Set(),
onLoad: async function (params) {
app = params.app;
const { router } = params;
const controllers = require('./lib/controllers');
const hostMiddleware = require.main.require('./src/middleware');
const routeHelpers = require.main.require('./src/routes/helpers');
const middlewares = [
hostMiddleware.maintenanceMode, hostMiddleware.registrationComplete, hostMiddleware.pluginHooks,
];
routeHelpers.setupAdminPageRoute(router, '/admin/plugins/markdown', controllers.renderAdmin);
// Return raw markdown via GET
router.get('/api/post/:pid/raw', middlewares, controllers.retrieveRaw);
Markdown.init();
await Markdown.loadThemes();
},
getConfig: async (config) => {
let { defaultHighlightLanguage, highlightTheme, hljsLanguages, highlightLinesLanguageList, externalMark } = await meta.settings.get('markdown');
try {
hljsLanguages = JSON.parse(hljsLanguages);
} catch (e) {
hljsLanguages = ['common'];
}
config.markdown = {
highlight: Markdown.highlight ? 1 : 0,
highlightLinesLanguageList,
hljsLanguages,
theme: highlightTheme || 'default.css',
defaultHighlightLanguage: defaultHighlightLanguage || '',
externalMark: externalMark === 'on',
};
return config;
},
getLinkTags: async (hookData) => {
const { highlightTheme } = await meta.settings.get('markdown');
hookData.links.push({
rel: 'prefetch stylesheet',
type: '',
href: `${nconf.get('relative_path')}/assets/plugins/nodebb-plugin-markdown/styles/${highlightTheme || 'default.css'}`,
}, {
rel: 'prefetch',
href: `${nconf.get('relative_path')}/assets/language/${meta.config.defaultLang || 'en-GB'}/markdown.json?${meta.config['cache-buster']}`,
});
return hookData;
},
init: function () {
// Load saved config
const _self = this;
const defaults = {
html: false,
langPrefix: 'language-',
highlight: true,
highlightTheme: 'default.css',
probe: true,
probeCacheSize: 256,
xhtmlOut: true,
breaks: true,
linkify: true,
typographer: false,
externalBlank: false,
nofollow: true,
allowRTLO: false,
checkboxes: true,
multimdTables: true,
};
const notCheckboxes = ['langPrefix', 'highlightTheme', 'probeCacheSize'];
meta.settings.get('markdown', (err, options) => {
if (err) {
winston.warn(`[plugin/markdown] Unable to retrieve settings, assuming defaults: ${err.message}`);
}
Object.keys(defaults).forEach((field) => {
// If not set in config (nil)
if (!options.hasOwnProperty(field)) {
_self.config[field] = defaults[field];
} else if (!notCheckboxes.includes(field)) {
_self.config[field] = options[field] === 'on';
} else {
_self.config[field] = options[field];
}
});
_self.highlight = _self.config.highlight;
delete _self.config.highlight;
parser = new MarkdownIt(_self.config);
Markdown.updateParserRules(parser);
// External image size cache
if (_self.config.probe) {
Markdown._externalImageCache = cacheCreate({
name: 'markdown.externalImageCache',
max: parseInt(_self.config.probeCacheSize, 10) || 256,
ttl: 1000 * 60 * 60 * 24, // 1 day
});
}
});
},
loadThemes: async () => {
try {
const files = await fs.promises.readdir(path.join(require.resolve('highlight.js'), '../../styles'));
const isStylesheet = /\.css$/;
Markdown.themes = files.filter(file => isStylesheet.test(file));
} catch (err) {
winston.error(`[plugin/markdown] Could not load Markdown themes: ${err.message}`);
Markdown.themes = [];
}
},
parsePost: async function (data) {
const env = await Markdown.beforeParse(data);
if (data && data.postData && data.postData.content && parser) {
data.postData.content = parser.render(data.postData.content, env || {});
}
return Markdown.afterParse(data);
},
parseSignature: async function (data) {
if (data && data.userData && data.userData.signature && parser) {
data.userData.signature = parser.render(data.userData.signature);
}
return Markdown.afterParse(data);
},
parseAboutMe: async function (aboutme) {
aboutme = (aboutme && parser) ? parser.render(aboutme) : aboutme;
// process.nextTick(next, null, aboutme);
return Markdown.afterParse(aboutme);
},
parseRaw: async function (raw) {
raw = (raw && parser) ? parser.render(raw) : raw;
return Markdown.afterParse(raw);
},
beforeParse: async (data) => {
const env = {
images: new Map(),
};
if (data && data.postData && data.postData.pid) {
// Check that pid for images, and return their sizes
const images = await posts.uploads.listWithSizes(data.postData.pid);
env.images = images.reduce((memo, cur) => {
memo.set(cur.name, cur);
delete cur.name;
return memo;
}, env.images);
}
// Probe post data for external images as well
if (Markdown.config.probe && data && data.postData && data.postData.content) {
const matcher = /!\[[^\]]*?\]\((https?[^)]+?)\)/g;
let current;
// eslint-disable-next-line no-cond-assign
while ((current = matcher.exec(data.postData.content)) !== null) {
const match = current[1];
if (match && Markdown.isExternalLink(match)) { // for security only parse external images
const parsedUrl = new URL(match, nconf.get('url'));
const filename = path.basename(parsedUrl.pathname);
const size = Markdown._externalImageCache.get(match);
// Short-circuit to ignore previous failures
const hasFailed = Markdown._externalImageFailures.has(match);
if (!hasFailed) {
if (size) {
env.images.set(filename, size);
} else {
// Size checked asynchronously, see: https://github.com/tomas/needle/issues/389
probe(match, {
follow_max: 2,
}).then((size) => {
let { width, height } = size;
// Swap width and height if orientation bit is set
if (size.orientation >= 5 && size.orientation <= 8) {
[width, height] = [height, width];
}
Markdown._externalImageCache.set(match, { width, height });
}).catch(() => {
// Likely an issue getting the external image size, ignore in the future
Markdown._externalImageFailures.add(match);
});
}
}
}
}
}
return env;
},
afterParse: function (payload) {
if (!payload) {
return payload;
}
const italicMention = /@<em>([^<]+)<\/em>/g;
const boldMention = /@<strong>([^<]+)<\/strong>/g;
const execute = function (html) {
// Replace all italicised mentions back to regular mentions
if (italicMention.test(html)) {
html = html.replace(italicMention, (match, slug) => `@_${slug}_`);
} else if (boldMention.test(html)) {
html = html.replace(boldMention, (match, slug) => `@__${slug}__`);
}
return html;
};
if (payload.hasOwnProperty('postData')) {
payload.postData.content = execute(payload.postData.content);
} else if (payload.hasOwnProperty('userData')) {
payload.userData.signature = execute(payload.userData.signature);
} else {
payload = execute(payload);
}
return payload;
},
renderHelp: async function (helpContent) {
const translated = await translator.translate('[[markdown:help-text]]');
const parsed = await plugins.hooks.fire('filter:parse.raw', `## Markdown\n${translated}`);
const html = await app.renderAsync('modals/markdown-help', {});
helpContent += parsed + html;
return helpContent;
},
registerFormatting: async function (payload) {
const formatting = [
{ name: 'bold', className: 'fa fa-bold', title: '[[modules:composer.formatting.bold]]' },
{ name: 'italic', className: 'fa fa-italic', title: '[[modules:composer.formatting.italic]]' },
{
className: 'fa fa-heading',
title: '[[modules:composer.formatting.heading]]',
dropdownItems: [
{ name: 'heading1', text: '[[modules:composer.formatting.heading1]]' },
{ name: 'heading2', text: '[[modules:composer.formatting.heading2]]' },
{ name: 'heading3', text: '[[modules:composer.formatting.heading3]]' },
{ name: 'heading4', text: '[[modules:composer.formatting.heading4]]' },
{ name: 'heading5', text: '[[modules:composer.formatting.heading5]]' },
{ name: 'heading6', text: '[[modules:composer.formatting.heading6]]' },
],
},
{ name: 'list', className: 'fa fa-list-ul', title: '[[modules:composer.formatting.list]]' },
{ name: 'strikethrough', className: 'fa fa-strikethrough', title: '[[modules:composer.formatting.strikethrough]]' },
{ name: 'code', className: 'fa fa-code', title: '[[modules:composer.formatting.code]]' },
{ name: 'link', className: 'fa fa-link', title: '[[modules:composer.formatting.link]]' },
{ name: 'picture-o', className: 'fa fa-picture-o', title: '[[modules:composer.formatting.picture]]' },
];
payload.options = formatting.concat(payload.options);
return payload;
},
updateSanitizeConfig: async (config) => {
config.allowedTags.push('input');
config.allowedAttributes.input = ['type', 'checked'];
config.allowedAttributes.ol.push('start');
config.allowedAttributes.th.push('colspan', 'rowspan');
config.allowedAttributes.td.push('colspan', 'rowspan');
return config;
},
updateParserRules: function (parser) {
if (Markdown.config.checkboxes) {
// Add support for checkboxes
parser.use(require('markdown-it-checkbox'), {
divWrap: true,
divClass: 'plugin-markdown',
});
}
if (Markdown.config.multimdTables) {
parser.use(require('markdown-it-multimd-table'), {
multiline: true,
rowspan: true,
headerless: true,
});
}
parser.use((md) => {
md.core.ruler.before('linkify', 'autodir', (state) => {
state.tokens.forEach((token) => {
if (token.type === 'paragraph_open') {
token.attrJoin('dir', 'auto');
}
});
});
});
// Update renderer to add some classes to all images
const renderImage = parser.renderer.rules.image || function (tokens, idx, options, env, self) {
// eslint-disable-next-line prefer-spread,prefer-rest-params
return self.renderToken.apply(self, arguments);
};
const renderLink = parser.renderer.rules.link_open || function (tokens, idx, options, env, self) {
// eslint-disable-next-line prefer-spread,prefer-rest-params
return self.renderToken.apply(self, arguments);
};
const renderTable = parser.renderer.rules.table_open || function (tokens, idx, options, env, self) {
// eslint-disable-next-line prefer-spread,prefer-rest-params
return self.renderToken.apply(self, arguments);
};
parser.renderer.rules.image = function (tokens, idx, options, env, self) {
const token = tokens[idx];
const attributes = new Map(token.attrs);
const parsedSrc = new URL(attributes.get('src'), nconf.get('url'));
// Validate the url
if (!Markdown.isUrlValid(attributes.get('src'))) { return ''; }
token.attrSet('class', `${token.attrGet('class') || ''} img-fluid img-markdown`);
// Append sizes to images
if (parsedSrc.pathname) {
const filename = path.basename(parsedSrc.pathname);
if (env.images && env.images.has(filename)) {
const size = env.images.get(filename);
token.attrSet('width', size.width);
token.attrSet('height', size.height);
}
}
return renderImage(tokens, idx, options, env, self);
};
parser.renderer.rules.link_open = function (tokens, idx, options, env, self) {
// Add target="_blank" to all links
const targetIdx = tokens[idx].attrIndex('target');
let relIdx = tokens[idx].attrIndex('rel');
const hrefIdx = tokens[idx].attrIndex('href');
if (Markdown.isExternalLink(tokens[idx].attrs[hrefIdx][1])) {
if (Markdown.config.externalBlank) {
if (targetIdx < 0) {
tokens[idx].attrPush(['target', '_blank']);
} else {
tokens[idx].attrs[targetIdx][1] = '_blank';
}
if (relIdx < 0) {
tokens[idx].attrPush(['rel', 'noopener noreferrer']);
relIdx = tokens[idx].attrIndex('rel');
} else {
tokens[idx].attrs[relIdx][1] = 'noopener noreferrer';
}
}
if (Markdown.config.nofollow) {
if (relIdx < 0) {
tokens[idx].attrPush(['rel', 'nofollow ugc']);
} else {
tokens[idx].attrs[relIdx][1] += ' nofollow ugc';
}
}
}
if (!Markdown.config.allowRTLO) {
if (tokens[idx + 1] && tokens[idx + 1].type === 'text') {
if (tokens[idx + 1].content.match(Markdown.regexes.rtl_override)) {
tokens[idx + 1].content = tokens[idx + 1].content.replace(Markdown.regexes.rtl_override, '');
}
}
}
return renderLink(tokens, idx, options, env, self);
};
parser.renderer.rules.table_open = function (tokens, idx, options, env, self) {
const classIdx = tokens[idx].attrIndex('class');
if (classIdx < 0) {
tokens[idx].attrPush(['class', 'table table-bordered table-striped']);
} else {
tokens[idx].attrs[classIdx][1] += ' table table-bordered table-striped';
}
return renderTable(tokens, idx, options, env, self);
};
plugins.hooks.fire('action:markdown.updateParserRules', parser);
},
isUrlValid: function (src) {
/**
* Images linking to a relative path are only allowed from the root prefixes
* defined in allowedRoots. We allow both with and without relative_path
* even though upload_url should handle it, because sometimes installs
* migrate to (non-)subfolder and switch mid-way, but the uploads urls don't
* get updated.
*/
const allowedRoots = [nconf.get('upload_url'), '/uploads'];
const allowed = pathname => allowedRoots.some(root => pathname.toString().startsWith(root) || pathname.toString().startsWith(nconf.get('relative_path') + root));
try {
const urlObj = new URL(src, nconf.get('url'));
return !(urlObj.host === null && !allowed(urlObj.pathname));
} catch (e) {
return false;
}
},
isExternalLink: function (urlString) {
let urlObj;
let baseUrlObj;
try {
urlObj = new URL(urlString, nconf.get('url'));
baseUrlObj = nconf.get('url_parsed');
} catch (err) {
return false;
}
if (
urlObj.host === null || // Relative paths are always internal links...
(
urlObj.host === baseUrlObj.host &&
urlObj.protocol === baseUrlObj.protocol && // Otherwise need to check that protocol and host match
(nconf.get('relative_path').length > 0 ? urlObj.pathname.indexOf(nconf.get('relative_path')) === 0 : true) // Subfolder installs need this additional check
)
) {
return false;
}
return true;
},
admin: {
menu: async function (custom_header) {
custom_header.plugins.push({
route: '/plugins/markdown',
icon: 'fa-edit',
name: 'Markdown',
});
return custom_header;
},
},
regexes: {
rtl_override: /\u202E/gi,
},
};
module.exports = Markdown;