-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.js
More file actions
55 lines (45 loc) · 1.36 KB
/
index.js
File metadata and controls
55 lines (45 loc) · 1.36 KB
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
'use strict';
var container = require('markdown-it-container');
module.exports = function alerts_plugin(md, options) {
var containerOpenCount = 0;
var links = options ? options.links : true;
init();
return;
function setupContainer(name) {
md.use(container, name, {
render: function (tokens, idx) {
if (tokens[idx].nesting === 1) {
containerOpenCount += 1;
return '<div class="alert alert-' + name + '" role="alert">\n';
} else {
containerOpenCount -= 1;
return '</div>\n';
}
}
});
}
function isContainerOpen() {
return containerOpenCount > 0;
}
function selfRender(tokens, idx, options, env, self) {
return self.renderToken(tokens, idx, options);
}
function setupLinks() {
var defaultRender = md.renderer.rules.link_open || selfRender;
md.renderer.rules.link_open = function (tokens, idx, options, env, self) {
if (isContainerOpen()) {
tokens[idx].attrPush(['class', 'alert-link']);
}
return defaultRender(tokens, idx, options, env, self);
};
}
function init() {
setupContainer('success');
setupContainer('info');
setupContainer('warning');
setupContainer('danger');
if (links) {
setupLinks();
}
}
};