Skip to content

Commit 83e84b8

Browse files
committed
MarkDown整形 : 実行ファイルをtsからjsに
1 parent 23580bb commit 83e84b8

1 file changed

Lines changed: 172 additions & 157 deletions

File tree

tool-generate-htmls/index.js

Lines changed: 172 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -1,188 +1,203 @@
1-
///<reference path="typings/bundle.d.ts" />
2-
"use strict";
3-
var fs = require("fs");
4-
var marked = require("marked");
5-
var mkdirp = require("mkdirp");
1+
const fs = require('fs');
2+
const marked = require('marked');
3+
const mkdirp = require('mkdirp');
4+
65
let promises = [];
7-
let samplesUrl = "https://ics-creative.github.io/tutorial-createjs/";
8-
let samplesHtmlUrl = "https://github.com/ics-creative/tutorial-createjs/blob/gh-pages/";
6+
let samplesUrl = 'https://ics-creative.github.io/tutorial-createjs/';
7+
let samplesHtmlUrl = 'https://github.com/ics-creative/tutorial-createjs/blob/gh-pages/';
98
let templateHtml;
9+
1010
/**
1111
* テンプレート文字列を展開
1212
* http://webdesign-dackel.com/2015/07/17/javascript-template-string/
1313
* @param text:string テンプレート文字列
1414
* @param values:Object 展開する値
1515
* @return string
1616
*/
17-
let template = (text, values) => {
18-
if (!text) {
19-
console.log("template-error!");
20-
return "";
21-
}
22-
return text.replace(/\$\{(.*?)\}/g, function (all, key) {
23-
return Object.prototype.hasOwnProperty.call(values, key) ? values[key] : "";
24-
});
17+
const template = (text, values) => {
18+
if (!text) {
19+
console.log('template-error!');
20+
return '';
21+
}
22+
return text.replace(/\$\{(.*?)\}/g, function (all, key) {
23+
return Object.prototype.hasOwnProperty.call(values, key) ? values[key] : '';
24+
});
2525
};
26+
27+
28+
29+
2630
const renderer = new marked.Renderer();
31+
2732
renderer.link = (href, title, text) => {
28-
//console.log("href:" + href);
29-
let sampledIndex = href.indexOf("samples/");
30-
let absolutePass = href.indexOf("http") == 0;
31-
if (!absolutePass && sampledIndex >= 0) {
32-
href = samplesHtmlUrl + href.slice(sampledIndex);
33-
}
34-
else {
35-
if (!absolutePass && href.indexOf("md")) {
36-
href = href.replace("md", "html");
37-
}
33+
//console.log("href:" + href);
34+
let sampledIndex = href.indexOf('samples/');
35+
let absolutePass = href.indexOf('http') == 0;
36+
if (!absolutePass && sampledIndex >= 0) {
37+
href = samplesHtmlUrl + href.slice(sampledIndex);
38+
}
39+
else {
40+
if (!absolutePass && href.indexOf('md')) {
41+
href = href.replace('md', 'html');
3842
}
39-
let htmlHref = (href != null && href != "") ? ` href="${href}"` : "";
40-
let htmlTitle = (title != null && title != "") ? ` title=${title}` : "";
41-
return `<a${htmlHref}${htmlTitle}>${text}</a>`;
43+
}
44+
let htmlHref = (href != null && href != '') ? ` href="${href}"` : '';
45+
let htmlTitle = (title != null && title != '') ? ` title=${title}` : '';
46+
return `<a${htmlHref}${htmlTitle}>${text}</a>`;
4247
};
4348
renderer.image = (href, title, text) => {
44-
//console.log("imgs:" + href);
45-
let absolutePass = href.indexOf("http") == 0;
46-
let sampledIndex = href.indexOf("../imgs/");
47-
if (!absolutePass && sampledIndex >= 0) {
48-
href = samplesUrl + href.slice(sampledIndex + ("../").length);
49-
}
50-
let htmlHref = (href != null && href != "") ? ` src="${href}"` : "";
51-
let htmlTitle = (title != null && title != "") ? ` title=${title}` : "";
52-
return `<img${htmlHref}${htmlTitle} />`;
49+
//console.log("imgs:" + href);
50+
let absolutePass = href.indexOf('http') == 0;
51+
let sampledIndex = href.indexOf('../imgs/');
52+
if (!absolutePass && sampledIndex >= 0) {
53+
href = samplesUrl + href.slice(sampledIndex + ('../').length);
54+
}
55+
let htmlHref = (href != null && href != '') ? ` src="${href}"` : '';
56+
let htmlTitle = (title != null && title != '') ? ` title=${title}` : '';
57+
return `<img${htmlHref}${htmlTitle} />`;
5358
};
5459
renderer.heading = function (text, level) {
55-
return `<h${level}>${text}</h${level}>`;
60+
return `<h${level}>${text}</h${level}>`;
5661
};
62+
63+
5764
marked.setOptions({
58-
highlight: function (code) {
59-
return require("highlight.js").highlightAuto(code).value;
60-
},
61-
renderer: renderer
65+
highlight: function (code) {
66+
return require('highlight.js').highlightAuto(code).value;
67+
},
68+
renderer: renderer
6269
});
63-
let generateHTML = (dirName, fileName, resolve) => {
64-
fs.readFile("../docs/" + dirName + fileName, "utf8", (error, text) => {
65-
if (error) {
66-
return;
67-
}
68-
let articleMarkdown = marked(text);
69-
let fileRawName = fileName.split(".md").join("");
70-
// --------------------------------
71-
// h1 要素の選定
72-
// --------------------------------
73-
let headerMatch = articleMarkdown.match(/<h1>(.*?)<\/h1>/);
74-
let articleTitle = headerMatch ? headerMatch[1] : "";
75-
if (!headerMatch) {
76-
console.error(`h1 Element is not written. : ${fileName}`);
77-
}
78-
else {
79-
// 最初のH1だけ削除するとき。
80-
articleMarkdown = articleMarkdown.replace(headerMatch[0], "");
81-
}
82-
// --------------------------------
83-
// メタデータの選定
84-
// --------------------------------
85-
let articleAuthorArr = articleMarkdown.match(/<p><article-author>(.*?)<\/article-author><\/p>/);
86-
let articleAuthorStr = articleAuthorArr ? articleAuthorArr[1] : "";
87-
if (!articleAuthorArr) {
88-
console.error(`<article-author> Element is not written. : ${fileName}`);
89-
}
90-
else {
91-
// 要素を削除
92-
articleMarkdown = articleMarkdown.replace(articleAuthorArr[0], "");
93-
}
94-
// --------------------------------
95-
// メタデータの選定 (公開日)
96-
// --------------------------------
97-
let articleUpdatedArr = articleMarkdown.match(/<article-date-published>(.*?)<\/article-date-published>/);
98-
let articleUpdatedStr = articleUpdatedArr ? articleUpdatedArr[1] : "";
99-
let articleUpdatedDate = new Date(articleUpdatedStr);
100-
let articleUpdatedStrLocale = articleUpdatedArr ? toLocaleString(articleUpdatedDate) : "";
101-
if (!articleUpdatedArr) {
102-
console.error(`<article-date-modified> Element is not written. : ${fileName}`);
103-
}
104-
else {
105-
// 要素を削除
106-
articleMarkdown = articleMarkdown.replace(articleUpdatedArr[0], "");
107-
}
108-
// --------------------------------
109-
// メタデータの選定 (更新日)
110-
// --------------------------------
111-
let articleModifiedArr = articleMarkdown.match(/<article-date-modified>(.*?)<\/article-date-modified>/);
112-
let articleModifiedStr = articleModifiedArr ? articleModifiedArr[1] : "";
113-
let articleModifiedDate = new Date(articleModifiedStr);
114-
let articleModifiedStrLocale = articleUpdatedArr ? toLocaleString(articleModifiedDate) : "";
115-
if (!articleModifiedArr) {
116-
console.error(`<article-date-published> Element is not written. : ${fileName}`);
117-
}
118-
else {
119-
// 要素を削除
120-
articleMarkdown = articleMarkdown.replace(articleModifiedArr[0], "");
121-
}
122-
// --------------------------------
123-
// テンプレートへの適用
124-
// --------------------------------
125-
articleMarkdown = articleMarkdown.replace(/\<code class\=\"lang-/g, "<code class=\"hljs ");
126-
let url = `https://ics.media/tutorial-createjs/${fileRawName}.html`;
127-
let values = {
128-
"article-title": articleTitle,
129-
"article-markdown": articleMarkdown,
130-
"article-author": articleAuthorStr,
131-
"article-datePublished": articleUpdatedStr,
132-
"article-dateModified": articleModifiedStr,
133-
"article-datePublished-locale": articleUpdatedStrLocale,
134-
"article-dateModified-locale": articleModifiedStrLocale,
135-
"url": url
136-
};
137-
if (!templateHtml) {
138-
console.log(fileName + " generate error!");
139-
return;
140-
}
141-
let textValue = template(templateHtml, values);
142-
fs.writeFile("../html/" + dirName + fileName.replace("md", "html"), textValue, (error) => {
143-
//console.log(fileName + "- maked");
144-
if (error) {
145-
return;
146-
}
147-
resolve();
148-
});
70+
71+
72+
73+
const generateHTML = (dirName, fileName, resolve) => {
74+
fs.readFile('../docs/' + dirName + fileName, 'utf8', (error, text) => {
75+
if (error) {
76+
return;
77+
}
78+
let articleMarkdown = marked(text);
79+
const fileRawName = fileName.split('.md').join('');
80+
// --------------------------------
81+
// h1 要素の選定
82+
// --------------------------------
83+
const headerMatch = articleMarkdown.match(/<h1>(.*?)<\/h1>/);
84+
const articleTitle = headerMatch ? headerMatch[1] : '';
85+
if (!headerMatch) {
86+
console.error(`h1 Element is not written. : ${fileName}`);
87+
}
88+
else {
89+
// 最初のH1だけ削除するとき。
90+
articleMarkdown = articleMarkdown.replace(headerMatch[0], '');
91+
}
92+
// --------------------------------
93+
// メタデータの選定
94+
// --------------------------------
95+
const articleAuthorArr = articleMarkdown.match(/<p><article-author>(.*?)<\/article-author><\/p>/);
96+
const articleAuthorStr = articleAuthorArr ? articleAuthorArr[1] : '';
97+
if (!articleAuthorArr) {
98+
console.error(`<article-author> Element is not written. : ${fileName}`);
99+
}
100+
else {
101+
// 要素を削除
102+
articleMarkdown = articleMarkdown.replace(articleAuthorArr[0], '');
103+
}
104+
// --------------------------------
105+
// メタデータの選定 (公開日)
106+
// --------------------------------
107+
const articleUpdatedArr = articleMarkdown.match(/<article-date-published>(.*?)<\/article-date-published>/);
108+
const articleUpdatedStr = articleUpdatedArr ? articleUpdatedArr[1] : '';
109+
const articleUpdatedDate = new Date(articleUpdatedStr);
110+
const articleUpdatedStrLocale = articleUpdatedArr ? toLocaleString(articleUpdatedDate) : '';
111+
if (!articleUpdatedArr) {
112+
console.error(`<article-date-modified> Element is not written. : ${fileName}`);
113+
}
114+
else {
115+
// 要素を削除
116+
articleMarkdown = articleMarkdown.replace(articleUpdatedArr[0], '');
117+
}
118+
// --------------------------------
119+
// メタデータの選定 (更新日)
120+
// --------------------------------
121+
const articleModifiedArr = articleMarkdown.match(/<article-date-modified>(.*?)<\/article-date-modified>/);
122+
const articleModifiedStr = articleModifiedArr ? articleModifiedArr[1] : '';
123+
const articleModifiedDate = new Date(articleModifiedStr);
124+
const articleModifiedStrLocale = articleUpdatedArr ? toLocaleString(articleModifiedDate) : '';
125+
if (!articleModifiedArr) {
126+
console.error(`<article-date-published> Element is not written. : ${fileName}`);
127+
}
128+
else {
129+
// 要素を削除
130+
articleMarkdown = articleMarkdown.replace(articleModifiedArr[0], '');
131+
}
132+
// --------------------------------
133+
// テンプレートへの適用
134+
// --------------------------------
135+
articleMarkdown = articleMarkdown.replace(/\<code class\=\"lang-/g, '<code class="hljs ');
136+
const url = `https://ics.media/tutorial-createjs/${fileRawName}.html`;
137+
const values = {
138+
'article-title': articleTitle,
139+
'article-markdown': articleMarkdown,
140+
'article-author': articleAuthorStr,
141+
'article-datePublished': articleUpdatedStr,
142+
'article-dateModified': articleModifiedStr,
143+
'article-datePublished-locale': articleUpdatedStrLocale,
144+
'article-dateModified-locale': articleModifiedStrLocale,
145+
'url': url
146+
};
147+
if (!templateHtml) {
148+
console.log(fileName + ' generate error!');
149+
return;
150+
}
151+
152+
153+
const textValue = template(templateHtml, values);
154+
fs.writeFile('../html/' + dirName + fileName.replace('md', 'html'), textValue, (error) => {
155+
//console.log(fileName + "- maked");
156+
if (error) {
157+
return;
158+
}
159+
resolve();
149160
});
161+
});
150162
};
151-
fs.readdir("../docs", (err, files) => {
152-
promises.push(new Promise((resolve) => {
153-
mkdirp("../html/", function (err) {
154-
if (err) {
155-
console.error("mkdir-error" + err);
156-
}
157-
else {
158-
resolve();
159-
}
160-
});
161-
}));
162-
promises.push(new Promise((resolve) => {
163-
fs.readFile("template-html.html", "utf8", (error, text) => {
164-
templateHtml = text;
165-
resolve();
166-
});
167-
}));
168-
for (let i = 0; i < files.length; i++) {
169-
let filename = files[i];
170-
let childPromise = new Promise((resolve) => {
171-
generateHTML("", filename, resolve);
172-
});
173-
promises.push(childPromise);
174-
}
175-
Promise
176-
.all(promises)
177-
.then((results) => {
178-
console.log("[Success] HTML files are generated.");
163+
164+
165+
fs.readdir('../docs', (err, files) => {
166+
promises.push(new Promise((resolve) => {
167+
mkdirp('../html/', function (err) {
168+
if (err) {
169+
console.error('mkdir-error' + err);
170+
}
171+
else {
172+
resolve();
173+
}
174+
});
175+
}));
176+
promises.push(new Promise((resolve) => {
177+
fs.readFile('template-html.html', 'utf8', (error, text) => {
178+
templateHtml = text;
179+
resolve();
180+
});
181+
}));
182+
for (let i = 0; i < files.length; i++) {
183+
let filename = files[i];
184+
let childPromise = new Promise((resolve) => {
185+
generateHTML('', filename, resolve);
186+
});
187+
promises.push(childPromise);
188+
}
189+
Promise
190+
.all(promises)
191+
.then((results) => {
192+
console.log('[Success] HTML files are generated.');
179193
});
180194
});
195+
181196
/**
182197
* 日付をフォーマットで変換します。
183198
* @param date Date オブジェクト
184199
* @returns {string} 「◯年◯月◯日」フォーマットの日付
185200
*/
186201
function toLocaleString(date) {
187-
return `${date.getFullYear()}${date.getMonth() + 1}${date.getDate()}日`;
202+
return `${date.getFullYear()}${date.getMonth() + 1}${date.getDate()}日`;
188203
}

0 commit comments

Comments
 (0)