11var ejs = require ( 'ejs' ) ;
22const path = require ( 'path' ) ;
3+ const kramed = require ( './src/kramed' ) ;
34
45function 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
2571module . 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} ;
0 commit comments