-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage-making.js
More file actions
64 lines (50 loc) · 2.34 KB
/
page-making.js
File metadata and controls
64 lines (50 loc) · 2.34 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const cheerio = require('cheerio');
const fs = require('fs');
const MAIN_URL = "https://unicyclegurus.com";
function GenBlogPreviewPage(articlesData, output_dir) {
// Read the template HTML file
const templateHtml = fs.readFileSync('home template.html', 'utf-8');
const $template = cheerio.load(templateHtml);
// reference main tag
const $main = $template('main');
// reference article tag so we can copy properties
const articleTemplate = $template('article');
for (let j = articlesData.length - 1; j >= 0; j -= 1) {
let articleData = articlesData[j];
let articleDupe = articleTemplate.clone();
if (articleData.text == "") throw new Error("NO TEXT FOUND IN POST!!!");
articleDupe.attr('id', articleData.parseTitle);
$template(articleDupe).find('h2').text(articleData.title);
$template(articleDupe).find('p').text(articleData.text.substring(0, 100) + "...");
$template(articleDupe).find('img').attr('src', articleData.imageRef);
$template(articleDupe).find('a').attr('href', MAIN_URL + "/blog/" + articleData.parseTitle + '.html');
$main.prepend($template.html(articleDupe));
}
articleTemplate.remove();
// Generate the output HTML code with cheerio
const outputHtml = $template.html();
fs.mkdirSync(output_dir, { recursive: true });
// Write the final HTML to a file
fs.writeFileSync('output/index.html', outputHtml);
}
// TODO:
// make finding search for ids instead of elements - in case
// you change the elements in the future
// in the future, add option to only gen one page
function GenBlogPages(articlesData, output_dir) {
const templateHtml = fs.readFileSync('page template.html', 'utf-8');
const $ = cheerio.load(templateHtml);
for (const articleData of articlesData)
{
let artElem = $('article');
$(artElem).find('h2').text(articleData.title);
// also set title tag for seo
$('title').text(articleData.title);
$('#text-body').text(articleData.text);
$(artElem).find('img').attr('src', "../" + articleData.imageRef);
const outputHtml = $.html();
fs.mkdirSync(output_dir, { recursive: true });
fs.writeFileSync(output_dir + '/' + articleData.parseTitle + ".html", outputHtml);
}
}
module.exports = { GenBlogPreviewPage, GenBlogPages };