Skip to content

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

Rust

fix(web): keep a wikilink in a raw HTML block as written

kevin9327b78e582d2a51
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 200ab69c..18f76709 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
provider. (#1026)
### Fixed
+- Fixed the web page view showing a `[[wikilink]]` inside a raw HTML block
+ (an HTML comment, a `<div>` block) as generated link markup such as
+ `[notes/foo](w/default/scratch/p/notes/foo.md)`. HTML blocks render as
+ escaped source text, so the wikilink now stays as the page wrote it.
- 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 6e9e991d..3505489d 100644
--- a/crates/ai-memory-web/src/markdown.rs
+++ b/crates/ai-memory-web/src/markdown.rs
@@ -104,8 +104,8 @@ fn scope_relative_link<'a>(dest: CowStr<'a>, workspace: &str, project: &str) ->
}
/// Convert `[[target]]` / `[[target|label]]` spans into `[label](href)`
-/// markdown links, skipping code: fenced and indented code blocks and
-/// inline-code spans. Targets that aren't internal pages (external
+/// markdown links, skipping code (fenced and indented code blocks and
+/// inline-code spans) and raw HTML blocks. Targets that aren't internal pages (external
/// schemes, traversal, empty) are left as literal `[[…]]`.
///
/// What counts as code is what the renderer's own parser reads as code,
@@ -113,10 +113,14 @@ fn scope_relative_link<'a>(dest: CowStr<'a>, workspace: &str, project: &str) ->
/// CommonMark says they do, so a nested list item or a paragraph's
/// continuation line indented four spaces is text, and its wikilink is
/// rewritten like any other (the engine's link extractor indexes it).
+///
+/// An HTML block is shown as escaped source text, so markdown inside it
+/// is never parsed: a rewritten wikilink would surface as its generated
+/// `[label](w/…/p/….md)` markup instead of the `[[…]]` the page holds.
fn preprocess_wikilinks(body: &str, workspace: &str, project: &str) -> String {
let mut out = String::with_capacity(body.len() + 64);
let mut pos = 0;
- for code in code_ranges(body) {
+ for code in literal_ranges(body) {
rewrite_wikilinks_in_lines(&body[pos..code.start], workspace, project, &mut out);
out.push_str(&body[code.clone()]);
pos = code.end;
@@ -125,14 +129,16 @@ fn preprocess_wikilinks(body: &str, workspace: &str, project: &str) -> String {
out
}
-/// Byte ranges of the code in `body` (fenced and indented code blocks,
-/// inline-code spans) as the renderer's parser reads it, in document
-/// order and without overlap.
-fn code_ranges(body: &str) -> Vec<Range<usize>> {
+/// Byte ranges of `body` the renderer shows verbatim (fenced and indented
+/// code blocks, inline-code spans, raw HTML blocks) as its parser reads
+/// them, in document order and without overlap.
+fn literal_ranges(body: &str) -> Vec<Range<usize>> {
let mut ranges: Vec<Range<usize>> = Vec::new();
for (event, range) in Parser::new_ext(body, options()).into_offset_iter() {
- if matches!(event, Event::Start(Tag::CodeBlock(_)) | Event::Code(_))
- && ranges.last().is_none_or(|last| range.start >= last.end)
+ if matches!(
+ event,
+ Event::Start(Tag::CodeBlock(_) | Tag::HtmlBlock) | Event::Code(_)
+ ) && ranges.last().is_none_or(|last| range.start >= last.end)
{
ranges.push(range);
}
@@ -669,6 +675,30 @@ mod tests {
assert!(!inline.contains("<a href"), "inline code: {inline}");
}
+ /// An HTML block renders as its escaped source, so a wikilink in it
+ /// has to stay as written rather than turn into rewritten link markup.
+ #[test]
+ fn wikilink_kept_as_written_in_html_block() {
+ let comment = render("<!-- see [[notes/foo]] -->", "default", "scratch");
+ assert!(comment.contains("see [[notes/foo]]"), "comment: {comment}");
+ assert!(!comment.contains("/p/notes/foo.md"), "comment: {comment}");
+
+ let div = render("<div>\n[[notes/foo|Foo]]\n</div>", "default", "scratch");
+ assert!(div.contains("[[notes/foo|Foo]]"), "div: {div}");
+
+ // Inline HTML is only the tag itself; the text around it is still
+ // a paragraph, and so is the paragraph after the block.
+ let inline = render(
+ "<div>x</div>\n\ntext <span>[[notes/foo]]</span>",
+ "default",
+ "scratch",
+ );
+ assert!(
+ inline.contains(r#"<a href="w/default/scratch/p/notes/foo.md">notes/foo</a>"#),
+ "inline: {inline}"
+ );
+ }
+
#[test]
fn malformed_or_external_wikilink_kept_literal() {
// unterminated