Skip to content

Commit cf66c2e

Browse files
committed
Merge pull request #302 from Martii/documentPageFixes
Document page fixes
2 parents 2caf115 + fbf9e4a commit cf66c2e

File tree

2 files changed

+73
-64
lines changed

2 files changed

+73
-64
lines changed

controllers/document.js

+72-63
Original file line numberDiff line numberDiff line change
@@ -14,88 +14,97 @@ exports.view = function (aReq, aRes, aNext) {
1414
var options = {};
1515
var tasks = [];
1616

17-
18-
var matches = null;
1917
var page = null;
20-
2118
var documentPath = null;
22-
23-
//---
24-
function preRender() {};
25-
function render() { aRes.render('pages/documentPage', options); }
26-
function asyncComplete(err) { if (err) { return aNext(); } else { preRender(); render(); } };
19+
var document = aReq.route.params.document;
2720

2821
// Session
2922
authedUser = options.authedUser = modelParser.parseUser(authedUser);
3023
options.isMod = authedUser && authedUser.isMod;
3124
options.isAdmin = authedUser && authedUser.isAdmin;
3225

33-
//--- Tasks
34-
35-
var document = aReq.route.params.document;
3626

3727
if (document) {
3828
documentPath = 'views/includes/documents';
3929

40-
fs.readdir(documentPath, function (aErr, aFiles) {
41-
if (!aErr) {
42-
var file = null;
43-
44-
// Dynamically create a file listing of the pages
45-
options.files = [];
46-
for (file in aFiles) {
47-
if (/\.md$/.test(aFiles[file])) {
48-
options.files.push({
49-
href: aFiles[file].replace(/\.md$/, ''),
50-
textContent: aFiles[file].replace(/\.md$/, '').replace(/-/g, ' ')
51-
});
52-
}
53-
}
54-
}
55-
});
56-
57-
fs.readFile(documentPath + '/' + document + '.md', 'UTF8', function (aErr, aData) {
58-
if (aErr) {
59-
return statusCodePage(aReq, aRes, aNext, {
60-
statusCode: 404,
61-
statusMessage: 'Page not found.'
62-
});
63-
}
64-
65-
var lines = null;
66-
var heading = null;
67-
var content = null;
68-
69-
// Check if first line is h2 and use for title/heading if present
70-
lines = aData.match(/.*/gm);
71-
if (lines) {
72-
matches = lines[0].match(/^##\s(.*)$/);
73-
if (matches) {
74-
heading = lines.shift().replace(/^##\s+/, "");
75-
} else {
76-
heading = page;
30+
//--- Tasks
31+
tasks.push(function (aCallback) {
32+
async.waterfall([
33+
34+
// Read file listing
35+
function (aCallback) {
36+
fs.readdir(documentPath, function (aErr, aFiles) {
37+
if (aErr) { aCallback('Error retrieving page list'); return; }
38+
39+
var file = null;
40+
41+
// Dynamically create a file listing of the pages
42+
options.files = [];
43+
for (file in aFiles) {
44+
if (/\.md$/.test(aFiles[file])) {
45+
options.files.push({
46+
href: aFiles[file].replace(/\.md$/, ''),
47+
textContent: aFiles[file].replace(/\.md$/, '').replace(/-/g, ' ')
48+
});
49+
}
50+
}
51+
52+
aCallback(null);
53+
});
54+
},
55+
56+
// Read md file contents
57+
function (aCallback) {
58+
fs.readFile(documentPath + '/' + document + '.md', 'UTF8', function (aErr, aData) {
59+
if (aErr) { aCallback('Error retrieving page'); return; }
60+
61+
var lines = null;
62+
var matches = null;
63+
var heading = null;
64+
var content = null;
65+
66+
// Check if first line is h2 and use for title/heading if present
67+
lines = aData.match(/.*/gm);
68+
if (lines) {
69+
matches = lines[0].match(/^##\s(.*)$/);
70+
if (matches) {
71+
heading = lines.shift().replace(/^##\s+/, "");
72+
} else {
73+
heading = page;
74+
}
75+
content = lines.join('\n');
76+
} else {
77+
heading = page;
78+
content = aData;
79+
}
80+
81+
// Page metadata
82+
pageMetadata(options, [heading, 'About']);
83+
84+
options.pageHeading = heading;
85+
options.pageData = renderMd(content);
86+
87+
aCallback(null);
88+
});
7789
}
78-
content = lines.join('\n');
79-
} else {
80-
heading = page;
81-
content = aData;
82-
}
83-
84-
// Page metadata
85-
pageMetadata(options, [heading, 'About']);
86-
87-
options.pageHeading = heading;
88-
options.pageData = renderMd(content);
89-
90-
async.parallel(tasks, asyncComplete);
90+
], aCallback)
9191
});
9292
}
9393
else {
9494
// Page metadata
9595
pageMetadata(options, ['About', 'About']);
9696

9797
options.isAbout = true;
98-
99-
async.parallel(tasks, asyncComplete);
10098
}
99+
100+
//---
101+
async.parallel(tasks, function (aErr) {
102+
if (aErr) {
103+
return statusCodePage(aReq, aRes, aNext, {
104+
statusMessage: aErr
105+
})
106+
}
107+
108+
aRes.render('pages/documentPage', options);
109+
});
101110
};

views/pages/documentPage.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ <h2 class="page-heading">
3939
<div class="col-sm-4">
4040
<div class="panel panel-default">
4141
<div class="panel-heading">
42-
<div class="panel-title">Pages</div>
42+
<div class="panel-title"><em class="fa fa-fw fa-file-text-o"></em> Pages</div>
4343
</div>
4444
<div class="panel-body">
4545
<ul class="list-group">

0 commit comments

Comments
 (0)