nodebb-plugin-markdown 11.0.3 → 11.1.0

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
@@ -44,11 +44,18 @@ const Markdown = {
44
44
  },
45
45
 
46
46
  getConfig: async (config) => {
47
- const { defaultHighlightLanguage, highlightTheme } = await meta.settings.get('markdown');
47
+ let { defaultHighlightLanguage, highlightTheme, hljsLanguages } = await meta.settings.get('markdown');
48
+
49
+ try {
50
+ hljsLanguages = JSON.parse(hljsLanguages);
51
+ } catch (e) {
52
+ hljsLanguages = ['common'];
53
+ }
48
54
 
49
55
  config.markdown = {
50
56
  highlight: Markdown.highlight ? 1 : 0,
51
57
  highlightLinesLanguageList: Markdown.config.highlightLinesLanguageList,
58
+ hljsLanguages,
52
59
  theme: highlightTheme || 'default.css',
53
60
  defaultHighlightLanguage: defaultHighlightLanguage || '',
54
61
  };
@@ -1,12 +1,19 @@
1
1
  'use strict';
2
2
 
3
+ const path = require('path');
4
+
3
5
  const parent = module.parent.exports;
4
6
  const posts = require.main.require('./src/posts');
7
+ const file = require.main.require('./src/file');
5
8
  const Controllers = {};
6
9
 
7
- Controllers.renderAdmin = function renderAdmin(req, res) {
10
+ Controllers.renderAdmin = async function renderAdmin(req, res) {
11
+ let hljsLanguages = await file.walk(path.resolve(require.main.path, 'node_modules/highlight.js/lib/languages'));
12
+ hljsLanguages = hljsLanguages.map(language => path.basename(language, '.js')).filter(language => !language.endsWith('.js'));
13
+
8
14
  res.render('admin/plugins/markdown', {
9
15
  themes: parent.themes,
16
+ hljsLanguages,
10
17
  });
11
18
  };
12
19
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodebb-plugin-markdown",
3
- "version": "11.0.3",
3
+ "version": "11.1.0",
4
4
  "description": "A Markdown parser for NodeBB",
5
5
  "main": "index.js",
6
6
  "repository": {
@@ -240,7 +240,41 @@
240
240
 
241
241
  async function highlight(elements) {
242
242
  if (parseInt(config.markdown.highlight, 10)) {
243
- const { default: hljs } = await import('highlight.js/lib/common');
243
+ console.debug('[plugin/markdown] Initializing highlight.js');
244
+ let hljs;
245
+ let list;
246
+ switch(true) {
247
+ case config.markdown.hljsLanguages.includes('common'): {
248
+ ({ default: hljs} = await import(`highlight.js/lib/common`));
249
+ list = 'common';
250
+ break;
251
+ }
252
+
253
+ case config.markdown.hljsLanguages.includes('all'): {
254
+ ({ default: hljs} = await import(`highlight.js`));
255
+ list = 'all';
256
+ break;
257
+ }
258
+
259
+ default: {
260
+ ({ default: hljs} = await import(`highlight.js/lib/core`));
261
+ list = 'core';
262
+ }
263
+ }
264
+
265
+ if (list !== 'all') {
266
+ await Promise.all(config.markdown.hljsLanguages.map(async (language) => {
267
+ if (['common', 'all'].includes(language)) {
268
+ return;
269
+ }
270
+
271
+ console.debug(`[plugins/markdown] Loading ${language} support`);
272
+ const { default: lang } = await import('../../node_modules/highlight.js/lib/languages/' + language + '.js');
273
+ hljs.registerLanguage(language, lang);
274
+ }));
275
+ }
276
+
277
+ console.debug(`[plugins/markdown] Loading support for line numbers`);
244
278
  window.hljs = hljs;
245
279
  require('highlightjs-line-numbers.js');
246
280
 
@@ -103,11 +103,34 @@
103
103
  <input class="form-control" placeholder="language-" type="text" name="langPrefix" id="langPrefix" />
104
104
  </div>
105
105
 
106
+ <div class="mb-3">
107
+ <label class="form-label" for="hljsLanguages">Apply syntax highlighting to the following languages</label>
108
+ <select class="form-select" multiple="true" name="hljsLanguages" id="hljsLanguages" size="20">
109
+ <optgroup label="Pre-defined lists">
110
+ <option value="all">All supported languages (greatest file size)</option>
111
+ <option value="common" selected>Common languages (a good compromise)</option>
112
+ </optgroup>
113
+ <optgroup label="Individual languages">
114
+ {{{ each hljsLanguages }}}
115
+ <option value="{@value}">{@value}</option>
116
+ {{{ end }}}
117
+ </optgroup>
118
+ </select>
119
+ <p class="form-text">
120
+ You can use <code>ctrl</code> and <code>shift</code> to select/deselect multiple
121
+ items and select/deselect items in ranges. <em>(Default: "Common languages".)</em>
122
+ </p>
123
+ <p class="form-text">
124
+ You are able to mix and match any of the items above, although "All" will include
125
+ everything anyway.
126
+ </p>
127
+ </div>
128
+
106
129
  <div class="mb-3">
107
130
  <label class="form-label" for="highlightLinesLanguageList">
108
131
  Enable line numbers for the following languages
109
132
  </label>
110
- <select class="form-control" multiple="true" name="highlightLinesLanguageList" id="highlightLinesLanguageList" size="20">
133
+ <select class="form-select" multiple="true" name="highlightLinesLanguageList" id="highlightLinesLanguageList" size="20">
111
134
  <option value="apache,apacheconf">Apache</option>
112
135
  <option value="bash,sh,zsh">Bash</option>
113
136
  <option value="cs,csharp">C#</option>