@@ -2,8 +2,7 @@ const fs = require('fs');
22const marked = require ( 'marked' ) ;
33const mkdirp = require ( 'mkdirp' ) ;
44const highlightJs = require ( 'highlight.js' ) ;
5-
6- const IGNORE_LIST = [ '.DS_Store' ] ;
5+ const htmlMinifier = require ( 'html-minifier' ) ;
76
87let promises = [ ] ;
98let samplesUrl = 'https://ics-creative.github.io/tutorial-three/' ;
@@ -19,21 +18,19 @@ let templateHtml;
1918 */
2019const template = ( text , values ) => {
2120 if ( ! text ) {
22- console . error ( 'template-error!' ) ;
21+ console . log ( 'template-error!' ) ;
2322 return '' ;
2423 }
25- return text . replace ( / \$ \{ ( .* ?) \} / g, function ( all , key ) {
26- return Object . prototype . hasOwnProperty . call ( values , key ) ? values [ key ] : '' ;
27- } ) ;
24+ return text . replace ( / \$ \{ ( .* ?) \} / g, ( all , key ) =>
25+ Object . prototype . hasOwnProperty . call ( values , key ) ? values [ key ] : '' ) ;
2826} ;
2927
30-
3128const renderer = new marked . Renderer ( ) ;
3229
3330renderer . link = ( href , title , text ) => {
3431 //console.log("href:" + href);
35- let sampledIndex = href . indexOf ( 'samples/' ) ;
36- let absolutePass = href . indexOf ( 'http' ) == 0 ;
32+ const sampledIndex = href . indexOf ( 'samples/' ) ;
33+ const absolutePass = href . indexOf ( 'http' ) = == 0 ;
3734 if ( ! absolutePass && sampledIndex >= 0 ) {
3835 href = samplesHtmlUrl + href . slice ( sampledIndex ) ;
3936 }
@@ -42,38 +39,48 @@ renderer.link = (href, title, text) => {
4239 href = href . replace ( 'md' , 'html' ) ;
4340 }
4441 }
45- let htmlHref = ( href != null && href != '' ) ? ` href="${ href } "` : '' ;
46- let htmlTitle = ( title != null && title != '' ) ? ` title=${ title } ` : '' ;
47- return `<a${ htmlHref } ${ htmlTitle } >${ text } </a>` ;
42+ const htmlHref = ( href != null && href != '' ) ? `href="${ href } "` : '' ;
43+ const htmlTitle = ( title != null && title != '' ) ? `title=${ title } ` : '' ;
44+ return `<a ${ htmlHref } ${ htmlTitle } >${ text } </a>` ;
4845} ;
46+
4947renderer . image = ( href , title , text ) => {
5048 //console.log("imgs:" + href);
51- let absolutePass = href . indexOf ( 'http' ) == 0 ;
52- let sampledIndex = href . indexOf ( '../imgs/' ) ;
49+ const absolutePass = href . indexOf ( 'http' ) = == 0 ;
50+ const sampledIndex = href . indexOf ( '../imgs/' ) ;
5351 if ( ! absolutePass && sampledIndex >= 0 ) {
5452 href = samplesUrl + href . slice ( sampledIndex + ( '../' ) . length ) ;
5553 }
56- let htmlHref = ( href != null && href != '' ) ? ` src="${ href } "` : '' ;
57- let htmlTitle = ( title != null && title != '' ) ? ` title=${ title } ` : '' ;
58- return `<img${ htmlHref } ${ htmlTitle } />` ;
54+ const htmlHref = ( href != null && href != '' ) ? `src="${ href } "` : '' ;
55+
56+ // Marked でサイズを指定する方法
57+ // https://github.com/markedjs/marked/issues/339
58+ let size = '' ;
59+ if ( title ) {
60+ sizes = title . split ( 'x' ) ;
61+ if ( sizes . length === 2 ) {
62+ size = 'width=' + sizes [ 0 ] + ' height=' + sizes [ 1 ] ;
63+ } else {
64+ size = 'width=' + sizes [ 0 ] ;
65+ }
66+ }
67+
68+ return `<img ${ htmlHref } ${ size } />` ;
5969} ;
6070renderer . heading = function ( text , level ) {
6171 return `<h${ level } >${ text } </h${ level } >` ;
6272} ;
6373
64-
6574marked . setOptions ( {
6675 highlight : function ( code ) {
6776 return highlightJs . highlightAuto ( code ) . value ;
6877 } ,
6978 renderer : renderer
7079} ) ;
7180
72-
7381const generateHTML = ( dirName , fileName , resolve ) => {
7482 fs . readFile ( '../docs/' + dirName + fileName , 'utf8' , ( error , text ) => {
7583 if ( error ) {
76- console . error ( error ) ;
7784 return ;
7885 }
7986 let articleMarkdown = marked ( text ) ;
@@ -134,7 +141,7 @@ const generateHTML = (dirName, fileName, resolve) => {
134141 // テンプレートへの適用
135142 // --------------------------------
136143 articleMarkdown = articleMarkdown . replace ( / \< c o d e c l a s s \= \" l a n g - / g, '<code class="hljs ' ) ;
137- const url = `https://ics.media/tutorial-three /${ fileRawName } .html` ;
144+ const url = `https://ics.media/tutorial-createjs /${ fileRawName } .html` ;
138145 const values = {
139146 'article-title' : articleTitle ,
140147 'article-markdown' : articleMarkdown ,
@@ -145,16 +152,26 @@ const generateHTML = (dirName, fileName, resolve) => {
145152 'article-dateModified-locale' : articleModifiedStrLocale ,
146153 'url' : url
147154 } ;
148-
149155 if ( ! templateHtml ) {
150- console . log ( templateHtml ) ;
151- console . error ( `Error : ${ fileName } ` ) ;
156+ console . log ( fileName + ' generate error!' ) ;
152157 return ;
153158 }
154159
155-
156160 const textValue = template ( templateHtml , values ) ;
157- fs . writeFile ( '../html/' + dirName + fileName . replace ( 'md' , 'html' ) , textValue , ( error ) => {
161+
162+ // HTMLのminifyを実行
163+ const minifiedHtml = htmlMinifier . minify ( textValue , {
164+ sortAttributes : false ,
165+ sortClassName : true ,
166+ removeComments : true ,
167+ removeScriptTypeAttributes : true ,
168+ removeStyleLinkTypeAttributes : true ,
169+ keepClosingSlash : true ,
170+ collapseInlineTagWhitespace : true ,
171+ collapseWhitespace : true
172+ } ) ;
173+
174+ fs . writeFile ( '../html/' + dirName + fileName . replace ( 'md' , 'html' ) , minifiedHtml , ( error ) => {
158175 //console.log(fileName + "- maked");
159176 if ( error ) {
160177 return ;
@@ -164,7 +181,6 @@ const generateHTML = (dirName, fileName, resolve) => {
164181 } ) ;
165182} ;
166183
167-
168184fs . readdir ( '../docs' , ( err , files ) => {
169185 promises . push ( new Promise ( ( resolve ) => {
170186 mkdirp ( '../html/' , function ( err ) {
@@ -182,17 +198,12 @@ fs.readdir('../docs', (err, files) => {
182198 resolve ( ) ;
183199 } ) ;
184200 } ) ) ;
185-
186-
187201 for ( let i = 0 ; i < files . length ; i ++ ) {
188-
189- let filename = files [ i ] ;
190- if ( IGNORE_LIST . includes ( filename ) === false ) {
191- let childPromise = new Promise ( ( resolve ) => {
192- generateHTML ( '' , filename , resolve ) ;
193- } ) ;
194- promises . push ( childPromise ) ;
195- }
202+ const filename = files [ i ] ;
203+ const childPromise = new Promise ( ( resolve ) => {
204+ generateHTML ( '' , filename , resolve ) ;
205+ } ) ;
206+ promises . push ( childPromise ) ;
196207 }
197208 Promise
198209 . all ( promises )
0 commit comments