-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
129 lines (99 loc) · 3.13 KB
/
index.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
const fs = require('fs');
const path = require('path');
let html = '';
/**
*
* @param {{
* layout: "regular" | "longform-standard" | "longform-visual",
* author: string,
* title: string,
* lead: string,
* content: string
* }} options
* @returns
*/
function getHtml(options = {}) {
const {
layout,
author,
title,
lead,
content,
builtCssFilename,
builtJsFilename,
customJsLinks,
} = options;
// Load the layout base.
html = fs.readFileSync(path.resolve(__dirname, `src/${layout}/index.html`), 'utf8');
setCss(options);
html = html.replaceAll('${AUTHOR}', author || 'Author');
html = html.replace('${LEAD}', lead || 'Lead');
html = html.replace('${CONTENT}', content || '');
html = html.replaceAll('${TITLE}', title || 'Title');
html = html.replace('${BUILT_CSS_FILE}', builtCssFilename);
html = html.replace('${BUILT_JS_FILE}', builtJsFilename);
replaceExternalCustomJsLinks(customJsLinks);
return html;
}
function setCss(options) {
const layout = options.layout;
// Set the fonts.
const nzzFonts = fs.readFileSync(path.resolve(__dirname, 'src/nzz.ch-fonts.css'), 'utf8');
html = html.replace('${NZZ_FONTS}', nzzFonts);
// Set the css in the HTML.
let nzzCSS = fs.readFileSync(path.resolve(__dirname, `src/${layout}/style.css`), 'utf8');
html = html.replace('${NZZ_CSS}', nzzCSS);
// Set custom css we defined in this library to override some styles.
const customCSS = fs.readFileSync(path.resolve(__dirname, 'src/nzz.ch-custom.css'), 'utf8');
html = html.replace('${CUSTOM_CSS}', customCSS);
replaceExternalCustomCssLinks(options.customCssLinks);
html = html.replace('${EXTERNAL_CUSTOM_RAW_CSS}', options.customCssRaw || '');
}
function replaceExternalCustomCssLinks(links = []) {
let str = '';
for (let i = 0; i < links.length; i++) {
const link = links[i];
str += `<link rel="stylesheet" type="text/css" href="${link}">`;
}
html = html.replace('${EXTERNAL_CUSTOM_CSS_LINKS}', str);
}
function replaceExternalCustomJsLinks(links = []) {
let str = '';
for (let i = 0; i < links.length; i++) {
const link = links[i];
str += `<script src="${link}"></script>`;
}
html = html.replace('${CUSTOM_JS_LINKS}', str);
}
function createQElement(id, fullWidth = false) {
let fwClass = 'widget--fullwidth';
if (fullWidth === false) fwClass = '';
return `
<div id="${id}" class="articlecomponent q-embed widget--qembed ${fwClass}"></div>
`;
}
function createFullwidthQElement(id) {
return createQElement(id, true);
}
function createContentWidthQElement(id) {
return createQElement(id, false);
}
function createSubtitle(content = '') {
return `
<h2 class="subtitle articlecomponent">
<span>${content}</span>
</h2>
`;
}
function createParagraph(content = '') {
return `
<p class="articlecomponent text">${content}</p>
`;
}
module.exports = {
getHtml,
createFullwidthQElement,
createContentWidthQElement,
createSubtitle,
createParagraph,
}