2024-11-27 19:02:30 +01:00
|
|
|
use std::{
|
|
|
|
collections::{HashMap, HashSet},
|
|
|
|
ops::ControlFlow,
|
|
|
|
};
|
2023-08-27 15:59:52 +02:00
|
|
|
|
2024-11-17 22:34:43 +01:00
|
|
|
use anyhow::{anyhow, Context};
|
2024-11-26 22:09:39 +01:00
|
|
|
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
|
2023-08-27 15:59:52 +02:00
|
|
|
use serde::{Deserialize, Serialize};
|
2024-11-26 22:00:02 +01:00
|
|
|
use tracing::{error, info_span, instrument};
|
2023-08-27 15:59:52 +02:00
|
|
|
|
2024-07-19 19:52:56 +02:00
|
|
|
use crate::{
|
|
|
|
html::highlight::{
|
|
|
|
compiled::{compile_syntax, CompiledSyntax},
|
|
|
|
Syntax,
|
|
|
|
},
|
2024-07-19 20:05:17 +02:00
|
|
|
import_map::ImportRoot,
|
2024-11-29 20:03:32 +01:00
|
|
|
vfs::{self, Content, Dir, DynDir, ImageSize, VPath, VPathBuf},
|
2024-03-10 23:23:50 +01:00
|
|
|
};
|
|
|
|
|
2025-01-10 14:10:23 +01:00
|
|
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
|
|
|
pub struct VfsConfig {
|
|
|
|
/// Cache salt string. Passed to `Blake3ContentVersionCache` as a salt for content version hashes.
|
|
|
|
/// Can be changed to bust cached assets for all clients.
|
|
|
|
pub cache_salt: String,
|
|
|
|
}
|
|
|
|
|
2023-08-27 15:59:52 +02:00
|
|
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
|
|
|
pub struct Config {
|
|
|
|
/// Website root; used when generating links.
|
|
|
|
/// Can also be specified using the environment variable `$TREEHOUSE_SITE`. (this is the
|
|
|
|
/// preferred way of setting this in production, so as not to clobber treehouse.toml.)
|
|
|
|
pub site: String,
|
|
|
|
|
2024-09-28 23:43:05 +02:00
|
|
|
/// This is used to generate a link in the footer that links to the page's source commit.
|
|
|
|
/// The final URL is `{commit_base_url}/{commit}/content/{tree_path}.tree`.
|
|
|
|
pub commit_base_url: String,
|
|
|
|
|
2023-08-27 15:59:52 +02:00
|
|
|
/// User-defined keys.
|
|
|
|
pub user: HashMap<String, String>,
|
|
|
|
|
2023-08-27 16:04:07 +02:00
|
|
|
/// Links exported to Markdown for use with reference syntax `[text][def:key]`.
|
|
|
|
pub defs: HashMap<String, String>,
|
2023-08-27 18:25:21 +02:00
|
|
|
|
2024-11-27 19:02:30 +01:00
|
|
|
/// Config for syndication feeds.
|
|
|
|
pub feed: Feed,
|
|
|
|
|
2024-03-08 17:06:47 +01:00
|
|
|
/// Redirects for moving pages around. These are used solely by the treehouse server.
|
|
|
|
///
|
|
|
|
/// Note that redirects are only resolved _non-recursively_ by the server. For a configuration
|
|
|
|
/// like:
|
|
|
|
///
|
|
|
|
/// ```toml
|
|
|
|
/// page.redirects.foo = "bar"
|
|
|
|
/// page.redirects.bar = "baz"
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// the user will be redirected from `foo` to `bar`, then from `bar` to `baz`. This isn't
|
|
|
|
/// optimal for UX and causes unnecessary latency. Therefore you should always make redirects
|
|
|
|
/// point to the newest version of the page.
|
|
|
|
///
|
|
|
|
/// ```toml
|
|
|
|
/// page.redirects.foo = "baz"
|
|
|
|
/// page.redirects.bar = "baz"
|
|
|
|
/// ```
|
|
|
|
pub redirects: Redirects,
|
|
|
|
|
2024-07-21 10:21:00 +02:00
|
|
|
/// How the treehouse should be built.
|
|
|
|
pub build: Build,
|
2024-07-19 20:05:17 +02:00
|
|
|
|
2024-11-16 18:33:41 +01:00
|
|
|
/// Overrides for emoji names. Useful for setting up aliases.
|
2023-08-27 18:25:21 +02:00
|
|
|
///
|
2024-11-16 18:33:41 +01:00
|
|
|
/// Paths are anchored within `static/emoji` and must not contain parent directories.
|
2023-08-27 18:25:21 +02:00
|
|
|
#[serde(default)]
|
2024-11-16 18:33:41 +01:00
|
|
|
pub emoji: HashMap<String, VPathBuf>,
|
2024-02-07 11:24:54 +01:00
|
|
|
|
|
|
|
/// Overrides for pic filenames. Useful for setting up aliases.
|
|
|
|
///
|
|
|
|
/// On top of this, pics are autodiscovered by walking the `static/pic` directory.
|
|
|
|
/// Only the part before the first dash is treated as the pic's id.
|
2024-11-16 18:33:41 +01:00
|
|
|
pub pics: HashMap<String, VPathBuf>,
|
2024-03-10 23:23:50 +01:00
|
|
|
|
|
|
|
/// Syntax definitions.
|
|
|
|
///
|
|
|
|
/// These are not part of the config file, but are loaded as part of site configuration from
|
|
|
|
/// `static/syntax`.
|
|
|
|
#[serde(skip)]
|
|
|
|
pub syntaxes: HashMap<String, CompiledSyntax>,
|
2023-08-27 15:59:52 +02:00
|
|
|
}
|
|
|
|
|
2024-11-27 19:02:30 +01:00
|
|
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
|
|
|
pub struct Feed {
|
|
|
|
/// Allowed tags in feed entries.
|
|
|
|
pub tags: HashSet<String>,
|
|
|
|
}
|
|
|
|
|
2024-03-08 17:06:47 +01:00
|
|
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
|
|
|
pub struct Redirects {
|
2025-01-14 23:36:09 +01:00
|
|
|
/// Path redirects. When a user requests a path, if they request `p`, they will be redirected
|
|
|
|
/// to `path[p]` with a `301 Moved Permanently` status code.
|
|
|
|
pub path: HashMap<VPathBuf, VPathBuf>,
|
2024-03-08 17:06:47 +01:00
|
|
|
}
|
|
|
|
|
2024-07-21 10:21:00 +02:00
|
|
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
|
|
|
pub struct Build {
|
|
|
|
/// Configuration for how JavaScript is compiled.
|
|
|
|
pub javascript: JavaScript,
|
|
|
|
}
|
|
|
|
|
2024-07-19 20:05:17 +02:00
|
|
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
|
|
|
pub struct JavaScript {
|
|
|
|
/// Import roots to generate in the project's import map.
|
|
|
|
pub import_roots: Vec<ImportRoot>,
|
|
|
|
}
|
|
|
|
|
2023-08-27 15:59:52 +02:00
|
|
|
impl Config {
|
2024-11-26 20:55:49 +01:00
|
|
|
#[instrument(name = "Config::autopopulate_emoji", skip(self))]
|
2024-11-17 22:34:43 +01:00
|
|
|
pub fn autopopulate_emoji(&mut self, dir: &dyn Dir) -> anyhow::Result<()> {
|
|
|
|
vfs::walk_dir_rec(dir, VPath::ROOT, &mut |path| {
|
2024-11-23 20:43:26 +01:00
|
|
|
if path.extension().is_some_and(is_image_file) {
|
2024-11-16 18:33:41 +01:00
|
|
|
if let Some(emoji_name) = path.file_stem() {
|
|
|
|
if !self.emoji.contains_key(emoji_name) {
|
|
|
|
self.emoji.insert(emoji_name.to_owned(), path.to_owned());
|
2023-08-27 18:47:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-02-07 11:24:54 +01:00
|
|
|
|
2024-11-16 18:33:41 +01:00
|
|
|
ControlFlow::Continue(())
|
|
|
|
});
|
2024-02-07 11:24:54 +01:00
|
|
|
|
2024-11-16 18:33:41 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
2024-02-07 11:24:54 +01:00
|
|
|
|
2024-11-26 20:55:49 +01:00
|
|
|
#[instrument(name = "Config::autopopulate_pics", skip(self))]
|
2024-11-17 22:34:43 +01:00
|
|
|
pub fn autopopulate_pics(&mut self, dir: &dyn Dir) -> anyhow::Result<()> {
|
|
|
|
vfs::walk_dir_rec(dir, VPath::ROOT, &mut |path| {
|
2024-11-23 20:43:26 +01:00
|
|
|
if path.extension().is_some_and(is_image_file) {
|
2024-11-16 18:33:41 +01:00
|
|
|
if let Some(pic_name) = path.file_stem() {
|
2024-02-07 11:24:54 +01:00
|
|
|
let pic_id = pic_name
|
|
|
|
.split_once('-')
|
|
|
|
.map(|(before_dash, _after_dash)| before_dash)
|
2024-11-16 18:33:41 +01:00
|
|
|
.unwrap_or(pic_name);
|
2024-02-07 11:24:54 +01:00
|
|
|
|
|
|
|
if !self.pics.contains_key(pic_id) {
|
2024-11-16 18:33:41 +01:00
|
|
|
self.pics.insert(pic_id.to_owned(), path.to_owned());
|
2024-02-07 11:24:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-11-16 18:33:41 +01:00
|
|
|
|
|
|
|
ControlFlow::Continue(())
|
|
|
|
});
|
2024-02-07 11:24:54 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
2024-02-14 23:31:39 +01:00
|
|
|
|
2024-03-02 20:53:44 +01:00
|
|
|
pub fn page_url(&self, page: &str) -> String {
|
|
|
|
format!("{}/{}", self.site, page)
|
|
|
|
}
|
|
|
|
|
2024-11-17 22:34:43 +01:00
|
|
|
pub fn pic_url(&self, pics_dir: &dyn Dir, id: &str) -> String {
|
2024-11-16 18:33:41 +01:00
|
|
|
vfs::url(
|
|
|
|
&self.site,
|
2024-11-17 22:34:43 +01:00
|
|
|
pics_dir,
|
2024-11-16 18:33:41 +01:00
|
|
|
self.pics
|
|
|
|
.get(id)
|
|
|
|
.map(|x| &**x)
|
|
|
|
.unwrap_or(VPath::new("404.png")),
|
2024-02-14 23:31:39 +01:00
|
|
|
)
|
2024-11-17 22:34:43 +01:00
|
|
|
.expect("pics_dir is not anchored anywhere")
|
2024-02-14 23:31:39 +01:00
|
|
|
}
|
2024-03-10 23:23:50 +01:00
|
|
|
|
2024-11-23 20:43:26 +01:00
|
|
|
pub fn pic_size(&self, pics_dir: &dyn Dir, id: &str) -> Option<ImageSize> {
|
2024-11-29 20:03:32 +01:00
|
|
|
self.pics
|
|
|
|
.get(id)
|
|
|
|
.and_then(|path| vfs::query::<ImageSize>(pics_dir, path))
|
2024-11-23 20:43:26 +01:00
|
|
|
}
|
|
|
|
|
2024-03-10 23:23:50 +01:00
|
|
|
/// Loads all syntax definition files.
|
2024-11-26 20:55:49 +01:00
|
|
|
#[instrument(name = "Config::load_syntaxes", skip(self))]
|
2024-11-26 22:09:39 +01:00
|
|
|
pub fn load_syntaxes(&mut self, dir: DynDir) -> anyhow::Result<()> {
|
|
|
|
let mut paths = vec![];
|
|
|
|
vfs::walk_dir_rec(&dir, VPath::ROOT, &mut |path| {
|
2024-11-17 22:34:43 +01:00
|
|
|
if path.extension() == Some("json") {
|
2024-11-26 22:09:39 +01:00
|
|
|
paths.push(path.to_owned());
|
|
|
|
}
|
|
|
|
ControlFlow::Continue(())
|
|
|
|
});
|
|
|
|
|
|
|
|
let syntaxes: Vec<_> = paths
|
|
|
|
.par_iter()
|
|
|
|
.flat_map(|path| {
|
2024-11-17 22:34:43 +01:00
|
|
|
let name = path
|
2024-03-10 23:23:50 +01:00
|
|
|
.file_stem()
|
2024-11-17 22:34:43 +01:00
|
|
|
.expect("syntax file name should have a stem due to the .json extension");
|
2024-03-10 23:23:50 +01:00
|
|
|
|
2024-11-29 20:03:32 +01:00
|
|
|
let result: Result<Syntax, _> = vfs::query::<Content>(&dir, path)
|
2024-11-17 22:34:43 +01:00
|
|
|
.ok_or_else(|| anyhow!("syntax .json is not a file"))
|
2024-11-29 20:03:32 +01:00
|
|
|
.and_then(|b| b.string().context("syntax .json contains invalid UTF-8"))
|
2024-11-17 22:34:43 +01:00
|
|
|
.and_then(|s| {
|
2024-11-26 20:55:49 +01:00
|
|
|
let _span = info_span!("Config::load_syntaxes::parse").entered();
|
2024-11-17 22:34:43 +01:00
|
|
|
serde_json::from_str(&s).context("could not deserialize syntax file")
|
|
|
|
});
|
|
|
|
match result {
|
|
|
|
Ok(syntax) => {
|
2024-11-26 20:55:49 +01:00
|
|
|
let _span = info_span!("Config::load_syntaxes::compile", ?name).entered();
|
2024-11-17 22:34:43 +01:00
|
|
|
let compiled = compile_syntax(&syntax);
|
2024-11-26 22:09:39 +01:00
|
|
|
Some((name.to_owned(), compiled))
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
error!("error while loading syntax file `{path}`: {err}");
|
|
|
|
None
|
2024-11-17 22:34:43 +01:00
|
|
|
}
|
|
|
|
}
|
2024-11-26 22:09:39 +01:00
|
|
|
})
|
|
|
|
.collect();
|
2024-11-17 22:34:43 +01:00
|
|
|
|
2024-11-26 22:09:39 +01:00
|
|
|
for (name, compiled) in syntaxes {
|
|
|
|
self.syntaxes.insert(name, compiled);
|
|
|
|
}
|
2024-03-10 23:23:50 +01:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-08-27 15:59:52 +02:00
|
|
|
}
|
2024-02-20 23:30:36 +01:00
|
|
|
|
2024-11-23 20:43:26 +01:00
|
|
|
pub fn is_image_file(extension: &str) -> bool {
|
2024-11-16 18:33:41 +01:00
|
|
|
matches!(extension, "png" | "svg" | "jpg" | "jpeg" | "webp")
|
2024-02-20 23:30:36 +01:00
|
|
|
}
|