REFACTOR: Do not rely on module order for discourse-markdown loading

Before this patch, discourse-markdown depended on the modules in its
bundle being defined in a specific order or it wouldn't load properly.

Now, any file in the bundle can export a `priority` const (default 0)
and files will be loaded in order of ascending priority instead. This
allows us to use a bundle packaged in any order we want.
This commit is contained in:
Robin Ward 2020-09-18 13:29:09 -04:00
parent 23abd0d953
commit ba3014f9f9
3 changed files with 24 additions and 33 deletions

View File

@ -1,20 +1,2 @@
//= require markdown-it.js
//= require ./pretty-text/engines/discourse-markdown/helpers
//= require ./pretty-text/engines/discourse-markdown/mentions
//= require ./pretty-text/engines/discourse-markdown/quotes
//= require ./pretty-text/engines/discourse-markdown/emoji
//= require ./pretty-text/engines/discourse-markdown/onebox
//= require ./pretty-text/engines/discourse-markdown/bbcode-block
//= require ./pretty-text/engines/discourse-markdown/bbcode-inline
//= require ./pretty-text/engines/discourse-markdown/code
//= require ./pretty-text/engines/discourse-markdown/category-hashtag
//= require ./pretty-text/engines/discourse-markdown/censored
//= require ./pretty-text/engines/discourse-markdown/table
//= require ./pretty-text/engines/discourse-markdown/paragraph
//= require ./pretty-text/engines/discourse-markdown/newline
//= require ./pretty-text/engines/discourse-markdown/html-img
//= require ./pretty-text/engines/discourse-markdown/text-post-process
//= require ./pretty-text/engines/discourse-markdown/upload-protocol
//= require ./pretty-text/engines/discourse-markdown/inject-line-number
//= require ./pretty-text/engines/discourse-markdown/resize-controls
//= require ./pretty-text/engines/discourse-markdown/d-wrap
//= require_tree ./pretty-text/engines/discourse-markdown

View File

@ -291,22 +291,28 @@ export function setup(opts, siteSettings, state) {
if (check.test(entry)) {
const module = requirejs(entry);
if (module && module.setup) {
const featureName = entry.split("/").reverse()[0];
features.push(featureName);
module.setup(
createHelper(
featureName,
opts,
optionCallbacks,
pluginCallbacks,
getOptions,
whiteListed
)
);
const id = entry.split("/").reverse()[0];
let priority = module.priority || 0;
features.unshift({ id, setup: module.setup, priority });
}
}
});
features
.sort((a, b) => a.priority - b.priority)
.forEach((f) => {
f.setup(
createHelper(
f.id,
opts,
optionCallbacks,
pluginCallbacks,
getOptions,
whiteListed
)
);
});
Object.entries(state.whiteListed || {}).forEach((entry) => {
whiteListed.push(entry);
});
@ -317,8 +323,8 @@ export function setup(opts, siteSettings, state) {
// enable all features by default
features.forEach((feature) => {
if (!opts.features.hasOwnProperty(feature)) {
opts.features[feature] = true;
if (!opts.features.hasOwnProperty(feature.id)) {
opts.features[feature.id] = true;
}
});

View File

@ -61,6 +61,9 @@ function buildScaleButton(selectedScale, scale) {
);
}
// We need this to load after `upload-protocol` which is priority 0
export const priority = 1;
export function setup(helper) {
const opts = helper.getOptions();
if (opts.previewing) {