work on splitting up extras css; start handling build metadata; start building sitemaps
This commit is contained in:
230
src/build-files/helpers.ts
Normal file
230
src/build-files/helpers.ts
Normal file
@@ -0,0 +1,230 @@
|
||||
|
||||
import { dirname, join as path_join } from 'path';
|
||||
import { mkdirp, write_text } from '../fs';
|
||||
import { icons } from '../icons';
|
||||
import { BuildState } from './state';
|
||||
import { load_partials, FrontMatter, Context, load_layout, render_template } from '../template';
|
||||
import { render_theme_css_properties } from '../themes';
|
||||
import { render_markdown_to_html, render_markdown_to_html_inline_sync } from '@doc-utils/markdown2html';
|
||||
import { CalendarConfig, RSSConfig } from '../conf';
|
||||
|
||||
export interface OutFileURL {
|
||||
base_url: string;
|
||||
rel_path: string;
|
||||
dir_url: string;
|
||||
abs_url: string;
|
||||
}
|
||||
|
||||
export function map_output_file_to_url(state: BuildState, out_file: string, index_file?: string) : OutFileURL {
|
||||
let rel_path = out_file.slice(state.conf.input.root.length);
|
||||
|
||||
if (index_file && rel_path.endsWith('/' + index_file)) {
|
||||
rel_path = rel_path.slice(0, -(index_file.length + 1));
|
||||
}
|
||||
|
||||
if (! rel_path.startsWith('/')) {
|
||||
rel_path = '/' + rel_path;
|
||||
}
|
||||
|
||||
const base_url = state.conf.base_url.endsWith('/')
|
||||
? state.conf.base_url.slice(0, -1)
|
||||
: state.conf.base_url;
|
||||
|
||||
return {
|
||||
base_url,
|
||||
rel_path,
|
||||
dir_url: base_url + dirname(rel_path),
|
||||
abs_url: base_url + rel_path
|
||||
};
|
||||
}
|
||||
|
||||
export async function map_input_file_to_output_file(state: BuildState, in_file: string, remove_exts?: string[], add_ext?: string) {
|
||||
if (! in_file.startsWith(state.conf.input.root)) {
|
||||
throw new Error('input file expected to be inside input root');
|
||||
}
|
||||
|
||||
let out_file = path_join(state.conf.output.root, in_file.slice(state.conf.input.root.length));
|
||||
|
||||
if (remove_exts) {
|
||||
for (const ext of remove_exts) {
|
||||
if (out_file.endsWith(ext)) {
|
||||
out_file = out_file.slice(0, -ext.length);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (add_ext) {
|
||||
out_file += add_ext;
|
||||
}
|
||||
|
||||
const dir = dirname(out_file);
|
||||
|
||||
if (! state.made_directories.has(dir)) {
|
||||
state.made_directories.add(dir);
|
||||
await mkdirp(dir);
|
||||
}
|
||||
|
||||
return out_file;
|
||||
}
|
||||
|
||||
export async function build_partials(state: BuildState) {
|
||||
state.partials = await load_partials(state.conf);
|
||||
|
||||
for (const [name, theme] of Object.entries(state.themes)) {
|
||||
state.partials[`.themes/${name}`] = render_theme_css_properties(theme);
|
||||
}
|
||||
|
||||
Object.assign(state.partials, state.extras);
|
||||
}
|
||||
|
||||
export function mustache_context(state: BuildState, page_url: string, frontmatter?: FrontMatter) : Context {
|
||||
return {
|
||||
env: state.env,
|
||||
page: frontmatter,
|
||||
base_url: state.conf.base_url,
|
||||
page_url: page_url,
|
||||
build_time: state.build_time,
|
||||
icons: icons,
|
||||
themes: Object.values(state.themes),
|
||||
theme_groups: structuredClone(state.theme_groups),
|
||||
markdown: {
|
||||
render_inline() {
|
||||
return (text, render) => {
|
||||
const md = render(text)
|
||||
const html = render_markdown_to_html_inline_sync(md,
|
||||
Object.assign({ }, state.conf.markdown, { base_url: page_url })
|
||||
);
|
||||
return html;
|
||||
};
|
||||
},
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export async function render_page(state: BuildState, out_file: string, out_url: OutFileURL, text: string, render_as_markdown: boolean, frontmatter?: any) {
|
||||
if (render_as_markdown) {
|
||||
const opts = Object.assign({ }, state.conf.markdown, {
|
||||
base_url: out_url.abs_url
|
||||
});
|
||||
|
||||
text = await render_markdown_to_html(text, opts);
|
||||
}
|
||||
|
||||
let layout: string;
|
||||
const layout_file = frontmatter?.layout;
|
||||
|
||||
if (layout_file) {
|
||||
if (! state.layouts[layout_file]) {
|
||||
state.layouts[layout_file] = await load_layout(state.conf, layout_file);
|
||||
}
|
||||
|
||||
layout = state.layouts[layout_file];
|
||||
}
|
||||
|
||||
const tags = state.conf.templates?.tags;
|
||||
const context = mustache_context(state, out_url.abs_url, frontmatter);
|
||||
const rendered = render_template(text, context, layout, structuredClone(state.partials), tags);
|
||||
await write_text(out_file, rendered);
|
||||
|
||||
handle_page_side_effects(state, out_file, out_url, frontmatter);
|
||||
}
|
||||
|
||||
function handle_page_side_effects(state: BuildState, out_file: string, out_url: OutFileURL, frontmatter?: any) {
|
||||
// Only actual HTML webpages are registered with things like the RSS feed; This is
|
||||
// to prevent things like CSS files showing up, which may be templated (and therefore
|
||||
// pass through this function), but are not really "pages"
|
||||
if (frontmatter && typeof frontmatter === 'object' && out_file.endsWith('.html')) {
|
||||
if (state.conf.rss) {
|
||||
if (Array.isArray(state.conf.rss)) {
|
||||
for (const rss_conf of state.conf.rss) {
|
||||
handle_rss(state, rss_conf, out_url, frontmatter);
|
||||
}
|
||||
}
|
||||
|
||||
else {
|
||||
handle_rss(state, { }, out_url, frontmatter);
|
||||
}
|
||||
}
|
||||
|
||||
handle_sitemap(state, out_url, frontmatter);
|
||||
handle_event(state, out_url, frontmatter);
|
||||
|
||||
if (state.conf.calendars) {
|
||||
for (const cal_conf of state.conf.calendars) {
|
||||
handle_calendar(state, cal_conf, out_url, frontmatter);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function handle_rss(state: BuildState, rss_conf: RSSConfig, out_url: OutFileURL, frontmatter: any) {
|
||||
// const field = rss_conf
|
||||
// const rss_frontmatter = state.
|
||||
// //
|
||||
}
|
||||
|
||||
function handle_sitemap(state: BuildState, out_url: OutFileURL, frontmatter: any) {
|
||||
if (! state.conf.sitemap) {
|
||||
return;
|
||||
}
|
||||
|
||||
const sitemap_frontmatter = state.conf.sitemap.front_matter_field
|
||||
? frontmatter?.[state.conf.sitemap.front_matter_field] || { }
|
||||
: frontmatter?.sitemap || { };
|
||||
|
||||
state.sitemap.push({
|
||||
url: out_url.abs_url,
|
||||
lastmod: state.build_time.iso,
|
||||
change_freq: sitemap_frontmatter?.change_freq,
|
||||
priority: sitemap_frontmatter?.priority,
|
||||
});
|
||||
}
|
||||
|
||||
function handle_event(state: BuildState, out_url: OutFileURL, frontmatter: any) {
|
||||
//
|
||||
}
|
||||
|
||||
function handle_calendar(state: BuildState, cal_conf: CalendarConfig, out_url: OutFileURL, frontmatter: any) {
|
||||
//
|
||||
}
|
||||
|
||||
export function file_hash_matches(state: BuildState, in_file: string, new_hash: string) {
|
||||
const { old_metadata, new_metadata } = state;
|
||||
|
||||
if (! old_metadata.last_build?.config_hash) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (old_metadata.last_build.config_hash !== new_metadata.last_build.config_hash) {
|
||||
return false;
|
||||
}
|
||||
|
||||
in_file = in_file.slice(state.conf.input.root.length);
|
||||
const old_hash = state.old_metadata.files[in_file]?.last_build_hash;
|
||||
|
||||
if (! old_hash) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (old_hash !== new_hash) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
export function skip_file(state: BuildState, in_file: string, out_file: string, out_url: OutFileURL, frontmatter?: any) {
|
||||
in_file = in_file.slice(state.conf.input.root.length);
|
||||
state.new_metadata.files[in_file] = structuredClone(state.old_metadata?.files?.[in_file]);
|
||||
handle_page_side_effects(state, out_file, out_url, frontmatter);
|
||||
}
|
||||
|
||||
export function update_metadata(state: BuildState, in_file: string, hash: string) {
|
||||
in_file = in_file.slice(state.conf.input.root.length);
|
||||
state.new_metadata.files[in_file] = {
|
||||
first_seen_time: state.old_metadata?.files?.[in_file]?.first_seen_time || state.build_time.iso,
|
||||
last_build_hash: hash,
|
||||
last_updated_time: state.build_time.iso,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user