Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

fix(web): percent-encode a wikilink's anchor or query suffix in the href

AkitaOnRails12c5a8ad48fb
diff --git a/CHANGELOG.md b/CHANGELOG.md
index bebb4b8d..3db01bbe 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,12 +12,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
provider. (#1026)
### Fixed
-- Fixed local and web wikilinks dropping `#anchor` and `?query` suffixes.
- Wikilinks like `[[page#section]]` or `[[page#section|label]]` stripped the
- fragment when rewritten to standard Markdown links or rendered in the web
- interface, leaving destinations pointed at the page root instead of the
- specific section. The rewriters now preserve anchor and query suffixes on
- the generated links.
+- Fixed wikilinks dropping `#anchor` and `?query` suffixes. `[[page#section]]`
+ and `[[page#section|label]]` lost the fragment when rewritten to standard
+ Markdown links (for example on OKF export), leaving the link pointed at the
+ page root. The rewriters now keep the suffix, percent-encoded where needed so
+ a space or parenthesis in it cannot break the link. In `/web` the suffix is
+ kept on the href; the page view does not emit heading ids. (#1089)
- Fixed a store write sent while the writer was shutting down occasionally
waiting forever instead of failing with `WriterClosed`: the writer now
closes its queue and drains in-flight commands before it stops.
diff --git a/crates/ai-memory-web/src/markdown.rs b/crates/ai-memory-web/src/markdown.rs
index 437aecc0..91a10ec2 100644
--- a/crates/ai-memory-web/src/markdown.rs
+++ b/crates/ai-memory-web/src/markdown.rs
@@ -248,8 +248,9 @@ fn wikilink_href_label(raw: &str, workspace: &str, project: &str) -> Option<(Str
let path = PagePath::new(path).ok()?;
let href = format!(
- "{}{suffix}",
- crate::templates::page_href(ws, proj, path.as_str())
+ "{}{}",
+ crate::templates::page_href(ws, proj, path.as_str()),
+ encode_link_suffix(suffix)
);
let display = label
.filter(|l| !l.is_empty())
@@ -258,6 +259,45 @@ fn wikilink_href_label(raw: &str, workspace: &str, project: &str) -> Option<(Str
Some((href, display))
}
+/// Percent-encode a wikilink's `#anchor` / `?query` suffix for a Markdown
+/// link destination. A space, parenthesis, angle bracket, quote or backslash
+/// would end or break the bare `[label](href)` destination; the URI delimiters
+/// a fragment or query uses (and existing `%` escapes) stay as written.
+fn encode_link_suffix(suffix: &str) -> String {
+ let mut out = String::with_capacity(suffix.len());
+ for byte in suffix.bytes() {
+ match byte {
+ b'A'..=b'Z'
+ | b'a'..=b'z'
+ | b'0'..=b'9'
+ | b'-'
+ | b'.'
+ | b'_'
+ | b'~'
+ | b'!'
+ | b'$'
+ | b'&'
+ | b'\''
+ | b'*'
+ | b'+'
+ | b','
+ | b';'
+ | b'='
+ | b':'
+ | b'@'
+ | b'/'
+ | b'?'
+ | b'#'
+ | b'%' => out.push(byte as char),
+ _ => {
+ use std::fmt::Write as _;
+ let _ = write!(&mut out, "%{byte:02X}");
+ }
+ }
+ }
+ out
+}
+
/// Peel an optional `[workspace/]project:` scope off a wikilink target,
/// defaulting to the current page's `workspace`/`project`. A `scope` made of
/// anything other than `[-_/.alnum]` is treated as part of the path (so a
@@ -666,6 +706,31 @@ mod tests {
);
}
+ #[test]
+ fn wikilink_suffix_with_space_or_parenthesis_stays_one_link() {
+ let html = render(
+ "[[notes/foo#my section]] and [[notes/foo#a)b]]",
+ "default",
+ "scratch",
+ );
+ assert!(
+ html.contains(r#"href="w/default/scratch/p/notes/foo.md#my%20section">"#),
+ "space in anchor: {html}"
+ );
+ assert!(
+ html.contains(r#"href="w/default/scratch/p/notes/foo.md#a%29b">"#),
+ "parenthesis in anchor: {html}"
+ );
+ assert!(
+ !html.contains("b)"),
+ "a ')' must not close the link early: {html}"
+ );
+ assert!(
+ !html.contains("[["),
+ "both wikilinks render as links: {html}"
+ );
+ }
+
#[test]
fn wikilink_cross_project_and_cross_workspace_scope() {
let html = render(