-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathread-content.js
127 lines (93 loc) · 3.53 KB
/
read-content.js
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
"use strict";
var supermarked = require('supermarked'),
fs = require('fs'),
cheerio = require('cheerio'),
Handlebars = require('handlebars'),
barrier = require('./barrier.js'),
figureTemplate = Handlebars.compile(
'<figure id="demo-{{name}}" data-demo="{{name}}" {{#if autoplay}}data-autoplay{{/if}}></figure>'
),
MARKDOWN_OPTS = {ignoreMath:true, smartypants:true, gfm:true, tables:true},
MD_PREFIX = '{{#if pdfLink}}This page is also [available as a PDF]({{pdfLink}}).{{/if}}\n',
MD_POSTFIX = fs.readFileSync('content/postfix.md');
Handlebars.registerHelper("demo", function(name, mode) {
var autoplay = (mode == "autoplay");
return new Handlebars.SafeString( figureTemplate({name:name, autoplay:autoplay}) );
});
function postProcessMarkup($) {
$('pre').each(function(i, ele){
var code = $(ele);
if( /deprecated/i.exec( code.text()) ) {
var details = $('<details>');
code.replaceWith(details);
details
.append('<summary>Deprecated API</summary>')
.append(code);
}
});
return $;
}
function outline($){
var mainHeadingEle = $('h1').first(),
mainHeading = {
text: mainHeadingEle.text(),
id: mainHeadingEle.attr('id')
},
sectionHeadings = [];
$('h2').each(function(i, element){
var jEle = $(element),
text = jEle.text();
sectionHeadings.push({
text: text,
id: jEle.attr('id')
});
});
mainHeadingEle.remove();
return {
content: $.html(),
heading: mainHeading,
sections: sectionHeadings,
multipleSections: sectionHeadings.length > 1
}
}
function readContent(requestedPage, opts, callback) {
console.log('request for page:', requestedPage.blue);
function pdfFile(pageName) {
return 'pdf/' + pageName + '.pdf';
}
function pdfUrl(pageName) {
return '/' + pageName + '.pdf';
}
function markdownFile(pageName) {
return 'content/' + pageName + '.md';
}
fs.exists(markdownFile(requestedPage), function(requestedMarkdownExists){
var actualPage = requestedMarkdownExists? requestedPage : '404',
markdownToRead = markdownFile(actualPage),
markdownContent,
bar = barrier(function(){
var surroundedMarkdown = MD_PREFIX + markdownContent + MD_POSTFIX,
filledInMarkdown = Handlebars.compile(surroundedMarkdown)(opts),
html = supermarked(filledInMarkdown, MARKDOWN_OPTS),
$ = postProcessMarkup(cheerio.load(html)),
response = outline($);
response.status = requestedMarkdownExists? 200 : 404;
if( !requestedMarkdownExists ) {
console.log('no such markdown file:'.red, markdownFile(requestedPage));
}
callback( response );
});
opts.page = actualPage;
opts.isDownloadPage = (actualPage == 'download');
fs.exists(pdfFile(requestedPage), bar.add(function(exists){
if( exists ) {
opts.pdfLink = pdfUrl(requestedPage);
}
}));
// fileToRead should point to legit page by now (possibly 404)
fs.readFile(markdownToRead, bar.add(function(err, markdownBuffer){
markdownContent = markdownBuffer.toString();
}));
});
}
module.exports = readContent;