UNPKG

@rr0/cms

Version:

RR0 Content Management System (CMS)

41 lines (40 loc) 1.38 kB
import path from "path"; export class TimeUrlBuilder { constructor(options) { this.options = options; } fromContext(time) { return this.fromYYMMDD(time.getYear(), time.getMonth(), time.getDayOfMonth()); } fromEdtf(time) { var _a, _b, _c; return this.fromYYMMDD((_a = time.year) === null || _a === void 0 ? void 0 : _a.value, (_b = time.month) === null || _b === void 0 ? void 0 : _b.value, (_c = time.day) === null || _c === void 0 ? void 0 : _c.value); } fromYYMMDD(year, month, day) { let url = this.options.rootDir; if (year) { url = path.join(url, year.toString().split("").join("/")); } if (month) { url += `/${month.toString().padStart(2, "0")}`; } if (day) { url += `/${day.toString().padStart(2, "0")}`; } return url; } isTimeFile(filePath) { return this.options.files.includes(filePath); } /** * @return the found time URL or undefined if not found. */ matchExistingTimeFile(url) { const rootDir = this.options.rootDir; while (url && url !== rootDir && !this.isTimeFile(`${url}/index.html`)) { const slash = url.lastIndexOf("/"); url = url.substring(0, slash); } return url === rootDir ? undefined : url; } }