@@ -11,30 +11,22 @@ var nodeModules = path.normalize(path.join(__dirname, '../node_modules')) + path
11
11
12
12
//1. 只导出文件nodeppt generate file.md
13
13
//2. 导出文件+目录 nodeppt generate ./ --all -o publish
14
- module . exports = function ( filepath , outputDir , isAll ) {
14
+ module . exports = function ( filepath , outputDir , isAll , rDir ) {
15
15
filepath = fs . realpathSync ( filepath ) ;
16
16
outputDir = outputDir ? $ . getDirPath ( outputDir ) : $ . getDirPath ( path . join ( process . cwd ( ) , './publish' ) ) ;
17
17
isAll = ! ! isAll ;
18
18
if ( isAll ) {
19
19
//1.导出默认的assets
20
- $ . copy ( assetsDir , outputDir , function ( filename , dir , subdir ) {
20
+ $ . copy ( assetsDir , outputDir , function ( filename , dir , subdir ) {
21
21
if ( ! subdir || subdir === 'scss' ) {
22
22
//不复制scss
23
23
return false ;
24
24
}
25
25
return true ;
26
26
} ) ;
27
- //2.导出ppts,特别针对img
28
- // $.copy(pptsDir, outputDir, function(filename, dir, subdir) {
29
- // if (!subdir || ['css', 'js'].indexOf(subdir) !== -1) {
30
- // //不复制css,js
31
- // return false;
32
- // }
33
- // return true;
34
- // });
35
27
}
36
28
//2.导出复制filepath除根目录下img、css和js等到assets,遇见/*.md就解析
37
- generate ( filepath , outputDir ) ;
29
+ generate ( filepath , outputDir , rDir ) ;
38
30
console . log ( '生成结束!' . bold . green + require ( 'path' ) . relative ( 'b:/' , outputDir ) . yellow ) ;
39
31
} ;
40
32
@@ -47,7 +39,6 @@ function parser(content, template) {
47
39
} catch ( e ) {
48
40
console . log ( 'ERROR: ' . bold . red + e . toString ( ) ) ;
49
41
}
50
-
51
42
return false ;
52
43
}
53
44
@@ -57,22 +48,22 @@ function parser(content, template) {
57
48
* @param {[type] } outputDir [description]
58
49
* @return {[type] } [description]
59
50
*/
60
- function generate ( filepath , outputDir ) {
51
+ function generate ( filepath , outputDir , rDir ) {
52
+ rDir = rDir || '.' ;
61
53
var filename = '' ;
62
54
var templateMd = $ . readFile ( templateDir + 'markdown.ejs' ) ;
63
55
var templateList = $ . readFile ( templateDir + 'list.ejs' ) ;
64
56
65
57
if ( $ . isDir ( filepath , true ) ) {
66
58
//遍历目录生成htm
67
59
var indexList = '' ;
68
-
69
- $ . copy ( filepath , outputDir , function ( filename , dir , subdir ) {
60
+ $ . copy ( filepath , outputDir , function ( filename , dir , subdir ) {
70
61
if ( ! subdir && / \. (?: m d | m a r k d o w n ) $ / i. test ( filename ) ) {
71
62
var content = $ . readFile ( path . join ( filepath , filename ) ) ;
72
63
var html = parser ( content ) ;
73
64
if ( html ) {
74
65
var title = html . match ( / < t i t l e > ( .* ?) < \/ t i t l e > / ) ;
75
- if ( title [ 1 ] ) {
66
+ if ( title && title [ 1 ] ) {
76
67
title = title [ 1 ] ;
77
68
} else {
78
69
title = filename ;
@@ -81,9 +72,8 @@ function generate(filepath, outputDir) {
81
72
var url = filename . replace ( / \. (?: m d | m a r k d o w n ) $ / i, '.htm' ) ;
82
73
indexList += '<li><a class="star" href="' + url + '" target="_blank">' + title + '</a> [<a href="' + url + '?_multiscreen=1" target="_blank" title="多窗口打开">多窗口</a>]</li>' ;
83
74
84
- copyLinkToOutput ( content , filepath , outputDir ) ;
85
-
86
-
75
+ copyLinkToOutput ( content , filepath , outputDir , rDir ) ;
76
+ html = handlerHTML ( html , rDir ) ;
87
77
$ . writeFile ( path . join ( outputDir , filename . replace ( / \. (?: m d | m a r k d o w n ) $ / i, '.htm' ) ) , html ) ;
88
78
}
89
79
return false ;
@@ -111,43 +101,45 @@ function generate(filepath, outputDir) {
111
101
return console . log ( 'ERROR: ' . bold . red + filepath + ' is not exists!' ) ;
112
102
}
113
103
filename = path . basename ( filepath ) ;
114
- copyLinkToOutput ( content , filepath , outputDir ) ;
104
+ copyLinkToOutput ( content , filepath , outputDir , rDir ) ;
115
105
var html = parser ( content ) ;
116
106
if ( html ) {
117
- html = handlerHTML ( html ) ;
107
+ html = handlerHTML ( html , rDir ) ;
118
108
$ . writeFile ( path . join ( outputDir , filename . replace ( / \. (?: m d | m a r k d o w n ) $ / i, '.htm' ) ) , html ) ;
119
109
}
120
110
}
121
111
}
122
112
123
113
//处理绝对路径的url
124
- function handlerHTML ( html ) {
125
- html = html . replace ( / ( s r c | h r e f | u r l ) ( [ = | \( ] ) ( [ " ' ] ) \/ / gi, '$1$2$3./' )
126
- . replace ( "loadJS('/js" , "loadJS('./js" ) . replace ( "dir: '/js/'," , "dir: './js/'," ) ;
114
+ function handlerHTML ( html , rDir ) {
115
+ html = html . replace ( / ( s r c | h r e f | u r l ) ( [ = | \( ] ) ( [ " ' ] ) \/ \/ / gi, '$1$2$3<=PLACEHOLDER=>//' )
116
+ . replace ( / ( s r c | h r e f | u r l ) ( [ = | \( ] ) ( [ " ' ] ) \/ / gi, '$1$2$3' + rDir + '/' )
117
+ . replace ( / ( s r c | h r e f | u r l ) ( [ = | \( ] ) ( [ " ' ] ) < = P L A C E H O L D E R = > \/ / gi, '$1$2$3//' )
118
+ . replace ( / l o a d J S \( [ ' " ] \/ j s / g, "loadJS($1" + rDir + "/js" ) . replace ( / d i r : \s * ( [ " ' ] ) \/ j s \/ \1, / g, "dir: $1" + rDir + "/js/$1," ) ;
127
119
128
120
return html ;
129
121
}
130
122
131
123
//处理页面相对url,到目标文件夹
132
124
function copyLinkToOutput ( content , filepath , outputDir ) {
133
125
var files = [ ] ;
134
- content . replace ( / ( ! ) ? \[ .+ ?\] \( \s ? ( .* ?) \s ? \) / g, function ( i , isImg , file ) {
126
+ content . replace ( / ( ! ) ? \[ .+ ?\] \( \s ? ( .* ?) \s ? \) / g, function ( i , isImg , file ) {
135
127
//处理markdown内部,[inline模式](/assets/box-fe-road/img/inline-mode.png)
136
128
if ( isImg && file ) {
137
129
file = file . split ( / \s + / ) [ 0 ] ;
138
130
}
131
+ // console.log(file);
139
132
files . push ( file ) ;
140
- } ) . replace ( / (?: h r e f | s r c | u r l ) [ = | \( ] ( [ ' " ] ) ? ( .+ ?) \1/ g, function ( i , q , file ) {
133
+ } ) . replace ( / (?: h r e f | s r c | u r l ) [ = | \( ] ( [ ' " ] ) ? ( .+ ?) \1/ g, function ( i , q , file ) {
141
134
files . push ( file ) ;
142
135
} ) ;
143
136
//解析cover
144
137
var json = md_parser . parseCover ( content . split ( / \[ s l i d e .* \] / i) [ 0 ] ) ;
145
138
if ( json . files ) {
146
-
147
139
files = files . concat ( json . files . split ( / \s ? , \s ? / ) ) ;
148
140
}
149
141
if ( json . usemathjax === 'yes' ) {
150
- $ . copy ( nodeModules + 'mathjax/' , outputDir + 'js/mathjax/' , function ( filename , dir , subdir ) {
142
+ $ . copy ( nodeModules + 'mathjax/' , outputDir + 'js/mathjax/' , function ( filename , dir , subdir ) {
151
143
if ( / ^ (?: d o c s | u n p a c k e d | t e s t ) / . test ( subdir ) ) {
152
144
//不复制
153
145
return false ;
@@ -160,13 +152,13 @@ function copyLinkToOutput(content, filepath, outputDir) {
160
152
return true ;
161
153
} ) ;
162
154
}
163
- files . filter ( function ( f ) {
155
+ files . filter ( function ( f ) {
164
156
if ( / ^ h t t p [ s ] ? : \/ \/ / . test ( f ) || / ^ \/ \/ / . test ( f ) || [ '#' , '/' ] . indexOf ( f ) !== - 1 || / ^ \? / . test ( f ) || / ^ a b o u t \: / . test ( f ) ) {
165
157
//过滤掉外链
166
158
return false ;
167
159
}
168
160
return true ;
169
- } ) . forEach ( function ( f ) {
161
+ } ) . forEach ( function ( f ) {
170
162
var topath = path . join ( outputDir , f ) ;
171
163
var realpath = path . join ( path . dirname ( filepath ) , f ) ;
172
164
if ( $ . exists ( realpath ) && $ . isFile ( realpath ) ) {
0 commit comments