fix: maybe preview works now

This commit is contained in:
2026-04-10 21:01:28 +02:00
parent c44354e33f
commit 9095912b48
6 changed files with 228 additions and 21 deletions

View File

@@ -12,7 +12,8 @@ pub use markdown::render_markdown_to_html;
pub use page_renderer::{RenderError, render_liquid_template};
pub use routes::{
RenderedPage, build_canonical_post_path, render_starter_list_page,
render_starter_single_post_page,
render_starter_list_page_with_media_map, render_starter_single_post_page,
render_starter_single_post_page_with_media_map,
};
pub use template_lookup::{
RenderCategorySettings, RenderTemplateLookup, TemplateLookupError,

View File

@@ -208,11 +208,24 @@ fn rewrite_attribute_urls(
}
fn normalize_preview_href(raw_href: &str, rewrite_context: &impl RewriteContextView) -> String {
if raw_href.is_empty() || is_external_or_special_url(raw_href) {
if raw_href.is_empty() {
return raw_href.to_string();
}
let (path_part, suffix) = split_path_suffix(raw_href.trim());
if let Some(media_lookup_key) = extract_media_lookup_key(path_part) {
let canonical = rewrite_context
.canonical_media_path_by_source_path()
.get(&media_lookup_key)
.cloned()
.unwrap_or_else(|| format!("/{media_lookup_key}"));
return format!("{canonical}{suffix}");
}
if is_external_or_special_url(raw_href) {
return raw_href.to_string();
}
if let Some(normalized) = normalize_day_route(path_part) {
return format!("{normalized}{suffix}");
}
@@ -226,7 +239,7 @@ fn normalize_preview_href(raw_href: &str, rewrite_context: &impl RewriteContextV
return format!("{canonical}{suffix}");
}
if let Some(media_source_key) = extract_media_source_key(path_part) {
if let Some(media_source_key) = extract_media_lookup_key(path_part) {
let canonical = rewrite_context
.canonical_media_path_by_source_path()
.get(&media_source_key)
@@ -239,12 +252,12 @@ fn normalize_preview_href(raw_href: &str, rewrite_context: &impl RewriteContextV
}
fn normalize_preview_src(raw_src: &str, rewrite_context: &impl RewriteContextView) -> String {
if raw_src.is_empty() || is_external_or_special_url(raw_src) {
if raw_src.is_empty() {
return raw_src.to_string();
}
let (path_part, suffix) = split_path_suffix(raw_src.trim());
if let Some(media_source_key) = extract_media_source_key(path_part) {
if let Some(media_source_key) = extract_media_lookup_key(path_part) {
let canonical = rewrite_context
.canonical_media_path_by_source_path()
.get(&media_source_key)
@@ -253,6 +266,10 @@ fn normalize_preview_src(raw_src: &str, rewrite_context: &impl RewriteContextVie
return format!("{canonical}{suffix}");
}
if is_external_or_special_url(raw_src) {
return raw_src.to_string();
}
raw_src.to_string()
}
@@ -316,7 +333,15 @@ fn extract_post_slug(path: &str) -> Option<String> {
}
}
fn extract_media_source_key(path: &str) -> Option<String> {
fn extract_media_lookup_key(path: &str) -> Option<String> {
if let Some(media_id) = path.trim().strip_prefix("bds-media://") {
let media_id = media_id.trim();
if media_id.is_empty() {
return None;
}
return Some(format!("bds-media://{media_id}"));
}
let trimmed = path.trim_start_matches('/');
let segments: Vec<_> = trimmed.split('/').collect();
match segments.as_slice() {
@@ -332,4 +357,43 @@ fn trim_html_suffix(value: &str) -> String {
.trim_end_matches(".html")
.trim_end_matches(".htm")
.to_string()
}
#[cfg(test)]
mod tests {
use super::{rewrite_rendered_html_urls, RewriteContextView};
use std::collections::HashMap;
struct TestRewriteContext {
canonical_post_path_by_slug: HashMap<String, String>,
canonical_media_path_by_source_path: HashMap<String, String>,
}
impl RewriteContextView for TestRewriteContext {
fn canonical_post_path_by_slug(&self) -> &HashMap<String, String> {
&self.canonical_post_path_by_slug
}
fn canonical_media_path_by_source_path(&self) -> &HashMap<String, String> {
&self.canonical_media_path_by_source_path
}
}
#[test]
fn rewrites_bds_media_image_src_to_canonical_media_path() {
let context = TestRewriteContext {
canonical_post_path_by_slug: HashMap::new(),
canonical_media_path_by_source_path: HashMap::from([(
"bds-media://media-1".to_string(),
"/media/2026/04/media-1.png".to_string(),
)]),
};
let html = rewrite_rendered_html_urls(
"<p><img src=\"bds-media://media-1\" alt=\"\" /></p>",
&context,
);
assert!(html.contains("src=\"/media/2026/04/media-1.png\""));
}
}

View File

@@ -119,6 +119,22 @@ pub fn render_starter_single_post_page(
body_markdown: &str,
metadata: &ProjectMetadata,
language: &str,
) -> Result<RenderedPage, RenderError> {
render_starter_single_post_page_with_media_map(
post,
body_markdown,
metadata,
language,
HashMap::new(),
)
}
pub fn render_starter_single_post_page_with_media_map(
post: &Post,
body_markdown: &str,
metadata: &ProjectMetadata,
language: &str,
canonical_media_path_by_source_path: HashMap<String, String>,
) -> Result<RenderedPage, RenderError> {
let relative_path = format!("{}/index.html", build_canonical_post_path(post, language, main_language(metadata)).trim_start_matches('/'));
let canonical_path = build_canonical_post_path(post, language, main_language(metadata));
@@ -151,7 +167,7 @@ pub fn render_starter_single_post_page(
.collect(),
backlinks: vec![],
canonical_post_path_by_slug: HashMap::from([(post.slug.clone(), canonical_path)]),
canonical_media_path_by_source_path: HashMap::new(),
canonical_media_path_by_source_path,
post_data_json_by_id: HashMap::new(),
};
@@ -168,6 +184,15 @@ pub fn render_starter_list_page(
posts: &[(Post, String)],
metadata: &ProjectMetadata,
language: &str,
) -> Result<RenderedPage, RenderError> {
render_starter_list_page_with_media_map(posts, metadata, language, HashMap::new())
}
pub fn render_starter_list_page_with_media_map(
posts: &[(Post, String)],
metadata: &ProjectMetadata,
language: &str,
canonical_media_path_by_source_path: HashMap<String, String>,
) -> Result<RenderedPage, RenderError> {
let relative_path = if language.eq_ignore_ascii_case(main_language(metadata)) {
"index.html".to_string()
@@ -217,7 +242,7 @@ pub fn render_starter_list_page(
prev_page_href: None,
next_page_href: None,
canonical_post_path_by_slug: canonical_paths,
canonical_media_path_by_source_path: HashMap::new(),
canonical_media_path_by_source_path,
post_data_json_by_id: HashMap::new(),
};
@@ -447,6 +472,25 @@ mod tests {
assert!(rendered.html.contains("href=\"/2024/03/09/hello\""));
}
#[test]
fn starter_single_post_renderer_rewrites_bds_media_image_links() {
let post = make_post();
let metadata = make_metadata();
let rendered = render_starter_single_post_page_with_media_map(
&post,
"![](bds-media://media-1)",
&metadata,
"en",
HashMap::from([(
"bds-media://media-1".to_string(),
"/media/2026/04/media-1.png".to_string(),
)]),
)
.unwrap();
assert!(rendered.html.contains("src=\"/media/2026/04/media-1.png\""));
}
#[test]
fn starter_list_renderer_groups_posts_and_uses_language_specific_index_path() {
let metadata = make_metadata();