-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphoto.js
49 lines (41 loc) · 1.48 KB
/
photo.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
import { photoData } from "./photos-builder/photo-data.js";
import fs from "fs"
const template = fs.readFileSync('./photos-builder/templates/template.html', "utf8");
const listTemplate = fs.readFileSync('./photos-builder/templates/template-list.html', "utf8");
const photoTemplate = (src, caption, alt) => `<section class='myphoto'>
<figure>
<img loading=lazy src="../assets/photos/${src}" alt="${alt}">
<figcaption>${caption}</figcaption>
</figure>
</section>`
/**
* mobile photos are 600px height as pattern
*
* @param {*} src
* @param {*} caption
* @param {*} alt
* @param {*} name
* @returns
*/
const photoListItemTemplate = (src, caption, alt, name) => `
<a href="./${name}.html" class="photo-card">
<img loading=lazy src="../assets/photos/m/${src}" alt="${alt}">
</a>`
const writePhotoItemHtmlFile = (item) => {
const photoItemHtml = photoTemplate(item.src, item.caption, item.alt)
let output = template.replace("{{photo}}", photoItemHtml);
fs.writeFileSync(`./photos/${item.name}.html`, output);
}
const writePhotoListHtmlFile = () => {
const photoList = photoData.map((item) => {
return photoListItemTemplate(item.src, item.caption, item.alt, item.name)
}).join("")
let output = listTemplate.replace("{{list}}", photoList);
fs.writeFileSync(`./photos/index.html`, output);
}
// main
// creates all the item html pages
photoData.forEach((item) => {
writePhotoItemHtmlFile(item)
})
writePhotoListHtmlFile()