-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharticles.js
More file actions
46 lines (33 loc) · 1.06 KB
/
articles.js
File metadata and controls
46 lines (33 loc) · 1.06 KB
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
const fs = require('fs');
const showdown = require('showdown');
// TODO: Do not use filename as title. Instead use the first h1.
const converter = new showdown.Converter();
exports.read = (name, truncate = 0, outputHtml = false) => {
if (!name.includes('.md'))
name = `${name}.md`;
const content = fs
.readFileSync(`./content/articles/${name}`)
.toString();
const truncated = content
.substring(0, truncate == 0 ? content.length : truncate);
if (outputHtml)
return converter.makeHtml(truncated)
return truncated;
};
exports.list = (outputHtml = false) => {
articleList = fs.readdirSync(`./content/articles`);
let articles = [];
articleList.forEach(article => {
articles.push({
"name": article.replace('.md', ''),
"caption": this.read(article, 200, outputHtml)
});
});
return articles;
};
exports.exists = (name) => {
console.log(name)
if (!name.includes('.md'))
name = `${name}.md`;
return fs.existsSync(`./content/articles/${name}`)
};