1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
const fm = require("front-matter");
const fs = require("fs");
const hljs = require("highlight.js");
const katex = require("katex");
const makeTweetUrl = require("./make-tweet-url.js");
const marked = require("marked");
const strftime = require("strftime");
marked.setOptions({
gfm: true,
highlight: (code, lang) => {
return lang
? hljs.highlight(lang, code).value
: hljs.highlightAuto(code).value;
},
});
function loadArticle(filename) {
return new Promise((resolve, reject) => {
fs.readFile(filename, (err, data) => {
if (err) { return reject(err); }
const article = fm(data.toString());
const body = marked(
article.body.replace(/\$\$([^$]*)\$\$/g, (_, tex) =>
katex.renderToString(tex, { displayMode: true })
)
);
let summary = article.attributes.description;
if (!summary) {
const paragraphBreak = article.body.match(/\r?\n\r?\n/);
if (paragraphBreak) {
summary = marked(article.body.slice(0, paragraphBreak.index));
} else {
summary = article.body;
}
}
const rawDate = article.attributes.date;
let date = null;
if (rawDate) {
;["FullYear", "Month", "Date", "Hours"].forEach(field => {
rawDate["set" + field](rawDate["getUTC" + field]());
});
// date = strftime("%B %d, %Y", rawDate);
date = strftime("%Y. %B. %d.", rawDate);
}
const tweetUrl = makeTweetUrl(article);
const defaults = { hidden: false, timeless: false };
const tags = article.attributes.tags
? article.attributes.tags.split(", ")
: [];
resolve(
Object.assign({}, defaults, article.attributes, {
filename,
summary,
date,
rawDate,
tweetUrl,
tags,
rawBody: article.body,
body,
})
);
});
});
}
module.exports = loadArticle;
|