nodebb-plugin-markdown 12.2.1 → 12.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -200,7 +200,7 @@ const Markdown = {
200
200
  while ((current = matcher.exec(data.postData.content)) !== null) {
201
201
  const match = current[1];
202
202
  if (match && Markdown.isExternalLink(match)) { // for security only parse external images
203
- const parsedUrl = new URL(match);
203
+ const parsedUrl = new URL(match, nconf.get('url'));
204
204
  const filename = path.basename(parsedUrl.pathname);
205
205
  const size = Markdown._externalImageCache.get(match);
206
206
 
@@ -432,7 +432,7 @@ const Markdown = {
432
432
  const allowed = pathname => allowedRoots.some(root => pathname.toString().startsWith(root) || pathname.toString().startsWith(nconf.get('relative_path') + root));
433
433
 
434
434
  try {
435
- const urlObj = new URL(src);
435
+ const urlObj = new URL(src, nconf.get('url'));
436
436
  return !(urlObj.host === null && !allowed(urlObj.pathname));
437
437
  } catch (e) {
438
438
  return false;
@@ -443,11 +443,7 @@ const Markdown = {
443
443
  let urlObj;
444
444
  let baseUrlObj;
445
445
  try {
446
- if (urlString.startsWith('//')) {
447
- urlString = `${nconf.get('url_parsed').protocol}${urlString}`;
448
- }
449
-
450
- urlObj = new URL(urlString);
446
+ urlObj = new URL(urlString, nconf.get('url'));
451
447
  baseUrlObj = nconf.get('url_parsed');
452
448
  } catch (err) {
453
449
  return false;
package/package-lock.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "nodebb-plugin-markdown",
3
- "version": "12.2.1",
3
+ "version": "12.2.3",
4
4
  "lockfileVersion": 2,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "nodebb-plugin-markdown",
9
- "version": "12.2.1",
9
+ "version": "12.2.3",
10
10
  "license": "MIT",
11
11
  "dependencies": {
12
12
  "highlight.js": "11.4.0",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodebb-plugin-markdown",
3
- "version": "12.2.1",
3
+ "version": "12.2.3",
4
4
  "description": "A Markdown parser for NodeBB",
5
5
  "main": "index.js",
6
6
  "repository": {
@@ -2,6 +2,41 @@
2
2
 
3
3
  (function () {
4
4
  require(['markdown', 'components'], (markdown, components) => {
5
+ async function initHljs() {
6
+ if (window.hljs) {
7
+ return;
8
+ }
9
+ console.debug('[plugin/markdown] Initializing highlight.js');
10
+ let hljs;
11
+ let list;
12
+ if (config.markdown.hljsLanguages.includes('common')) {
13
+ ({ default: hljs} = await import(`highlight.js/lib/common`));
14
+ list = 'common';
15
+ } else if (config.markdown.hljsLanguages.includes('all')) {
16
+ ({ default: hljs} = await import(`highlight.js`));
17
+ list = 'all';
18
+ } else {
19
+ ({ default: hljs} = await import(`highlight.js/lib/core`));
20
+ list = 'core';
21
+ }
22
+
23
+ console.debug(`[plugins/markdown] Loaded ${list} hljs library`);
24
+
25
+ if (list !== 'all') {
26
+ await Promise.all(config.markdown.hljsLanguages.map(async (language) => {
27
+ if (['common', 'all'].includes(language)) {
28
+ return;
29
+ }
30
+
31
+ console.debug(`[plugins/markdown] Loading ${language} support`);
32
+ const { default: lang } = await import('../../node_modules/highlight.js/lib/languages/' + language + '.js');
33
+ hljs.registerLanguage(language, lang);
34
+ }));
35
+ }
36
+ window.hljs = hljs;
37
+ markdown.buildAliasMap();
38
+ }
39
+
5
40
  $(window).on('action:composer.enhanced', function (evt, data) {
6
41
  var textareaEl = data.postContainer.find('textarea');
7
42
  markdown.capturePaste(textareaEl);
@@ -10,9 +45,13 @@
10
45
 
11
46
  $(window).on('action:composer.preview', {
12
47
  selector: '.composer .preview pre code',
13
- }, markdown.highlight);
48
+ }, async (params) => {
49
+ await initHljs();
50
+ markdown.highlight(params)
51
+ });
14
52
 
15
- $(window).on('action:posts.loaded action:topic.loaded action:posts.edited', function (ev, data) {
53
+ $(window).on('action:posts.loaded action:topic.loaded action:posts.edited', async function (ev, data) {
54
+ await initHljs();
16
55
  markdown.highlight(components.get('post/content').find('pre code'));
17
56
  markdown.enhanceCheckbox(ev, data);
18
57
  markdown.markExternalLinks();
@@ -251,45 +251,11 @@ export function highlight(data) {
251
251
  }
252
252
  };
253
253
 
254
- async function processHighlight(elements) {
255
- if (parseInt(config.markdown.highlight, 10)) {
256
- console.debug('[plugin/markdown] Initializing highlight.js');
257
- let hljs;
258
- let list;
259
- let aliasMap = new Map();
260
- switch(true) {
261
- case config.markdown.hljsLanguages.includes('common'): {
262
- ({ default: hljs} = await import(`highlight.js/lib/common`));
263
- list = 'common';
264
- break;
265
- }
266
-
267
- case config.markdown.hljsLanguages.includes('all'): {
268
- ({ default: hljs} = await import(`highlight.js`));
269
- list = 'all';
270
- break;
271
- }
272
-
273
- default: {
274
- ({ default: hljs} = await import(`highlight.js/lib/core`));
275
- list = 'core';
276
- }
277
- }
278
- console.debug(`[plugins/markdown] Loaded ${list} hljs library`);
279
-
280
- if (list !== 'all') {
281
- await Promise.all(config.markdown.hljsLanguages.map(async (language) => {
282
- if (['common', 'all'].includes(language)) {
283
- return;
284
- }
254
+ const aliasMap = new Map();
285
255
 
286
- console.debug(`[plugins/markdown] Loading ${language} support`);
287
- const { default: lang } = await import('../../node_modules/highlight.js/lib/languages/' + language + '.js');
288
- hljs.registerLanguage(language, lang);
289
- }));
290
- }
291
-
292
- // Build alias set
256
+ export function buildAliasMap() {
257
+ if (window.hljs) {
258
+ const hljs = window.hljs;
293
259
  hljs.listLanguages().forEach((language) => {
294
260
  const { aliases } = hljs.getLanguage(language);
295
261
  if (aliases && Array.isArray(aliases)) {
@@ -300,9 +266,19 @@ async function processHighlight(elements) {
300
266
 
301
267
  aliasMap.set(language, language);
302
268
  });
269
+ }
270
+ }
271
+
272
+ async function processHighlight(elements) {
273
+ if (parseInt(config.markdown.highlight, 10)) {
274
+ const hljs = window.hljs;
275
+ if (!hljs) {
276
+ console.debug(`[plugins/markdown] Tryin to highlight without initializing hljs`);
277
+ return;
278
+ }
303
279
 
304
280
  console.debug(`[plugins/markdown] Loading support for line numbers`);
305
- window.hljs = hljs;
281
+
306
282
  require('highlightjs-line-numbers.js');
307
283
 
308
284
  elements.each(function (i, block) {
@@ -321,7 +297,7 @@ async function processHighlight(elements) {
321
297
 
322
298
  // Check detected language against whitelist and add lines if enabled
323
299
  const classIterator = block.classList.values();
324
- for(className of classIterator) {
300
+ for (const className of classIterator) {
325
301
  if (className.startsWith('language-')) {
326
302
  const language = className.split('-')[1];
327
303
  const list = config.markdown.highlightLinesLanguageList;