Skip to content

Commit 1af3a73

Browse files
committed
feature: terminull as a markdown
1 parent dde0799 commit 1af3a73

File tree

4 files changed

+107
-20
lines changed

4 files changed

+107
-20
lines changed

assets/terminull.css

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
color: white;
2121
text-align: left;
2222
font-family: monospace;
23-
overflow: hidden;
24-
word-wrap:break-word;
23+
white-space: nowrap;
24+
overflow-y: auto;
2525
}
2626

2727
.terminullComment {
@@ -61,7 +61,12 @@
6161

6262
.terminullOutput {
6363
display: block;
64+
white-space: pre-line;
6465
}
6566

66-
.terminullCopyBtn { transition: all .2s ease-in-out; }
67-
.terminullCopyBtn:hover { transform: scale(1.1); }
67+
.terminullCopyBtn {
68+
transition: all 0.2s ease-in-out;
69+
}
70+
.terminullCopyBtn:hover {
71+
transform: scale(1.1);
72+
}

index.js

Lines changed: 75 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,71 @@
11
var ejs = require('ejs');
22
const path = require('path');
3+
const kramed = require('./src/kramed');
34

45
function processBlock(rootBlock) {
56
var terminulls = [];
6-
var term = {}
7+
var term = {};
78
rootBlock.blocks.forEach(_blk => {
8-
if(term[_blk.name]){
9-
terminulls.push(term)
10-
term = {}
11-
}
9+
if (term[_blk.name]) {
10+
terminulls.push(term);
11+
term = {};
12+
}
1213
term[_blk.name] = _blk.body.trim();
1314
});
14-
terminulls.push(term)
15-
return new Promise(function(resolve, reject){
16-
ejs.renderFile(path.join(__dirname, './src/terminull.html.ejs'),{terminulls:terminulls},function(err, str){
17-
if(err) {
18-
throw err
19-
}
20-
resolve(str)
21-
})
22-
})
15+
terminulls.push(term);
16+
return new Promise(function(resolve, reject) {
17+
ejs.renderFile(
18+
path.join(__dirname, './src/terminull.html.ejs'),
19+
{ terminulls: terminulls },
20+
function(err, str) {
21+
if (err) {
22+
throw err;
23+
}
24+
resolve(str);
25+
}
26+
);
27+
});
28+
}
29+
30+
function terminullBlock(text) {
31+
var contexts = [];
32+
var context = {};
33+
var lines = text.split('\n');
34+
lines.forEach(function(line) {
35+
if (line.indexOf('$') > -1 || lines.length == 1) {
36+
if (context.command) {
37+
contexts.push(context);
38+
}
39+
context = {};
40+
var command = line;
41+
if (line.indexOf('$') > -1) {
42+
context.directory = line.substring(0, line.indexOf('$')) || '';
43+
command = line.substring(line.indexOf('$') + 1, line.length);
44+
}
45+
context.command = command.split('#')[0];
46+
context.comment = command.split('#')[1] ? command.split('#')[1] : '';
47+
} else {
48+
context.output = context.output ? context.output + '\n' + line : line;
49+
}
50+
});
51+
contexts.push(context);
52+
var blocks = contexts.map(function(context) {
53+
return (
54+
`{% directory %}${context.directory || ''}{% command %}${
55+
context.command
56+
}{% comment %}${context.comment || ''}` +
57+
`{% output %} ${context.output || ''}`
58+
);
59+
});
60+
return '{% term %}' + [...blocks].join('\n') + '{% endterm %}';
61+
}
62+
63+
function markdown2Tag(text) {
64+
var blockCode = terminullBlock(text);
65+
return {
66+
type: 'paragraph',
67+
text: blockCode
68+
};
2369
}
2470

2571
module.exports = {
@@ -30,8 +76,22 @@ module.exports = {
3076
},
3177
blocks: {
3278
term: {
33-
blocks: ['directory', 'command','comment', 'output'],
79+
blocks: ['directory', 'command', 'comment', 'output'],
3480
process: processBlock
3581
}
82+
},
83+
hooks: {
84+
'page:before': function(page) {
85+
if (page.type == 'markdown') {
86+
var lexed = kramed.lexer(page.content);
87+
lexed.forEach(function(section, index) {
88+
if (section.type === 'code' && section.lang === 'term') {
89+
lexed[index] = markdown2Tag(section.text);
90+
}
91+
});
92+
page.content = kramed.render(lexed);
93+
}
94+
return page;
95+
}
3696
}
3797
};

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
"gitbook": ">2.5.0"
2424
},
2525
"dependencies": {
26-
"ejs": "^3.0.1"
26+
"ejs": "^3.0.1",
27+
"kramed": "^0.5.6",
28+
"kramed-markdown-renderer": "^0.1.2"
2729
}
2830
}

src/kramed.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var kramed = require('kramed');
2+
var renderer = require('kramed-markdown-renderer');
3+
4+
function render(lexed) {
5+
var options = Object.create(kramed.defaults);
6+
options.escape = false;
7+
options.renderer = renderer();
8+
return kramed.parser(lexed, options);
9+
}
10+
11+
function lexer(content) {
12+
var options = Object.create(kramed.defaults);
13+
options.escape = false;
14+
return kramed.lexer(content, options);
15+
}
16+
17+
module.exports = {
18+
render: render,
19+
lexer: lexer
20+
};

0 commit comments

Comments
 (0)