You are ranking individual git commits to an open source project. Compare these two commits. Decide which commit contributed more. Return ONLY a JSON object: {"winner": "A" or "B", "ratio": "N:M", "explanation": "..."} Side A — contributor: tommy-mor Side A — commit message: [08565bea] Tokenize prose refs for garden URL links (#147) * Tokenize prose refs for garden URL links Co-authored-by: tommy * Stop prose URLs at line boundaries Co-authored-by: tommy * Require braced DSL item bodies Co-authored-by: tommy --------- Co-authored-by: Cursor Agent Side A — unified diff (full patch): diff --git a/server/src/dsl.rs b/server/src/dsl.rs index 4203c2f59dd8825d7a91513c4023f3ccd37f1efc..b3c785674560a0b42a7f4c3aa13d80cd350f485b 100644 --- a/server/src/dsl.rs +++ b/server/src/dsl.rs @@ -1,7 +1,5 @@ use std::collections::HashMap; -use rand::Rng; - /// Parsed DSL document. #[derive(Debug, Clone, PartialEq, Eq)] pub struct Document { @@ -39,31 +37,57 @@ pub enum DslError { /// Matches the legacy Python parser behavior: /// - Supports toggle markers (open == close), e.g. ```...``` /// - Supports nested markers (open != close), e.g. { ... { ... } ... } +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum BlockKind { + CodeFence, + DoubleBrace, + Brace, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +struct MaskedBlock { + kind: BlockKind, +} + #[derive(Debug, Default, Clone)] pub struct BlockMasker { pub replacements: HashMap, + blocks: HashMap, + next_id: u32, } impl BlockMasker { pub fn new() -> Self { Self { replacements: HashMap::new(), + blocks: HashMap::new(), + next_id: 0, } } - fn new_token(&mut self) -> String { - let mut rng = rand::thread_rng(); - let n: u32 = rng.gen(); - let token = format!("__BLOCK_{:08x}__", n); - // Extremely unlikely collision; if it happens, regenerate. - if self.replacements.contains_key(&token) { - return self.new_token(); + fn new_token(&mut self, haystack: &str) -> String { + loop { + let token = format!("__BLOCK_{:08x}__", self.next_id); + self.next_id = self.next_id.wrapping_add(1); + if !self.replacements.contains_key(&token) && !haystack.contains(&token) { + return token; + } } - token } /// Replace outermost balanced blocks with tokens. pub fn mask(&mut self, text: &str, open_marker: &str, close_marker: &str) -> String { + self.mask_kind(text, open_marker, close_marker, BlockKind::Brace) + } + + /// Replace outermost balanced blocks with typed deterministic tokens. + pub fn mask_kind( + &mut self, + text: &str, + open_marker: &str, + close_marker: &str, + kind: BlockKind, + ) -> String { if text.is_empty() { return text.to_string(); } @@ -97,9 +121,10 @@ impl BlockMasker { // Found end of outermost block let s = start_idx.max(0) as usize; let original_block = &text[s..i]; - let token = self.new_token(); + let token = self.new_token(text); self.replacements .insert(token.clone(), original_block.to_string()); + self.blocks.insert(token.clone(), MaskedBlock { kind }); result_parts.push(token); current_idx = i; } @@ -176,13 +201,22 @@ impl BlockMasker { } token.to_string() } + + pub fn block_kind(&self, token: &str) -> Option { + self.blocks.get(token).map(|b| b.kind) + } } fn mask_all(mut masker: BlockMasker, text: &str) -> (BlockMasker, String) { // Mask hierarchy: Code -> Double Brace -> Single Brace. - let t = masker.mask(text, "```", "```"); - let t = masker.mask(&t, "{{", "}}"); - let t = masker.mask(&t, "{", "}"); + let t = masker.mask_kind(text, "```", "```", BlockKind::CodeFence); + let t = masker.mask_kind(&t, "{{", "}}", BlockKind::DoubleBrace); + let t = masker.mask_kind(&t, "{", "}", BlockKind::Brace); + (masker, t) +} + +fn mask_code_fences(mut masker: BlockMasker, text: &str) -> (BlockMasker, String) { + let t = masker.mask_kind(text, "```", "```", BlockKind::CodeFence); (masker, t) } @@ -253,7 +287,34 @@ fn skip_ws(s: &str, mut i: usize) -> usize { i } -fn parse_item_name_at(s: &str, i: usize) -> Option<(String, usize)> { +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum ProseToken { + Text(String), + ItemRef(String), +} + +fn trim_prose_item_ref_end(s: &str, mut end: usize) -> usize { + while end > 0 { + let Some((idx, c)) = s[..end].char_indices().next_back() else { + break; + }; + if matches!( + c, + '.' | ',' | ';' | ':' | '!' | '?' | ')' | ']' | '}' | '"' | '\'' + ) { + end = idx; + } else { + break; + } + } + end +} + +fn parse_item_name_at_with_mode( + s: &str, + i: usize, + trim_trailing_punctuation: bool, +) -> Option<(String, usize)> { let bytes = s.as_bytes(); if i >= bytes.len() { return None; @@ -263,6 +324,9 @@ fn parse_item_name_at(s: &str, i: usize) -> Option<(String, usize)> { if s[i..].starts_with("https://") || s[i..].starts_with("http://") { let mut j = i; while j < bytes.len() { + if trim_trailing_punctuation && bytes[j] == b'\n' { + break; + } if bytes[j..].starts_with(b"__BLOCK_") || is_ws_byte(bytes[j]) { break; } @@ -271,6 +335,12 @@ fn parse_item_name_at(s: &str, i: usize) -> Option<(String, usize)> { if j <= i { return None; } + if trim_trailing_punctuation { + j = trim_prose_item_ref_end(s, j); + if j <= i { + return None; + } + } return Some((s[i..j].to_string(), j)); } @@ -296,6 +366,12 @@ fn parse_item_name_at(s: &str, i: usize) -> Option<(String, usize)> { if j <= i + 2 { return None; } + if trim_trailing_punctuation { + j = trim_prose_item_ref_end(s, j); + if j <= i + 2 { + return None; + } + } let raw = &s[i..j]; if !is_item_name(raw) { return None; @@ -336,6 +412,46 @@ fn parse_item_name_at(s: &str, i: usize) -> Option<(String, usize)> { Some((format!("~/{}", name), j)) } +fn parse_item_name_at(s: &str, i: usize) -> Option<(String, usize)> { + parse_item_name_at_with_mode(s, i, false) +} + +pub fn parse_prose_item_ref_at(s: &str, i: usize) -> Option<(String, usize)> { + parse_item_name_at_with_mode(s, i, true) +} + +pub fn tokenize_prose_item_refs(text: &str) -> Vec { + if text.is_empty() { + return Vec::new(); + } + let (masker, masked) = mask_code_fences(BlockMasker::new(), text); + let mut tokens = Vec::new(); + let mut text_start = 0usize; + let mut i = 0usize; + + while i < masked.len() { + if let Some((raw, end)) = parse_prose_item_ref_at(&masked, i) { + if text_start < i { + tokens.push(ProseToken::Text(masker.unmask(&masked[text_start..i]))); + } + tokens.push(ProseToken::ItemRef(masker.unmask(&raw))); + i = end; + text_start = i; + continue; + } + + let Some((_, c)) = masked[i..].char_indices().next() else { + break; + }; + i += c.len_utf8(); + } + + if text_start < masked.len() { + tokens.push(ProseToken::Text(masker.unmask(&masked[text_start..]))); + } + tokens +} + fn parse_block_token_at(s: &str, i: usize) -> Option<(String, usize)> { let bytes = s.as_bytes(); if i >= bytes.len() { @@ -401,6 +517,12 @@ fn parse_block_prefixed_statement( tail: &str, masker: &BlockMasker, ) -> Result { + if masker.block_kind(block_token) == Some(BlockKind::CodeFence) { + return Err(DslError::Parse( + "vote explanations must use `{ ... }`; code fences belong inside body blocks" + .to_string(), + )); + } // vote: block item_ref comparison item_ref let s = tail.trim_start(); if s.is_empty() { @@ -462,6 +584,11 @@ fn parse_item_definition_statement(stripped: &str, masker: &BlockMasker) -> Resu } if let Some((tok, end)) = parse_block_token_at(stripped, i) { + if masker.block_kind(&tok) == Some(BlockKind::CodeFence) { + return Err(DslError::Parse( + "item bodies must use `{ ... }`; code fences belong inside body blocks".to_string(), + )); + } let body = masker.extract_body(&tok); let tail = stripped[end..].trim(); if !tail.is_empty() { @@ -572,6 +699,10 @@ pub fn parse_full(text: &str) -> Result { if let Some((tok, end)) = parse_block_token_at(stripped, 0) { if stripped[end..].trim().is_empty() { + if masker.block_kind(&tok) == Some(BlockKind::CodeFence) { + prose_buffer.push(line); + continue; + } pending_block = Some(tok); continue; } @@ -619,6 +750,70 @@ mod tests { assert_eq!(roundtrip, input); } + #[test] + fn blockmasker_tokens_are_deterministic_and_typed() { + let input = "x ```code``` y {body}"; + let (masker, masked) = mask_all(BlockMasker::new(), input); + assert!(masked.contains("__BLOCK_00000000__")); + assert!(masked.contains("__BLOCK_00000001__")); + assert_eq!( + masker.block_kind("__BLOCK_00000000__"), + Some(BlockKind::CodeFence) + ); + assert_eq!( + masker.block_kind("__BLOCK_00000001__"), + Some(BlockKind::Brace) + ); + assert_eq!(masker.unmask(&masked), input); + } + + #[test] + fn prose_tokenizer_finds_tilde_dash_and_raw_url_refs() { + let tokens = + tokenize_prose_item_refs("see ~/a/b then -/example.com/x and https://Example.com/A/B."); + assert_eq!( + tokens, + vec![ + ProseToken::Text("see ".to_string()), + ProseToken::ItemRef("~/a/b".to_string()), + ProseToken::Text(" then ".to_string()), + ProseToken::ItemRef("-/example.com/x".to_string()), + ProseToken::Text(" and ".to_string()), + ProseToken::ItemRef("https://Example.com/A/B".to_string()), + ProseToken::Text(".".to_string()), + ] + ); + } + + #[test] + fn prose_tokenizer_stops_raw_urls_at_newlines() { + let tokens = tokenize_prose_item_refs("https://example.com/a/b.\n-/example.com/a/b"); + assert_eq!( + tokens, + vec![ + ProseToken::ItemRef("https://example.com/a/b".to_string()), + ProseToken::Text(".\n".to_string()), + ProseToken::ItemRef("-/example.com/a/b".to_string()), + ] + ); + } + + #[test] + fn prose_tokenizer_does_not_linkify_inside_code_fences() { + let tokens = tokenize_prose_item_refs( + "before ```json\n{\"url\":\"https://example.com\"}\n``` after ~/x", + ); + assert_eq!( + tokens, + vec![ + ProseToken::Text( + "before ```json\n{\"url\":\"https://example.com\"}\n``` after ".to_string() + ), + ProseToken::ItemRef("~/x".to_string()), + ] + ); + } + #[test] fn parse_item_with_body_strips_outer_braces() { let input = "~/rust { Systems language }"; @@ -633,8 +828,8 @@ mod tests { } #[test] - fn parse_item_with_fenced_json_body_preserves_braces() { - let input = "~/item/in/url ```json\n{\"test\": true}\n```"; + fn parse_item_with_braced_fenced_json_body_preserves_braces() { + let input = "~/item/in/url {\n```json\n{\"test\": true}\n```\n}"; let doc = parse_full(input).unwrap(); assert_eq!( doc.statements, @@ -645,6 +840,51 @@ mod tests { ); } + #[test] + fn parse_rejects_singleton_fenced_json_item_body() { + let input = "~/item/in/url ```json\n{\"test\": true}\n```"; + let err = parse_full(input).unwrap_err().to_string(); + assert!( + err.contains("item bodies must use"), + "unexpected error: {err}" + ); + } + + #[test] + fn parse_keeps_standalone_code_fence_as_prose() { + let input = "```json\n{\"test\": true}\n```"; + let doc = parse_full(input).unwrap(); + assert_eq!( + doc.statements, + vec![Stmt::Prose { + text: input.to_string(), + }] + ); + } + + #[test] + fn parse_rejects_code_fence_vote_explanation() { + let input = "```json\n{\"why\": true}\n```\n~/a 2:1 ~/b"; + let err = parse_full(input).unwrap_err().to_string(); + assert!( + err.contains("vote explanations must start"), + "unexpected error: {err}" + ); + } + + #[test] + fn parse_raw_url_item_with_braced_fenced_json_body() { + let input = "https://example.com/itembody/slug {\n```json\n{\"test\": true}\n```\n}"; + let doc = parse_full(input).unwrap(); + assert_eq!( + doc.statements, + vec![Stmt::Item { + title: "https://example.com/itembody/slug".to_string(), + body: Some("```json\n{\"test\": true}\n```".to_string()), + }] + ); + } + #[test] fn parse_vote_ratio_and_symbols() { let d1 = parse_full("{because}\n~/a 3:1 ~/b").unwrap(); diff --git a/server/src/external_resolver.rs b/server/src/external_resolver.rs index 8481f1fbff1c4265da182a225f539f97d6829002..04bc3de32b6df1aefe197d942db67de2cbdbde4f 100644 --- a/server/src/external_resolver.rs +++ b/server/src/external_resolver.rs @@ -256,7 +256,7 @@ fn children_to_dsl(children: &[ResolvedChild]) -> String { .filter(|s| !s.trim().is_empty()) .unwrap_or(child.title.as_str()); if body.trim_start().starts_with("```") { - out.push_str(&format!("{} {}\n\n", child.url, body.trim())); + out.push_str(&format!("{} {{\n{}\n}}\n\n", child.url, body.trim())); } else { out.push_str(&format!( "{} {{\n{}\n}}\n\n", @@ -378,7 +378,8 @@ mod tests { title: "#1 title".into(), body: Some("```json\n{\"test\": true}\n```".into()), }]); - assert!(dsl.contains("https://github.com/o/r/issues/1 ```json")); + assert!(dsl.contains("https://github.com/o/r/issues/1 {\n```json")); assert!(dsl.contains("{\"test\": true}")); + assert!(dsl.contains("```\n}\n")); } } diff --git a/server/src/html/breadcrumb_path.rs b/server/src/html/breadcrumb_path.rs index 3e949ce2cb70de3f024f46af773a93f3f8852260..e48612d7d3c4df52c161499a11865d4caca607aa 100644 --- a/server/src/html/breadcrumb_path.rs +++ b/server/src/html/breadcrumb_path.rs @@ -88,16 +88,12 @@ impl ExternalOntologyPath { .filter(|x| !x.is_empty()) .map(|x| x.to_string()) .collect(); - let segments = if segments == ["."] { - vec![] - } else { - segments - }; + let segments = if segments == ["."] { vec![] } else { segments }; Self { item, segments } } pub(super) fn is_root(&self) -> bool { - self.segments.len() <= 1 + self.segments.is_empty() } pub(super) fn segments(&self) -> &[String] { @@ -108,3 +104,28 @@ impl ExternalOntologyPath { self.item.as_str() } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn external_root_and_host_paths_are_distinct() { + let root = ExternalOntologyPath::from_input(""); + assert!(root.is_root()); + assert!(root.segments().is_empty()); + + let host = ExternalOntologyPath::from_input("example.com"); + assert!(!host.is_root()); + assert_eq!(host.segments(), &["example.com".to_string()]); + } + + #[test] + fn external_path_keeps_each_url_segment_for_breadcrumbs() { + let path = ExternalOntologyPath::from_input("https://example.com/a/b"); + assert_eq!( + path.segments(), + &["example.com".to_string(), "a".to_string(), "b".to_string()] + ); + } +} diff --git a/server/src/html/mod.rs b/server/src/html/mod.rs index 8876a1a00a16ee3bf7a371f7a5ca27b5862b62cc..d5877eeb3632ed5d770f23a04cb4d1881209bc74 100644 --- a/server/src/html/mod.rs +++ b/server/src/html/mod.rs @@ -20,27 +20,30 @@ mod search; pub mod ui_action; use breadcrumb_path::{ExternalOntologyPath, OntologyPath}; -pub use auth::{auth_complete_page, auth_signed_in_fragment, choose_username_error_fragment, choose_username_page}; +pub use auth::{ + auth_complete_page, auth_signed_in_fragment, choose_username_error_fragment, + choose_username_page, +}; pub use editor::{editor_check, editor_page}; pub use forum::{ home, room_page, room_thread_post_view, room_thread_view, thread_feed_html, thread_feed_html_for_room, thread_feed_region_markup, thread_post_view, thread_view, ThreadNav, }; +pub use forum::user_profile_page; pub(crate) use forum::{ fragment_new_thread_slot, login_to_post_hint_markup, room_members_section_markup, thread_ui_collapse_redacted_post, thread_ui_expand_post_full, thread_ui_expand_redacted_post, user_can_post_room, user_can_view_room, }; +pub(crate) use garden::{encode_pin_cookie_value, vote_compare_post_success_js, GARDEN_PIN_COOKIE}; pub use garden::{ external_garden_index, external_ontology_path, garden_index, ontology_path, - room_external_garden_index, room_external_ontology_path, room_garden_index, - room_ontology_path, room_vote_compare_page, vote_compare_page, + room_external_garden_index, room_external_ontology_path, room_garden_index, room_ontology_path, + room_vote_compare_page, vote_compare_page, }; -pub(crate) use garden::{encode_pin_cookie_value, vote_compare_post_success_js, GARDEN_PIN_COOKIE}; pub use routing::RouteContext; pub use search::{search_page, search_results_fragment}; -pub use forum::user_profile_page; pub use ui_action::{parse_html_ui_from_form, HtmlUiAction, HtmlUiParseError, UI_RPC_FIELD}; /// Public profile URL path for a stored username (no `@`). @@ -107,8 +110,8 @@ pub struct ThemeForm { pub async fn post_theme(Form(form): Form) -> impl IntoResponse { let theme = normalize_theme(&form.theme); let next = sanitize_theme_next(form.next.as_deref()); - let loc = HeaderValue::try_from(next.as_str()) - .unwrap_or_else(|_| HeaderValue::from_static("/")); + let loc = + HeaderValue::try_from(next.as_str()).unwrap_or_else(|_| HeaderValue::from_static("/")); Response::builder() .status(StatusCode::SEE_OTHER) .header(header::LOCATION, loc) @@ -179,7 +182,9 @@ pub(crate) struct JsQueryBuilder { impl JsBuilder { pub(crate) fn new() -> Self { - Self { snippets: Vec::new() } + Self { + snippets: Vec::new(), + } } pub(crate) fn morph_selector(self, selector: &str, markup: Markup) -> Self { @@ -195,7 +200,12 @@ impl JsBuilder { self.qs(selector).morph_inner(markup) } - pub(crate) fn morph_expr(mut self, expr: &str, markup: Markup, morph_style: Option<&str>) -> Self { + pub(crate) fn morph_expr( + mut self, + expr: &str, + markup: Markup, + morph_style: Option<&str>, + ) -> Self { let html = js_string_literal(&markup.into_string()); let opts = morph_style .map(|style| format!(", {{morphStyle: {}}}", js_string_literal(style))) @@ -217,7 +227,11 @@ impl JsBuilder { self.qs(&format!("#{id}")) } - pub(crate) fn if_current_path_matches(mut self, path: &str, f: impl FnOnce(JsBuilder) -> JsBuilder) -> Self { + pub(crate) fn if_current_path_matches( + mut self, + path: &str, + f: impl FnOnce(JsBuilder) -> JsBuilder, + ) -> Self { let inner = f(JsBuilder::new()).build(); self.snippets.push(format!( "var __slugHere = window.location.pathname + window.location.search; var __slugPath = {path}; if (__slugHere === __slugPath || __slugHere.indexOf(__slugPath + '?') === 0) {{ {inner} }}", @@ -226,7 +240,11 @@ impl JsBuilder { self } - pub(crate) fn if_current_path_not_matches(mut self, path: &str, f: impl FnOnce(JsBuilder) -> JsBuilder) -> Self { + pub(crate) fn if_current_path_not_matches( + mut self, + path: &str, + f: impl FnOnce(JsBuilder) -> JsBuilder, + ) -> Self { let inner = f(JsBuilder::new()).build(); self.snippets.push(format!( "var __slugHere = window.location.pathname + window.location.search; var __slugPath = {path}; if (!(__slugHere === __slugPath || __slugHere.indexOf(__slugPath + '?') === 0)) {{ {inner} }}", @@ -301,7 +319,17 @@ pub(super) fn layout( garden_room_wire: Option<&str>, garden_path_prefix: Option<&str>, ) -> Markup { - layout_embed_controls(title, view, body, views, theme, theme_next, garden_room_wire, garden_path_prefix, true) + layout_embed_controls( + title, + view, + body, + views, + theme, + theme_next, + garden_room_wire, + garden_path_prefix, + true, + ) } /// Minimal document shell: no bottom controls, no garden HUD data attributes (`data-garden-room` / @@ -315,15 +343,7 @@ pub(super) fn layout_full_bleed_chromeless( theme_next: &str, ) -> Markup { layout_embed_controls( - title, - view, - body, - views, - theme, - theme_next, - None, - None, - false, + title, view, body, views, theme, theme_next, None, None, false, ) } @@ -545,7 +565,58 @@ fn item_body_title_snippet(body: &str) -> Option { Some(format!("{truncated}{ellipsis}")) } -/// Replace ~/path slugs in raw text with clickable links. +fn garden_href_for_item_ref( + raw_ref: &str, + garden_prefix: &str, +) -> Option<(crate::path_types::ItemId, String)> { + let key = slug_types::canonicalize_item(raw_ref); + let id = crate::path_types::ItemId::parse(&key)?; + let href = if let Some(tail) = id.tilde_tail() { + if tail.is_empty() { + garden_prefix.trim_end_matches('/').to_string() + } else { + format!("{}/{}", garden_prefix.trim_end_matches('/'), tail) + } + } else if id.as_str().starts_with("https://") || id.as_str().starts_with("http://") { + let display = id.display_path(); + let rest = display.strip_prefix("-/").unwrap_or(display.as_str()); + let ext_prefix = format!("{}-", garden_prefix.trim_end_matches('~')); + format!("{}/{}", ext_prefix, rest) + } else { + return None; + }; + Some((id, href)) +} + +fn push_item_ref_anchor( + out: &mut String, + raw_ref: &str, + garden_prefix: &str, + item_bodies: Option<&HashMap>, +) -> bool { + let Some((id, href)) = garden_href_for_item_ref(raw_ref, garden_prefix) else { + return false; + }; + out.push_str(r#"'); + out.push_str(&escape_html(raw_ref)); + out.push_str(""); + true +} + +/// Replace item refs in raw prose with clickable garden links. /// /// When `item_bodies` is set, matching ontology items get a `title` attribute with a truncated /// body preview for native browser tooltips (forum posts, item pages). @@ -554,53 +625,16 @@ pub(super) fn linkify_slugs_with_prefix( garden_prefix: &str, item_bodies: Option<&HashMap>, ) -> String { - let escaped = escape_html(raw); - let mut out = String::with_capacity(escaped.len() + 64); - let mut i = 0; - let s = escaped.as_str(); - while i < s.len() { - let rest = &s[i..]; - if let Some(after_tilde) = rest.strip_prefix("~/") { - let path_len = after_tilde - .chars() - .take_while(|c| c.is_ascii_alphanumeric() || *c == '_' || *c == '-' || *c == '/') - .map(|c| c.len_utf8()) - .sum::(); - if path_len > 0 { - let path = &after_tilde[..path_len]; - out.push_str(r#" out.push_str(&escape_html(&text)), + crate::dsl::ProseToken::ItemRef(raw_ref) => { + if !push_item_ref_anchor(&mut out, &raw_ref, garden_prefix, item_bodies) { + out.push_str(&escape_html(&raw_ref)); } - out.push('>'); - out.push_str("~/"); - out.push_str(path); - out.push_str(""); - i += 2 + path_len; - continue; } } - if let Some((j, c)) = rest.char_indices().next() { - out.push(c); - i += j + c.len_utf8(); - } else { - break; - } } out } @@ -638,7 +672,13 @@ fn spotify_embed_src(url: &str) -> Option { if !(host == "open.spotify.com" || host == "www.open.spotify.com") { return None; } - let path = tail.split('#').next().unwrap_or(tail).split('?').next().unwrap_or(tail); + let path = tail + .split('#') + .next() + .unwrap_or(tail) + .split('?') + .next() + .unwrap_or(tail); let mut segs: Vec<&str> = path.split('/').filter(|s| !s.is_empty()).collect(); if segs.first().is_some_and(|s| s.starts_with("intl-")) { segs.remove(0); @@ -670,8 +710,16 @@ fn youtube_embed_src(url: &str) -> Option { let host = host.to_lowercase(); let video_id = if host == "youtu.be" || host == "www.youtu.be" { - clean_media_id(tail.split(['?', '#']).next().unwrap_or(tail).trim_matches('/')) - } else if matches!(host.as_str(), "youtube.com" | "www.youtube.com" | "m.youtube.com" | "music.youtube.com") { + clean_media_id( + tail.split(['?', '#']) + .next() + .unwrap_or(tail) + .trim_matches('/'), + ) + } else if matches!( + host.as_str(), + "youtube.com" | "www.youtube.com" | "m.youtube.com" | "music.youtube.com" + ) { let path = format!("/{}", tail.split('#').next().unwrap_or(tail)); if path.starts_with("/watch") { clean_media_id(&query_param(url, "v")?) @@ -745,10 +793,7 @@ pub(super) fn render_linkified_with_embeds_in_scope( /// CLI strings are embedded in a single-quoted JS literal; they must never need escaping. fn assert_cli_panel_cmd_js_single_quote_safe(s: &str) { assert!( - !s.contains('\\') - && !s.contains('\'') - && !s.contains('\n') - && !s.contains('\r'), + !s.contains('\\') && !s.contains('\'') && !s.contains('\n') && !s.contains('\r'), "cli_panel cmd must not contain `\\`, `'`, or newlines (got {s:?})" ); } @@ -805,15 +850,40 @@ mod linkify_title_tests { let mut bodies = HashMap::new(); let key = ItemId::parse(&slug_types::canonicalize_item("~/foo/bar")).unwrap(); bodies.insert(key, "Hello world\nline".to_string()); - let html = linkify_slugs_with_prefix( - "see ~/foo/bar ok", - "/r/x/~", - Some(&bodies), - ); + let html = linkify_slugs_with_prefix("see ~/foo/bar ok", "/r/x/~", Some(&bodies)); assert!(html.contains("title=\"Hello world line\"")); assert!(html.contains("href=\"/r/x/~/foo/bar\"")); } + #[test] + fn raw_url_links_to_public_external_garden_page() { + let html = linkify_slugs_with_prefix("see https://example.com/z.", "/~", None); + assert!(html + .contains(r#"https://example.com/z."#)); + } + + #[test] + fn dash_ref_links_to_room_external_garden_page_with_title() { + let mut bodies = HashMap::new(); + let key = ItemId::parse(&slug_types::canonicalize_item("-/example.com/z")).unwrap(); + bodies.insert(key, "External body\npreview".to_string()); + let html = + linkify_slugs_with_prefix("see -/example.com/z", "/r/9ab12cdroom/~", Some(&bodies)); + assert!(html.contains(r#"href="/r/9ab12cdroom/-/example.com/z""#)); + assert!(html.contains(r#"title="External body preview""#)); + } + + #[test] + fn code_fence_urls_are_not_linkified() { + let html = linkify_slugs_with_prefix( + "```json\n{\"url\":\"https://example.com/z\"}\n```\nthen https://example.com/a", + "/~", + None, + ); + assert!(!html.contains(r#"href="/-/example.com/z""#)); + assert!(html.contains(r#"href="/-/example.com/a""#)); + } + #[test] fn no_title_when_body_missing_or_empty() { let html = linkify_slugs_with_prefix("x ~/a/b y", "/~", Some(&HashMap::new())); Side B — contributor: tommy-mor Side B — commit message: [09842c93] remove shell Side B — unified diff (full patch): diff --git a/server/src/html/garden.rs b/server/src/html/garden.rs index 8fc6be1de8dd975f9547de615809222236be4b70..76ba96e45c9e16291a6ccd8096fdd01b274c1560 100644 --- a/server/src/html/garden.rs +++ b/server/src/html/garden.rs @@ -1321,55 +1321,53 @@ async fn vote_compare_inner( .expect("vote compare rpc json"); let body = html! { - section class="vote-compare-shell" { - h2 { "compare" } - div class="vote-compare-pair" { - a class="vote-compare-item" href=(nav.garden_item_href(&left)) { - code { (item_display_path(left.as_str())) } - } - span class="vote-compare-vs" { "vs" } - a class="vote-compare-item" href=(nav.garden_item_href(&right)) { - code { (item_display_path(right.as_str())) } - } - } - div id="vote-edge-history-region" { - (edge_history) - } - @if can_post { - form id="vote-compare-form" method="POST" action="/ui" { - input type="hidden" name=(UI_RPC_FIELD) value=(rpc_json); - div class="vote-thread-picker" { - label class="vote-thread-picker-label" { "thread" } - select id="vote-thread-select" name="thread_tag" aria-label="Thread to post vote into" { - @if thread_tags.is_empty() { - option value="vote" selected { "#vote" } - } - @for t in &thread_tags { - @if *t == auto_thread { - option value=(t) selected { "#" (t) } - } @else { - option value=(t) { "#" (t) } - } - } - } + h2 { "compare" } + div class="vote-compare-pair" { + a class="vote-compare-item" href=(nav.garden_item_href(&left)) { + code { (item_display_path(left.as_str())) } + } + span class="vote-compare-vs" { "vs" } + a class="vote-compare-item" href=(nav.garden_item_href(&right)) { + code { (item_display_path(right.as_str())) } + } + } + div id="vote-edge-history-region" { + (edge_history) + } + @if can_post { + form id="vote-compare-form" method="POST" action="/ui" { + input type="hidden" name=(UI_RPC_FIELD) value=(rpc_json); + div class="vote-thread-picker" { + label class="vote-thread-picker-label" { "thread" } + select id="vote-thread-select" name="thread_tag" aria-label="Thread to post vote into" { + @if thread_tags.is_empty() { + option value="vote" selected { "#vote" } } - input type="hidden" name="ratio_left" id="vote-ratio-left" value="50"; - input type="hidden" name="ratio_right" id="vote-ratio-right" value="50"; - label class="vote-compare-slider-label" { - span id="vote-slider-left-label" { (item_display_path(left.as_str())) } - input type="range" id="vote-preference-slider" min="0" max="100" value="50" - aria-valuemin="0" aria-valuemax="100"; - span id="vote-slider-right-label" { (item_display_path(right.as_str())) } + @for t in &thread_tags { + @if *t == auto_thread { + option value=(t) selected { "#" (t) } + } @else { + option value=(t) { "#" (t) } + } } - label class="vote-explain-label" { "reason (required)" } - textarea name="explanation" id="vote-explain" rows="5" placeholder="why this split?" required {} - div id="vote-compare-errors" {} - p { button type="submit" { "post vote" } } } - } @else { - p class="muted" { a href="/login" { "log in" } " to post this vote." } } + input type="hidden" name="ratio_left" id="vote-ratio-left" value="50"; + input type="hidden" name="ratio_right" id="vote-ratio-right" value="50"; + label class="vote-compare-slider-label" { + span id="vote-slider-left-label" { (item_display_path(left.as_str())) } + input type="range" id="vote-preference-slider" min="0" max="100" value="50" + aria-valuemin="0" aria-valuemax="100"; + span id="vote-slider-right-label" { (item_display_path(right.as_str())) } + } + label class="vote-explain-label" { "reason (required)" } + textarea name="explanation" id="vote-explain" rows="5" placeholder="why this split?" required {} + div id="vote-compare-errors" {} + p { button type="submit" { "post vote" } } } + } @else { + p class="muted" { a href="/login" { "log in" } " to post this vote." } + } }; let page = layout_full_bleed_chromeless( diff --git a/server/static/theme_default.css b/server/static/theme_default.css index a441c4f79d8cf88f5a8240f9992f47dbbd1ab46b..fdcde86c718eb53cce8273e45a9844c2d8041f88 100644 --- a/server/static/theme_default.css +++ b/server/static/theme_default.css @@ -1270,8 +1270,12 @@ body.view-ontology-dark .ont-ranking-list li { body.view-ontology-dark .ont-ranking-list li::before { color: var(--meta); content: counter(ont-rank) "."; - font-size: 11px; - min-width: 18px; + flex-shrink: 0; + font-size: 1.35rem; + font-weight: 700; + font-variant-numeric: tabular-nums; + line-height: 1; + min-width: 2.25ch; text-align: right; } body.view-ontology-dark .ont-rank-score { @@ -1424,8 +1428,12 @@ body.view-ontology-light .ont-ranking-list li { body.view-ontology-light .ont-ranking-list li::before { color: var(--meta); content: counter(ont-rank) "."; - font-size: 11px; - min-width: 18px; + flex-shrink: 0; + font-size: 1.35rem; + font-weight: 700; + font-variant-numeric: tabular-nums; + line-height: 1; + min-width: 2.25ch; text-align: right; } body.view-ontology-light .ont-rank-score { diff --git a/server/static/theme_retro.css b/server/static/theme_retro.css index 373f15bbd75b62604cdd14b9f0fadda2d6176991..61e1448b2f66a075c0e33325d6980448712fc927 100644 --- a/server/static/theme_retro.css +++ b/server/static/theme_retro.css @@ -135,6 +135,30 @@ body.view-ontology nav.breadcrumb a:hover { body.view-ontology nav.breadcrumb a.bc-current { color: #111; font-weight: 600; } body.view-ontology nav.breadcrumb .bc-sep { color: #888; padding: 0 2px; } +body.view-ontology ol.ont-ranking-list { + counter-reset: ont-rank; + list-style: none; + margin: 0.5rem 0; + padding: 0; +} +body.view-ontology ol.ont-ranking-list li { + align-items: baseline; + counter-increment: ont-rank; + display: flex; + gap: 0.35rem; +} +body.view-ontology ol.ont-ranking-list li::before { + flex-shrink: 0; + color: #666; + content: counter(ont-rank) "."; + font-size: 1.35rem; + font-weight: 700; + font-variant-numeric: tabular-nums; + line-height: 1; + min-width: 2.25ch; + text-align: right; +} + nav.breadcrumb.ont-sibling-nav { margin-top: 0; width: 100%; diff --git a/server/static/theme_retro_craft.css b/server/static/theme_retro_craft.css index 6eb9222184a8795d67a5d09d41de08c8ac1b148f..7da102040484c887833158a37c307d078205c701 100644 --- a/server/static/theme_retro_craft.css +++ b/server/static/theme_retro_craft.css @@ -742,12 +742,32 @@ body.view-ontology button.ont-garden-pin-ico:focus-visible { outline-offset: 2px; } +body.view-ontology ol.ont-ranking-list { + counter-reset: ont-rank; + list-style: none; + margin: 0; + padding: 0; +} body.view-ontology ol.ont-ranking-list li, body.view-ontology ul.ont-group-list li { display: flex; align-items: baseline; gap: 0.35rem; } +body.view-ontology ol.ont-ranking-list li { + counter-increment: ont-rank; +} +body.view-ontology ol.ont-ranking-list li::before { + flex-shrink: 0; + color: #5c574e; + content: counter(ont-rank) "."; + font-size: 1.35rem; + font-weight: 700; + font-variant-numeric: tabular-nums; + line-height: 1; + min-width: 2.25ch; + text-align: right; +} body.view-ontology ol.ont-ranking-list li .item-link, body.view-ontology ul.ont-group-list li .item-link { flex: 1;