UNPKG

meblog

Version:

A simple blog engine for personal blogging

118 lines (117 loc) 4.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const path_1 = tslib_1.__importDefault(require("path")); const moment_1 = tslib_1.__importDefault(require("moment")); require("moment-timezone"); const model_1 = require("./model"); const i18n_1 = tslib_1.__importDefault(require("i18n")); class ConfigHolder { _config; constructor(config) { if (!config) { throw new Error('Missing config'); } this._config = config; } get config() { return this._config; } rootUrl(urlPath, locale) { if (urlPath.startsWith('http')) { return urlPath; } const { baseUrl = '' } = this.config; let url = this.url(urlPath, locale); if (baseUrl) { url = baseUrl + url; } return url; } url(urlPath, locale) { if (urlPath.startsWith('http')) { return urlPath; } let localePath = locale; if (this.isDefaultLocale(locale)) { localePath = ''; } const { baseContext = '' } = this.config; return path_1.default.join('/', baseContext, localePath, urlPath); } postPartialPath(post) { const { postUrlStyle } = this.config; const defaultPostsDir = 'posts'; switch (postUrlStyle) { case model_1.PostUrlStyle.POSTS_YEAR_MONTH_SLUG: return `/${defaultPostsDir}/${post.publishedMonth}/${post.slug}.html`; case model_1.PostUrlStyle.POSTS_YEAR_SLUG: return `/${defaultPostsDir}/${post.publishedYear}/${post.slug}.html`; case model_1.PostUrlStyle.YEAR_MONTH_SLUG: return `/${post.publishedMonth}/${post.slug}.html`; case model_1.PostUrlStyle.YEAR_SLUG: return `/${post.publishedYear}/${post.slug}.html`; case model_1.PostUrlStyle.SLUG: return `/${post.slug}.html`; case model_1.PostUrlStyle.POSTS_SLUG: default: return `/${defaultPostsDir}/${post.slug}.html`; } } postUrl(post) { return this.url(this.postPartialPath(post), post.language); } postRootUrl(post) { return this.rootUrl(this.postPartialPath(post), post.language); } tagUrl(tag, locale) { return this.url(`/tags/${tag}.html`, locale); } tagRootUrl(tag, locale) { return this.rootUrl(`/tags/${tag}.html`, locale); } formatDateTime(date, locale) { return moment_1.default(date) .locale(locale || this.config.defaultLocale) .format(this.getDateTimeFormat(locale)); } formatDate(date, locale) { return moment_1.default(date) .locale(locale || this.config.defaultLocale) .format(this.getDateFormat(locale)); } getI18nFallbackToConfig(name, locale) { locale = locale || this.config.defaultLocale; if (!this.config.locales.includes(locale)) { return this.config[name]; } const currentLocale = i18n_1.default['locale']; try { if (currentLocale !== locale) { i18n_1.default.setLocale(locale); } const format = i18n_1.default.__(name); return format === name ? this.config[name] : format; } finally { if (currentLocale !== locale) { i18n_1.default.setLocale(currentLocale); } } } getDateTimeFormat(locale) { return this.getI18nFallbackToConfig('dateTimeFormat', locale); } getDateFormat(locale) { return this.getI18nFallbackToConfig('dateFormat', locale); } formatRFC822DateTime(date) { return moment_1.default(date).tz('UTC').format('ddd, DD MMM YYYY HH:mm:ss ZZ'); } isDefaultLocale(locale) { return !locale || locale === this.config.defaultLocale; } } exports.default = ConfigHolder;