-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
67 lines (49 loc) · 1.36 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
"use strict";
var md = require('markdown-it');
var markdownItAttrs = require('markdown-it-attrs');
var loaderUtils = require("loader-utils");
var assign = require("object-assign");
var presetUse = markdownItAttrs;
var defaultOptions = {};
function mergePlugins(array1, array2) {
var array = array1.concat(array2);
array.push(presetUse);
for (var i = 0; i < array.length; ++i) {
for (var j = i + 1; j < array.length; ++j) {
if (array[i] === array[j])
array.splice(j--, 1);
}
}
return array;
}
function getPluginsFromQuery(query) {
var pluginsFromQuery = [];
if (query) {
query.forEach(function(str) {
try {
pluginsFromQuery.push(require(str));
} catch (e) {
console.error(e);
}
});
}
return pluginsFromQuery;
}
module.exports = function(src) {
this.cacheable();
var query = loaderUtils.parseQuery(this.query);
var configKey = query.config || "markdownattrsLoader";
delete query.config;
var options = assign({}, defaultOptions, this.options[configKey]);
var plugins = mergePlugins(options.use || [], getPluginsFromQuery(query.use));
delete query.use;
delete options.use;
options = assign({}, query, options);
var configuredMd = md(options);
if (plugins) {
plugins.forEach(function(plugin) {
configuredMd.use(plugin);
});
}
return configuredMd.render(src);
};