-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
98 lines (94 loc) · 2.51 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
var ejs = require('ejs');
const path = require('path');
const kramed = require('./src/kramed');
function processBlock(rootBlock) {
var terminulls = [];
var term = {};
var options = this.options.pluginsConfig["terminull"];
rootBlock.blocks.forEach(_blk => {
if (term[_blk.name]) {
terminulls.push(term);
term = {};
}
term[_blk.name] = _blk.body.trim();
});
terminulls.push(term);
return new Promise(function(resolve, reject) {
ejs.renderFile(
path.join(__dirname, './src/terminull.html.ejs'),
{ terminulls: terminulls ,options: options },
function(err, str) {
if (err) {
throw err;
}
resolve(str);
}
);
});
}
function terminullBlock(text) {
var contexts = [];
var context = {};
var lines = text.split('\n');
lines.forEach(function(line) {
if (line.indexOf('$') > -1 || lines.length == 1) {
if (context.command) {
contexts.push(context);
}
context = {};
var command = line;
if (line.indexOf('$') > -1) {
context.directory = line.substring(0, line.indexOf('$')) || '';
command = line.substring(line.indexOf('$') + 1, line.length);
}
context.command = command.split('#')[0];
context.comment = command.split('#')[1] ? command.split('#')[1] : '';
} else {
context.output = context.output ? context.output + '\n' + line : line;
}
});
contexts.push(context);
var blocks = contexts.map(function(context) {
return (
`{% directory %}${context.directory || ''}{% command %}${
context.command
}{% comment %}${context.comment || ''}` +
`{% output %} ${context.output || ''}`
);
});
return '{% term %}' + [...blocks].join('\n') + '{% endterm %}';
}
function markdown2Tag(text) {
var blockCode = terminullBlock(text);
return {
type: 'paragraph',
text: blockCode
};
}
module.exports = {
website: {
assets: './assets',
css: ['terminull.css'],
js: ['terminull.js']
},
blocks: {
term: {
blocks: ['directory', 'command', 'comment', 'output'],
process: processBlock
}
},
hooks: {
'page:before': function(page) {
if (page.type == 'markdown') {
var lexed = kramed.lexer(page.content);
lexed.forEach(function(section, index) {
if (section.type === 'code' && section.lang === 'term') {
lexed[index] = markdown2Tag(section.text);
}
});
page.content = kramed.render(lexed);
}
return page;
}
}
};