-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmscontent.js
170 lines (155 loc) · 5.3 KB
/
cmscontent.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const path = require('path');
const read = require('node-readability');
const winston = require('winston');
const Promise = require('bluebird');
const fs = Promise.promisifyAll(require('fs'));
const uiLangs = require('./configs/uiLangs.json');
const jsdom = require('jsdom');
const { JSDOM } = jsdom;
const appconstants = require('./constants');
/**
* get the path to the pages.json file
*/
const getPagesConfigFile = () => path.join(
appconstants.CONFIG_FOLDER,
appconstants.CONFIG_CONTENT_PAGES
);
/**
* Get the path to a specific content file
* @param {string} pageName
* @param {string} lang
*/
const getContentOutputFile = (pageName, lang) => path.join(
appconstants.CONTENT_CACHE,
`${pageName}_${lang}.html`
);
const getErrorFile = (lang) => {
var langCode = lang.split("_");
var availableLangs = uiLangs.uiLanguages;
// check if the current language language is there in the list
// of ui languages otherwise return english
if (availableLangs.indexOf(langCode[0]) > -1) {
return
path.join(
appconstants.CONTENT_CACHE, "error",
`_error_${langCode[0]}.html`
);
} else {
return
path.join(
appconstants.CONTENT_CACHE, "error",
"_error_en.html"
);
}
}
;
/**
* Reads pages.json and generates static content for each of the listed content pages
* for different languages.
*/
const processContentFiles = () => {
fs.readFileAsync(getPagesConfigFile(), 'utf8')
.then(
(content) => {
let objPages = JSON.parse(content);
return objPages;
}
)
.then(
(objPages) => {
// first add the name to each of the content object
// this will return an array within an array
let contentWithName = objPages.pages.map(
page => page.content.map(
content => Object.assign({"name": page.name}, content)
)
);
// flatten the array to get a flat list of content children
let contents = Array.prototype.concat(...contentWithName);
// process each item in the array and generate page per language
contents.forEach( (page) => processPage(page) );
}
)
.catch( (error) => {
winston.log(" ERROR ", error);
throw error;
});
};
/**
* Process the page specified in pageInfo and process it
* @param {object} pageInfo object with url, name and lang
*/
const processPage = (pageInfo) => {
winston.log( "info", `processing page ${pageInfo.url} for lang ${pageInfo.lang}`);
read(
pageInfo.url,
(err, article, meta) => {
// page title and content
let title = article.title;
let content = article.content;
// close the jsdom handle
article.close();
// render the page html
let pageHtml = `<div class="aif-content-page"><h1>${title}</h1><div>${content}</div></div>`;
// cleanup the page removing uneccesary footers
let contentHtml = pageCleanup(pageHtml);
// get the output file name
let outputFile = getContentOutputFile(pageInfo.name, pageInfo.lang);
// write the file
fs.writeFileAsync(outputFile, contentHtml, {})
.then(() => {
winston.log('info', 'wrote file ' + outputFile);
})
.catch(
(err) => {
winston.log('error', 'error while saving file', err);
throw err;
}
);
}
);
};
/**
* Cleanup the html page
* @param {string} pageHtml
*/
const pageCleanup = (pageHtml) => {
const domjs = new JSDOM(`${pageHtml}`);
const foundElem = domjs.window.document.querySelector("#jp-post-flair");
foundElem.parentNode.removeChild(foundElem);
return domjs.serialize();
};
/**
* Get the file from the file system
* @param {object} req
* @param {object} res
* @param {function} next
* @param {string} page
* @param {string} lang
*/
function serveFile(req, res, next, page, lang) {
let cmsPage = page.startsWith('_error') ? getErrorFile(lang) : getContentOutputFile(page, lang);
fs.readFileAsync(cmsPage, 'utf8')
.then(
(content) => {
const domjs = new JSDOM(`${content}`);
const foundElem = domjs.window.document.querySelector("div.aif-content-page");
res.set('Content-Type', 'text/html');
res.send(foundElem.outerHTML);
}
).catch(
(err) => {
winston.log('error', "error while serving file " + page, err );
let errLang = lang || "en" ;
// prevent infinite loops
if (!page.startsWith('_error')) {
serveFile(req, res, next, '_error', errLang);
}
}
);
}
// call this on module load to initialize the pages
// called subsequently on daily cron
processContentFiles();
module.exports.serveFile = serveFile;
module.exports.processContentFiles = processContentFiles;