Skip to content

Commit 9d0055f

Browse files
committed
feat: v3 compatibility
2 parents 6c4c0e9 + 7200ec6 commit 9d0055f

File tree

10 files changed

+1824
-4399
lines changed

10 files changed

+1824
-4399
lines changed

index.js

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,25 @@ const SocketPlugins = require.main.require('./src/socket.io/plugins');
1919
SocketPlugins.markdown = require('./websockets');
2020

2121
let parser;
22-
22+
let app;
2323
const Markdown = {
2424
config: {},
2525
_externalImageCache: undefined,
2626
_externalImageFailures: new Set(),
2727
onLoad: async function (params) {
28+
app = params.app;
29+
const { router } = params;
2830
const controllers = require('./lib/controllers');
2931
const hostMiddleware = require.main.require('./src/middleware');
32+
const routeHelpers = require.main.require('./src/routes/helpers');
3033
const middlewares = [
3134
hostMiddleware.maintenanceMode, hostMiddleware.registrationComplete, hostMiddleware.pluginHooks,
3235
];
3336

34-
params.router.get('/admin/plugins/markdown', params.middleware.admin.buildHeader, controllers.renderAdmin);
35-
params.router.get('/api/admin/plugins/markdown', controllers.renderAdmin);
37+
routeHelpers.setupAdminPageRoute(router, '/admin/plugins/markdown', controllers.renderAdmin);
3638

3739
// Return raw markdown via GET
38-
params.router.get('/api/post/:pid/raw', middlewares, controllers.retrieveRaw);
40+
router.get('/api/post/:pid/raw', middlewares, controllers.retrieveRaw);
3941

4042
Markdown.init();
4143
await Markdown.loadThemes();
@@ -44,11 +46,18 @@ const Markdown = {
4446
},
4547

4648
getConfig: async (config) => {
47-
const { defaultHighlightLanguage, highlightTheme } = await meta.settings.get('markdown');
49+
let { defaultHighlightLanguage, highlightTheme, hljsLanguages, highlightLinesLanguageList } = await meta.settings.get('markdown');
50+
51+
try {
52+
hljsLanguages = JSON.parse(hljsLanguages);
53+
} catch (e) {
54+
hljsLanguages = ['common'];
55+
}
4856

4957
config.markdown = {
5058
highlight: Markdown.highlight ? 1 : 0,
51-
highlightLinesLanguageList: Markdown.config.highlightLinesLanguageList,
59+
highlightLinesLanguageList,
60+
hljsLanguages,
5261
theme: highlightTheme || 'default.css',
5362
defaultHighlightLanguage: defaultHighlightLanguage || '',
5463
};
@@ -79,7 +88,6 @@ const Markdown = {
7988

8089
langPrefix: 'language-',
8190
highlight: true,
82-
highlightLinesLanguageList: [],
8391
highlightTheme: 'default.css',
8492

8593
probe: true,
@@ -95,7 +103,7 @@ const Markdown = {
95103
checkboxes: true,
96104
multimdTables: true,
97105
};
98-
const notCheckboxes = ['langPrefix', 'highlightTheme', 'highlightLinesLanguageList', 'probeCacheSize'];
106+
const notCheckboxes = ['langPrefix', 'highlightTheme', 'probeCacheSize'];
99107

100108
meta.settings.get('markdown', (err, options) => {
101109
if (err) {
@@ -116,17 +124,6 @@ const Markdown = {
116124
_self.highlight = _self.config.highlight;
117125
delete _self.config.highlight;
118126

119-
if (typeof _self.config.highlightLinesLanguageList === 'string') {
120-
try {
121-
_self.config.highlightLinesLanguageList = JSON.parse(_self.config.highlightLinesLanguageList);
122-
} catch (e) {
123-
winston.warn('[plugins/markdown] Invalid config for highlightLinesLanguageList, blanking.');
124-
_self.config.highlightLinesLanguageList = [];
125-
}
126-
127-
_self.config.highlightLinesLanguageList = _self.config.highlightLinesLanguageList.join(',').split(',');
128-
}
129-
130127
parser = new MarkdownIt(_self.config);
131128

132129
Markdown.updateParserRules(parser);
@@ -273,7 +270,8 @@ const Markdown = {
273270
renderHelp: async function (helpContent) {
274271
const translated = await translator.translate('[[markdown:help_text]]');
275272
const parsed = await plugins.hooks.fire('filter:parse.raw', `## Markdown\n${translated}`);
276-
helpContent += parsed;
273+
const html = await app.renderAsync('modals/markdown-help', {});
274+
helpContent += parsed + html;
277275
return helpContent;
278276
},
279277

@@ -361,7 +359,7 @@ const Markdown = {
361359
// Validate the url
362360
if (!Markdown.isUrlValid(attributes.get('src'))) { return ''; }
363361

364-
token.attrSet('class', `${token.attrGet('class') || ''} img-responsive img-markdown`);
362+
token.attrSet('class', `${token.attrGet('class') || ''} img-fluid img-markdown`);
365363

366364
// Append sizes to images
367365
if (parsedSrc.pathname) {

lib/controllers.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
'use strict';
22

3+
const path = require('path');
4+
35
const parent = module.parent.exports;
46
const posts = require.main.require('./src/posts');
7+
const file = require.main.require('./src/file');
58
const Controllers = {};
69

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+
814
res.render('admin/plugins/markdown', {
915
themes: parent.themes,
16+
hljsLanguages,
17+
title: 'Markdown',
1018
});
1119
};
1220

0 commit comments

Comments
 (0)