work on events
This commit is contained in:
@@ -5,12 +5,13 @@ import { BuildState } from './state';
|
||||
import { mkdirp, write_text } from '../fs';
|
||||
import { CalendarConfig, RSSConfig } from '../conf';
|
||||
import { render_theme_css_properties } from '../themes';
|
||||
import { load_partials, FrontMatter, Context, load_layout, render_template } from '../template';
|
||||
import { load_partials, FrontMatter, Context, load_layout, render_template, EventFrontmatter } from '../template';
|
||||
import { render_markdown_to_html, render_markdown_to_html_inline_sync } from '@doc-utils/markdown2html';
|
||||
import { RSSEntry } from './rss';
|
||||
import { DateTime } from 'luxon';
|
||||
import { EventEntry } from './icalendar';
|
||||
import { as_context_time, as_html_time, from_iso } from '../time';
|
||||
import { FileMetadata } from '../metadata';
|
||||
|
||||
export interface OutFileURL {
|
||||
base_url: string;
|
||||
@@ -82,31 +83,42 @@ export async function build_partials(state: BuildState) {
|
||||
Object.assign(state.partials, state.extras);
|
||||
}
|
||||
|
||||
export function mustache_context(state: BuildState, page_url: string, frontmatter?: FrontMatter) : Context {
|
||||
let event: Context['event'] = {
|
||||
start: null,
|
||||
end: null,
|
||||
zone: null,
|
||||
};
|
||||
export function mustache_context(state: BuildState, page_url: string, metadata: FileMetadata, frontmatter?: FrontMatter) : Context {
|
||||
let event: Context['event'];
|
||||
|
||||
if (frontmatter?.event) {
|
||||
const start = from_iso(frontmatter.event.start, frontmatter.event.zone);
|
||||
event.start = as_context_time(start);
|
||||
|
||||
const end = from_iso(frontmatter.event.end, frontmatter.event.zone);
|
||||
event.end = as_context_time(end);
|
||||
event = Array.isArray(frontmatter.event)
|
||||
? frontmatter.event.map(to_context_event)
|
||||
: to_context_event(frontmatter.event);
|
||||
|
||||
event.zone = frontmatter.event.zone;
|
||||
function to_context_event(event_fm: EventFrontmatter) {
|
||||
const start = from_iso(event_fm.start, event_fm.time_zone);
|
||||
const end = from_iso(event_fm.end, event_fm.time_zone);
|
||||
|
||||
return {
|
||||
start: as_context_time(start),
|
||||
end: as_context_time(end),
|
||||
time_zone: event_fm.time_zone,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const has_been_updated = metadata.first_seen_time !== metadata.last_updated_time;
|
||||
|
||||
return {
|
||||
env: state.env,
|
||||
page: frontmatter,
|
||||
base_url: state.conf.base_url,
|
||||
page_url: page_url,
|
||||
page_published: as_context_time(from_iso(metadata.first_seen_time), 'dt-published'),
|
||||
page_updated: has_been_updated ? as_context_time(from_iso(metadata.last_updated_time), 'dt-updated') : null,
|
||||
site_title: state.conf.title,
|
||||
author: get_author(state, frontmatter),
|
||||
event: event,
|
||||
event_series: Array.isArray(event) && {
|
||||
start: event[0].start,
|
||||
end: event[event.length - 1].end,
|
||||
},
|
||||
build_time: state.build_time,
|
||||
icons: icons,
|
||||
rss_feeds: state.conf.rss || [ ],
|
||||
@@ -127,7 +139,7 @@ export function mustache_context(state: BuildState, page_url: string, frontmatte
|
||||
};
|
||||
}
|
||||
|
||||
export async function render_page(state: BuildState, in_file: string, out_file: string, out_url: OutFileURL, text: string, render_as_markdown: boolean, frontmatter?: any) {
|
||||
export async function render_page(state: BuildState, in_file: string, out_file: string, out_url: OutFileURL, text: string, render_as_markdown: boolean, hash: string, frontmatter?: any) {
|
||||
if (render_as_markdown) {
|
||||
const opts = Object.assign({ }, state.conf.markdown, {
|
||||
base_url: out_url.abs_url
|
||||
@@ -147,11 +159,21 @@ export async function render_page(state: BuildState, in_file: string, out_file:
|
||||
layout = state.layouts[layout_file];
|
||||
}
|
||||
|
||||
const hash_matches = file_hash_matches(state, in_file, hash);
|
||||
const rel_in_file = in_file.slice(state.conf.input.root.length);
|
||||
const old_metadata = state.old_metadata?.files?.[rel_in_file];
|
||||
const new_metadata = hash_matches ? structuredClone(old_metadata) : {
|
||||
first_seen_time: old_metadata?.first_seen_time || state.build_time.iso,
|
||||
last_build_hash: hash,
|
||||
last_updated_time: state.build_time.iso,
|
||||
};
|
||||
|
||||
const tags = state.conf.templates?.tags;
|
||||
const context = mustache_context(state, out_url.abs_url, frontmatter);
|
||||
const context = mustache_context(state, out_url.abs_url, new_metadata, frontmatter);
|
||||
const rendered = render_template(text, context, layout, structuredClone(state.partials), tags);
|
||||
await write_text(out_file, rendered);
|
||||
|
||||
state.new_metadata.files[rel_in_file] = new_metadata;
|
||||
handle_page_side_effects(state, in_file, out_file, out_url, text, frontmatter);
|
||||
}
|
||||
|
||||
@@ -230,16 +252,30 @@ function handle_event(state: BuildState, in_file: string, out_url: OutFileURL, f
|
||||
const author_or_authors = get_author(state, frontmatter);
|
||||
const author = Array.isArray(author_or_authors) ? author_or_authors[0] : author_or_authors;
|
||||
|
||||
if (Array.isArray(frontmatter.event)) {
|
||||
state.event_series.push({
|
||||
url: out_url.abs_url,
|
||||
in_file: in_file,
|
||||
title: frontmatter.title,
|
||||
description: frontmatter.description,
|
||||
author_name: author?.name,
|
||||
author_email: author?.email,
|
||||
entries: frontmatter.event.slice(),
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
state.events.push({
|
||||
url: out_url.abs_url,
|
||||
in_file: in_file,
|
||||
title: frontmatter.title,
|
||||
title: frontmatter.event.title || frontmatter.title,
|
||||
description: frontmatter.description,
|
||||
author_name: author?.name,
|
||||
author_email: author?.email,
|
||||
start_time: frontmatter.event?.start,
|
||||
end_time: frontmatter.event?.end,
|
||||
time_zone: frontmatter.event?.zone,
|
||||
start: frontmatter.event.start,
|
||||
end: frontmatter.event.end,
|
||||
time_zone: frontmatter.event.time_zone,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -247,6 +283,12 @@ function handle_calendar(state: BuildState, cal_conf: CalendarConfig, entries: E
|
||||
const author_or_authors = get_author(state, frontmatter);
|
||||
const author = Array.isArray(author_or_authors) ? author_or_authors[0] : author_or_authors;
|
||||
|
||||
if (Array.isArray(frontmatter.event)) {
|
||||
// todo: add each event to the calendar
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
entries.push({
|
||||
url: out_url.abs_url,
|
||||
in_file: in_file,
|
||||
@@ -254,9 +296,9 @@ function handle_calendar(state: BuildState, cal_conf: CalendarConfig, entries: E
|
||||
description: frontmatter.description,
|
||||
author_name: author?.name,
|
||||
author_email: author?.email,
|
||||
start_time: frontmatter.event?.start,
|
||||
end_time: frontmatter.event?.end,
|
||||
time_zone: frontmatter.event?.zone,
|
||||
start: frontmatter.event?.start,
|
||||
end: frontmatter.event?.end,
|
||||
time_zone: frontmatter.event?.time_zone,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -295,20 +337,6 @@ export function skip_file(state: BuildState, in_file: string, out_file: string,
|
||||
handle_page_side_effects(state, in_file, out_file, out_url, frontmatter);
|
||||
}
|
||||
|
||||
export function copy_metadata(state: BuildState, in_file: string) {
|
||||
in_file = in_file.slice(state.conf.input.root.length);
|
||||
state.new_metadata.files[in_file] = structuredClone(state.old_metadata?.files?.[in_file]);
|
||||
}
|
||||
|
||||
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,
|
||||
};
|
||||
}
|
||||
|
||||
export function get_author(state: BuildState, frontmatter?: FrontMatter) {
|
||||
if (! frontmatter?.author) {
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user