-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.js
More file actions
123 lines (110 loc) · 3.84 KB
/
utils.js
File metadata and controls
123 lines (110 loc) · 3.84 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
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
const fs = require('fs');
const path = require('path');
const DEFAULT_OG_IMAGE = '/img/opengraph-default.png';
const GIT_DATES_CACHE = path.join(__dirname, '.cache', 'git-dates.json');
const cfs = function(val) {
return String(val).charAt(0).toUpperCase() + String(val).slice(1);
};
function loadGitDatesCache() {
if (fs.existsSync(GIT_DATES_CACHE)) {
try {
return JSON.parse(fs.readFileSync(GIT_DATES_CACHE, 'utf8'));
} catch (e) {
return {};
}
}
return {};
}
function relToAbsURL(page, link) {
if (!link.startsWith("https:") && link.indexOf(page.url) < 0) {
const dir = page.filePathStem.replace("index", "").replace(/\/+$/, "");
const imgPath = link.replace(/^\/+/, "");
return dir + "/" + imgPath;
}
else {
return link;
}
}
module.exports = {
// The date returned may be off by 1 day due to UTC Offset
// This split on T is probably not ideal, but good enough for now
formatDate: function(date) {
return date.toISOString().split("T")[0];
},
// displayDate: function(data) {
// TODO: desired format is January 01, 2025
// },
// TODO: actually this is for Atom/RSS feeds, not sitemaps
formatDateForAtomFeed: function(date) {
let dateObj = new Date(date);
// Atom uses RFC 3339 dates
// https://tools.ietf.org/html/rfc3339#section-5.8
let s = dateObj.toISOString();
// remove milliseconds
let split = s.split(".");
split.pop();
return split.join("") + "Z";
},
buildOGImageURL: data => {
if (!data.openGraphImage) {
// TODO: this is where to generate the OG images, for now use the IDD template
// TODO: OR this should be the default OG image but its relative path may require work to compute
console.warn(`buildOGImageURL is missing an OG image for ${data.title}`);
return DEFAULT_OG_IMAGE;
}
else {
return relToAbsURL(data.page, data.openGraphImage);
}
// else if (!data.openGraphImage.startsWith("https:")) {
// const dir = data.page.filePathStem.replace("index", "").replace(/\/+$/, "");
// const imgPath = data.openGraphImage.replace(/^\/+/, "");
// return dir + "/" + imgPath;
// }
// else {
// return data.openGraphImage;
// }
},
getCover: data => {
return data.cover ? relToAbsURL(data.page, data.cover) : relToAbsURL(data.page, data.openGraphImage);
},
// TODO: currently unused since mediaTags are now titlecased
// capitalizeFirstLetter: cfs,
titleCase: function(str) {
const words = str.split(" ");
if (words.length < 2) {
return cfs(str);
}
for (let i = 0; i < words.length; i++) {
words[i] = cfs(words[i]);
}
return words.join(" ");
},
gitDates: function(pagePath, readCache = loadGitDatesCache) {
let cache;
try {
cache = readCache();
} catch (e) {
cache = null;
}
if (!cache || typeof cache !== 'object') {
return Promise.resolve({
modified: null,
created: null
});
}
// Normalize path for cache lookup
const normalizedPath = pagePath.startsWith('./') ? pagePath.substring(2) : pagePath;
if (cache[normalizedPath]) {
return Promise.resolve({
modified: new Date(cache[normalizedPath].modified),
created: new Date(cache[normalizedPath].created)
});
}
// Fallback to page date if no cache entry
return Promise.resolve({
modified: null,
created: null
});
}
// TODO: if date is missing, try to parse it from the folder name?
};