Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
Latest commit
41 lines (38 loc) · 1.43 KB
/
Copy pathserver.js
File metadata and controls
41 lines (38 loc) · 1.43 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
importNullstackfrom'nullstack';
import{readdirSync,readFileSync}from'fs';
importprismjsfrom'prismjs';
import{Remarkable}from'remarkable';
importmetafrom'remarkable-meta';
importApplicationfrom'./src/Application';
constcontext=Nullstack.start(Application);
context.start=asyncfunctionstart(){
awaitimport('prismjs/components/prism-jsx.min');
constarticles=readdirSync('articles');
constmd=newRemarkable({
// eslint-disable-next-line no-undef
highlight: (code)=>Prism.highlight(code,prismjs.languages.jsx,'javascript'),
});
md.use(meta);
md.use((md)=>{
constoriginalRender=md.renderer.rules.link_open;
md.renderer.rules.link_open=(...args)=>{
letresult=originalRender(...args);
constregexp=/href="([^"]*)"/;
consthref=regexp.exec(result)[1];
if(!href.startsWith('/')){
result=result.replace('>',' target="_blank" rel="noopener">');
}
returnresult;
};
});
context.articles=articles.map((name)=>{
constmarkdown=readFileSync(`articles/${name}`,'utf-8');
const[slug]=name.split('.');
constcontent=md.render(markdown);
const{date: dateString, ...meta}=md.meta;
constdate=newDate(dateString);
constreadingTime=~~(content.split(' ').length/200);
return{ slug, content, date, readingTime, ...meta};
}).sort((a,b)=>b.date-a.date);
};
exportdefaultcontext;