Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9c22b52fda | ||
|
|
01504fb120 | ||
|
|
b25a5839b0 | ||
|
|
c4609f4370 | ||
|
|
2c94ba4379 | ||
|
|
065bd0a86a | ||
|
|
035ad7830b | ||
|
|
bf7f2e11e7 |
Generated
+1
-1
@@ -2948,7 +2948,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "portal"
|
name = "portal"
|
||||||
version = "0.3.26"
|
version = "0.3.31"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"arc-swap",
|
"arc-swap",
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "portal"
|
name = "portal"
|
||||||
version = "0.3.28"
|
version = "0.3.32"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
|
|||||||
+10
-1
@@ -1,5 +1,5 @@
|
|||||||
use leptos::prelude::*;
|
use leptos::prelude::*;
|
||||||
use leptos_meta::{provide_meta_context, HashedStylesheet, Html, MetaTags, Stylesheet, Title};
|
use leptos_meta::{provide_meta_context, HashedStylesheet, Html, Link, MetaTags, Stylesheet, Title};
|
||||||
use leptos_router::{
|
use leptos_router::{
|
||||||
components::{Route, Router, Routes},
|
components::{Route, Router, Routes},
|
||||||
hooks::{use_location, use_navigate, use_query_map},
|
hooks::{use_location, use_navigate, use_query_map},
|
||||||
@@ -89,10 +89,19 @@ pub fn App() -> impl IntoView {
|
|||||||
.and_then(|s| s.stylesheet)
|
.and_then(|s| s.stylesheet)
|
||||||
.map(|p| format!("/site/{p}"))
|
.map(|p| format!("/site/{p}"))
|
||||||
});
|
});
|
||||||
|
// A content-shipped favicon overrides the built-in one; injected
|
||||||
|
// into the head after the static defaults, so it wins.
|
||||||
|
let favicon = Memo::new(move |_| {
|
||||||
|
site.get()
|
||||||
|
.and_then(|r| r.ok())
|
||||||
|
.and_then(|s| s.favicon)
|
||||||
|
.map(|p| format!("/site/{p}"))
|
||||||
|
});
|
||||||
|
|
||||||
view! {
|
view! {
|
||||||
<Html attr:lang=move || lang.get()/>
|
<Html attr:lang=move || lang.get()/>
|
||||||
{move || custom_css.get().map(|href| view! { <Stylesheet id="site-custom" href=href/> })}
|
{move || custom_css.get().map(|href| view! { <Stylesheet id="site-custom" href=href/> })}
|
||||||
|
{move || favicon.get().map(|href| view! { <Link rel="icon" href=href/> })}
|
||||||
<Suspense fallback=|| ()>
|
<Suspense fallback=|| ()>
|
||||||
{move || {
|
{move || {
|
||||||
site.get()
|
site.get()
|
||||||
|
|||||||
+101
-1
@@ -191,6 +191,7 @@ pub fn render_inline_markdown(text: &str) -> String {
|
|||||||
Event::Start(Tag::Link { dest_url, .. })
|
Event::Start(Tag::Link { dest_url, .. })
|
||||||
if !(dest_url.starts_with("https://")
|
if !(dest_url.starts_with("https://")
|
||||||
|| dest_url.starts_with("mailto:")
|
|| dest_url.starts_with("mailto:")
|
||||||
|
|| dest_url.starts_with("tel:")
|
||||||
|| dest_url.starts_with('/')) =>
|
|| dest_url.starts_with('/')) =>
|
||||||
{
|
{
|
||||||
in_link += 1;
|
in_link += 1;
|
||||||
@@ -219,7 +220,73 @@ pub fn render_inline_markdown(text: &str) -> String {
|
|||||||
});
|
});
|
||||||
let mut out = String::new();
|
let mut out = String::new();
|
||||||
html::push_html(&mut out, filtered);
|
html::push_html(&mut out, filtered);
|
||||||
out.trim().to_string()
|
apply_image_hints(out.trim())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Translate a markdown image's title (``)
|
||||||
|
/// into layout: `left`/`right` float via a class, a bare number
|
||||||
|
/// (optionally with `rem`) into a validated `max-width`. Unknown
|
||||||
|
/// tokens are ignored; the title attribute is dropped either way.
|
||||||
|
/// Only touches `<img>` tags in our own render output, and only ever
|
||||||
|
/// emits a numeric max-width - no arbitrary CSS reaches the page.
|
||||||
|
fn apply_image_hints(html: &str) -> String {
|
||||||
|
let mut out = String::new();
|
||||||
|
let mut rest = html;
|
||||||
|
while let Some(pos) = rest.find("<img ") {
|
||||||
|
out.push_str(&rest[..pos]);
|
||||||
|
let after = &rest[pos..];
|
||||||
|
let end = after.find('>').map(|e| e + 1).unwrap_or(after.len());
|
||||||
|
out.push_str(&rewrite_img_tag(&after[..end]));
|
||||||
|
rest = &after[end..];
|
||||||
|
}
|
||||||
|
out.push_str(rest);
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
|
fn rewrite_img_tag(tag: &str) -> String {
|
||||||
|
// Pull the title value, if any.
|
||||||
|
let title = tag
|
||||||
|
.find("title=\"")
|
||||||
|
.map(|i| &tag[i + 7..])
|
||||||
|
.and_then(|r| r.find('"').map(|e| &r[..e]))
|
||||||
|
.unwrap_or("");
|
||||||
|
|
||||||
|
let mut class = String::new();
|
||||||
|
let mut max_rem: Option<f32> = None;
|
||||||
|
for tok in title.split_whitespace() {
|
||||||
|
match tok {
|
||||||
|
"left" => class = "md-float-left".into(),
|
||||||
|
"right" => class = "md-float-right".into(),
|
||||||
|
other => {
|
||||||
|
if let Ok(n) = other.trim_end_matches("rem").parse::<f32>() {
|
||||||
|
if n > 0.0 && n <= 60.0 {
|
||||||
|
max_rem = Some(n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Strip the title attribute from the tag.
|
||||||
|
let mut cleaned = tag.to_string();
|
||||||
|
if let Some(i) = cleaned.find(" title=\"") {
|
||||||
|
if let Some(e) = cleaned[i + 8..].find('"') {
|
||||||
|
cleaned.replace_range(i..i + 8 + e + 1, "");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Inject class/style right after `<img`.
|
||||||
|
let mut attrs = String::new();
|
||||||
|
if !class.is_empty() {
|
||||||
|
attrs.push_str(&format!(" class=\"{class}\""));
|
||||||
|
}
|
||||||
|
if let Some(n) = max_rem {
|
||||||
|
attrs.push_str(&format!(" style=\"max-width:{n}rem\""));
|
||||||
|
}
|
||||||
|
if attrs.is_empty() {
|
||||||
|
return cleaned;
|
||||||
|
}
|
||||||
|
cleaned.replacen("<img", &format!("<img{attrs}"), 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Directory-scoped defaults: a `_section.yaml` file applies to every
|
/// Directory-scoped defaults: a `_section.yaml` file applies to every
|
||||||
@@ -544,6 +611,10 @@ pub struct SiteConfig {
|
|||||||
/// same-origin at `/site/<path>`; plain path only.
|
/// same-origin at `/site/<path>`; plain path only.
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub stylesheet: Option<String>,
|
pub stylesheet: Option<String>,
|
||||||
|
/// A content-repo-relative favicon (svg/png/ico), served at
|
||||||
|
/// `/site/<path>` and used in place of the built-in one.
|
||||||
|
#[serde(default)]
|
||||||
|
pub favicon: Option<String>,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub hero: HeroConfig,
|
pub hero: HeroConfig,
|
||||||
}
|
}
|
||||||
@@ -606,6 +677,14 @@ impl SiteConfig {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if let Some(icon) = &self.favicon {
|
||||||
|
let ok = [".svg", ".png", ".ico"].iter().any(|e| icon.ends_with(e));
|
||||||
|
if !is_safe_site_path(icon) || !ok {
|
||||||
|
anyhow::bail!(
|
||||||
|
"site.yaml: favicon {icon:?} must be a plain repo-relative .svg/.png/.ico path"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1469,6 +1548,8 @@ pub async fn site_asset_handler(
|
|||||||
Some("json") => "application/json",
|
Some("json") => "application/json",
|
||||||
Some("png") => "image/png",
|
Some("png") => "image/png",
|
||||||
Some("webp") => "image/webp",
|
Some("webp") => "image/webp",
|
||||||
|
Some("avif") => "image/avif",
|
||||||
|
Some("ico") => "image/x-icon",
|
||||||
Some("woff2") => "font/woff2",
|
Some("woff2") => "font/woff2",
|
||||||
_ => "application/octet-stream",
|
_ => "application/octet-stream",
|
||||||
};
|
};
|
||||||
@@ -2079,6 +2160,10 @@ alternatives:
|
|||||||
fn markdown_drops_html_and_unsafe_links() {
|
fn markdown_drops_html_and_unsafe_links() {
|
||||||
assert_eq!(render_inline_markdown("x <script>y</script> z"), "x y z");
|
assert_eq!(render_inline_markdown("x <script>y</script> z"), "x y z");
|
||||||
assert_eq!(render_inline_markdown("[bad](javascript:alert(1))"), "bad");
|
assert_eq!(render_inline_markdown("[bad](javascript:alert(1))"), "bad");
|
||||||
|
assert_eq!(
|
||||||
|
render_inline_markdown("[ring](tel:+4791180485)"),
|
||||||
|
"<a href=\"tel:+4791180485\">ring</a>"
|
||||||
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
render_inline_markdown(""),
|
render_inline_markdown(""),
|
||||||
"<img src=\"https://x.no/a.jpg\" alt=\"site\" />"
|
"<img src=\"https://x.no/a.jpg\" alt=\"site\" />"
|
||||||
@@ -2088,6 +2173,21 @@ alternatives:
|
|||||||
render_inline_markdown(""),
|
render_inline_markdown(""),
|
||||||
"<img src=\"/images/a.jpg\" alt=\"local\" />"
|
"<img src=\"/images/a.jpg\" alt=\"local\" />"
|
||||||
);
|
);
|
||||||
|
// Image title hints: float + max-width, title dropped.
|
||||||
|
assert_eq!(
|
||||||
|
render_inline_markdown(""),
|
||||||
|
"<img class=\"md-float-right\" style=\"max-width:9rem\" src=\"https://x.no/j.jpg\" alt=\"J\" />"
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
render_inline_markdown(""),
|
||||||
|
"<img style=\"max-width:12rem\" src=\"https://x.no/j.jpg\" alt=\"J\" />"
|
||||||
|
);
|
||||||
|
// Unknown hint tokens are ignored; a safe image with no title
|
||||||
|
// is untouched.
|
||||||
|
assert_eq!(
|
||||||
|
render_inline_markdown(""),
|
||||||
|
"<img src=\"https://x.no/j.jpg\" alt=\"J\" />"
|
||||||
|
);
|
||||||
assert_eq!(render_inline_markdown("[ok](/shape)"), "<a href=\"/shape\">ok</a>");
|
assert_eq!(render_inline_markdown("[ok](/shape)"), "<a href=\"/shape\">ok</a>");
|
||||||
assert_eq!(render_inline_markdown("[mail](mailto:bl@uhhm.no)"), "<a href=\"mailto:bl@uhhm.no\">mail</a>");
|
assert_eq!(render_inline_markdown("[mail](mailto:bl@uhhm.no)"), "<a href=\"mailto:bl@uhhm.no\">mail</a>");
|
||||||
}
|
}
|
||||||
|
|||||||
+30
-2
@@ -368,8 +368,12 @@ main.not-found {
|
|||||||
.alt-image {
|
.alt-image {
|
||||||
display: block;
|
display: block;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
max-height: 14rem;
|
height: auto;
|
||||||
object-fit: cover;
|
/* Natural aspect, not a cropped band; a very tall image is still
|
||||||
|
bounded so it can't tower. The deck below overrides for its
|
||||||
|
fixed-height cards. */
|
||||||
|
max-height: 32rem;
|
||||||
|
object-fit: contain;
|
||||||
border-radius: calc(var(--radius) - 0.3rem);
|
border-radius: calc(var(--radius) - 0.3rem);
|
||||||
margin-bottom: 1.1rem;
|
margin-bottom: 1.1rem;
|
||||||
}
|
}
|
||||||
@@ -552,6 +556,30 @@ main.not-found {
|
|||||||
border: 0.06rem solid var(--line);
|
border: 0.06rem solid var(--line);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Content hint via a markdown image title (``):
|
||||||
|
float and shrink so text wraps around a portrait. */
|
||||||
|
.alt-description img.md-float-left,
|
||||||
|
.item-card-description img.md-float-left,
|
||||||
|
.feature p img.md-float-left,
|
||||||
|
.alt-description img.md-float-right,
|
||||||
|
.item-card-description img.md-float-right,
|
||||||
|
.feature p img.md-float-right {
|
||||||
|
width: auto;
|
||||||
|
max-width: 45%;
|
||||||
|
}
|
||||||
|
.alt-description img.md-float-left,
|
||||||
|
.item-card-description img.md-float-left,
|
||||||
|
.feature p img.md-float-left {
|
||||||
|
float: left;
|
||||||
|
margin: 0.2rem 1.1rem 0.5rem 0;
|
||||||
|
}
|
||||||
|
.alt-description img.md-float-right,
|
||||||
|
.item-card-description img.md-float-right,
|
||||||
|
.feature p img.md-float-right {
|
||||||
|
float: right;
|
||||||
|
margin: 0.2rem 0 0.5rem 1.1rem;
|
||||||
|
}
|
||||||
|
|
||||||
.alt-description code,
|
.alt-description code,
|
||||||
.feature p code {
|
.feature p code {
|
||||||
font-size: 0.9em;
|
font-size: 0.9em;
|
||||||
|
|||||||
Reference in New Issue
Block a user