UNPKG

@mdfriday/foundry

Version:

The core engine of MDFriday. Convert Markdown and shortcodes into fully themed static sites – Hugo-style, powered by TypeScript.

224 lines 6.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FrontMatterParserImpl = void 0; exports.newFrontMatter = newFrontMatter; exports.getParamToLower = getParamToLower; exports.getParam = getParam; const kind_1 = require("./kind"); const cast_1 = require("../../../../pkg/cast"); const string_1 = require("../../../../pkg/string"); const types_1 = require("../../../../pkg/types"); /** * NewFrontMatter creates a new FrontMatter instance with default values * Exact replica of Go version: func NewFrontMatter() *FrontMatter */ function newFrontMatter() { return { path: '', lang: '', kind: '', title: '', weight: 999, date: new Date(), terms: {}, params: {}, }; } /** * FrontMatterParser class - exact replica of Go's FrontMatterParser struct */ class FrontMatterParserImpl { constructor(params, langSvc, taxonomySvc) { this.params = params; this.langSvc = langSvc; this.taxonomySvc = taxonomySvc; } /** * Parse front matter from parameters * Exact replica of Go version: func (b *FrontMatterParser) Parse() (*FrontMatter, error) */ async parse() { const fm = { terms: {}, params: this.params, path: '', lang: '', kind: '', title: '', weight: 0, date: new Date(), }; try { await this.parseCustomized(fm); } catch (err) { // Return fm even with error, same as Go version throw err; } try { await this.parseTerms(fm); } catch (err) { throw err; } try { await this.parseTitle(fm); } catch (err) { throw err; } try { await this.parseWeight(fm); } catch (err) { throw err; } try { await this.parseDate(fm); } catch (err) { throw err; } return fm; } /** * Parse date field * Exact replica of Go version: func (b *FrontMatterParser) parseDate(fm *FrontMatter) error */ async parseDate(fm) { fm.date = new Date(); if (this.params) { const v = this.params['date']; if (v !== undefined && v !== null) { fm.date = cast_1.Cast.toTime(v); } } } /** * Parse weight field * Exact replica of Go version: func (b *FrontMatterParser) parseWeight(fm *FrontMatter) error */ async parseWeight(fm) { fm.weight = 10000; if (this.params) { const v = this.params['weight']; if (v !== undefined && v !== null) { fm.weight = cast_1.Cast.toInt(v); } } } /** * Parse title field * Exact replica of Go version: func (b *FrontMatterParser) parseTitle(fm *FrontMatter) error */ async parseTitle(fm) { if (this.params) { const v = this.params['title']; if (v !== undefined && v !== null) { fm.title = cast_1.Cast.toString(v); } } } /** * Parse customized fields (path, lang, kind) * Exact replica of Go version: func (b *FrontMatterParser) parseCustomized(fm *FrontMatter) error */ async parseCustomized(fm) { if (!this.params) return; // Look for path, lang and kind, all of which values we need early on. const pathValue = this.params['path']; if (pathValue !== undefined && pathValue !== null) { // Equivalent to paths.ToSlashPreserveLeading(cast.ToString(v)) fm.path = this.toSlashPreserveLeading(cast_1.Cast.toString(pathValue)); } const langValue = this.params['lang']; if (langValue !== undefined && langValue !== null) { const lang = cast_1.Cast.toString(langValue).toLowerCase(); if (this.langSvc.isLanguageValid(lang)) { fm.lang = lang; } } const kindValue = this.params['kind']; if (kindValue !== undefined && kindValue !== null) { const s = cast_1.Cast.toString(kindValue); if (s !== '') { const kind = (0, kind_1.getKindMain)(s); if (kind === '') { throw new Error(`unknown kind "${s}" in front matter`); } fm.kind = kind; } } } /** * Parse terms (taxonomies) * Exact replica of Go version: func (b *FrontMatterParser) parseTerms(fm *FrontMatter) error */ async parseTerms(fm) { const views = this.taxonomySvc.views(); for (const viewName of views) { const vals = types_1.Types.toStringSlicePreserveString(getParam(this.params || {}, viewName.plural(), false)); if (vals === null) { continue; } fm.terms[viewName.plural()] = vals; } } /** * Convert path to slash preserving leading slash * Equivalent to Go's paths.ToSlashPreserveLeading */ toSlashPreserveLeading(path) { // Convert backslashes to forward slashes return path.replace(/\\/g, '/'); } } exports.FrontMatterParserImpl = FrontMatterParserImpl; /** * Get parameter with case-insensitive key lookup (lowercase version) * Exact replica of Go version: func GetParamToLower(m maps.Params, key string) any */ function getParamToLower(m, key) { return getParam(m, key, true); } /** * Get parameter with case-insensitive key lookup * Exact replica of Go version: func GetParam(p maps.Params, key string, stringToLower bool) any */ function getParam(p, key, stringToLower) { const v = p[key.toLowerCase()]; if (v === null || v === undefined) { return null; } // Exact replica of Go version switch statement switch (typeof v) { case 'boolean': return v; case 'string': if (stringToLower) { return v.toLowerCase(); } return v; case 'number': // In Go, integers are converted to int, floats to float64 if (Number.isInteger(v)) { return cast_1.Cast.toInt(v); } else { return v; // Keep as float64 equivalent } default: if (v instanceof Date) { return v; } if (Array.isArray(v) && v.every(item => typeof item === 'string')) { if (stringToLower) { return string_1.Strings.sliceToLower(v); } return v; } return v; } } //# sourceMappingURL=frontmatter.js.map