diff --git a/README.md b/README.md index 6825752..7f6e3a9 100644 --- a/README.md +++ b/README.md @@ -168,6 +168,14 @@ Restore replaces the current note's body and asks for confirmation if it would r Open **Note info** from the editor toolbar. Expand **Frontmatter** to inspect the original YAML header, including custom keys, without editing it. This shows the last loaded or saved metadata; unsaved changes are not included, and the display updates after saving. +### Saving notes + +Notes autosave after three seconds without edits. During continuous typing, a save is queued at least every ten seconds. Ctrl+S (Cmd+S on macOS), switching notes, and closing an editor flush pending changes immediately. A failed save leaves the note marked **Unsaved** and blocks leaving that editor. + +Note writes run off the UI thread and replace the file atomically. Unchanged notes are not rewritten. Ordinary saves update only the affected note in the list, wiki-link cache, and Tasks view; search-index updates are batched in the background. External file changes are still picked up by the watcher. + +With **Close to tray** enabled and a tray icon available, closing the main window hides it after saving. With the setting off, closing the main window closes the application. The close-to-tray setting takes effect immediately; enabling the tray icon itself requires a restart. Explicit **Quit** exits rather than hiding to the tray. + Full documentation: [helixnotes.com/docs](https://helixnotes.com/docs.html) ## Tech Stack diff --git a/src-tauri/src/asset_scope.rs b/src-tauri/src/asset_scope.rs index 0425cfc..877fe0e 100644 --- a/src-tauri/src/asset_scope.rs +++ b/src-tauri/src/asset_scope.rs @@ -1,5 +1,38 @@ use std::path::Path; use tauri::{AppHandle, Manager, Runtime}; +use tauri_plugin_fs::FsExt; + +/// Only configured user-selected vaults may provide an external note's vault-relative root. +pub fn external_note_root( + app: &AppHandle, + note_path: &Path, +) -> Result { + let note = std::fs::canonicalize(note_path).map_err(|error| error.to_string())?; + if let Some(state) = app.try_state::() { + let (active, mut roots) = { + let config = state.config.lock().map_err(|error| error.to_string())?; + ( + config.active_vault.clone(), + config + .vaults + .iter() + .map(|vault| vault.path.clone()) + .collect::>(), + ) + }; + roots.sort_by_key(|root| std::cmp::Reverse(root.len())); + for root in active.into_iter().chain(roots) { + if let Ok(root) = std::fs::canonicalize(root) { + if note.starts_with(&root) { + return Ok(root); + } + } + } + } + note.parent() + .map(Path::to_path_buf) + .ok_or_else(|| "External note has no parent directory".into()) +} /// Grants asset-protocol access only to a vault selected by the user. /// `.helixnotes` is added explicitly because Unix glob matching excludes hidden paths. @@ -37,7 +70,23 @@ pub fn allow_external_note_assets( .ok_or_else(|| "External note has no parent directory".to_string())?; app.asset_protocol_scope() .allow_directory(parent, true) - .map_err(|error| error.to_string()) + .map_err(|error| error.to_string())?; + let root = external_note_root(app, ¬e)?; + let attachments = root.join(".helixnotes/attachments"); + // Never follow a hidden attachment-directory symlink when extending either scope. + if std::fs::canonicalize(&attachments) + .is_ok_and(|canonical| canonical == attachments && canonical.is_dir()) + { + app.asset_protocol_scope() + .allow_directory(&attachments, true) + .map_err(|error| error.to_string())?; + if let Some(scope) = app.try_fs_scope() { + scope + .allow_directory(&attachments, true) + .map_err(|error| error.to_string())?; + } + } + Ok(()) } #[cfg(test)] @@ -85,6 +134,101 @@ mod tests { fs::remove_dir_all(root).unwrap(); } + #[test] + fn external_note_scopes_include_its_attachments_but_not_hidden_private_metadata() { + use tauri_plugin_fs::FsExt; + let root = tempfile::tempdir().unwrap(); + let parent = root.path().join("external"); + let attachments = parent.join(".helixnotes/attachments"); + fs::create_dir_all(&attachments).unwrap(); + fs::create_dir_all(parent.join(".helixnotes/history")).unwrap(); + fs::create_dir_all(parent.join(".ssh")).unwrap(); + let note = parent.join("Note.md"); + let image = attachments.join("image.png"); + let history = parent.join(".helixnotes/history/snapshot.md"); + let private = parent.join(".ssh/private.bin"); + for file in [¬e, &image, &history, &private] { + fs::write(file, b"fixture").unwrap(); + } + let app = tauri::test::mock_builder() + .plugin(tauri_plugin_fs::init()) + .build(tauri::test::mock_context(tauri::test::noop_assets())) + .unwrap(); + app.fs_scope().allow_file(¬e).unwrap(); + app.fs_scope().allow_directory(&parent, true).unwrap(); + let before_private = [ + app.fs_scope().is_allowed(&history), + app.fs_scope().is_allowed(&private), + ]; + allow_external_note_assets(app.handle(), ¬e).unwrap(); + assert!( + app.fs_scope().is_allowed(&image), + "import must be able to read its managed attachments" + ); + assert!( + app.asset_protocol_scope().is_allowed(&image), + "the viewer must display managed attachments" + ); + for (file, previous) in [&history, &private].into_iter().zip(before_private) { + assert_eq!( + app.fs_scope().is_allowed(file), + previous, + "attachment grants must not widen private metadata access" + ); + assert!(!app.asset_protocol_scope().is_allowed(file)); + } + } + + #[test] + fn external_notebook_uses_its_registered_vault_root_for_attachment_scopes() { + use tauri_plugin_fs::FsExt; + let root = tempfile::tempdir().unwrap(); + let vault = root.path().join("source"); + fs::create_dir_all(vault.join("Notebook")).unwrap(); + fs::create_dir_all(vault.join(".helixnotes/attachments")).unwrap(); + let note = vault.join("Notebook/Note.md"); + let image = vault.join(".helixnotes/attachments/image.png"); + fs::write(¬e, b"Note").unwrap(); + fs::write(&image, b"Image").unwrap(); + let app = tauri::test::mock_builder() + .plugin(tauri_plugin_fs::init()) + .manage(crate::state::AppState::new(crate::types::AppConfig { + vaults: vec![crate::types::VaultConfig { + path: vault.to_string_lossy().into_owned(), + ..Default::default() + }], + ..Default::default() + })) + .build(tauri::test::mock_context(tauri::test::noop_assets())) + .unwrap(); + allow_external_note_assets(app.handle(), ¬e).unwrap(); + assert!(app.asset_protocol_scope().is_allowed(&image)); + assert!(app.fs_scope().is_allowed(&image)); + } + + #[cfg(unix)] + #[test] + fn external_attachment_scope_does_not_follow_hidden_directory_symlinks() { + use tauri_plugin_fs::FsExt; + let root = tempfile::tempdir().unwrap(); + let parent = root.path().join("external"); + let private = root.path().join("private"); + fs::create_dir_all(parent.join(".helixnotes")).unwrap(); + fs::create_dir(&private).unwrap(); + let note = parent.join("Note.md"); + let file = private.join("private.bin"); + fs::write(¬e, b"Note").unwrap(); + fs::write(&file, b"private fixture").unwrap(); + std::os::unix::fs::symlink(&private, parent.join(".helixnotes/attachments")).unwrap(); + let app = tauri::test::mock_builder() + .plugin(tauri_plugin_fs::init()) + .build(tauri::test::mock_context(tauri::test::noop_assets())) + .unwrap(); + allow_external_note_assets(app.handle(), ¬e).unwrap(); + assert!(!app.fs_scope().is_allowed(&file)); + assert!(!app.asset_protocol_scope().is_allowed(&file)); + } + #[test] fn external_note_scope_allows_relative_assets_only_below_its_directory() { let root = test_directory("external-note-assets"); diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index b669b85..939f506 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -776,15 +776,31 @@ pub async fn get_notes( } #[tauri::command] -pub async fn read_note( +pub async fn read_note( + app: AppHandle, state: State<'_, AppState>, vault_path: String, path: String, ) -> Result { state.validate_vault(&vault_path)?; - tauri::async_runtime::spawn_blocking(move || operations::read_note(&vault_path, &path)) - .await - .map_err(|error| error.to_string())? + tauri::async_runtime::spawn_blocking(move || { + let vault = std::fs::canonicalize(&vault_path).map_err(|error| error.to_string())?; + let target = std::fs::canonicalize(&path).map_err(|error| error.to_string())?; + if target.starts_with(vault) { + // Keep vault/trash validation, even if app metadata was explicitly selected. + return operations::read_note(&vault_path, &path); + } + // OS file-open requests, dialogs and drops grant this scope. Never grant it here. + ensure_scoped_path(&app, &target)?; + if !target.is_file() + || target.extension().and_then(|extension| extension.to_str()) != Some("md") + { + return Err("Note path must point to a Markdown file".to_string()); + } + operations::read_note_content(&target, &path) + }) + .await + .map_err(|error| error.to_string())? } #[tauri::command] @@ -825,6 +841,44 @@ pub async fn save_note( .map_err(|error| error.to_string())? } +#[tauri::command] +pub async fn import_note( + app: AppHandle, + state: State<'_, AppState>, + vault_path: String, + source_path: String, + notebook_relative: Option, + expected_raw: String, + references: Vec, +) -> Result { + let search = state.vault_snapshot(&vault_path)?; + let maintenance = state.vault_maintenance.clone().lock_owned().await; + let writes = state.note_save.clone().lock_owned().await; + tauri::async_runtime::spawn_blocking(move || { + let (_maintenance, _writes) = (maintenance, writes); + let source_root = asset_scope::external_note_root(&app, Path::new(&source_path))?; + let note = operations::import_note( + &vault_path, + &source_path, + &source_root, + notebook_relative.as_deref(), + &expected_raw, + references, + |path| ensure_scoped_path(&app, path), + )?; + index_note_bg(search, ¬e.path); + let event = crate::vault::watcher::changed_entry( + PathBuf::from(¬e.path), + "create", + Path::new(&vault_path), + ); + let _ = app.emit("file-changed", event); + Ok(note) + }) + .await + .map_err(|error| error.to_string())? +} + #[tauri::command] pub async fn duplicate_note( state: State<'_, AppState>, @@ -2977,10 +3031,13 @@ fn open_url_with_system(url: &str) -> Result<(), String> { #[cfg(test)] mod external_access_tests { - use super::{ensure_readable_path, ensure_writable_path, validate_external_url}; + use super::{ + ensure_readable_path, ensure_writable_path, read_note, save_note, validate_external_url, + }; use crate::state::AppState; use crate::types::AppConfig; use std::fs; + use tauri::Manager; use tauri_plugin_fs::FsExt; #[test] @@ -3008,6 +3065,250 @@ mod external_access_tests { } } + #[tokio::test] + async fn selected_external_markdown_can_be_viewed_but_not_saved_in_place() { + let root = tempfile::tempdir().unwrap(); + let vault = root.path().join("vault"); + let vault_path = vault.to_string_lossy().into_owned(); + crate::vault::operations::ensure_vault_structure(&vault_path).unwrap(); + let outside = root.path().join("café 雪.md"); + let raw = "---\ntitle: External note\ncustom: preserved\n---\n# External note\n\n![Image](images/picture.png)\n"; + fs::write(&outside, raw).unwrap(); + let app = tauri::test::mock_builder() + .plugin(tauri_plugin_fs::init()) + .manage(AppState::new(AppConfig { + active_vault: Some(vault_path.clone()), + ..Default::default() + })) + .build(tauri::test::mock_context(tauri::test::noop_assets())) + .unwrap(); + let path = outside.to_string_lossy().into_owned(); + + assert!(read_note( + app.handle().clone(), + app.state(), + vault_path.clone(), + path.clone() + ) + .await + .is_err()); + app.fs_scope().allow_file(&outside).unwrap(); + let note = read_note( + app.handle().clone(), + app.state(), + vault_path.clone(), + path.clone(), + ) + .await + .expect("an explicitly selected external Markdown file must open in the viewer"); + assert_eq!(note.path, path); + assert_eq!(note.raw, raw); + assert_eq!(note.meta.title, "External note"); + assert!(note.content.contains("![Image](images/picture.png)")); + assert!(save_note( + app.state(), + vault_path, + path, + note.meta, + "Changed".into(), + note.raw + ) + .await + .is_err()); + assert_eq!(fs::read_to_string(&outside).unwrap(), raw); + } + + #[tokio::test] + async fn external_import_requires_scoped_attachments_and_keeps_its_queued_vault() { + use futures::FutureExt; + let root = tempfile::tempdir().unwrap(); + let a = root.path().join("A").to_string_lossy().into_owned(); + let b = root.path().join("B").to_string_lossy().into_owned(); + crate::vault::operations::ensure_vault_structure(&a).unwrap(); + crate::vault::operations::ensure_vault_structure(&b).unwrap(); + let external = root.path().join("external"); + fs::create_dir(&external).unwrap(); + let source = external.join("External.md"); + let image = external.join("photo.png"); + let raw = "![Image](photo.png)"; + fs::write(&source, raw).unwrap(); + fs::write(&image, b"image bytes").unwrap(); + let app = tauri::test::mock_builder() + .plugin(tauri_plugin_fs::init()) + .manage(AppState::new(AppConfig { + active_vault: Some(a.clone()), + vaults: vec![ + crate::types::VaultConfig { + path: a.clone(), + ..Default::default() + }, + crate::types::VaultConfig { + path: b.clone(), + ..Default::default() + }, + ], + ..Default::default() + })) + .build(tauri::test::mock_context(tauri::test::noop_assets())) + .unwrap(); + app.fs_scope().allow_file(&source).unwrap(); + let plan = || { + vec![crate::vault::operations::ExternalNoteReference { + from: raw.find("photo.png").unwrap(), + to: raw.find("photo.png").unwrap() + "photo.png".len(), + href: "photo.png".into(), + html: false, + pdf: false, + }] + }; + let run = || { + super::import_note( + app.handle().clone(), + app.state(), + a.clone(), + source.to_string_lossy().into_owned(), + None, + raw.into(), + plan(), + ) + }; + assert!(run().await.unwrap_err().contains("selected")); + assert!(!std::path::Path::new(&a).join("External.md").exists()); + app.fs_scope().allow_file(&image).unwrap(); + let state = app.state::(); + let writes = state.note_save.lock().await; + let mut importing = Box::pin(run()); + assert!(importing.as_mut().now_or_never().is_none()); + state.config.lock().unwrap().active_vault = Some(b.clone()); + drop(writes); + let note = importing.await.unwrap(); + assert!(std::path::Path::new(¬e.path).starts_with(&a)); + assert!(!std::path::Path::new(&b).join("External.md").exists()); + assert_eq!(fs::read_to_string(source).unwrap(), raw); + assert_eq!(fs::read(image).unwrap(), b"image bytes"); + } + + #[tokio::test] + async fn imported_external_managed_attachments_use_the_source_vault_with_real_scopes() { + use crate::{asset_scope, types::VaultConfig, vault::operations}; + let root = tempfile::tempdir().unwrap(); + let source_root = root.path().join("source"); + let destination = root + .path() + .join("destination") + .to_string_lossy() + .into_owned(); + operations::ensure_vault_structure(source_root.to_str().unwrap()).unwrap(); + operations::ensure_vault_structure(&destination).unwrap(); + std::fs::create_dir(source_root.join("Notebook")).unwrap(); + let source = source_root.join("Notebook/Note.md"); + let image = source_root.join(".helixnotes/attachments/photo%20).png"); + let href = ".helixnotes/attachments/photo%2520%29.png"; + let raw = format!("![Image]({href})"); + std::fs::write(&source, &raw).unwrap(); + std::fs::write(&image, b"source image").unwrap(); + let app = tauri::test::mock_builder() + .plugin(tauri_plugin_fs::init()) + .manage(AppState::new(AppConfig { + active_vault: Some(destination.clone()), + vaults: vec![VaultConfig { + path: source_root.to_string_lossy().into_owned(), + ..Default::default() + }], + ..Default::default() + })) + .build(tauri::test::mock_context(tauri::test::noop_assets())) + .unwrap(); + app.fs_scope().allow_file(&source).unwrap(); + app.fs_scope() + .allow_directory(source.parent().unwrap(), true) + .unwrap(); + asset_scope::allow_external_note_assets(app.handle(), &source).unwrap(); + let from = raw.find(href).unwrap(); + let note = super::import_note( + app.handle().clone(), + app.state(), + destination.clone(), + source.to_string_lossy().into_owned(), + None, + raw.clone(), + vec![operations::ExternalNoteReference { + from, + to: from + href.len(), + href: href.into(), + html: false, + pdf: false, + }], + ) + .await + .unwrap(); + assert!(note.raw.contains(".helixnotes/attachments/")); + assert_eq!(std::fs::read_to_string(source).unwrap(), raw); + assert_eq!(std::fs::read(image).unwrap(), b"source image"); + let files: Vec<_> = + std::fs::read_dir(std::path::Path::new(&destination).join(".helixnotes/attachments")) + .unwrap() + .collect(); + assert_eq!(files.len(), 1); + assert_eq!( + std::fs::read(files[0].as_ref().unwrap().path()).unwrap(), + b"source image" + ); + } + + #[tokio::test] + async fn external_note_scope_does_not_allow_other_files_or_internal_metadata() { + let root = tempfile::tempdir().unwrap(); + let vault = root.path().join("vault"); + let vault_path = vault.to_string_lossy().into_owned(); + crate::vault::operations::ensure_vault_structure(&vault_path).unwrap(); + let outside = root.path().join("outside.md"); + let non_markdown = root.path().join("outside.txt"); + let internal = vault.join(".helixnotes/history/snapshot.md"); + fs::create_dir_all(internal.parent().unwrap()).unwrap(); + for path in [&outside, &non_markdown, &internal] { + fs::write(path, "Original").unwrap(); + } + let app = tauri::test::mock_builder() + .plugin(tauri_plugin_fs::init()) + .manage(AppState::new(AppConfig { + active_vault: Some(vault_path.clone()), + ..Default::default() + })) + .build(tauri::test::mock_context(tauri::test::noop_assets())) + .unwrap(); + let read = |path: &std::path::Path| { + read_note( + app.handle().clone(), + app.state(), + vault_path.clone(), + path.to_string_lossy().into_owned(), + ) + }; + assert!(read(&outside).await.is_err()); + for path in [&non_markdown, &internal] { + app.fs_scope().allow_file(path).unwrap(); + assert!(read(path).await.is_err()); + } + app.fs_scope().allow_file(&outside).unwrap(); + assert!(read_note( + app.handle().clone(), + app.state(), + root.path().to_string_lossy().into_owned(), + outside.to_string_lossy().into_owned() + ) + .await + .is_err()); + #[cfg(unix)] + { + let unselected = root.path().join("unselected.md"); + let link = vault.join("escape.md"); + fs::write(&unselected, "Private").unwrap(); + std::os::unix::fs::symlink(&unselected, &link).unwrap(); + assert!(read(&link).await.is_err()); + } + } + #[test] fn file_access_requires_the_active_vault_or_an_explicit_user_scope() { let root = std::env::temp_dir().join(format!( @@ -4279,13 +4580,22 @@ mod mutation_order_tests { .await .unwrap(); assert_eq!( - read_note(app.state(), a.clone(), alpha.path.clone()) - .await - .unwrap() - .content, + read_note( + app.handle().clone(), + app.state(), + a.clone(), + alpha.path.clone() + ) + .await + .unwrap() + .content, "# Shared\n\nDetached save" ); - assert!(read_note(app.state(), b.clone(), alpha.path).await.is_err()); + assert!( + read_note(app.handle().clone(), app.state(), b.clone(), alpha.path) + .await + .is_err() + ); assert_eq!(std::fs::read(&beta.path).unwrap(), beta_before); } diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 2b5d813..c1279fe 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -187,6 +187,7 @@ pub fn run() { commands::get_notes, commands::read_note, commands::save_note, + commands::import_note, commands::create_note, commands::duplicate_note, commands::create_daily_note, diff --git a/src-tauri/src/sync.rs b/src-tauri/src/sync.rs index a862743..35c6b30 100644 --- a/src-tauri/src/sync.rs +++ b/src-tauri/src/sync.rs @@ -409,7 +409,7 @@ impl WebdavClient { "Remote file {relpath} changed during sync; retry sync" )); } - let bytes = resp.bytes().map_err(|e| e.to_string())?.to_vec(); + let bytes: Vec = resp.bytes().map_err(|e| e.to_string())?.into(); if !expected_etag.is_empty() && self.etag_for(relpath)? != expected_etag { return Err(format!( "Remote file {relpath} changed during sync; retry sync" @@ -624,10 +624,34 @@ fn parse_multistatus(xml: &str, base_path: &str) -> Result, Str } Ok(Event::Text(e)) => { if cap != Cap::None { - if let Ok(decoded) = e.decode() { - if let Ok(text) = quick_xml::escape::unescape(&decoded) { - buf.push_str(&text); - } + let text = e + .decode() + .map_err(|e| format!("XML text decode error: {e}"))?; + buf.push_str(&text); + } + } + Ok(Event::CData(e)) => { + if cap != Cap::None { + let text = e + .decode() + .map_err(|e| format!("XML CDATA decode error: {e}"))?; + buf.push_str(&text); + } + } + Ok(Event::GeneralRef(e)) => { + if cap != Cap::None { + if let Some(ch) = e + .resolve_char_ref() + .map_err(|e| format!("XML reference error: {e}"))? + { + buf.push(ch); + } else { + let name = e + .decode() + .map_err(|e| format!("XML reference decode error: {e}"))?; + let text = quick_xml::escape::resolve_xml_entity(&name) + .ok_or_else(|| format!("Unknown XML entity: &{name};"))?; + buf.push_str(text); } } } @@ -1332,6 +1356,17 @@ mod tests { } fn webdav_test_client( responses: Vec<(&'static str, &'static str)>, + ) -> (WebdavClient, std::thread::JoinHandle>) { + webdav_test_client_bytes( + responses + .into_iter() + .map(|(status, body)| (status, body.as_bytes().to_vec())) + .collect(), + ) + } + + fn webdav_test_client_bytes( + responses: Vec<(&'static str, Vec)>, ) -> (WebdavClient, std::thread::JoinHandle>) { use std::io::Write; use std::net::TcpListener; @@ -1370,10 +1405,11 @@ mod tests { } requests.push(headers); let response = format!( - "HTTP/1.1 {status}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}", + "HTTP/1.1 {status}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n", body.len() ); stream.write_all(response.as_bytes()).unwrap(); + stream.write_all(&body).unwrap(); } requests }); @@ -1561,4 +1597,209 @@ mod tests { .contains("no strong ETag")); assert!(requests.join().unwrap().is_empty()); } + + #[test] + fn multistatus_preserves_named_and_numeric_references() { + let xml = r#" + /vault/a&b'<>"-é-雪.md + + "v&'<>雪é" + + "#; + let entries = parse_multistatus(xml, "/vault").unwrap(); + assert_eq!(entries.len(), 1); + assert_eq!(entries[0].relpath, "a&b'<>\"-é-雪.md"); + assert_eq!(entries[0].etag, "\"v&'<>雪é\""); + assert!(!entries[0].is_dir); + } + + #[test] + fn multistatus_preserves_mixed_cdata_and_strong_etag_policy() { + let xml = r#" + + /vault/雪.md + "雪" + + + + + + + "#; + let entries = parse_multistatus(xml, "/vault").unwrap(); + assert_eq!(entries.len(), 2); + assert_eq!(entries[0].relpath, "café&雪.md"); + assert_eq!(entries[0].etag, "\"v&雪\""); + assert_eq!(entries[1].relpath, "folder"); + assert!(entries[1].etag.is_empty()); + assert!(entries[1].is_dir); + } + + #[test] + fn multistatus_rejects_unknown_and_malformed_captured_references() { + for reference in [ + "&unknown;", + "&;", + "&#;", + "&#xZZ;", + "�", + "�", + "�", + "&", + "{", + ] { + for field in ["href", "getetag"] { + let (href, etag) = if field == "href" { + (format!("/vault/bad{reference}.md"), "\"ok\"".to_string()) + } else { + ("/vault/bad.md".to_string(), format!("\"bad{reference}\"")) + }; + let xml = format!( + "/vault/good.md\ + \"good\"\ + {href}{etag}\ + " + ); + assert!( + parse_multistatus(&xml, "/vault").is_err(), + "accepted {reference:?} in {field}" + ); + } + } + } + + #[test] + fn multistatus_does_not_expand_custom_or_external_dtd_entities() { + for declaration in [ + r#""#, + r#""#, + ] { + let xml = format!( + "\ + /vault/&custom;.md\"etag\"\ + " + ); + assert!(parse_multistatus(&xml, "/vault") + .err() + .unwrap() + .contains("Unknown XML entity")); + } + } + + #[test] + fn get_preserves_binary_download_and_rechecks_strong_etag() { + let bytes: Vec = (0..HASH_BUFFER_SIZE * 2 + 137) + .map(|i| (i % 256) as u8) + .collect(); + let listing = br#" + /vault/file.bin + "stable" + "#; + let (client, requests) = webdav_test_client_bytes(vec![ + ("200 OK\r\nETag: \"stable\"", bytes.clone()), + ("207 Multi-Status", listing.to_vec()), + ]); + assert_eq!(client.get("file.bin", "\"stable\"").unwrap(), bytes); + let requests = requests.join().unwrap(); + assert_eq!(requests.len(), 2); + assert!(requests[0].starts_with("GET /vault/file.bin ")); + assert!(requests[0] + .to_ascii_lowercase() + .contains("if-match: \"stable\"")); + assert!(requests[1].starts_with("PROPFIND /vault/file.bin ")); + assert!(requests[1].to_ascii_lowercase().contains("depth: 0")); + } + + #[test] + fn get_rejects_etag_changed_after_download_and_http_failures() { + let listing = r#"/vault/note.md + "new""#; + let (client, requests) = webdav_test_client(vec![ + ("200 OK\r\nETag: \"old\"", "snapshot"), + ("207 Multi-Status", listing), + ]); + assert!(client + .get("note.md", "\"old\"") + .unwrap_err() + .contains("retry")); + assert_eq!(requests.join().unwrap().len(), 2); + + for (status, expected_error) in [ + ("412 Precondition Failed", "retry"), + ("404 Not Found", "HTTP 404"), + ] { + let (client, requests) = webdav_test_client(vec![(status, "ignored")]); + assert!(client + .get("note.md", "\"old\"") + .unwrap_err() + .contains(expected_error)); + assert_eq!(requests.join().unwrap().len(), 1); + } + } + + #[test] + fn get_enforces_whole_body_timeout_for_progressing_chunked_download() { + use std::io::Write; + use std::net::TcpListener; + use std::time::Duration; + + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let url = format!("http://{}/vault", listener.local_addr().unwrap()); + let server = std::thread::spawn(move || { + let (mut stream, _) = listener.accept().unwrap(); + stream + .set_read_timeout(Some(Duration::from_secs(2))) + .unwrap(); + stream + .set_write_timeout(Some(Duration::from_secs(2))) + .unwrap(); + let mut request = Vec::new(); + let mut buffer = [0; 4096]; + while !request.windows(4).any(|part| part == b"\r\n\r\n") { + let count = stream.read(&mut buffer).unwrap(); + assert!(count > 0); + request.extend_from_slice(&buffer[..count]); + } + assert!(request.starts_with(b"GET /vault/file.bin ")); + stream + .write_all( + b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\ + Connection: close\r\n\r\n1\r\nx\r\n", + ) + .unwrap(); + let mut chunks_sent = 1; + // Each gap is below the 400ms timeout, but the full body takes 1s. + for _ in 0..20 { + std::thread::sleep(Duration::from_millis(50)); + if stream.write_all(b"1\r\nx\r\n").is_err() { + return chunks_sent; + } + chunks_sent += 1; + } + let _ = stream.write_all(b"0\r\n\r\n"); + chunks_sent + }); + let mut client = WebdavClient::new(WebdavConfig { + url, + username: String::new(), + password: String::new(), + }) + .unwrap(); + client.client = reqwest::blocking::Client::builder() + .no_proxy() + .timeout(Duration::from_millis(400)) + .build() + .unwrap(); + + let result = client.get("file.bin", ""); + let chunks_sent = server.join().unwrap(); + assert!( + chunks_sent > 1, + "server must make progress during the download" + ); + assert!( + result.is_err(), + "accepted a body exceeding the total timeout" + ); + } } diff --git a/src-tauri/src/vault/import.rs b/src-tauri/src/vault/import.rs index 37cd4e0..c08bf81 100644 --- a/src-tauri/src/vault/import.rs +++ b/src-tauri/src/vault/import.rs @@ -883,30 +883,79 @@ fn pathdiff(target: &str, base: &str) -> Result { } fn percent_decode(s: &str) -> String { - let mut result = String::with_capacity(s.len()); - let mut chars = s.bytes(); - while let Some(b) = chars.next() { - if b == b'%' { - let hi = chars.next(); - let lo = chars.next(); - if let (Some(h), Some(l)) = (hi, lo) { - if let Ok(byte) = u8::from_str_radix(&format!("{}{}", h as char, l as char), 16) { - result.push(byte as char); - continue; - } - } - result.push('%'); - } else { - result.push(b as char); - } - } - result + // Keep the complete original reference if escaped bytes are not valid UTF-8. + urlencoding::decode(s) + .map(|decoded| decoded.into_owned()) + .unwrap_or_else(|_| s.to_string()) } #[cfg(test)] mod tests { use super::*; + #[test] + fn percent_decode_preserves_unicode_plus_and_malformed_escapes() { + for (input, expected) in [ + ("café 雪+.png", "café 雪+.png"), + ("caf%C3%A9%20%E9%9B%AA+.png", "café 雪+.png"), + ("bad%GG%20name.png", "bad%GG name.png"), + ("tail%", "tail%"), + ("tail%2", "tail%2"), + ("雪%FF%20+%GG", "雪%FF%20+%GG"), + ("%E9%9B", "%E9%9B"), + ] { + assert_eq!(percent_decode(input), expected, "input {input:?}"); + } + } + + #[test] + fn import_rewrites_unicode_encoded_and_malformed_attachment_references() { + let vault = tempfile::tempdir().unwrap(); + let root = vault.path(); + let note_dir = root.join("笔记"); + std::fs::create_dir(¬e_dir).unwrap(); + let attachments = [ + "café 雪+.png", + "bad%GG.png", + "tail%.png", + "tail%2.png", + "bytes%FF%20.png", + ]; + for name in attachments { + std::fs::write(note_dir.join(name), name.as_bytes()).unwrap(); + } + let note_path = note_dir.join("Entry.md"); + std::fs::write( + ¬e_path, + "# Entry\n\n![raw](café%20雪+.png)\n\ + [encoded](caf%C3%A9%20%E9%9B%AA+.png#preview)\n\ + ![malformed](bad%GG.png)\n![percent](tail%.png)\n\ + [short](tail%2.png)\n[invalid UTF-8](bytes%FF%20.png)\n", + ) + .unwrap(); + + let result = import(root.to_str().unwrap()).unwrap(); + assert_eq!(result.attachments_moved, attachments.len() as u64); + let note = std::fs::read_to_string(¬e_path).unwrap(); + for reference in [ + "![raw](../.helixnotes/attachments/笔记/café%20雪+.png)", + "[encoded](../.helixnotes/attachments/笔记/café%20雪+.png#preview)", + "![malformed](../.helixnotes/attachments/笔记/bad%GG.png)", + "![percent](../.helixnotes/attachments/笔记/tail%.png)", + "[short](../.helixnotes/attachments/笔记/tail%2.png)", + "[invalid UTF-8](../.helixnotes/attachments/笔记/bytes%FF%20.png)", + ] { + assert!(note.contains(reference), "missing {reference:?} in {note}"); + } + for name in attachments { + assert!(!note_dir.join(name).exists()); + assert_eq!( + std::fs::read(root.join(".helixnotes/attachments/笔记").join(name)).unwrap(), + name.as_bytes() + ); + } + } + #[test] fn test_normalize_tags_comma_string() { let mapping: serde_yaml::Mapping = serde_yaml::from_str("tags: \"a, b, c\"").unwrap(); diff --git a/src-tauri/src/vault/mod.rs b/src-tauri/src/vault/mod.rs index 4ea15eb..e4e5348 100644 --- a/src-tauri/src/vault/mod.rs +++ b/src-tauri/src/vault/mod.rs @@ -2,3 +2,6 @@ pub mod frontmatter; pub mod import; pub mod operations; pub mod watcher; + +#[cfg(test)] +mod note_import_tests; diff --git a/src-tauri/src/vault/note_import_tests.rs b/src-tauri/src/vault/note_import_tests.rs new file mode 100644 index 0000000..eb9bebd --- /dev/null +++ b/src-tauri/src/vault/note_import_tests.rs @@ -0,0 +1,493 @@ +use super::operations::{ + ensure_vault_structure, import_note as import_from_root, ExternalNoteReference, +}; + +fn import_note( + vault: &str, + source: &str, + notebook: Option<&str>, + raw: &str, + references: Vec, + authorize: impl Fn(&Path) -> Result<(), String>, +) -> Result { + import_from_root( + vault, + source, + Path::new(source).parent().unwrap(), + notebook, + raw, + references, + authorize, + ) +} +use std::fs; +use std::path::{Path, PathBuf}; + +fn reference(raw: &str, original: &str, href: &str, html: bool) -> ExternalNoteReference { + let from = raw.find(original).unwrap(); + ExternalNoteReference { + from, + to: from + original.len(), + href: href.into(), + html, + pdf: false, + } +} + +fn fixture(raw: &str) -> (tempfile::TempDir, String, PathBuf) { + let root = tempfile::tempdir().unwrap(); + let vault = root.path().join("vault").to_string_lossy().into_owned(); + ensure_vault_structure(&vault).unwrap(); + let external = root.path().join("external"); + fs::create_dir_all(external.join("images")).unwrap(); + let source = external.join("External.md"); + fs::write(&source, raw).unwrap(); + (root, vault, source) +} + +fn attachments(vault: &str) -> Vec { + fs::read_dir(Path::new(vault).join(".helixnotes/attachments")) + .unwrap() + .map(|entry| entry.unwrap().path()) + .collect() +} + +#[test] +fn imports_relative_files_once_preserving_originals_frontmatter_and_literal_content() { + let raw = "---\r\ntitle: Portable\r\ncustom: 雪 📝\r\n---\r\n![One](images/café%20雪.png)\r\n![Two](images/./café%20雪.png)\r\nPDF\r\n\r\n~~~md\r\n![Example](missing.png)\r\n~~~\r\n"; + for notebook in [None, Some("Projects/Nested")] { + let (_root, vault, source) = fixture(raw); + let parent = source.parent().unwrap(); + fs::write(parent.join("images/café 雪.png"), b"image bytes").unwrap(); + fs::write(parent.join("images/report.pdf"), b"pdf bytes").unwrap(); + fs::create_dir_all(Path::new(&vault).join("Projects/Nested")).unwrap(); + let references = vec![ + reference(raw, "images/café%20雪.png", "images/café%20雪.png", false), + reference( + raw, + "images/./café%20雪.png", + "images/./café%20雪.png", + false, + ), + reference( + raw, + "images/report.pdf#page=2&mode=fit", + "images/report.pdf#page=2&mode=fit", + true, + ), + ]; + let note = import_note( + &vault, + source.to_str().unwrap(), + notebook, + raw, + references, + |path| { + if path.starts_with(parent) { + Ok(()) + } else { + Err("Not selected".into()) + } + }, + ) + .unwrap(); + assert!(note + .raw + .starts_with("---\r\ntitle: Portable\r\ncustom: 雪 📝\r\n---\r\n")); + assert!(note.raw.contains("![Example](missing.png)")); + assert!(note.raw.contains("#page=2&mode=fit")); + assert_eq!(fs::read_to_string(&source).unwrap(), raw); + assert_eq!(fs::read_to_string(¬e.path).unwrap(), note.raw); + let files = attachments(&vault); + assert_eq!( + files.len(), + 2, + "aliases of one file share one portable copy" + ); + for file in files { + let filename = urlencoding::encode(file.file_name().unwrap().to_str().unwrap()); + let relative = format!(".helixnotes/attachments/{filename}"); + assert!(note.raw.contains(&relative)); + assert!(Path::new(&vault) + .join(urlencoding::decode(&relative).unwrap().as_ref()) + .is_file()); + } + assert_eq!( + fs::read(parent.join("images/café 雪.png")).unwrap(), + b"image bytes" + ); + assert_eq!( + fs::read(parent.join("images/report.pdf")).unwrap(), + b"pdf bytes" + ); + } +} + +#[test] +fn imported_pdf_embeds_keep_the_vault_root_path_convention_in_nested_notebooks() { + let raw = "
"; + let (_root, vault, source) = fixture(raw); + fs::write(source.parent().unwrap().join("images/report.pdf"), b"pdf").unwrap(); + fs::create_dir_all(Path::new(&vault).join("Projects/Nested")).unwrap(); + let mut pdf = reference(raw, "images/report.pdf", "images/report.pdf", true); + pdf.pdf = true; + let note = import_note( + &vault, + source.to_str().unwrap(), + Some("Projects/Nested"), + raw, + vec![pdf], + |_| Ok(()), + ) + .unwrap(); + assert!(note.raw.contains("data-pdf-src=\".helixnotes/attachments/")); + assert!(note.raw.contains("data-pdf-name=\"Report\"")); + assert_eq!(attachments(&vault).len(), 1); +} + +#[test] +fn imported_document_links_remain_managed_and_stable_after_note_and_notebook_moves() { + let raw = "[Report](images/report.pdf#page=2)"; + let (_root, vault, source) = fixture(raw); + fs::write(source.parent().unwrap().join("images/report.pdf"), b"pdf").unwrap(); + fs::create_dir_all(Path::new(&vault).join("Projects/Nested")).unwrap(); + fs::create_dir_all(Path::new(&vault).join("Archive")).unwrap(); + let note = import_note( + &vault, + source.to_str().unwrap(), + Some("Projects/Nested"), + raw, + vec![reference( + raw, + "images/report.pdf#page=2", + "images/report.pdf#page=2", + false, + )], + |_| Ok(()), + ) + .unwrap(); + assert!( + note.raw.starts_with("[Report](.helixnotes/attachments/"), + "managed links must not depend on notebook depth" + ); + let href = note + .raw + .strip_prefix("[Report](") + .unwrap() + .strip_suffix(')') + .unwrap(); + let attachment = Path::new(&vault).join( + urlencoding::decode(href.split('#').next().unwrap()) + .unwrap() + .as_ref(), + ); + assert_eq!(fs::read(attachment).unwrap(), b"pdf"); + let moved = super::operations::move_note(&vault, ¬e.path, &vault).unwrap(); + assert_eq!(fs::read_to_string(moved).unwrap(), note.raw); + // Imported managed paths remain stable when their containing notebook is moved too. + let second = import_note( + &vault, + source.to_str().unwrap(), + Some("Projects/Nested"), + raw, + vec![reference( + raw, + "images/report.pdf#page=2", + "images/report.pdf#page=2", + false, + )], + |_| Ok(()), + ) + .unwrap(); + super::operations::move_notebook( + &vault, + Path::new(&vault).join("Projects").to_str().unwrap(), + Path::new(&vault).join("Archive").to_str().unwrap(), + ) + .unwrap(); + assert_eq!( + fs::read_to_string(Path::new(&vault).join("Archive/Projects/Nested/External.md")).unwrap(), + second.raw + ); +} + +#[test] +fn import_does_not_replace_existing_note_or_same_named_attachment() { + let raw = "![Image](images/photo.png)"; + let (_root, vault, source) = fixture(raw); + fs::write( + source.parent().unwrap().join("images/photo.png"), + b"new image", + ) + .unwrap(); + fs::write(Path::new(&vault).join("External.md"), b"keep note").unwrap(); + fs::write( + Path::new(&vault).join(".helixnotes/attachments/photo.png"), + b"keep image", + ) + .unwrap(); + let note = import_note( + &vault, + source.to_str().unwrap(), + None, + raw, + vec![reference( + raw, + "images/photo.png", + "images/photo.png", + false, + )], + |_| Ok(()), + ) + .unwrap(); + assert!(note.path.ends_with("External (2).md")); + assert_eq!( + fs::read(Path::new(&vault).join("External.md")).unwrap(), + b"keep note" + ); + assert_eq!( + fs::read(Path::new(&vault).join(".helixnotes/attachments/photo.png")).unwrap(), + b"keep image" + ); + assert_eq!(attachments(&vault).len(), 2); +} + +#[test] +fn stale_sources_missing_files_and_denied_attachments_publish_nothing() { + let raw = "![Image](images/photo.png)"; + let (_root, vault, source) = fixture(raw); + let plan = || { + vec![reference( + raw, + "images/photo.png", + "images/photo.png", + false, + )] + }; + assert!(import_note( + &vault, + source.to_str().unwrap(), + None, + "stale", + plan(), + |_| Ok(()) + ) + .is_err()); + assert!( + import_note(&vault, source.to_str().unwrap(), None, raw, plan(), |_| Ok( + () + )) + .is_err() + ); + fs::write(source.parent().unwrap().join("images/photo.png"), b"image").unwrap(); + let denied = import_note( + &vault, + source.to_str().unwrap(), + None, + raw, + plan(), + |path| { + if path == source { + Ok(()) + } else { + Err("Attachment not selected".into()) + } + }, + ); + assert!(denied.unwrap_err().contains("not selected")); + assert!(attachments(&vault).is_empty()); + assert!(!Path::new(&vault).join("External.md").exists()); + assert_eq!(fs::read_to_string(&source).unwrap(), raw); +} + +#[test] +fn invalid_destinations_ranges_and_absolute_references_are_rejected() { + let raw = "雪 ![Image](images/photo.png)"; + let (_root, vault, source) = fixture(raw); + fs::write(source.parent().unwrap().join("images/photo.png"), b"image").unwrap(); + assert!(import_note( + &vault, + source.to_str().unwrap(), + Some("../escape"), + raw, + vec![], + |_| Ok(()) + ) + .is_err()); + for href in [ + "/private.png", + "%2Fprivate.png", + "C:/private.png", + "https://example.com/file.png", + "Related.md", + ] { + assert!(import_note( + &vault, + source.to_str().unwrap(), + None, + raw, + vec![reference(raw, "images/photo.png", href, false)], + |_| Ok(()) + ) + .is_err()); + } + assert!(import_note( + &vault, + source.to_str().unwrap(), + None, + raw, + vec![ExternalNoteReference { + from: 1, + to: 3, + href: "images/photo.png".into(), + html: false, + pdf: false, + }], + |_| Ok(()) + ) + .is_err()); + let plan = vec![ + reference(raw, "images/photo.png", "images/photo.png", false), + reference(raw, "images/photo.png", "images/photo.png", false), + ]; + assert!(import_note( + &vault, + source.to_str().unwrap(), + None, + raw, + plan, + |_| Ok(()) + ) + .is_err()); + assert!(attachments(&vault).is_empty()); +} + +#[test] +fn failed_note_publication_removes_only_its_new_attachments() { + let raw = "![Image](images/photo.png)"; + let (_root, vault, source) = fixture(raw); + fs::write(source.parent().unwrap().join("images/photo.png"), b"image").unwrap(); + fs::write(Path::new(&vault).join("External.md"), b"keep").unwrap(); + for number in 2..=100 { + fs::write( + Path::new(&vault).join(format!("External ({number}).md")), + b"keep", + ) + .unwrap(); + } + let original_asset = Path::new(&vault).join(".helixnotes/attachments/original.png"); + fs::write(&original_asset, b"keep attachment").unwrap(); + assert!(import_note( + &vault, + source.to_str().unwrap(), + None, + raw, + vec![reference( + raw, + "images/photo.png", + "images/photo.png", + false + )], + |_| Ok(()) + ) + .is_err()); + assert_eq!(attachments(&vault), vec![original_asset]); + assert_eq!( + fs::read(Path::new(&vault).join("External.md")).unwrap(), + b"keep" + ); + assert_eq!(fs::read_to_string(&source).unwrap(), raw); + assert!(!fs::read_dir(&vault).unwrap().any(|entry| entry + .unwrap() + .file_name() + .to_string_lossy() + .starts_with(".helixnotes-import-"))); +} + +#[test] +fn note_content_cannot_import_parent_files_or_hidden_secrets_from_a_broad_scope() { + let raw = "[File](asset.bin)"; + let (root, vault, source) = fixture(raw); + let parent = source.parent().unwrap(); + for name in [ + ".ssh/key.bin", + ".helixnotes/history/private.bin", + ".helixnotes/attachments/photo.bin", + ] { + let file = parent.join(name); + fs::create_dir_all(file.parent().unwrap()).unwrap(); + fs::write(file, b"private fixture").unwrap(); + } + fs::write(root.path().join("private.bin"), b"private outside fixture").unwrap(); + for href in [ + "../private.bin", + ".ssh/key.bin", + ".helixnotes/history/private.bin", + ] { + assert!(import_note( + &vault, + source.to_str().unwrap(), + None, + raw, + vec![reference(raw, "asset.bin", href, false)], + |_| Ok(()) + ) + .is_err()); + assert!(attachments(&vault).is_empty()); + assert!(!Path::new(&vault).join("External.md").exists()); + } + let note = import_note( + &vault, + source.to_str().unwrap(), + None, + raw, + vec![reference( + raw, + "asset.bin", + ".helixnotes/attachments/photo.bin", + false, + )], + |_| Ok(()), + ) + .unwrap(); + assert!(note.raw.contains(".helixnotes/attachments/")); + assert_eq!(attachments(&vault).len(), 1); +} + +#[cfg(unix)] +#[test] +fn import_rejects_attachment_and_notebook_destinations_that_follow_symlinks() { + let raw = "![Image](images/photo.png)"; + let (root, vault, source) = fixture(raw); + fs::write(source.parent().unwrap().join("images/photo.png"), b"image").unwrap(); + let outside = root.path().join("outside"); + fs::create_dir(&outside).unwrap(); + std::os::unix::fs::symlink(&outside, Path::new(&vault).join("Notebook")).unwrap(); + assert!(import_note( + &vault, + source.to_str().unwrap(), + Some("Notebook"), + raw, + vec![], + |_| Ok(()) + ) + .is_err()); + let directory = Path::new(&vault).join(".helixnotes/attachments"); + fs::remove_dir(&directory).unwrap(); + std::os::unix::fs::symlink(&outside, &directory).unwrap(); + assert!(import_note( + &vault, + source.to_str().unwrap(), + None, + raw, + vec![reference( + raw, + "images/photo.png", + "images/photo.png", + false + )], + |_| Ok(()) + ) + .is_err()); + assert!(fs::read_dir(outside).unwrap().next().is_none()); +} diff --git a/src-tauri/src/vault/operations.rs b/src-tauri/src/vault/operations.rs index 5f04084..a749d48 100644 --- a/src-tauri/src/vault/operations.rs +++ b/src-tauri/src/vault/operations.rs @@ -714,7 +714,10 @@ pub fn read_vault_note(vault_path: &str, path: &str) -> Result Result { +pub(crate) fn read_note_content( + validated: &Path, + reported_path: &str, +) -> Result { let p = validated; let raw = fs::read_to_string(p).map_err(|e| e.to_string())?; let filename = p @@ -1663,13 +1666,11 @@ pub fn get_all_tags(vault_path: &str) -> Result, String> { let hn_dir = helixnotes_dir(vault_path); let md_files: Vec = WalkDir::new(vault_path) .into_iter() + .filter_entry(|entry| entry.depth() == 0 || entry.path() != hn_dir.as_path()) .filter_map(|e| e.ok()) .filter(|e| { let p = e.path(); - p.is_file() - && p.extension().and_then(|x| x.to_str()) == Some("md") - && !is_hidden(p) - && !p.starts_with(&hn_dir) + p.is_file() && p.extension().and_then(|x| x.to_str()) == Some("md") && !is_hidden(p) }) .map(|e| e.path().to_path_buf()) .collect(); @@ -1734,6 +1735,256 @@ pub fn save_attachment(vault_path: &str, name: &str, data: &[u8]) -> Result, + expected_raw: &str, + mut references: Vec, + authorize: impl Fn(&Path) -> Result<(), String>, +) -> Result { + let source = canonicalize_path(Path::new(source_path), "source note")?; + authorize(&source)?; + if !source.is_file() || source.extension().and_then(|ext| ext.to_str()) != Some("md") { + return Err("Import source must be a Markdown file".into()); + } + let source_root = canonicalize_path(source_root, "source attachment root")?; + if !source.starts_with(&source_root) { + return Err("Source attachment root does not own the note".into()); + } + let source_attachments = source_root.join(".helixnotes/attachments"); + let raw = fs::read_to_string(&source).map_err(|error| error.to_string())?; + if raw != expected_raw { + return Err("The source note changed on disk. Reopen it before importing.".into()); + } + let relative = notebook_relative.map(safe_relative_path).transpose()?; + let directory = Path::new(vault_path).join(relative.unwrap_or(Path::new(""))); + ensure_vault_content_dir(vault_path, &directory)?; + let vault = canonicalize_path(Path::new(vault_path), "vault path")?; + let canonical_directory = canonicalize_path(&directory, "import destination")?; + if canonical_directory != vault.join(relative.unwrap_or(Path::new(""))) { + return Err("Import destination must not use symbolic links".into()); + } + let attachments = helixnotes_dir(vault_path).join("attachments"); + references.sort_by_key(|reference| reference.from); + let mut previous = 0; + let mut targets = Vec::new(); + // Validate every range and permission before publishing any attachment. + for reference in &references { + if reference.from < previous + || reference.from >= reference.to + || raw.get(reference.from..reference.to).is_none() + { + return Err("Invalid attachment reference range".into()); + } + previous = reference.to; + let (path, suffix) = reference + .href + .find(['#', '?']) + .map(|index| reference.href.split_at(index)) + .unwrap_or((&reference.href, "")); + let decoded = urlencoding::decode(path) + .map_err(|error| error.to_string())? + .replace('\\', "/"); + let path = Path::new(&decoded); + if decoded.is_empty() + || decoded.contains(':') + || path.is_absolute() + || path + .extension() + .and_then(|ext| ext.to_str()) + .is_some_and(|ext| ext.eq_ignore_ascii_case("md")) + { + return Err("Import attachments must be relative non-Markdown files".into()); + } + let base = if decoded.starts_with(".helixnotes/attachments/") || reference.pdf { + source_root.as_path() + } else { + source.parent().ok_or("Source note has no parent")? + }; + let target = canonicalize_path(&base.join(path), "attachment")?; + authorize(&target)?; + let relative_target = if let Ok(relative) = target.strip_prefix(&source_attachments) { + relative + } else { + target.strip_prefix(source.parent().ok_or("Source note has no parent")?) + .map_err(|_| "Attachments must stay inside the source note's folder or managed attachment root")? + }; + let components: Vec<_> = relative_target.components().collect(); + let helix_attachment = components + .first() + .is_some_and(|part| part.as_os_str() == ".helixnotes") + && components + .get(1) + .is_some_and(|part| part.as_os_str() == "attachments"); + if components.iter().enumerate().any(|(index, part)| { + part.as_os_str().to_string_lossy().starts_with('.') && !(helix_attachment && index == 0) + }) { + return Err("Hidden metadata is not imported as an attachment".into()); + } + if !target.is_file() { + return Err("Attachment reference must point to a file".into()); + } + targets.push((target, suffix.to_string())); + } + if !references.is_empty() { + fs::create_dir_all(&attachments).map_err(|error| error.to_string())?; + if canonicalize_path(&attachments, "attachment destination")? + != vault.join(".helixnotes/attachments") + { + return Err("Attachment destination must not use symbolic links".into()); + } + } + let mut created = Vec::new(); + let result = (|| { + let mut copied = std::collections::HashMap::::new(); + let mut rewritten = String::with_capacity(raw.len()); + let mut previous = 0; + for (reference, (target, suffix)) in references.iter().zip(targets) { + let relative = if let Some(relative) = copied.get(&target) { + relative.clone() + } else { + let name = sanitize_filename( + &target + .file_name() + .ok_or("Attachment has no filename")? + .to_string_lossy(), + ); + let mut temporary = tempfile::Builder::new() + .prefix(".helixnotes-import-") + .tempfile_in(&attachments) + .map_err(|error| error.to_string())?; + let mut file = fs::File::open(&target).map_err(|error| error.to_string())?; + let before = file.metadata().map_err(|error| error.to_string())?; + let bytes = std::io::copy(&mut file, temporary.as_file_mut()) + .map_err(|error| error.to_string())?; + let after = file.metadata().map_err(|error| error.to_string())?; + if bytes != before.len() + || before.len() != after.len() + || before.modified().ok() != after.modified().ok() + { + return Err("An attachment changed while importing. Retry the import.".into()); + } + temporary + .as_file() + .sync_all() + .map_err(|error| error.to_string())?; + let filename = loop { + let filename = format!("{}_{}", Uuid::new_v4(), name); + let destination = attachments.join(&filename); + match temporary.persist_noclobber(&destination) { + Ok(_) => { + created.push(destination); + break filename; + } + Err(error) if error.error.kind() == std::io::ErrorKind::AlreadyExists => { + temporary = error.file + } + Err(error) => return Err(error.error.to_string()), + } + }; + let relative = + format!(".helixnotes/attachments/{}", urlencoding::encode(&filename)); + copied.insert(target, relative.clone()); + relative + }; + // Use the same stable vault-root convention as normal attachment insertion/saving. + let mut href = format!("{relative}{suffix}"); + if reference.html { + href = href + .replace('&', "&") + .replace('"', """) + .replace('\'', "'") + .replace('<', "<") + .replace('>', ">"); + } else { + href = href + .replace(' ', "%20") + .replace('"', "%22") + .replace('<', "%3C") + .replace('>', "%3E"); + } + rewritten.push_str(&raw[previous..reference.from]); + rewritten.push_str(&href); + previous = reference.to; + } + rewritten.push_str(&raw[previous..]); + let filename = source.file_name().ok_or("Source note has no filename")?; + let mut temporary = tempfile::Builder::new() + .prefix(".helixnotes-import-") + .tempfile_in(&directory) + .map_err(|error| error.to_string())?; + temporary + .write_all(rewritten.as_bytes()) + .map_err(|error| error.to_string())?; + temporary + .as_file() + .sync_all() + .map_err(|error| error.to_string())?; + let mut destination = directory.join(filename); + let mut suffix = 2; + loop { + match temporary.persist_noclobber(&destination) { + Ok(_) => break, + Err(error) + if error.error.kind() == std::io::ErrorKind::AlreadyExists && suffix <= 100 => + { + temporary = error.file; + destination = directory.join(format!( + "{} ({suffix}).md", + source.file_stem().unwrap_or_default().to_string_lossy() + )); + suffix += 1; + } + Err(error) => return Err(error.error.to_string()), + } + } + let (meta, content) = frontmatter::parse_note( + &rewritten, + &destination + .file_name() + .unwrap_or_default() + .to_string_lossy(), + ); + Ok(NoteContent { + path: destination.to_string_lossy().into_owned(), + meta, + content, + raw: rewritten, + }) + })(); + if let Err(error) = result { + let mut failed_cleanup = Vec::new(); + for path in created { + if let Err(cleanup) = fs::remove_file(&path) { + if cleanup.kind() != std::io::ErrorKind::NotFound { + failed_cleanup.push(format!("{}: {cleanup}", path.display())); + } + } + } + if !failed_cleanup.is_empty() { + return Err(format!( + "{error}; could not remove unused attachments: {}", + failed_cleanup.join("; ") + )); + } + return Err(error); + } + result +} + pub(crate) fn normalize_notebook_icon_key(path: &str) -> String { path.replace('\\', "/") } @@ -2200,6 +2451,71 @@ mod tests { use std::fs; use std::io::Read; + #[test] + fn all_tags_excludes_only_root_metadata_subtree_and_hidden_filenames() { + let parent = tempfile::tempdir().unwrap(); + let root = parent.path().join(".helixnotes"); + let fixtures = [ + ("Root.md", "root"), + (".helixnotes/history/Stored.md", "excluded"), + ("Notebook/.helixnotes/Nested.md", "nested"), + (".helixnotes-other/Visible.md", "similar"), + (".obsidian/Visible.md", "obsidian"), + ("assets/Visible.md", "assets"), + (".other/Visible.md", "other"), + ("Notebook/.Hidden.md", "hidden"), + ("Notebook/Upper.MD", "uppercase"), + ("Notebook/other.txt", "text"), + ]; + for (relative, tag) in fixtures { + let path = root.join(relative); + fs::create_dir_all(path.parent().unwrap()).unwrap(); + fs::write(path, format!("---\ntags: [{tag}]\n---\n# Note")).unwrap(); + } + + assert_eq!( + super::get_all_tags(root.to_str().unwrap()).unwrap(), + vec![ + ("assets".to_string(), 1), + ("nested".to_string(), 1), + ("obsidian".to_string(), 1), + ("other".to_string(), 1), + ("root".to_string(), 1), + ("similar".to_string(), 1), + ] + ); + } + + #[cfg(unix)] + #[test] + fn all_tags_keeps_file_symlinks_without_following_directory_symlinks() { + use std::os::unix::fs::symlink; + let vault = tempfile::tempdir().unwrap(); + let outside = tempfile::tempdir().unwrap(); + fs::write( + outside.path().join("External.md"), + "---\ntags: [linked]\n---\n# Note", + ) + .unwrap(); + symlink( + outside.path().join("External.md"), + vault.path().join("Linked.md"), + ) + .unwrap(); + symlink(outside.path(), vault.path().join("Directory")).unwrap(); + fs::create_dir(vault.path().join(".helixnotes")).unwrap(); + symlink( + outside.path().join("External.md"), + vault.path().join(".helixnotes/Excluded.md"), + ) + .unwrap(); + + assert_eq!( + super::get_all_tags(vault.path().to_str().unwrap()).unwrap(), + vec![("linked".to_string(), 1)] + ); + } + #[test] fn same_named_attachment_writes_never_replace_each_other() { let vault = tempfile::tempdir().unwrap(); diff --git a/src/lib/api.ts b/src/lib/api.ts index 0aa17a0..a61b290 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -1,4 +1,5 @@ import { invoke } from "@tauri-apps/api/core"; +import type { ExternalNoteReference } from "./editor/externalNote"; import type { AppConfig, CustomTheme, @@ -163,6 +164,16 @@ export async function saveNote( return invoke("save_note", { vaultPath, path, meta, body, expectedRaw }); } +export async function importNote( + vaultPath: string, + sourcePath: string, + notebookRelative: string | null, + expectedRaw: string, + references: ExternalNoteReference[], +): Promise { + return invoke("import_note", { vaultPath, sourcePath, notebookRelative, expectedRaw, references }); +} + export async function createNote( vaultPath: string, notebookRelative: string | null, diff --git a/src/lib/components/AppLayout.svelte b/src/lib/components/AppLayout.svelte index e7296a9..ea1131b 100644 --- a/src/lib/components/AppLayout.svelte +++ b/src/lib/components/AppLayout.svelte @@ -89,6 +89,13 @@ let sidebar = $state(); let noteList = $state(); let editor = $state(); + let outlineOpen = $state(false); + let outlinePanelId = $state(''); + + function handleOutlineChange(open: boolean, panelId: string) { + outlineOpen = open; + outlinePanelId = panelId; + } let pendingSearchReveal = $state<{ path: string; token: number } | null>(null); let unlistenFileChange: (() => void) | null = null; let unlistenClose: (() => void) | null = null; @@ -979,7 +986,9 @@ if (!ownerIsCurrent()) return; $sidebarWidth = state.sidebar_width; $notelistWidth = state.notelist_width; - if (typeof state.outline_width === 'number') $outlineWidth = state.outline_width; + if (typeof state.outline_width === 'number' && Number.isFinite(state.outline_width)) { + $outlineWidth = Math.max(160, Math.min(500, state.outline_width)); + } $sidebarCollapsed = state.sidebar_collapsed; $notelistCollapsed = state.notelist_collapsed ?? false; $collapsedNotebooks = state.collapsed_notebooks ?? []; @@ -1190,7 +1199,8 @@ - +
    + {#each outlineHeadings as h, i} +
  • + +
  • {/each} - +
{/if} - + {/if} {#if showInfo && $activeNote && !showHistory}
@@ -9591,6 +9529,7 @@ flex-direction: column; overflow: hidden; flex-shrink: 0; + min-height: 0; } .outline-header { @@ -9635,6 +9574,9 @@ .outline-list { flex: 1; + min-height: 0; + margin: 0; + list-style: none; overflow-y: auto; padding: 4px 0; } @@ -9649,9 +9591,8 @@ font-size: var(--editor-font-size, 14px); color: var(--text-secondary); cursor: pointer; - white-space: nowrap; - overflow: hidden; - text-overflow: ellipsis; + white-space: normal; + overflow-wrap: anywhere; line-height: 1.5; } @@ -9660,6 +9601,12 @@ color: var(--text-primary); } + .outline-item.current { + background: color-mix(in srgb, var(--accent) 12%, var(--bg-secondary)); + color: var(--accent); + box-shadow: inset 3px 0 var(--accent); + } + .outline-level-1 { padding-left: 14px; font-weight: 600; } .outline-level-2 { padding-left: 26px; font-weight: 500; } .outline-level-3 { padding-left: 38px; } @@ -9745,6 +9692,8 @@ font-size: 0.9em; } + :global(.tiptap-wrapper .tiptap :not(pre) > code) { white-space: pre-wrap; } + :global(.tiptap-wrapper .tiptap pre) { background: color-mix(in srgb, var(--accent) 8%, var(--bg-tertiary)); border-radius: 8px; @@ -12379,6 +12328,10 @@ border-left: none; } + .editor-container.mobile .outline-item { min-height: 44px; padding-top: 10px; padding-bottom: 10px; } + .editor-container.mobile .outline-close { min-width: 44px; min-height: 44px; justify-content: center; } + .editor-container.mobile :global(.resize-handle) { display: none; } + .editor-container.mobile .editor-body-row.reviewing-history { flex-direction: column-reverse; } .editor-container.mobile .history-panel { width: 100%; flex: none; border-left: none; border-bottom: 1px solid var(--border-light); } .editor-container.mobile .history-list { display: none; } diff --git a/src/lib/components/GraphView.svelte b/src/lib/components/GraphView.svelte index d9c2334..d112fc9 100644 --- a/src/lib/components/GraphView.svelte +++ b/src/lib/components/GraphView.svelte @@ -4,6 +4,7 @@ import { activeNotePath, appConfig } from '$lib/stores/app'; import { isMobile } from '$lib/platform'; import { applyGraphRepulsion } from '$lib/utils/graph-layout'; + import { buildGraphTopology, getGraphNeighborhood } from '$lib/utils/graph-topology'; let { onclose, onnavigate }: { onclose: () => void; @@ -26,6 +27,7 @@ let nodeIndexMap: Map = new Map(); let connectedSet: Set = new Set(); let nodeDegree: number[] = []; + let incidentEdges = $state.raw([]); let searchMatchSet = $state.raw | null>(null); let folderColorMap = new Map(); @@ -41,6 +43,8 @@ let hoveredNeighborSet: Set = new Set(); let hoveredEdgeSet: Set = new Set(); let activeNodeIdx = $state(-1); + // Svelte caches this until the active node or graph topology changes. + let localNeighborhood = $derived(getGraphNeighborhood(activeNodeIdx, edges, incidentEdges)); let navigatedFromGraph = false; // Physics runs in short yielded batches; the completed graph is static. @@ -49,6 +53,7 @@ const layoutChannel = new MessageChannel(); let layoutScheduled = false; let disposed = false; + let drawFrame: number | null = null; // DPI fix let dpr = 1; @@ -115,35 +120,9 @@ function updateHoveredState(node: GraphNode | null) { hoveredNode = node; hoveredIdx = node ? nodes.indexOf(node) : -1; - hoveredNeighborSet = new Set(); - hoveredEdgeSet = new Set(); - if (hoveredIdx < 0) return; - hoveredNeighborSet.add(hoveredIdx); - for (let i = 0; i < edges.length; i++) { - const e = edges[i]; - if (e.sourceIdx === hoveredIdx || e.targetIdx === hoveredIdx) { - hoveredEdgeSet.add(i); - hoveredNeighborSet.add(e.sourceIdx); - hoveredNeighborSet.add(e.targetIdx); - } - } - } - - // Nodes/edges visible in local mode (active note + direct neighbours) - function computeLocalSets(): { nodeSet: Set; edgeSet: Set } { - const nodeSet = new Set(); - const edgeSet = new Set(); - if (activeNodeIdx < 0) return { nodeSet, edgeSet }; - nodeSet.add(activeNodeIdx); - for (let i = 0; i < edges.length; i++) { - const e = edges[i]; - if (e.sourceIdx === activeNodeIdx || e.targetIdx === activeNodeIdx) { - edgeSet.add(i); - nodeSet.add(e.sourceIdx); - nodeSet.add(e.targetIdx); - } - } - return { nodeSet, edgeSet }; + const neighborhood = getGraphNeighborhood(hoveredIdx, edges, incidentEdges); + hoveredNeighborSet = neighborhood.nodeSet; + hoveredEdgeSet = neighborhood.edgeSet; } function updateSearchMatch() { @@ -175,15 +154,13 @@ }; }); - connectedSet = new Set(); - nodeDegree = new Array(nodes.length).fill(0); - edges = data.edges.map(e => { - connectedSet.add(e.source); - connectedSet.add(e.target); - nodeDegree[e.source] = (nodeDegree[e.source] || 0) + 1; - nodeDegree[e.target] = (nodeDegree[e.target] || 0) + 1; - return { sourceIdx: e.source, targetIdx: e.target, bidirectional: e.bidirectional }; - }); + edges = data.edges.map(e => ({ + sourceIdx: e.source, targetIdx: e.target, bidirectional: e.bidirectional, + })); + const topology = buildGraphTopology(nodes.length, edges); + connectedSet = topology.connectedSet; + nodeDegree = topology.nodeDegree; + incidentEdges = topology.incidentEdges; buildFolderColors(); updateSearchMatch(); @@ -196,15 +173,11 @@ function centerOnActiveNote() { if (!canvas || nodes.length === 0 || activeNodeIdx < 0) return; - const activeNode = nodes[activeNodeIdx]; - const neighborhood: GraphNode[] = [activeNode]; - for (const edge of edges) { - if (edge.sourceIdx === activeNodeIdx) neighborhood.push(nodes[edge.targetIdx]); - else if (edge.targetIdx === activeNodeIdx) neighborhood.push(nodes[edge.sourceIdx]); - } + const neighborhood = localNeighborhood.nodeSet; const w = canvasW, h = canvasH, padding = 100; let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity; - for (const n of neighborhood) { + for (const i of neighborhood) { + const n = nodes[i]; if (n.x < minX) minX = n.x; if (n.y < minY) minY = n.y; if (n.x > maxX) maxX = n.x; if (n.y > maxY) maxY = n.y; } @@ -230,7 +203,7 @@ function focusActive() { if (activeNodeIdx >= 0) centerOnActiveNote(); else fitToView(); - draw(); + requestDraw(); } function startSimulation() { @@ -239,7 +212,7 @@ physicsStep = null; if (physicsRemaining === 0) { loading = false; - draw(); + requestDraw(); } else { startLayoutLoop(); } @@ -264,7 +237,7 @@ if (physicsRemaining === 0) { loading = false; if (activeNodeIdx >= 0) centerOnActiveNote(); else fitToView(); - draw(); + requestDraw(); } else { layoutScheduled = true; layoutChannel.port2.postMessage(null); @@ -311,8 +284,16 @@ } } + function requestDraw() { + if (disposed || drawFrame !== null) return; + drawFrame = requestAnimationFrame(() => { + drawFrame = null; + draw(); + }); + } + function draw() { - if (!canvas || loading) return; + if (disposed || !canvas || loading) return; const ctx = canvas.getContext('2d'); if (!ctx) return; @@ -328,7 +309,8 @@ const isSearching = searchMatchSet !== null; // Local mode: restrict which nodes/edges are visible - const { nodeSet: localNodeSet, edgeSet: localEdgeSet } = localMode ? computeLocalSets() : { nodeSet: null as Set | null, edgeSet: null as Set | null }; + const localNodeSet = localMode ? localNeighborhood.nodeSet : null; + const localEdgeSet = localMode ? localNeighborhood.edgeSet : null; // ── Edges ────────────────────────────────────────────────────────────── if (isHovering) { @@ -594,17 +576,17 @@ dragging.x = (e.clientX - rect.left - pan.x) / zoom; dragging.y = (e.clientY - rect.top - pan.y) / zoom; dragging.vx = 0; dragging.vy = 0; - draw(); + requestDraw(); } else if (panning) { pan.x = e.clientX - panStart.x; pan.y = e.clientY - panStart.y; - draw(); + requestDraw(); } else if (!dragging) { const node = getNodeAt(e.clientX, e.clientY); if (node !== hoveredNode) { updateHoveredState(node); if (canvas) canvas.style.cursor = node ? 'pointer' : 'grab'; - if (!layoutScheduled) draw(); + if (!layoutScheduled) requestDraw(); } } } @@ -629,7 +611,7 @@ zoom = Math.max(0.2, Math.min(5, zoom * (e.deltaY > 0 ? 0.9 : 1.1))); pan.x = mx - (mx - pan.x) * (zoom / oldZoom); pan.y = my - (my - pan.y) * (zoom / oldZoom); - draw(); + requestDraw(); } function handleKeydown(e: KeyboardEvent) { @@ -668,11 +650,11 @@ dragging.x = (t.clientX - rect.left - pan.x) / zoom; dragging.y = (t.clientY - rect.top - pan.y) / zoom; dragging.vx = 0; dragging.vy = 0; - draw(); + requestDraw(); } else if (panning) { pan.x = t.clientX - panStart.x; pan.y = t.clientY - panStart.y; - draw(); + requestDraw(); } } else if (e.touches.length === 2) { const dx = e.touches[0].clientX - e.touches[1].clientX; @@ -687,7 +669,7 @@ zoom = Math.max(0.2, Math.min(5, zoom * (dist / lastTouchDist))); pan.x = mx - (mx - pan.x) * (zoom / oldZoom); pan.y = my - (my - pan.y) * (zoom / oldZoom); - draw(); + requestDraw(); } lastTouchDist = dist; } @@ -730,18 +712,20 @@ navigatedFromGraph = false; centerOnActiveNote(); } - if (!layoutScheduled && canvas) untrack(draw); + if (!layoutScheduled && canvas) untrack(requestDraw); }); // React to search query changes $effect(() => { const _q = searchQuery; // track updateSearchMatch(); - if (canvas && !layoutScheduled) untrack(draw); + if (canvas && !layoutScheduled) untrack(requestDraw); }); onDestroy(() => { disposed = true; + if (drawFrame !== null) cancelAnimationFrame(drawFrame); + drawFrame = null; layoutChannel.port1.onmessage = null; layoutChannel.port1.close(); layoutChannel.port2.close(); @@ -775,7 +759,7 @@
{#if !loading} {#if localMode && activeNodeIdx >= 0} - {computeLocalSets().nodeSet.size} notes · {computeLocalSets().edgeSet.size} links + {localNeighborhood.nodeSet.size} notes · {localNeighborhood.edgeSet.size} links {:else} {nodes.length} notes · {edges.length} links {/if} diff --git a/src/lib/components/NoteList.svelte b/src/lib/components/NoteList.svelte index f5bd740..5785fcd 100644 --- a/src/lib/components/NoteList.svelte +++ b/src/lib/components/NoteList.svelte @@ -631,12 +631,15 @@ $notes = [entry, ...$notes]; onNoteMoved(); if (!navigationIsCurrent(token)) return; + // The first save must use the exact file created on disk, not wait for a watcher echo. + const content = await readNote(ownerVault!, entry.path); + if (!navigationIsCurrent(token)) return; if (!(await onBeforeNoteSwitch())) return; if (!navigationIsCurrent(token)) return; - $activeNote = { path: entry.path, meta: entry.meta, content: '\n', raw: '' }; + $activeNote = content; $activeNotePath = entry.path; $editorDirty = false; - onNoteSelected(entry.path, '\n'); + onNoteSelected(entry.path, content.content); onNoteCreated(token); } catch (e) { console.error('Failed to create note:', e); @@ -1578,11 +1581,15 @@
{note.preview}
- {#if getNotebookPath(note)} - {getNotebookPath(note)} - {#if showDates}·{/if} + {#if getNotebookPath(note) || showDates} + + {#if getNotebookPath(note)} + {getNotebookPath(note)} + {#if showDates}·{/if} + {/if} + {#if showDates}{formatRelativeTime($sortMode === 'created' ? note.meta.created : note.meta.modified)}{/if} + {/if} - {#if showDates}{formatRelativeTime($sortMode === 'created' ? note.meta.created : note.meta.modified)}{/if} {#if note.meta.tags.length > 0} {#each note.meta.tags.slice(0, 3) as tag, i (tagIterationKey(tag, i))} @@ -2199,9 +2206,18 @@ .note-meta { display: flex; + flex-wrap: wrap; + align-items: center; + gap: 4px 8px; + margin-top: 4px; + } + + .note-context { + display: inline-flex; align-items: center; gap: 8px; - margin-top: 4px; + min-width: 0; + max-width: 100%; } .note-notebook { @@ -2216,6 +2232,7 @@ .note-meta-sep { color: var(--text-tertiary); font-size: 0.78em; + flex-shrink: 0; } .note-compact-row .note-notebook { @@ -2227,10 +2244,15 @@ .note-date { font-size: 0.78em; color: var(--text-tertiary); + flex-shrink: 0; + white-space: nowrap; } .note-tags { display: flex; + flex-wrap: wrap; + min-width: 0; + max-width: 100%; gap: 4px; } diff --git a/src/lib/components/ResizeHandle.svelte b/src/lib/components/ResizeHandle.svelte index 15e43ec..3f683b3 100644 --- a/src/lib/components/ResizeHandle.svelte +++ b/src/lib/components/ResizeHandle.svelte @@ -1,5 +1,14 @@ - -
+ + +
diff --git a/src/lib/components/TasksView.svelte b/src/lib/components/TasksView.svelte index b1e5803..bf75c53 100644 --- a/src/lib/components/TasksView.svelte +++ b/src/lib/components/TasksView.svelte @@ -120,10 +120,10 @@ const prioRank: Record = { high: 0, med: 1, low: 2 }; function prank(p: string | null) { return p ? (prioRank[p] ?? 3) : 3; } // Free-text task filter: matches the task text or its note title (case-insensitive). + const normalizedTaskQuery = $derived(taskQuery.trim().toLowerCase()); function matchesQuery(t: TaskItem): boolean { - const q = taskQuery.trim().toLowerCase(); - if (!q) return true; - return t.text.toLowerCase().includes(q) || t.note_title.toLowerCase().includes(q); + if (!normalizedTaskQuery) return true; + return t.text.toLowerCase().includes(normalizedTaskQuery) || t.note_title.toLowerCase().includes(normalizedTaskQuery); } const matchingTasks = $derived(tasks.filter((t) => diff --git a/src/lib/editor/externalNote.ts b/src/lib/editor/externalNote.ts new file mode 100644 index 0000000..838a826 --- /dev/null +++ b/src/lib/editor/externalNote.ts @@ -0,0 +1,107 @@ +import { markdownLanguage } from '@codemirror/lang-markdown'; +import { splitSnapshot } from '../utils/history-comparison.ts'; +import { createEditorMarkdownParser, displayMathRanges, inlineMathRanges, legacyLinkRanges } from './markdownParser.ts'; + +export interface ExternalNoteReference { + from: number; + to: number; + href: string; + html: boolean; + pdf?: boolean; +} + +const markdown = createEditorMarkdownParser(); + +/** Byte ranges for local attachment destinations, without reserializing the user's Markdown. */ +export function externalNoteReferences(raw: string): ExternalNoteReference[] { + const { body, metadata } = splitSnapshot(raw); + const spans: ExternalNoteReference[] = []; + const seen = new Set(); + const legacy = legacyLinkRanges(body); + const legacyByStart = new Map(legacy.map(range => [range.from, range])); + const math = [...displayMathRanges(body), ...inlineMathRanges(body)]; + const environment: { references?: Record } = {}; + markdown.parse(body, environment); + const tree = markdownLanguage.parser.parse(body); + const usedLabels = new Set(); + const label = (node: { from: number; to: number }) => markdown.utils.normalizeReference(body.slice(node.from + 1, node.to - 1)); + tree.iterate({ enter(node) { + if (!['Link', 'Image'].includes(node.name) || node.node.getChild('URL') || body[node.to] === '(' || math.some(range => node.from >= range.from && node.from < range.to)) return; + for (let parent = node.node.parent; parent; parent = parent.parent) if (parent.name === 'Image') return false; + const explicit = node.node.getChild('LinkLabel'); + let key = explicit ? label(explicit) : ''; + if (!key) { + const closing = node.node.getChildren('LinkMark').find(mark => body.slice(mark.from, mark.to) === ']'); + if (closing) key = markdown.utils.normalizeReference(body.slice(node.from + (node.name === 'Image' ? 2 : 1), closing.from)); + } + if (environment.references?.[key]) usedLabels.add(key); + } }); + const selectedDefinitions = new Set(); + const add = (from: number, to: number, html = false, pdf = false) => { + if (math.some(range => from >= range.from && from < range.to)) return; + const href = markdown.utils.unescapeAll(body.slice(from, to)); + let path = href.split(/[?#]/, 1)[0]; + try { path = decodeURIComponent(path); } catch { /* Native validation handles malformed escapes. */ } + if (!path || /^[a-z][a-z\d+.-]*:/i.test(path) || /^[\\/]/.test(path) || /\.md$/i.test(path)) return; + const key = `${from}:${to}`; + if (seen.has(key)) return; + seen.add(key); + spans.push({ from: metadata.length + from, to: metadata.length + to, href, html, pdf }); + }; + tree.iterate({ + enter(node) { + // Links and HTML inside image alt text are labels, not additional attachments. + let direct = true; + for (let ancestor = node.node.parent; ancestor; ancestor = ancestor.parent) { + if (ancestor.name === 'Image' && !(node.name === 'URL' && direct)) return false; + direct = false; + } + if (node.name === 'URL' && ['Image', 'Link', 'LinkReference'].includes(node.node.parent?.name ?? '')) { + const angle = body[node.from] === '<' && body[node.to - 1] === '>'; + const from = node.from + (angle ? 1 : 0); + if (node.node.parent?.name === 'LinkReference') { + const definition = node.node.parent.getChild('LinkLabel'); + if (!definition) return; + const key = label(definition); + const href = markdown.normalizeLink(markdown.utils.unescapeAll(body.slice(from, node.to - (angle ? 1 : 0)))); + if (!usedLabels.has(key) || selectedDefinitions.has(key) || environment.references?.[key]?.href !== href) return; + selectedDefinitions.add(key); + } + const compatible = legacyByStart.get(from); + add(from, compatible?.to ?? node.to - (angle ? 1 : 0)); + } else if (node.name === 'Paragraph' || /Heading[1-6]$/.test(node.name)) { + for (const range of legacy) { + if (range.from >= node.from && range.to <= node.to) add(range.from, range.to); + } + } else if (node.name === 'HTMLTag' || node.name === 'HTMLBlock') { + const html = body.slice(node.from, node.to); + // Skip comments and literal HTML code, not just Markdown fences. + const tags = /|$)|<(script|style|textarea|pre|code)\b[^>]*>[\s\S]*?(?:<\/\1\s*>|$)|<(img|a|div)\b(?:"[^"]*"|'[^']*'|[^'">])*>/gi; + for (const tag of html.matchAll(tags)) { + if (!tag[2]) continue; + const attributes = /(?:^|\s)([\w:-]+)\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/gi; + for (const attribute of tag[0].matchAll(attributes)) { + const name = attribute[1].toLowerCase(); + const element = tag[2].toLowerCase(); + if (!(element === 'img' && name === 'src') && !(element === 'a' && name === 'href') && !(element === 'div' && name === 'data-pdf-src')) continue; + const value = attribute[2] ?? attribute[3] ?? attribute[4]; + const quoted = attribute[2] !== undefined || attribute[3] !== undefined; + const start = node.from + tag.index! + attribute.index! + attribute[0].length - value.length - (quoted ? 1 : 0); + add(start, start + value.length, true, name === 'data-pdf-src'); + } + } + } + }, + }); + spans.sort((a, b) => a.from - b.from); + const encoder = new TextEncoder(); + let previous = 0; + let bytes = 0; + return spans.map(span => { + bytes += encoder.encode(raw.slice(previous, span.from)).length; + const from = bytes; + bytes += encoder.encode(raw.slice(span.from, span.to)).length; + previous = span.to; + return { ...span, from, to: bytes }; + }); +} diff --git a/src/lib/editor/markdown.ts b/src/lib/editor/markdown.ts index b3c52f9..04ce0d4 100644 --- a/src/lib/editor/markdown.ts +++ b/src/lib/editor/markdown.ts @@ -11,6 +11,34 @@ type MarkdownMark = { const identityAssetSource: AssetSourceNormalizer = (source) => source; +function backtickDelimiter(text: string, minimum: number): string { + let length = minimum; + for (const match of text.matchAll(/`+/g)) { + length = Math.max(length, match[0].length + 1); + } + return '`'.repeat(length); +} + +export function serializeCodeBlock(code: string, language: string = ''): string { + const fence = backtickDelimiter(code, 3); + return `${fence}${language}\n${code}\n${fence}\n`; +} + +function serializeCodeSpan(text: string): string { + const delimiter = backtickDelimiter(text, 1); + const needsPadding = text.startsWith('`') || text.endsWith('`') + || ((text.startsWith(' ') || text.endsWith(' ')) && /[^ ]/.test(text)); + const padding = needsPadding ? ' ' : ''; + return `${delimiter}${padding}${text}${padding}${delimiter}`; +} + +function serializeInlineMath(tex: string, forceEncoded: boolean): string { + if (forceEncoded || !tex || /[\r\n$]/.test(tex) || /^\s|\s$/.test(tex) || /(?:^|[^\\])(?:\\\\)*\\$/.test(tex)) { + return ``; + } + return `$${tex}$`; +} + function stringAttr(mark: Mark, name: string): string { const value = mark.attrs?.[name]; return typeof value === 'string' ? value : ''; @@ -52,8 +80,6 @@ function markdownMark(mark: Mark, text: string): MarkdownMark | null { } : null; } - case 'code': - return { key: 'code', priority: 90, open: '`', close: '`' }; case 'link': { const href = stringAttr(mark, 'href'); return { @@ -78,8 +104,9 @@ function markdownMark(mark: Mark, text: string): MarkdownMark | null { } } -function marksForText(node: ProseMirrorNode): MarkdownMark[] { +function marksForText(node: ProseMirrorNode, code = false): MarkdownMark[] { return node.marks + .filter((mark) => !code || mark.type.name === 'link' || mark.type.name === 'wikiLink') .map((mark) => markdownMark(mark, node.text || '')) .filter((mark): mark is MarkdownMark => mark !== null) .sort((left, right) => left.priority - right.priority || left.key.localeCompare(right.key)); @@ -110,33 +137,55 @@ export function serializeInlineMarkdown( activeMarks = nextMarks; }; - node.forEach((child, _offset, index) => { + for (let index = 0; index < node.childCount; index += 1) { + const child = node.child(index); if (child.isText) { - transitionMarks(marksForText(child)); + const isCode = child.marks.some((mark) => mark.type.name === 'code'); + const marks = marksForText(child, isCode); + transitionMarks(marks); let text = child.text || ''; - if (index === 0) { - text = text.replace(/^[\t\u2003]+/, (whitespace) => ' '.repeat(whitespace.length)); + if (isCode) { + while (index + 1 < node.childCount) { + const next = node.child(index + 1); + if (!next.isText || !next.marks.some((mark) => mark.type.name === 'code')) break; + const nextMarks = marksForText(next, true); + if (marks.length !== nextMarks.length || commonMarkCount(marks, nextMarks) !== marks.length) break; + text += next.text || ''; + index += 1; + } + result += serializeCodeSpan(text); + } else { + if (index === 0) { + text = text.replace(/^[\t\u2003]+/, (whitespace) => ' '.repeat(whitespace.length)); + } + text = text.replace(/\\*\$/g, (match) => '\\'.repeat((match.length - 1) * 2 + 1) + '$'); + if (index + 1 < node.childCount && node.child(index + 1).type.name === 'mathInline') { + text = text.replace(/\\+$/, (backslashes) => backslashes + backslashes); + } + result += text; } - result += text; - return; + continue; } transitionMarks([]); - if (child.type.name === 'image') { const source = normalizeAssetSource(child.attrs.src || ''); - if (!source) return; + if (!source) continue; const alt = child.attrs.alt || ''; const size = child.attrs['data-size'] || child.attrs.size || 'full'; const sizeSuffix = size && size !== 'full' ? `|size=${size}` : ''; if (result) result += '\n'; result += `![${alt}${sizeSuffix}](${source})`; } else if (child.type.name === 'mathInline') { - result += `$${child.attrs.tex || ''}$`; + const previous = index > 0 ? node.child(index - 1) : null; + const next = index + 1 < node.childCount ? node.child(index + 1) : null; + const forceEncoded = previous?.type.name === 'mathInline' || next?.type.name === 'mathInline' + || /\$$/.test(previous?.text || '') || /^[\d$]/.test(next?.text || ''); + result += serializeInlineMath(child.attrs.tex || '', forceEncoded); } else if (child.type.name === 'hardBreak') { result += ' \n'; } - }); + } transitionMarks([]); return result; diff --git a/src/lib/editor/markdownParser.ts b/src/lib/editor/markdownParser.ts new file mode 100644 index 0000000..bc46b5a --- /dev/null +++ b/src/lib/editor/markdownParser.ts @@ -0,0 +1,350 @@ +import MarkdownIt from 'markdown-it'; +import type Token from 'markdown-it/lib/token.mjs'; +import type StateBlock from 'markdown-it/lib/rules_block/state_block.mjs'; +import type StateInline from 'markdown-it/lib/rules_inline/state_inline.mjs'; +import fenceRule from 'markdown-it/lib/rules_block/fence.mjs'; +import markdownItMark from 'markdown-it-mark'; +import markdownItSup from 'markdown-it-sup'; +import markdownItSub from 'markdown-it-sub'; + +interface WikiTitle { path: string; title: string } +export interface MarkdownEnvironment { wikiLinkTitles?: readonly WikiTitle[] } +const wikiIndexKey = Symbol('wiki-index'); +type WikiIndex = { paths: Map; titles: Map }; + +function displayMath(state: StateBlock, startLine: number, endLine: number, silent: boolean): boolean { + const start = state.bMarks[startLine] + state.tShift[startLine]; + if (state.src.slice(start, state.eMarks[startLine]).trim() !== '$$') return false; + let closing = startLine + 1; + for (; closing < endLine; closing++) { + if (state.sCount[closing] < state.blkIndent && !state.isEmpty(closing)) return false; + const from = state.bMarks[closing] + state.tShift[closing]; + if (state.src.slice(from, state.eMarks[closing]).trim() === '$$') break; + } + if (closing === endLine) return false; + if (silent) return true; + const token = state.push('helix_math_block', 'div', 0); + token.block = true; + token.content = state.getLines(startLine + 1, closing, state.blkIndent, false).trim(); + token.map = [startLine, closing + 1]; + token.meta = { sourceStart: start }; + token.attrSet('data-math-block', encodeURIComponent(token.content)); + token.attrSet('class', 'math-block'); + state.line = closing + 1; + return true; +} + +function inlineMath(state: StateInline, silent: boolean): boolean { + const start = state.pos; + if (state.src[start] !== '$' || state.src[start - 1] === '$' || + start + 1 >= state.posMax || /[\s$]/u.test(state.src[start + 1])) return false; + let end = start + 1; + for (; end < state.posMax; end++) { + const char = state.src[end]; + if (char === '\n') return false; + if (char === '\\') { end++; continue; } + if (char === '$') break; + } + if (end >= state.posMax || end === start + 1 || /\s/u.test(state.src[end - 1]) || + state.src[end + 1] === '$' || /[0-9]/.test(state.src[end + 1] ?? '')) return false; + if (!silent) { + const token = state.push('helix_math_inline', 'span', 0); + token.content = state.src.slice(start + 1, end).trim(); + token.meta = { from: start, to: end + 1 }; + token.attrSet('data-math-inline', encodeURIComponent(token.content)); + token.attrSet('class', 'math-inline'); + } + state.pos = end + 1; + return true; +} + +// markdown-it trims two spaces even when a code span contains only spaces. +// Preserve CommonMark's all-space exception without replacing its normal backtick parser. +function spaceCode(state: StateInline, silent: boolean): boolean { + if (state.src[state.pos] !== '`') return false; + let content = state.pos; + while (content < state.posMax && state.src[content] === '`') content++; + const length = content - state.pos; + let closing = content; + while (closing < state.posMax && (state.src[closing] === ' ' || state.src[closing] === '\n')) closing++; + if (closing === content) return false; + let end = closing; + while (end < state.posMax && state.src[end] === '`') end++; + if (end - closing !== length) return false; + if (!silent) { + const token = state.push('code_inline', 'code', 0); + token.markup = state.src.slice(state.pos, content); + token.content = state.src.slice(content, closing).replace(/\n/g, ' '); + } + state.pos = end; + return true; +} + +function installMath(md: MarkdownIt) { + md.block.ruler.before('fence', 'helix_math_block', displayMath, { alt: ['paragraph', 'reference', 'blockquote', 'list'] }); + md.inline.ruler.after('backticks', 'helix_math_inline', inlineMath); + md.renderer.rules.helix_math_block = (tokens, index, _options, _env, renderer) => + `
\n`; + md.renderer.rules.helix_math_inline = (tokens, index, _options, _env, renderer) => + ``; +} + +function wikiLink(state: StateInline, silent: boolean): boolean { + const notes: readonly WikiTitle[] | undefined = state.env.wikiLinkTitles; + if (!notes || state.src.slice(state.pos, state.pos + 2) !== '[[') return false; + const end = state.src.indexOf(']]', state.pos + 2); + if (end < 0 || end + 2 > state.posMax) return false; + const raw = state.src.slice(state.pos + 2, end); + if (!raw || raw.includes(']')) return false; + if (!silent) { + let index: WikiIndex | undefined = state.env[wikiIndexKey]; + if (!index) { + index = { paths: new Map(), titles: new Map() }; + for (const note of notes) { + if (!index.paths.has(note.path)) index.paths.set(note.path, note.path); + const title = note.title.toLowerCase(); + let depth = 0; + for (let i = 0; i < note.path.length; i++) if (note.path[i] === '/') depth++; + const previous = index.titles.get(title); + if (!previous || depth < previous.depth) index.titles.set(title, { path: note.path, depth }); + } + state.env[wikiIndexKey] = index; + } + const pipe = raw.indexOf('|'); + const reference = (pipe < 0 ? raw : raw.slice(0, pipe)).trim(); + const display = (pipe < 0 ? reference : raw.slice(pipe + 1)).trim(); + const title = reference.replace(/#.*$/, '').replace(/\^.*$/, '').trim(); + const basename = title.includes('/') ? title.slice(title.lastIndexOf('/') + 1) : title; + const path = (title.includes('/') ? index.paths.get(title + '.md') : undefined) + ?? index.titles.get(basename.toLowerCase())?.path ?? ''; + const token = state.push('helix_wiki', 'span', 0); + token.attrSet('data-wiki-link', ''); + token.attrSet('data-path', path); + token.attrSet('data-title', reference); + token.attrSet('class', 'wiki-link'); + token.content = display; + } + state.pos = end + 2; + return true; +} + +/** Preserve legacy space-containing and %20/title destinations only in real link syntax. */ +function legacyLink(state: StateInline, silent: boolean): boolean { + const start = state.pos; + const image = state.src[start] === '!' && state.src[start + 1] === '['; + const labelStart = start + (image ? 1 : 0); + if (state.src[labelStart] !== '[') return false; + const labelEnd = state.md.helpers.parseLinkLabel(state, labelStart, !image); + if (labelEnd < 0 || state.src[labelEnd + 1] !== '(') return false; + const destinationStart = labelEnd + 2; + let end = destinationStart; + let depth = 0; + let quote = ''; + let angle = false; + for (; end < state.posMax; end++) { + const char = state.src[end]; + if (char === '\n') return false; + if (char === '\\') { end++; continue; } + if (quote) { if (char === quote) quote = ''; continue; } + if (angle) { if (char === '>') angle = false; continue; } + if (char === '<') { angle = true; continue; } + if ((char === '"' || char === "'") && + (/\s/u.test(state.src[end - 1]) || state.src.slice(end - 3, end) === '%20')) { quote = char; continue; } + if (char === '(') depth++; + else if (char === ')') { if (!depth) break; depth--; } + } + if (end >= state.posMax) return false; + const raw = state.src.slice(destinationStart, end); + // Native CommonMark links, including nested destinations, retain their normal parser. + if (!/\s|%20["'(]/u.test(raw)) return false; + let destination = raw.trim(); + if (destination.startsWith('<')) { + const angle = destination.match(/^<([^>]*)>(?:(?:\s|%20)*(?:"[^"]*"|'[^']*'|\([^)]*\)))?(?:\s|%20)*$/u); + if (!angle) return false; + destination = angle[1]; + } else { + destination = destination.replace(/(?:\s|%20)+(?:"[^"]*"|'[^']*')(?:\s|%20)*$/u, '').trim(); + } + const href = state.md.normalizeLink(destination.replace(/ /g, '%20')); + if (!state.md.validateLink(href)) return false; + if (!silent) { + const label = state.src.slice(labelStart + 1, labelEnd); + const from = destinationStart + raw.indexOf(destination); + const sourceHref = { from, to: from + destination.length }; + if (image) { + const children: Token[] = []; + state.md.inline.parse(label, state.md, state.env, children); + const token = state.push('image', 'img', 0); + token.meta = { sourceHref }; + token.attrSet('src', href.replace(/^\/{2,}(?=home\/)/u, '/')); + token.attrSet('alt', ''); + token.children = children; + token.content = label; + } else { + const token = state.push('link_open', 'a', 1); + token.meta = { sourceHref }; + token.attrSet('href', href); + const oldMax = state.posMax; + state.pos = labelStart + 1; + state.posMax = labelEnd; + const linkState = state as StateInline & { linkLevel: number }; + linkState.linkLevel++; + state.md.inline.tokenize(state); + linkState.linkLevel--; + state.push('link_close', 'a', -1); + state.posMax = oldMax; + } + } + state.pos = end + 1; + return true; +} + +function emptyParagraph(state: StateBlock, startLine: number, _endLine: number, silent: boolean): boolean { + const from = state.bMarks[startLine] + state.tShift[startLine]; + if (state.src.slice(from, state.eMarks[startLine]).trim() !== '') return false; + if (silent) return true; + const token = state.push('helix_empty', 'p', 0); + token.block = true; + token.map = [startLine, startLine + 1]; + state.line = startLine + 1; + return true; +} + +export function createEditorMarkdownParser(): MarkdownIt { + const md = MarkdownIt({ html: true, linkify: false, breaks: false }) + .use(markdownItMark).use(markdownItSup).use(markdownItSub); + // Existing editor convention: tab-indented prose does not become an indented code block. + md.disable('code'); + installMath(md); + md.inline.ruler.before('link', 'helix_wiki', wikiLink); + md.inline.ruler.before('backticks', 'helix_space_code', spaceCode); + md.inline.ruler.before('link', 'helix_legacy_link', legacyLink); + md.block.ruler.before('html_block', 'helix_empty', emptyParagraph, { alt: ['paragraph', 'reference', 'blockquote', 'list'] }); + md.renderer.rules.helix_empty = () => '

\n'; + md.renderer.rules.helix_wiki = (tokens, index, _options, _env, renderer) => + `${md.utils.escapeHtml(tokens[index].content)}`; + md.core.ruler.before('block', 'helix_wiki_index', state => { delete state.env[wikiIndexKey]; }); + + md.core.ruler.before('inline', 'helix_tasks', state => { + const lists: { open: Token; firstItem: boolean; item: Token | null; firstParagraph: boolean }[] = []; + for (const token of state.tokens) { + if (token.type === 'bullet_list_open' || token.type === 'ordered_list_open') { + lists.push({ open: token, firstItem: true, item: null, firstParagraph: true }); + } else if (token.type === 'bullet_list_close' || token.type === 'ordered_list_close') lists.pop(); + else if (token.type === 'list_item_open' && lists.length) { + const list = lists[lists.length - 1]; + list.item = token; + list.firstParagraph = true; + } else if (token.type === 'list_item_close' && lists.length) { + const list = lists[lists.length - 1]; + list.item = null; + list.firstItem = false; + } else if (token.type === 'inline' && lists.length) { + const list = lists[lists.length - 1]; + if (!list.item || !list.firstParagraph || token.level !== list.item.level + 2) continue; + list.firstParagraph = false; + const checkbox = token.content.match(/^\[(x| )\](?:[^\S\n]+|$)/u); + if (!checkbox) continue; + token.content = token.content.slice(checkbox[0].length); + list.item.attrSet('data-type', 'taskItem'); + list.item.attrSet('data-checked', checkbox[1] === 'x' ? 'true' : 'false'); + if (list.firstItem && list.open.type === 'bullet_list_open') list.open.attrSet('data-type', 'taskList'); + } + } + }); + + md.core.ruler.after('inline', 'helix_images', state => { + let paragraph: Token | null = null; + let lines: string[] | undefined; + for (const token of state.tokens) { + if (token.type === 'paragraph_open' || token.type === 'heading_open') paragraph = token; + else if (token.type === 'paragraph_close' || token.type === 'heading_close') paragraph = null; + if (token.type !== 'inline') continue; + for (const child of token.children ?? []) { + // ProseMirror's DOM parser otherwise collapses or drops whitespace-only code spans. + if (child.type === 'code_inline') { + child.attrSet('style', 'white-space: pre-wrap'); + paragraph?.attrSet('data-helix-code', ''); + } + if (child.type === 'image') { + const src = child.attrGet('src'); + if (src?.startsWith('//')) child.attrSet('src', src.replace(/^\/{2,}(?=home\/)/u, '/')); + } + if (child.type === 'image' || child.type === 'link_open') { + child.attrs = child.attrs?.filter(([name]) => name !== 'title') ?? null; + } + } + if (!paragraph || paragraph.type !== 'paragraph_open' || paragraph.level !== 0 || token.children?.length !== 1 || + token.children[0].type !== 'image' || !token.map || token.map[0] < 2) continue; + lines ??= state.src.split('\n'); + const line = token.map[0]; + if (lines[line].startsWith('![') && lines[line - 1].trim() === '') paragraph.meta = { helixImageGap: true }; + } + }); + md.renderer.rules.paragraph_open = (tokens, index, options, _env, renderer) => + (tokens[index].meta?.helixImageGap ? '

\n' : '') + renderer.renderToken(tokens, index, options); + + md.block.ruler.at('fence', (state, startLine, endLine, silent) => { + if (!fenceRule(state, startLine, endLine, silent)) return false; + if (silent) return true; + const token = state.tokens[state.tokens.length - 1]; + if (token.info.trim() === 'helix-secret' && token.map) { + const line = token.map[1] - 1; + const closing = state.src.slice(state.bMarks[line] + state.tShift[line], state.eMarks[line]); + let length = 0; + while (closing[length] === token.markup[0]) length++; + const indentation = state.sCount[line] - state.blkIndent; + token.meta = { helixSecretClosed: indentation >= 0 && indentation < 4 && + length >= token.markup.length && /^[ \t]*$/u.test(closing.slice(length)) }; + } + return true; + }, { alt: ['paragraph', 'reference', 'blockquote', 'list'] }); + const renderFence = md.renderer.rules.fence!; + md.renderer.rules.fence = (tokens, index, options, env, renderer) => { + const token = tokens[index]; + if (token.info.trim() === 'helix-secret' && token.meta?.helixSecretClosed) { + return `
\n`; + } + // Only the final structural LF is parser-added; leading and extra trailing blank lines are literal. + return renderFence(tokens, index, options, env, renderer).replace(/\n<\/code>/u, ''); + }; + md.renderer.rules.softbreak = () => ' '; + md.renderer.rules.hardbreak = () => '
'; + return md; +} + +const mathBlocks = MarkdownIt({ html: true }); +installMath(mathBlocks); +mathBlocks.inline.ruler.before('link', 'helix_legacy_link', legacyLink); + +/** Source destinations for the editor's legacy space/title link compatibility. */ +export function legacyLinkRanges(source: string): { from: number; to: number }[] { + if (!source.includes('](')) return []; + const tokens: Token[] = []; + mathBlocks.inline.parse(source, mathBlocks, {}, tokens); + return tokens.filter(token => token.meta?.sourceHref).map(token => token.meta.sourceHref); +} + +/** Inline math coordinates use the same delimiter and escape rules as rendering. */ +export function inlineMathRanges(source: string): { from: number; to: number }[] { + if (!source.includes('$')) return []; + const tokens: Token[] = []; + mathBlocks.inline.parse(source, mathBlocks, {}, tokens); + return tokens.filter(token => token.type === 'helix_math_inline').map(token => token.meta); +} + +/** Source coordinates of semantic display-math blocks, including nested containers. */ +export function displayMathRanges(source: string): { from: number; to: number; prefix: string }[] { + if (!source.includes('$$')) return []; + const tokens: Token[] = []; + mathBlocks.block.parse(source, mathBlocks, {}, tokens); + const ranges = tokens.filter(token => token.type === 'helix_math_block' && token.map); + if (!ranges.length) return []; + const starts = [0]; + for (let i = 0; i < source.length; i++) if (source[i] === '\n') starts.push(i + 1); + return ranges.map(token => { + const from = starts[token.map![0]]; + return { from, to: starts[token.map![1]] ?? source.length, + prefix: source.slice(from, token.meta.sourceStart) }; + }); +} diff --git a/src/lib/editor/markdownRules.d.ts b/src/lib/editor/markdownRules.d.ts new file mode 100644 index 0000000..8e346e1 --- /dev/null +++ b/src/lib/editor/markdownRules.d.ts @@ -0,0 +1,5 @@ +declare module 'markdown-it/lib/rules_block/fence.mjs' { + import type StateBlock from 'markdown-it/lib/rules_block/state_block.mjs'; + const fence: (state: StateBlock, startLine: number, endLine: number, silent: boolean) => boolean; + export default fence; +} diff --git a/src/lib/editor/outline.ts b/src/lib/editor/outline.ts new file mode 100644 index 0000000..3142e04 --- /dev/null +++ b/src/lib/editor/outline.ts @@ -0,0 +1,129 @@ +import { markdownLanguage } from '@codemirror/lang-markdown'; +import { MapMode, type ChangeDesc, type EditorState } from '@codemirror/state'; +import type { Node as ProseMirrorNode } from '@tiptap/pm/model'; +import type { Mapping } from '@tiptap/pm/transform'; +import MarkdownIt from 'markdown-it'; +import type Token from 'markdown-it/lib/token.mjs'; +import markdownItMark from 'markdown-it-mark'; +import markdownItSub from 'markdown-it-sub'; +import markdownItSup from 'markdown-it-sup'; +import { displayMathRanges } from './markdownParser.ts'; + +export interface OutlineHeading { + level: number; + text: string; + pos: number; + end: number; +} + +const inlineMarkdown = MarkdownIt({ html: true, linkify: false, breaks: false }) + .use(markdownItMark) + .use(markdownItSub) + .use(markdownItSup); + +function inlineText(tokens: readonly Token[]): string { + let text = ''; + for (const token of tokens) { + if (token.children) text += inlineText(token.children); + else if (token.type === 'text' || token.type === 'code_inline') text += token.content; + else if (token.type === 'softbreak' || token.type === 'hardbreak' || + (token.type === 'html_inline' && /^$/i.test(token.content))) text += ' '; + } + return text; +} + +export function documentHeadings(doc: ProseMirrorNode): OutlineHeading[] { + const headings: OutlineHeading[] = []; + doc.descendants((node, pos) => { + if (node.type.name !== 'heading') return; + headings.push({ level: node.attrs.level, text: node.textContent, pos, end: pos + node.nodeSize }); + return false; + }); + return headings; +} + +/** Match the rich editor's closed $$ blocks without shifting source coordinates. */ +function maskDisplayMath(markdown: string): string { + const ranges = displayMathRanges(markdown); + if (!ranges.length) return markdown; + const parts: string[] = []; + let copied = 0; + for (const { from, to, prefix } of ranges) { + parts.push(markdown.slice(copied, from), prefix, markdown.slice(from + prefix.length, to).replace(/[^\n]/g, ' ')); + copied = to; + } + parts.push(markdown.slice(copied)); + return parts.join(''); +} + +/** Markdown headings only: raw HTML heading blocks are not interpreted as headings. */ +export function sourceHeadings(state: EditorState): OutlineHeading[] { + const headings: OutlineHeading[] = []; + // Parse the complete document, independent of CodeMirror's viewport/time-limited syntax tree. + const markdown = state.doc.toString(); + const parsedMarkdown = maskDisplayMath(markdown); + const tree = markdownLanguage.parser.parse(parsedMarkdown); + const environment = {}; + // Resolve reference labels only when the document can contain link definitions. + if (parsedMarkdown.includes(']:')) inlineMarkdown.parse(parsedMarkdown, environment); + tree.iterate({ + enter(node) { + if (!/^(?:ATX|Setext)Heading[1-6]$/.test(node.name)) return; + const setext = node.name.startsWith('SetextHeading'); + const cursor = node.node.cursor(); + let from = node.from; + let label = ''; + do { + if (cursor.name !== 'HeaderMark' && cursor.name !== 'QuoteMark' && cursor.name !== 'ListMark') continue; + // Setext's underline and its enclosing quote/list prefix are not label content. + const to = setext && cursor.name === 'HeaderMark' + ? state.doc.lineAt(cursor.from).from : cursor.from; + label += state.sliceDoc(from, Math.max(from, to)); + from = cursor.to; + } while (cursor.next() && cursor.from < node.to); + if (!setext) label += state.sliceDoc(from, node.to); + headings.push({ + level: Number(node.name.at(-1)), + text: inlineText(inlineMarkdown.parseInline(label, environment)).replace(/\s+/gu, ' ').trim(), + pos: node.from, + end: node.to, + }); + return false; + }, + }); + return headings; +} + +/** Headings are in document order; body content remains associated with the preceding heading. */ +export function activeHeadingIndex(headings: readonly OutlineHeading[], pos: number): number { + let low = 0; + let high = headings.length; + while (low < high) { + const middle = (low + high) >>> 1; + if (headings[middle].pos <= pos) low = middle + 1; + else high = middle; + } + return low - 1; +} + +/** Temporary anchors only; validate against fresh extraction before navigating. */ +export function mapSourceHeadings(headings: readonly OutlineHeading[], changes: ChangeDesc): OutlineHeading[] { + const mapped: OutlineHeading[] = []; + for (const heading of headings) { + const pos = changes.mapPos(heading.pos, 1, MapMode.TrackAfter); + const end = changes.mapPos(heading.end, -1); + if (pos !== null && pos < end) mapped.push({ ...heading, pos, end }); + } + return mapped; +} + +/** A replaced opening token no longer identifies the original semantic heading. */ +export function mapDocumentHeadings(headings: readonly OutlineHeading[], mapping: Mapping): OutlineHeading[] { + const mapped: OutlineHeading[] = []; + for (const heading of headings) { + const start = mapping.mapResult(heading.pos, 1); + const end = mapping.map(heading.end, -1); + if (!start.deleted && start.pos < end) mapped.push({ ...heading, pos: start.pos, end }); + } + return mapped; +} diff --git a/src/lib/editor/source/controller.ts b/src/lib/editor/source/controller.ts index 712e23f..0022fdb 100644 --- a/src/lib/editor/source/controller.ts +++ b/src/lib/editor/source/controller.ts @@ -77,6 +77,7 @@ export class SourceController { constructor(onChange: (changes: ChangeDesc, edited: boolean) => void = () => {}, events: { paste?: (event: ClipboardEvent) => boolean; drop?: (event: DragEvent) => boolean; + scroll?: () => void; } = {}) { this.onChange = onChange; this.extensions = [ @@ -95,6 +96,7 @@ export class SourceController { }, paste: (event, view) => !view.state.readOnly && (events.paste?.(event) ?? false), drop: (event, view) => !view.state.readOnly && (events.drop?.(event) ?? false), + scroll: () => { events.scroll?.(); return false; }, }), this.configuration.of(this.settings()), ]; diff --git a/src/lib/utils/graph-topology.test.mjs b/src/lib/utils/graph-topology.test.mjs new file mode 100644 index 0000000..ca769aa --- /dev/null +++ b/src/lib/utils/graph-topology.test.mjs @@ -0,0 +1,97 @@ +import assert from 'node:assert/strict'; +import test from 'node:test'; +import { buildGraphTopology, getGraphNeighborhood } from './graph-topology.ts'; + +const edge = (sourceIdx, targetIdx) => ({ sourceIdx, targetIdx }); + +function scannedNeighborhood(nodeIdx, edges) { + const nodeSet = new Set(); + const edgeSet = new Set(); + if (nodeIdx < 0) return { nodeSet, edgeSet }; + nodeSet.add(nodeIdx); + edges.forEach((e, i) => { + if (e.sourceIdx === nodeIdx || e.targetIdx === nodeIdx) { + edgeSet.add(i); + nodeSet.add(e.sourceIdx); + nodeSet.add(e.targetIdx); + } + }); + return { nodeSet, edgeSet }; +} + +test('topology retains duplicate edges and counts self-edges twice in degree but once in adjacency', () => { + const edges = [edge(0, 1), edge(0, 1), edge(1, 0), edge(0, 0), edge(2, 2)]; + const topology = buildGraphTopology(4, edges); + assert.deepEqual(topology.incidentEdges, [[0, 1, 2, 3], [0, 1, 2], [4], []]); + assert.deepEqual(topology.nodeDegree, [5, 3, 2, 0]); + assert.deepEqual(topology.connectedSet, new Set([0, 1, 2])); + assert.deepEqual(getGraphNeighborhood(0, edges, topology.incidentEdges), { + nodeSet: new Set([0, 1]), + edgeSet: new Set([0, 1, 2, 3]), + }); +}); + +test('changing active nodes produces only direct neighbors without changing prior sets', () => { + const edges = [edge(0, 1), edge(2, 1), edge(2, 3), edge(3, 4)]; + const { incidentEdges } = buildGraphTopology(6, edges); + const first = getGraphNeighborhood(0, edges, incidentEdges); + const second = getGraphNeighborhood(2, edges, incidentEdges); + assert.deepEqual(first, { nodeSet: new Set([0, 1]), edgeSet: new Set([0]) }); + assert.deepEqual(second, { nodeSet: new Set([2, 1, 3]), edgeSet: new Set([1, 2]) }); + assert.deepEqual(getGraphNeighborhood(5, edges, incidentEdges), { + nodeSet: new Set([5]), edgeSet: new Set(), + }); + assert.deepEqual(getGraphNeighborhood(-1, edges, incidentEdges), { + nodeSet: new Set(), edgeSet: new Set(), + }); +}); + +test('rebuilding topology uses the replacement edges for the same active node', () => { + const beforeEdges = [edge(0, 1)]; + const afterEdges = [edge(2, 0), edge(0, 0)]; + const before = buildGraphTopology(3, beforeEdges); + const after = buildGraphTopology(3, afterEdges); + assert.deepEqual(getGraphNeighborhood(0, beforeEdges, before.incidentEdges), { + nodeSet: new Set([0, 1]), edgeSet: new Set([0]), + }); + assert.deepEqual(getGraphNeighborhood(0, afterEdges, after.incidentEdges), { + nodeSet: new Set([0, 2]), edgeSet: new Set([0, 1]), + }); +}); + +test('indexed neighborhoods match full scans for directed, duplicate and self edges', () => { + const edges = Array.from({ length: 150 }, (_, i) => edge(i % 13, (i * 7 + 3) % 13)); + edges.push(edge(0, 0), edge(0, 1), edge(0, 1)); + const { incidentEdges } = buildGraphTopology(14, edges); + for (let nodeIdx = -1; nodeIdx < 14; nodeIdx++) { + assert.deepEqual(getGraphNeighborhood(nodeIdx, edges, incidentEdges), scannedNeighborhood(nodeIdx, edges)); + } +}); + +test('construction reads each endpoint once and neighborhood work visits only incident edges', () => { + let endpointReads = 0; + const pairs = [[0, 1], [0, 0], [2, 0], ...Array.from({ length: 1000 }, () => [2, 3])]; + const edges = pairs.map(([source, target]) => ({ + get sourceIdx() { endpointReads++; return source; }, + get targetIdx() { endpointReads++; return target; }, + })); + const { incidentEdges } = buildGraphTopology(5, edges); + assert.equal(endpointReads, edges.length * 2); + endpointReads = 0; + assert.deepEqual(getGraphNeighborhood(0, edges, incidentEdges), { + nodeSet: new Set([0, 1, 2]), edgeSet: new Set([0, 1, 2]), + }); + assert.equal(endpointReads, 6); + endpointReads = 0; + getGraphNeighborhood(4, edges, incidentEdges); + getGraphNeighborhood(-1, edges, incidentEdges); + assert.equal(endpointReads, 0); +}); + +test('an empty graph has no degree, adjacency or neighborhood entries', () => { + const topology = buildGraphTopology(0, []); + assert.deepEqual(topology, { incidentEdges: [], nodeDegree: [], connectedSet: new Set() }); + assert.deepEqual(getGraphNeighborhood(-1, [], topology.incidentEdges), { + nodeSet: new Set(), edgeSet: new Set(), + }); +}); diff --git a/src/lib/utils/graph-topology.ts b/src/lib/utils/graph-topology.ts new file mode 100644 index 0000000..a46ca7b --- /dev/null +++ b/src/lib/utils/graph-topology.ts @@ -0,0 +1,40 @@ +interface GraphTopologyEdge { + sourceIdx: number; + targetIdx: number; +} + +export function buildGraphTopology(nodeCount: number, edges: readonly GraphTopologyEdge[]) { + const incidentEdges: number[][] = Array.from({ length: nodeCount }, () => []); + const nodeDegree = new Array(nodeCount).fill(0); + const connectedSet = new Set(); + for (let i = 0; i < edges.length; i++) { + const { sourceIdx, targetIdx } = edges[i]; + connectedSet.add(sourceIdx); + connectedSet.add(targetIdx); + nodeDegree[sourceIdx]++; + nodeDegree[targetIdx]++; + incidentEdges[sourceIdx].push(i); + // A self-edge contributes twice to degree, but only once to the neighborhood. + if (targetIdx !== sourceIdx) incidentEdges[targetIdx].push(i); + } + return { incidentEdges, nodeDegree, connectedSet }; +} + +export function getGraphNeighborhood( + nodeIdx: number, + edges: readonly GraphTopologyEdge[], + incidentEdges: readonly (readonly number[])[], +) { + const nodeSet = new Set(); + const edgeSet = new Set(); + const incident = incidentEdges[nodeIdx]; + if (!incident) return { nodeSet, edgeSet }; + nodeSet.add(nodeIdx); + for (const i of incident) { + const edge = edges[i]; + edgeSet.add(i); + nodeSet.add(edge.sourceIdx); + nodeSet.add(edge.targetIdx); + } + return { nodeSet, edgeSet }; +} diff --git a/src/lib/utils/paths.ts b/src/lib/utils/paths.ts index 57637a5..7589c04 100644 --- a/src/lib/utils/paths.ts +++ b/src/lib/utils/paths.ts @@ -46,6 +46,26 @@ export function assetUrlToLocalPath(source: string): string | null { } } +/** The owning configured vault, never the active vault of an unrelated external note. */ +export function noteAssetRoot(notePath: string | null, activeVault: string | null, vaults: readonly string[] = []): string | null { + if (!notePath || isPathInDirectory(notePath, activeVault)) return activeVault; + return [...vaults].filter(root => isPathInDirectory(notePath, root)).sort((a, b) => b.length - a.length)[0] ?? null; +} + +/** Decode only the URI path, keeping query/fragment data out of filesystem paths. */ +export function resolveFileReference(source: string, notePath: string | null, vaultRoot: string | null): { path: string; suffix: string } { + const boundary = source.search(/[?#]/); + const encoded = boundary < 0 ? source : source.slice(0, boundary); + let path: string; + try { path = decodeURIComponent(encoded); } catch { path = encoded; } + return { path: resolveVaultFilePath(path, notePath, vaultRoot), suffix: boundary < 0 ? '' : source.slice(boundary) }; +} + +function encodeMarkdownPath(path: string): string { + return path.replace(/\\/g, '/').split('/').map(segment => encodeURIComponent(segment) + .replace(/[!'()*]/g, character => `%${character.charCodeAt(0).toString(16).toUpperCase()}`)).join('/'); +} + export function resolveVaultFilePath( targetPath: string, notePath: string | null, @@ -55,8 +75,11 @@ export function resolveVaultFilePath( if (target.startsWith('/') || /^[A-Za-z]:\//.test(target)) return target; const root = vaultRoot?.replace(/\\/g, '/').replace(/\/$/, '') ?? ''; - if (target.startsWith('.helixnotes/') && root) { - return resolvePathFromFile(`${root}/.vault-root`, target); + if (root) { + // Older imported managed links may still contain notebook-depth prefixes. + const managed = target.match(/^(?:\.{1,2}\/)*(\.helixnotes\/attachments\/.*)$/); + if (managed) return resolvePathFromFile(`${root}/.vault-root`, managed[1]); + if (target.startsWith('.helixnotes/')) return resolvePathFromFile(`${root}/.vault-root`, target); } if (notePath) return resolvePathFromFile(notePath, target); if (root) return resolvePathFromFile(`${root}/.vault-root`, target); @@ -79,14 +102,15 @@ export function assetSourceToMarkdown( const absolutePath = assetUrlToLocalPath(source); if (absolutePath === null) return source; + const url = new URL(source); + const suffix = url.search + url.hash; const normalizedRoot = vaultRoot?.replace(/\\/g, '/').replace(/\/$/, ''); - if (!normalizedRoot || !absolutePath.startsWith(normalizedRoot + '/')) return absolutePath; + if (!normalizedRoot || !isPathInDirectory(absolutePath, normalizedRoot)) return encodeMarkdownPath(absolutePath) + suffix; const vaultRelative = absolutePath.substring(normalizedRoot.length + 1); - if (vaultRelative.startsWith('.helixnotes/')) return vaultRelative; - if (!notePath) return vaultRelative; + if (vaultRelative.startsWith('.helixnotes/') || !notePath) return encodeMarkdownPath(vaultRelative) + suffix; const normalizedNotePath = notePath.replace(/\\/g, '/'); const noteDirectory = normalizedNotePath.substring(0, normalizedNotePath.lastIndexOf('/')); - return relativePath(noteDirectory, absolutePath); + return encodeMarkdownPath(relativePath(noteDirectory, absolutePath)) + suffix; } export function resolvePathFromFile(filePath: string, targetPath: string): string { diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index 3b0a675..3fd0d13 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -7,7 +7,7 @@ import { darkThemes, isMobile, isAndroid } from '$lib/platform'; import type { CustomTheme } from '$lib/types'; import ResizeHandles from '$lib/components/ResizeHandles.svelte'; - import { resolveVaultFilePath } from '$lib/utils/paths'; + import { noteAssetRoot, resolveFileReference } from '$lib/utils/paths'; let { children } = $props(); @@ -90,11 +90,9 @@ openUrl(href).catch((err) => console.error('Failed to open URL:', err)); } else if (!href.startsWith('#')) { const config = get(appConfig); - const absPath = resolveVaultFilePath( - decodeURIComponent(href), - get(activeNotePath), - config?.active_vault ?? null, - ); + const note = get(activeNotePath); + const root = noteAssetRoot(note, config?.active_vault ?? null, config?.vaults.map(vault => vault.path) ?? []); + const absPath = resolveFileReference(href, note, root).path; // Internal .md note link - navigate within the app if (absPath.endsWith('.md')) { window.dispatchEvent(new CustomEvent('helixnotes-open-note', { detail: absPath })); diff --git a/tests/attachment-uri.test.mjs b/tests/attachment-uri.test.mjs new file mode 100644 index 0000000..bf4c10a --- /dev/null +++ b/tests/attachment-uri.test.mjs @@ -0,0 +1,72 @@ +import assert from 'node:assert/strict'; +import { readFile } from 'node:fs/promises'; +import test from 'node:test'; +import { createContext, Script } from 'node:vm'; +import { parse } from 'svelte/compiler'; +import { transformWithEsbuild } from 'vite'; +import * as paths from '../src/lib/utils/paths.ts'; +import { createEditorMarkdownParser } from '../src/lib/editor/markdownParser.ts'; + +const source = await readFile(new URL('../src/lib/components/Editor.svelte', import.meta.url), 'utf8'); +const declaration = parse(source, { modern: true }).instance.content.body.find(node => + node.type === 'FunctionDeclaration' && node.id.name === 'resolveImageSrc' +); +const owningRoot = parse(source, { modern: true }).instance.content.body.find(node => node.type === 'FunctionDeclaration' && node.id.name === 'currentNoteAssetRoot'); +const { code } = await transformWithEsbuild(`${owningRoot ? source.slice(owningRoot.start, owningRoot.end) : ''}\n${source.slice(declaration.start, declaration.end)}`, 'resolve-image.ts', { loader: 'ts', format: 'esm' }); +const script = new Script(`${code}\nresolveImageSrc;`); +const parser = createEditorMarkdownParser(); +const imageSource = markdown => parser.parseInline(markdown, {}).flatMap(token => token.children ?? []).find(token => token.type === 'image')?.attrGet('src'); +const assetPath = url => decodeURIComponent(new URL(url).pathname.slice(1)); +const encodedName = name => encodeURIComponent(name).replace(/[!'()*]/g, char => `%${char.charCodeAt(0).toString(16).toUpperCase()}`); +function fixture(note = '/vault/Projects/Note.md', vault = '/vault', known = []) { + const context = createContext({ ...paths, + $activeNotePath: note, $appConfig: { active_vault: vault, vaults: known.map(path => ({ path })) }, + normalizePath: path => paths.resolvePathFromFile('', path), + convertFileSrc: path => `http://asset.localhost/${encodeURIComponent(path)}`, + }); + return { context, resolve: script.runInContext(context) }; +} + +for (const name of ['photo%20one.png', 'photo).png', 'photo(one.png', 'photo#one?.png', "café 雪 + 'image'.png"]) { + test(`rich save/reload preserves the actual attachment filename ${JSON.stringify(name)}`, () => { + const f = fixture(); + const expected = `/vault/.helixnotes/attachments/${name}`; + let markdown = `![Photo](../.helixnotes/attachments/${encodedName(name)}?v=1#preview)`; + for (let round = 0; round < 3; round++) { + const src = imageSource(markdown); + assert.ok(src, 'round trips must not turn an image into malformed Markdown'); + const asset = f.resolve(src); + assert.equal(assetPath(asset), expected, 'query and fragment never become part of the filesystem path'); + assert.equal(new URL(asset).search, '?v=1'); + assert.equal(new URL(asset).hash, '#preview'); + markdown = `![Photo](${paths.assetSourceToMarkdown(asset, f.context.$activeNotePath, '/vault')})`; + } + }); +} + +test('Windows and UNC asset URLs preserve encoded filenames across repeated saves', () => { + for (const [vault, note, absolute] of [ + ['C:\\Notes', 'C:\\Notes\\Projects\\Note.md', 'C:/Notes/.helixnotes/attachments/photo%20).png'], + ['\\\\server\\share\\Notes', '\\\\server\\share\\Notes\\Projects\\Note.md', '//server/share/Notes/.helixnotes/attachments/photo%20).png'] + ]) { + const f = fixture(note, vault); + let markdown = '![Photo](.helixnotes/attachments/photo%2520%29.png#preview)'; + for (let round = 0; round < 3; round++) { + const asset = f.resolve(imageSource(markdown)); + assert.equal(assetPath(asset), absolute); + markdown = `![Photo](${paths.assetSourceToMarkdown(asset, note, vault)})`; + } + } +}); + +test('an external note uses its configured source vault, not the active destination vault', () => { + const f = fixture('/source/Notebook/Note.md', '/destination', ['/source', '/destination']); + assert.equal(assetPath(f.resolve('.helixnotes/attachments/image.png')), '/source/.helixnotes/attachments/image.png'); + assert.equal(assetPath(f.resolve('../.helixnotes/attachments/image.png')), '/source/.helixnotes/attachments/image.png'); +}); + +test('an unconfigured external note resolves its own relative assets without borrowing active-vault metadata', () => { + const f = fixture('/external/Note.md', '/destination'); + assert.equal(assetPath(f.resolve('.helixnotes/attachments/image.png')), '/external/.helixnotes/attachments/image.png'); + assert.equal(assetPath(f.resolve('images/image.png')), '/external/images/image.png'); +}); diff --git a/tests/external-note-references.test.mjs b/tests/external-note-references.test.mjs new file mode 100644 index 0000000..bcad398 --- /dev/null +++ b/tests/external-note-references.test.mjs @@ -0,0 +1,82 @@ +import assert from 'node:assert/strict'; +import test from 'node:test'; +import { externalNoteReferences } from '../src/lib/editor/externalNote.ts'; + +const ranges = raw => externalNoteReferences(raw).map(reference => ({ + ...reference, original: Buffer.from(raw).subarray(reference.from, reference.to).toString('utf8') +})); + +test('finds Markdown attachment destinations without changing labels, titles, angles, or fragments', () => { + const raw = '![Image|size=small](images/photo%20one.png "Caption")\n[PDF]( "Title")\n[CSV][ref]\n\n[ref]: files/list.csv\n'; + assert.deepEqual(ranges(raw).map(({ href, original, html }) => ({ href, original, html })), [ + { href: 'images/photo%20one.png', original: 'images/photo%20one.png', html: false }, + { href: 'files/report (1).pdf#page=2', original: 'files/report (1).pdf#page=2', html: false }, + { href: 'files/list.csv', original: 'files/list.csv', html: false } + ]); +}); + +test('legacy space-containing destinations supported by the editor import without copying code examples', () => { + const raw = '![Legacy](images/photo one.png "Caption")\n[File](files/report%20"Title")\n\n~~~md\n![Example](missing image.png)\n~~~\n\n`![Inline](missing image.png)`\n'; + assert.deepEqual(ranges(raw).map(reference => reference.href), ['images/photo one.png', 'files/report']); +}); + +test('unused and overridden reference definitions are not imported', () => { + assert.deepEqual(ranges('A text-only note.\n\n[unused]: missing.png\n'), []); + const raw = '![Image][photo]\n\n[photo]: images/real.png\n[PHOTO]: missing.png\n[unused]: also-missing.png\n'; + assert.deepEqual(ranges(raw).map(reference => reference.href), ['images/real.png']); +}); + +test('full, collapsed, and shortcut references select the first matching definition', () => { + for (const use of ['![One][ Photo ]', '![photo][]', '![photo]', '[Photo][]', '[photo]']) { + const raw = `${use}\n\n[PHOTO]: images/real.png\n[ photo ]: missing.png\n`; + assert.deepEqual(ranges(raw).map(reference => reference.href), ['images/real.png'], use); + } +}); + +test('reference labels in code, math, and image alt text do not authorize unused definitions', () => { + const raw = '`![fake][hidden]`\n\n$[hidden]$\n\n![An [hidden] label](real.png)\n\n[hidden]: missing.png\n'; + assert.deepEqual(ranges(raw).map(reference => reference.href), ['real.png']); +}); + +test('UTF-8 byte offsets retain Unicode, BOM, CRLF, and untouched custom frontmatter', () => { + const raw = '\uFEFF---\r\ncustom: "![metadata](do-not-copy.png)"\r\n---\r\n雪 📝 ![图片](资源/café%20雪.png)\r\n'; + assert.equal(ranges(raw).length, 1); + assert.equal(ranges(raw)[0].original, '资源/café%20雪.png'); +}); + +test('literal code, comments, closed display math, and HTML code do not become attachments', () => { + const raw = '`![code](inline.png)`\n\n~~~~md\n![code](fenced.png)\n~~~~\n\n$$\n![math](math.png)\n$$\n\n\n\n
\n\n![real](real.png)'; + assert.deepEqual(ranges(raw).map(reference => reference.href), ['real.png']); +}); + +test('inline math stays literal while escaped dollars and dollar-containing destinations remain links', () => { + const raw = '$![math](math.png)$ \\$![real](real.png)\\$ ![price](images/$price$.png)'; + assert.deepEqual(ranges(raw).map(reference => reference.href), ['real.png', 'images/$price$.png']); +}); + +test('remote URLs, anchors, absolute paths, and linked Markdown notes stay unchanged', () => { + const raw = '![remote](https://example.com/pic.png) [mail](mailto:test@example.com) [anchor](#section)\n![absolute](/private/file.png) [note](Related.MD#heading) [encoded](Related%2EMD)\n![data](data:image/png;base64,AAAA)'; + assert.deepEqual(ranges(raw), []); +}); + +test('HTML images, file links, and PDF embeds retain attribute byte ranges and decode entities', () => { + const raw = 'a > b src="missing.png" File\n\n
'; + assert.deepEqual(ranges(raw).map(({ href, original, html }) => ({ href, original, html })), [ + { href: 'images/a&b.png', original: 'images/a&b.png', html: true }, + { href: 'files/report.pdf', original: 'files/report.pdf', html: true }, + { href: 'files/view.pdf#page=2', original: 'files/view.pdf#page=2', html: true } + ]); + assert.equal(ranges(raw)[2].pdf, true); + assert.equal(ranges(raw)[0].pdf, false); +}); + +test('links inside image alt text are not copied as additional attachments', () => { + const raw = '![An [example](missing.txt)](real.png)'; + assert.deepEqual(ranges(raw).map(reference => reference.href), ['real.png']); +}); + +test('escaped Markdown paths are decoded without consuming code examples or link titles', () => { + const raw = '[Report](files/report\\(1\\).pdf "Other (unused.png)")'; + assert.equal(ranges(raw)[0].href, 'files/report(1).pdf'); + assert.equal(ranges(raw)[0].original, 'files/report\\(1\\).pdf'); +}); diff --git a/tests/markdown-parser.test.mjs b/tests/markdown-parser.test.mjs new file mode 100644 index 0000000..234039d --- /dev/null +++ b/tests/markdown-parser.test.mjs @@ -0,0 +1,167 @@ +import assert from 'node:assert/strict'; +import test from 'node:test'; +import { createEditorMarkdownParser, displayMathRanges } from '../src/lib/editor/markdownParser.ts'; + +const parser = createEditorMarkdownParser(); +const payload = '$x$\n$$\na+b\n$$\n[[Note]]\n[label](path with spaces)\n![image]( "title")\n- [x] task\n\n
'; +const escapedCode = (html) => html.match(/
]*>([\s\S]*?)<\/code><\/pre>/)?.[1];
+const unescape = (html) => html.replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/&/g, '&');
+const mathPayloads = (html) => [...html.matchAll(/data-math-(?:inline|block)="([^"]*)"/g)].map(match => decodeURIComponent(unescape(match[1])));
+
+for (const [name, opening, closing] of [
+  ['backtick', '```text', '```'], ['tilde', '~~~text', '~~~'],
+  ['long backtick', '````text', '````'], ['indented fence', '   ~~~text', '   ~~~'],
+]) {
+  test(`${name} fences preserve all literal syntax`, () => {
+    const html = parser.render(`${opening}\n${payload}\n${closing}`, { wikiLinkTitles: [{ path: 'Note.md', title: 'Note' }] });
+    assert.equal(unescape(escapedCode(html)), payload);
+    assert.equal(mathPayloads(html).length, 0);
+    assert.doesNotMatch(html, /]*data-wiki-link|]*data-checked|]*data-secret-block/);
+  });
+}
+
+test('shorter and mismatched fences remain code; following math still renders', () => {
+  const code = '```\n~~~\n$x$';
+  const html = parser.render('````text\n' + code + '\n````\n\nActual $y$.');
+  assert.equal(unescape(escapedCode(html)), code);
+  assert.deepEqual(mathPayloads(html), ['y']);
+});
+
+test('fences interrupt prose without needing a blank line', () => {
+  const html = parser.render('Prose\n```text\n$x$\n```\nMath $y$.');
+  assert.equal(unescape(escapedCode(html)), '$x$');
+  assert.deepEqual(mathPayloads(html), ['y']);
+});
+
+test('quoted and listed fences protect their contents', () => {
+  const quoted = parser.render('> ~~~text\n> $x$\n> [[Note]]\n> ~~~', { wikiLinkTitles: [] });
+  assert.equal(unescape(escapedCode(quoted)), '$x$\n[[Note]]');
+  const listed = parser.render('- item\n\n  ```text\n  $x$\n  \n  ```');
+  assert.equal(unescape(escapedCode(listed)), '$x$\n');
+});
+
+test('unclosed fences keep their payload literal', () => {
+  const html = parser.render('~~~text\n$x$\n');
+  assert.equal(unescape(escapedCode(html)), '$x$\n');
+  assert.deepEqual(mathPayloads(html), []);
+});
+
+test('fence rendering preserves leading, internal and extra trailing blank lines', () => {
+  const code = '\nfirst\n\n\nlast\n\n';
+  const html = parser.render('```text\n' + code + '\n```');
+  assert.equal(unescape(escapedCode(html)), code);
+});
+
+test('inline and multiline code protect math, wiki and legacy links', () => {
+  const html = parser.render('`` $x$ [[Note]] [a](a b) ` `` and `line\n$x$`', { wikiLinkTitles: [] });
+  assert.match(html, /]*>\$x\$ \[\[Note\]\] \[a\]\(a b\) `<\/code>/);
+  assert.match(html, /]*>line \$x\$<\/code>/);
+  assert.deepEqual(mathPayloads(html), []);
+});
+
+test('escaped dollars stay prose while real math retains exact escaped-dollar and backtick TeX', () => {
+  const html = parser.render('Escaped \\$x$ and $a\\$b$ and $`raw`$; $10 to $50 and $ x$ stay prose.');
+  assert.deepEqual(mathPayloads(html), ['a\\$b', '`raw`']);
+  assert.match(html, /Escaped \$x\$/);
+  assert.match(html, /\$10 to \$50/);
+});
+
+test('math payloads are consumed before wiki, link and task transformations', () => {
+  const tex = '[[Note]]\n[label](path with spaces)\n```\n- [x] TeX';
+  const html = parser.render('$$\n' + tex + '\n$$\n\n$x+y$', { wikiLinkTitles: [] });
+  assert.deepEqual(mathPayloads(html), [tex, 'x+y']);
+  assert.doesNotMatch(html, /data-wiki-link|data-checked|
/);
+});
+
+test('unclosed display math stays literal and normal fenced indentation convention stays unchanged', () => {
+  assert.deepEqual(mathPayloads(parser.render('$$\nunclosed')), []);
+  assert.doesNotMatch(parser.render('\tordinary tab-indented text'), /
/);
+});
+
+test('semantic math ranges do not mistake fenced or inline code for display math', () => {
+  const source = '~~~text\n$$\n# code\n$$\n~~~\n\n> $$\n> x\n> ---\n> $$\n\n# Real';
+  const ranges = displayMathRanges(source);
+  assert.equal(ranges.length, 1);
+  assert.equal(source.slice(ranges[0].from, ranges[0].to), '> $$\n> x\n> ---\n> $$\n');
+  assert.deepEqual(displayMathRanges('```text\n$$\n```\n\n# Real'), []);
+});
+
+test('wiki lookup preserves path precedence, shallowest title and escaped attributes', () => {
+  const env = { wikiLinkTitles: [
+    { path: 'deep/nested/Note.md', title: 'Note' }, { path: 'folder/Note.md', title: 'NOTE' },
+    { path: 'other/Note.md', title: 'Note' }, { path: 'Quoted".md', title: 'Quoted"' },
+  ] };
+  const html = parser.render('[[Note]] [[deep/nested/Note#Heading|Alias]] [[Quoted"]] [[Missing]]', env);
+  assert.match(html, /data-path="folder\/Note\.md"/);
+  assert.match(html, /data-path="deep\/nested\/Note\.md" data-title="deep\/nested\/Note#Heading"[^>]*>Alias<\/span>/);
+  assert.match(html, /data-path="Quoted"\.md" data-title="Quoted""/);
+  assert.match(html, /data-path="" data-title="Missing"/);
+  assert.doesNotMatch(parser.render('\\[[Note]]', env), /data-wiki-link/);
+  assert.doesNotMatch(parser.render('[[Note]]'), /data-wiki-link/);
+});
+
+test('wiki lookup reads titles once per render and refreshes after in-place cache changes', () => {
+  let reads = 0;
+  const notes = Array.from({ length: 250 }, (_, index) => ({
+    path: `folder/Note ${index}.md`, get title() { reads++; return `Note ${index}`; },
+  }));
+  const env = { wikiLinkTitles: notes };
+  const source = '[[Note 249]] '.repeat(100);
+  parser.render(source, env);
+  assert.equal(reads, notes.length);
+  notes[249] = { path: 'Moved.md', title: 'Note 249' };
+  assert.match(parser.render(source, env), /data-path="Moved\.md"/);
+});
+
+test('ordinary and legacy links/images remain functional while unsafe schemes remain rejected', () => {
+  const html = parser.render('[a]( "title") [b](path with spaces) [c](path%20"title") ![d](%20"title") ![e](///home/user/image.png)');
+  assert.match(html, /href="path%20with%20spaces"/);
+  assert.match(html, /href="path"/);
+  assert.match(html, /src="image%20path"/);
+  assert.match(html, /src="\/home\/user\/image\.png"/);
+  assert.doesNotMatch(html, / title=/);
+  assert.doesNotMatch(parser.render('[bad](javascript:alert with spaces) ![bad](data:text/html;base64,foo bar)'), /href=|src=/);
+});
+
+test('compatibility destinations retain quoted parentheses and native nested URLs', () => {
+  const html = parser.render('[a](path with spaces "a (title)") [b]( (title)) [c](path(one))');
+  assert.match(html, /href="path%20with%20spaces"/);
+  assert.match(html, /href="image%20path"/);
+  assert.match(html, /href="path\(one\)"/);
+});
+
+test('task and empty-paragraph handling uses real block structure, including nested and empty tasks', () => {
+  const html = parser.render('- [x] **done**\n- [ ]\n  - [ ] nested\n\n\n# Next');
+  assert.equal([...html.matchAll(/data-type="taskList"/g)].length, 2);
+  assert.equal([...html.matchAll(/data-type="taskItem"/g)].length, 3);
+  assert.match(html, /data-checked="true"[^>]*>[\s\S]*done<\/strong>/);
+  assert.match(html, /

<\/p>\n

Next<\/h1>/); + assert.doesNotMatch(parser.render('`- [x] not a task` and ``'), /data-checked|

<\/p>/); +}); + +test('image-only gaps remain outside code and do not alter inline images', () => { + assert.match(parser.render('Text\n\n![a](image.png)'), /

Text<\/p>\n

<\/p>\n

<\/p>/); +}); + +test('only actual closed secret fences become secret blocks, not literal examples', () => { + assert.match(parser.render('```helix-secret\nexample-payload\n```'), /data-secret-block="example-payload"/); + const literal = '```helix-secret\nexample-payload\n```'; + assert.equal(unescape(escapedCode(parser.render('````text\n' + literal + '\n````'))), literal); + assert.doesNotMatch(parser.render('```helix-secret\nunclosed'), /data-secret-block/); +}); + +test('invalid final secret-fence markers remain literal code', () => { + for (const closing of [' ```', '\t```', '```\u00a0']) { + const html = parser.render('```helix-secret\nexample-payload\n' + closing); + assert.doesNotMatch(html, /data-secret-block/); + assert.equal(unescape(escapedCode(html)), 'example-payload\n' + closing); + } +}); + +test('code whitespace is marked for DOM import without changing soft or hard line-break semantics', () => { + const html = parser.render('# ` `\n\nbefore `trailing `\nnext \nlast'); + assert.match(html, /

/); + assert.match(html, /

/); + assert.match(html, /<\/code> next
last/); +}); diff --git a/tests/markdown-roundtrip.test.mjs b/tests/markdown-roundtrip.test.mjs index c52165b..0833060 100644 --- a/tests/markdown-roundtrip.test.mjs +++ b/tests/markdown-roundtrip.test.mjs @@ -3,7 +3,7 @@ import { readFile } from 'node:fs/promises'; import test from 'node:test'; import MarkdownIt from 'markdown-it'; import { Schema } from '@tiptap/pm/model'; -import { transformWithEsbuild } from 'vite'; +import { createServer, transformWithEsbuild } from 'vite'; const source = await readFile( new URL('../src/lib/editor/markdown.ts', import.meta.url), @@ -14,22 +14,39 @@ const { code } = await transformWithEsbuild(source, 'markdown.ts', { format: 'esm', target: 'esnext' }); -const { serializeInlineMarkdown } = await import( +const { serializeCodeBlock, serializeInlineMarkdown } = await import( `data:text/javascript;base64,${Buffer.from(code).toString('base64')}` ); +const vite = await createServer({ + configFile: false, + server: { middlewareMode: true }, + appType: 'custom' +}); +let editorMarkdown; +try { + const { createEditorMarkdownParser } = await vite.ssrLoadModule( + new URL('../src/lib/editor/markdownParser.ts', import.meta.url).pathname + ); + editorMarkdown = createEditorMarkdownParser(); +} finally { + await vite.close(); +} + // Link intentionally precedes formatting marks in the schema. This matches the // mark order produced by the app and reproduces the original per-text-node bug. const schema = new Schema({ nodes: { doc: { content: 'block+' }, - paragraph: { content: 'text*', group: 'block' }, - text: { group: 'inline' } + paragraph: { content: 'inline*', group: 'block' }, + text: { group: 'inline' }, + mathInline: { inline: true, group: 'inline', atom: true, attrs: { tex: { default: '' } } } }, marks: { link: { attrs: { href: {} } }, bold: {}, italic: {}, + code: {}, wikiLink: { attrs: { title: { default: null }, @@ -116,3 +133,231 @@ test('preserves wiki-link aliases inside a bold span', () => { assert.equal(serialized, '**See [[Target note|this note]] now**'); }); + +test('round-trips inline code backticks, delimiter padding and literal whitespace', () => { + const codeMark = schema.marks.code.create(); + for (const text of [ + 'a`b', + 'a``b', + 'a````b', + '`', + '``', + '`edge`', + ' a ', + ' leading', + 'trailing ', + ' ', + ' ', + '\tleading', + '\u2003leading', + '\t\u2003 interior\t\u2003 ', + '\\$x$' + ]) { + const serialized = serializeInlineMarkdown(paragraph([{ text, marks: [codeMark] }])); + const tokens = editorMarkdown.parseInline(serialized, {})[0].children; + assert.deepEqual(tokens.map((token) => [token.type, token.content]), [['code_inline', text]], text); + assert.equal(editorMarkdown.render(serialized).includes('data-math-inline'), false, text); + } + assert.equal(serializeInlineMarkdown(paragraph([{ text: '`', marks: [codeMark] }])), '`` ` ``'); + assert.equal(serializeInlineMarkdown(paragraph([{ text: ' a ', marks: [codeMark] }])), '` a `'); + assert.equal(serializeInlineMarkdown(paragraph([{ text: ' ', marks: [codeMark] }])), '` `'); +}); + +test('joins contiguous code across formatting changes with code taking precedence', () => { + const codeMark = schema.marks.code.create(); + const serialized = serializeInlineMarkdown(paragraph([ + { text: '`a', marks: [codeMark, bold] }, + { text: '``', marks: [codeMark, italic] }, + { text: 'b`', marks: [codeMark] } + ])); + assert.equal(serialized, '``` `a``b` ```'); + assert.deepEqual( + editorMarkdown.parseInline(serialized, {})[0].children.map((token) => [token.type, token.content]), + [['code_inline', '`a``b`']] + ); + assert.equal(editorMarkdown.render(serialized), '

`a``b`

\n'); +}); + +test('keeps link syntax outside code and joins code across formatting changes within links', () => { + const codeMark = schema.marks.code.create(); + const serialized = serializeInlineMarkdown(paragraph([ + { text: 'before ', marks: [] }, + { text: '`a', marks: [codeMark, link, bold] }, + { text: 'b`', marks: [codeMark, link, italic] }, + { text: ' after', marks: [] } + ])); + assert.equal(serialized, 'before [`` `ab` ``](https://example.com) after'); + assert.equal( + editorMarkdown.render(serialized), + '

before `ab` after

\n' + ); +}); + +test('preserves formatting on non-code parts of a link', () => { + const codeMark = schema.marks.code.create(); + const serialized = serializeInlineMarkdown(paragraph([ + { text: 'first', marks: [link, bold] }, + { text: 'code', marks: [link, codeMark] }, + { text: 'last', marks: [link, bold] } + ])); + assert.equal( + editorMarkdown.render(serialized), + '

firstcodelast

\n' + ); +}); + +test('round-trips code blocks with embedded fences and every blank-line boundary', () => { + for (const text of [ + '', + 'plain', + '\nleading', + 'trailing\n', + '\n\nleading\n\nmiddle\n\ntrailing\n\n', + '\t \n\u2003\n\n', + '```\nembedded\n```\n', + '``````\n~~~\n$not math$\n```' + ]) { + const serialized = serializeCodeBlock(text, 'js'); + const [fence] = markdown.parse(serialized, {}); + const expectedFence = '`'.repeat(Math.max(3, ...[...text.matchAll(/`+/g)].map((match) => match[0].length + 1))); + assert.equal(serialized, `${expectedFence}js\n${text}\n${expectedFence}\n`, text); + assert.equal(fence.type, 'fence', text); + assert.equal(fence.info, 'js', text); + // CommonMark includes the structural newline before the closing fence. + assert.equal(fence.content, `${text}\n`, text); + assert.equal(fence.content.slice(0, -1), text); + assert.equal(editorMarkdown.render(serialized).includes('data-math-inline'), false, text); + } + assert.equal(serializeCodeBlock('code'), '```\ncode\n```\n'); +}); + +test('escapes literal prose dollars without changing preceding literal backslashes', () => { + for (const text of [ + '$x$', + '$$', + 'cost $5 and $10', + '\\$x$', + '\\\\$x$', + '\\\\\\$x$', + 'before \\$x\\$ after', + '$a$ and $b$', + 'literal $with spaces$', + 'ordinary punctuation: a+b, c=d; e/f!' + ]) { + const serialized = serializeInlineMarkdown(paragraph([{ text, marks: [] }])); + const tokens = editorMarkdown.parseInline(serialized, {})[0].children; + assert.equal(tokens.every((token) => token.type === 'text'), true, text); + assert.equal(tokens.map((token) => token.content).join(''), text); + assert.equal(editorMarkdown.render(serialized).includes('data-math-inline'), false, text); + } + assert.equal(serializeInlineMarkdown(paragraph([{ text: '$x$', marks: [] }])), '\\$x\\$'); + assert.equal( + serializeInlineMarkdown(paragraph([{ text: 'ordinary punctuation: a+b, c=d; e/f!', marks: [] }])), + 'ordinary punctuation: a+b, c=d; e/f!' + ); +}); + +test('keeps escaped Markdown dollars as prose through load, save and reload', () => { + for (const source of ['\\$x\\$', 'price \\$5', '\\\\\\$x\\$', '\\\\\\\\\\$x\\$']) { + const loaded = editorMarkdown.parseInline(source, {})[0].children; + assert.equal(loaded.every((token) => token.type === 'text'), true); + const text = loaded.map((token) => token.content).join(''); + const serialized = serializeInlineMarkdown(paragraph([{ text, marks: [] }])); + const reloaded = editorMarkdown.parseInline(serialized, {})[0].children; + assert.equal(reloaded.every((token) => token.type === 'text'), true); + assert.equal(reloaded.map((token) => token.content).join(''), text); + } +}); + +test('preserves real inline math TeX and falls back to existing encoded HTML when needed', () => { + for (const tex of [ + 'x^2', + '\\frac{a}{b}', + '', + ' ', + ' leading', + 'trailing ', + '\tleading', + 'trailing\u2003', + 'a\nb', + 'a\rb', + 'x$y', + '\\$', + 'trailing\\' + ]) { + const mathNode = schema.node('mathInline', { tex }); + const serialized = serializeInlineMarkdown(schema.node('paragraph', null, [mathNode])); + const html = editorMarkdown.render(serialized); + const stored = html.match(/data-math-inline="([^"]*)"/); + assert.ok(stored, tex); + assert.equal(decodeURIComponent(stored[1]), tex); + if (tex === 'x^2' || tex === '\\frac{a}{b}') { + assert.equal(serialized, `$${tex}$`); + } else { + assert.equal(serialized, ``); + } + const reloaded = schema.node('paragraph', null, [ + schema.node('mathInline', { tex: decodeURIComponent(stored[1]) }) + ]); + assert.equal(serializeInlineMarkdown(reloaded), serialized); + } +}); + +test('keeps literal dollars escaped within rich formatting and link labels', () => { + const serialized = serializeInlineMarkdown(paragraph([ + { text: 'before ', marks: [bold] }, + { text: '$literal$', marks: [bold, link] }, + { text: ' after', marks: [bold] } + ])); + assert.equal(serialized, '**before [\\$literal\\$](https://example.com) after**'); + assert.equal( + editorMarkdown.render(serialized), + '

before $literal$ after

\n' + ); +}); + +test('uses encoded math storage at ambiguous dollar, digit and adjacent-atom boundaries', () => { + const mathNode = schema.node('mathInline', { tex: 'x' }); + for (const children of [ + [mathNode, schema.text('2')], + [mathNode, schema.text('$literal$')], + [schema.text('$literal$'), mathNode], + [mathNode, schema.node('mathInline', { tex: 'y' })] + ]) { + const serialized = serializeInlineMarkdown(schema.node('paragraph', null, children)); + const stored = [...editorMarkdown.render(serialized).matchAll(/data-math-inline="([^"]*)"/g)]; + const expected = children.filter((child) => child.type.name === 'mathInline').map((child) => child.attrs.tex); + assert.deepEqual(stored.map((match) => decodeURIComponent(match[1])), expected); + assert.equal(serialized.startsWith('$x$'), false); + } +}); + +test('preserves trailing prose backslashes before real math atoms', () => { + for (const text of ['\\', '\\\\', '\\\\\\']) { + const serialized = serializeInlineMarkdown(schema.node('paragraph', null, [ + schema.text(text), + schema.node('mathInline', { tex: 'x' }) + ])); + const tokens = editorMarkdown.parseInline(serialized, {})[0].children; + assert.equal(tokens[0].type, 'text'); + assert.equal(tokens[0].content, text); + const stored = editorMarkdown.render(serialized).match(/data-math-inline="([^"]*)"/); + assert.ok(stored); + assert.equal(decodeURIComponent(stored[1]), 'x'); + } +}); + +test('splits contiguous code at link boundaries without losing code text or link targets', () => { + const codeMark = schema.marks.code.create(); + const otherLink = schema.marks.link.create({ href: 'https://other.example' }); + const serialized = serializeInlineMarkdown(paragraph([ + { text: '`first', marks: [codeMark] }, + { text: 'second`', marks: [codeMark, link] }, + { text: 'third', marks: [codeMark, otherLink] }, + { text: 'last', marks: [codeMark] } + ])); + assert.equal( + editorMarkdown.render(serialized), + '

`firstsecond`thirdlast

\n' + ); +}); diff --git a/tests/new-note.test.mjs b/tests/new-note.test.mjs new file mode 100644 index 0000000..f4ef626 --- /dev/null +++ b/tests/new-note.test.mjs @@ -0,0 +1,162 @@ +import assert from 'node:assert/strict'; +import { readFile } from 'node:fs/promises'; +import test from 'node:test'; +import { createContext, Script } from 'node:vm'; +import { parse } from 'svelte/compiler'; +import { transformWithEsbuild } from 'vite'; +import { RevisionSaveQueue } from '../src/lib/utils/revision-save.ts'; +import { beginNoteNavigation, isCurrentNoteNavigation } from '../src/lib/utils/note-navigation.ts'; + +const source = await readFile(new URL('../src/lib/components/NoteList.svelte', import.meta.url), 'utf8'); +const ast = parse(source, { modern: true }); +const declaration = ast.instance.content.body.find(node => + node.type === 'ExportNamedDeclaration' && node.declaration?.id?.name === 'handleCreateNote' +).declaration; +const { code } = await transformWithEsbuild(source.slice(declaration.start, declaration.end), 'new-note.ts', { + loader: 'ts', format: 'esm', target: 'esnext' +}); +const createScript = new Script(`${code}\nhandleCreateNote;`); + +function deferred() { + let resolve; + let reject; + const promise = new Promise((res, rej) => { resolve = res; reject = rej; }); + return { promise, resolve, reject }; +} + +function fixture(overrides = {}) { + const vault = '/vault'; + const entry = { path: '/vault/Untitled.md', relative_path: 'Untitled.md', preview: '', + meta: { id: 'created-id', title: 'Untitled', tags: [], pinned: false, + created: '2026-10-05T10:00:00Z', modified: '2026-10-05T10:00:00Z' } }; + const created = { path: entry.path, meta: entry.meta, content: '', + raw: '---\nid: created-id\ntitle: Untitled\ntags: []\npinned: false\ncreated: 2026-10-05T10:00:00Z\nmodified: 2026-10-05T10:00:00Z\n---\n\n' }; + const selected = []; + const focused = []; + const errors = []; + const context = createContext({ + ownerVault: vault, + ownerIsCurrent: () => context.$appConfig.active_vault === vault, + beginNavigation: beginNoteNavigation, + navigationIsCurrent: token => context.ownerIsCurrent() && isCurrentNoteNavigation(token), + $appConfig: { active_vault: vault }, $viewMode: 'all', $sortMode: 'modified', + $activeNotebook: null, $activeNotePath: null, $activeNote: null, $editorDirty: false, $notes: [], + noteCache: new Map(), onBeforeNoteSwitch: async () => true, + createNote: async (origin, notebook, title) => { + assert.equal(origin, vault); + assert.equal(notebook, null); + assert.equal(title, 'Untitled'); + return entry; + }, + readNote: async (origin, path) => { + assert.equal(origin, vault); + assert.equal(path, entry.path); + return created; + }, + onNoteMoved() {}, + onNoteSelected: (path, content) => selected.push({ path, content }), + onNoteCreated: token => focused.push(token), + console: { error: (...args) => errors.push(args) }, + ...overrides + }); + return { context, entry, created, selected, focused, errors, create: createScript.runInContext(context) }; +} + +for (const mode of ['rich', 'source']) { + test(`the first ${mode} edit saves before any watcher event using the created file's exact baseline`, async () => { + const f = fixture(); + const saves = new RevisionSaveQueue(); + let saving; + f.context.onNoteSelected = (path, content) => { + assert.equal(path, f.entry.path); + saves.startSession(f.context.$activeNote.raw); + saves.markChanged(); + f.context.$editorDirty = true; + saving = saves.save(saves.capture(), async expectedRaw => { + assert.equal(expectedRaw, f.created.raw, 'first save must accept the file created on disk'); + return `${expectedRaw}First ${mode} edit\n`; + }); + }; + await f.create(); + assert.equal(await saving, true); + assert.equal(saves.dirty, false); + assert.equal(f.context.$activeNote.raw, f.created.raw); + assert.equal(f.focused.length, 1); + }); +} + +test('creation loads current disk content and metadata instead of fabricating an empty note', async () => { + const f = fixture(); + const current = { ...f.created, meta: { ...f.created.meta, title: 'External title', tags: ['external'] }, + content: 'External content', raw: '---\ntitle: External title\n---\nExternal content\n' }; + f.context.readNote = async () => current; + await f.create(); + assert.equal(f.context.$activeNote, current); + assert.equal(f.selected[0].content, current.content); +}); + +test('a delayed new-note read cannot replace a newer selected draft or steal title focus', async () => { + const read = deferred(); + let reads = 0; + const f = fixture({ readNote: () => { reads++; return read.promise; } }); + const creating = f.create(); + await new Promise(resolve => setImmediate(resolve)); + const newer = { path: '/vault/Other.md', content: 'Newer unsaved draft', raw: 'Other baseline' }; + beginNoteNavigation(); + f.context.$activeNote = newer; + f.context.$activeNotePath = newer.path; + f.context.$editorDirty = true; + read.resolve(f.created); + await creating; + assert.equal(reads, 1); + assert.equal(f.context.$activeNote, newer); + assert.equal(f.context.$editorDirty, true); + assert.equal(f.selected.length, 0); + assert.equal(f.focused.length, 0); +}); + +test('a delayed new-note read cannot publish into another vault', async () => { + const read = deferred(); + let reads = 0; + const f = fixture({ readNote: () => { reads++; return read.promise; } }); + const creating = f.create(); + await new Promise(resolve => setImmediate(resolve)); + f.context.$appConfig = { active_vault: '/other-vault' }; + f.context.$activeNote = null; + f.context.$activeNotePath = null; + read.resolve(f.created); + await creating; + assert.equal(reads, 1); + assert.equal(f.context.$activeNote, null); + assert.equal(f.selected.length, 0); + assert.equal(f.focused.length, 0); +}); + +test('a failed read keeps the existing draft and never opens a fabricated new note', async () => { + const old = { path: '/vault/Old.md', content: 'Draft', raw: 'Old baseline' }; + const f = fixture({ $activeNote: old, $activeNotePath: old.path, + readNote: async () => { throw new Error('Read failed'); } }); + await f.create(); + assert.equal(f.context.$activeNote, old); + assert.equal(f.selected.length, 0); + assert.equal(f.focused.length, 0); + assert.equal(f.errors.length, 1); +}); + +test('typing while the new-note read is pending must pass another save flush before selection', async () => { + const read = deferred(); + let flushes = 0; + const old = { path: '/vault/Old.md', content: 'New draft', raw: 'Old baseline' }; + const f = fixture({ $activeNote: old, $activeNotePath: old.path, + readNote: () => read.promise, onBeforeNoteSwitch: async () => ++flushes === 1 }); + const creating = f.create(); + await new Promise(resolve => setImmediate(resolve)); + f.context.$editorDirty = true; + read.resolve(f.created); + await creating; + assert.equal(flushes, 2); + assert.equal(f.context.$activeNote, old); + assert.equal(f.context.$editorDirty, true); + assert.equal(f.selected.length, 0); + assert.equal(f.focused.length, 0); +}); diff --git a/tests/note-link-paths.test.mjs b/tests/note-link-paths.test.mjs index 001af33..6c49616 100644 --- a/tests/note-link-paths.test.mjs +++ b/tests/note-link-paths.test.mjs @@ -19,7 +19,9 @@ const { isPathInDirectory, isTrashedNote, resolvePathFromFile, - resolveVaultFilePath + resolveVaultFilePath, + resolveFileReference, + noteAssetRoot } = await import( `data:text/javascript;base64,${Buffer.from(code).toString('base64')}` ); @@ -118,6 +120,28 @@ test('resolves vault files consistently across platforms', () => { ); }); +test('document URI suffixes are excluded from filesystem paths without double-decoding filename escapes', () => { + assert.deepEqual(resolveFileReference('.helixnotes/attachments/report%2520%29.pdf#page=2', '/vault/Notes/Note.md', '/vault'), { + path: '/vault/.helixnotes/attachments/report%20).pdf', suffix: '#page=2' + }); + assert.deepEqual(resolveFileReference('../files/report%23%3F.pdf?mode=fit#page=2', '/vault/Notes/Note.md', '/vault'), { + path: '/vault/files/report#?.pdf', suffix: '?mode=fit#page=2' + }); +}); + +test('older imported managed links stay inside their vault after moving to another notebook depth', () => { + for (const note of ['/vault/Projects/Nested/Note.md', '/vault/Note.md', '/vault/Archive/Note.md']) { + assert.equal(resolveFileReference('../../.helixnotes/attachments/report.pdf#page=2', note, '/vault').path, '/vault/.helixnotes/attachments/report.pdf'); + } +}); + +test('external source roots use configured ownership without borrowing the active vault', () => { + assert.equal(noteAssetRoot('/source/Notes/Note.md', '/destination', ['/source']), '/source'); + assert.equal(noteAssetRoot('/external/Note.md', '/destination', ['/source']), null); + assert.equal(noteAssetRoot('/destination-extra/Note.md', '/destination'), null); + assert.equal(noteAssetRoot('C:\\SOURCE\\Note.md', 'C:\\Destination', ['c:/Source']), 'c:/Source'); +}); + test('restored Windows and UNC notes remain vault-owned across separator and case differences', () => { assert.equal(isPathInDirectory('C:\\NOTES\\Alpha.md', 'c:\\Notes\\'), true); assert.equal(isPathInDirectory('C:/Notes/Folder/Alpha.md', 'C:\\Notes'), true); diff --git a/tests/outline.test.mjs b/tests/outline.test.mjs new file mode 100644 index 0000000..978a48a --- /dev/null +++ b/tests/outline.test.mjs @@ -0,0 +1,250 @@ +import assert from 'node:assert/strict'; +import test from 'node:test'; +import { ChangeSet, EditorState } from '@codemirror/state'; +import { Schema } from '@tiptap/pm/model'; +import { Mapping, StepMap, Transform } from '@tiptap/pm/transform'; +import { + activeHeadingIndex, + documentHeadings, + mapDocumentHeadings, + mapSourceHeadings, + sourceHeadings, +} from '../src/lib/editor/outline.ts'; + +const schema = new Schema({ + nodes: { + doc: { content: 'block+' }, + paragraph: { group: 'block', content: 'inline*' }, + heading: { group: 'block', content: 'inline*', attrs: { level: { default: 1 } } }, + blockquote: { group: 'block', content: 'block+' }, + bullet_list: { group: 'block', content: 'list_item+' }, + list_item: { content: 'block+' }, + code_block: { group: 'block', content: 'text*', code: true }, + text: { group: 'inline' }, + }, + marks: { strong: {}, em: {} }, +}); +const paragraph = (text) => schema.node('paragraph', null, text ? schema.text(text) : null); +const heading = (text, level = 1) => schema.node('heading', { level }, text ? schema.text(text) : null); +const stateFor = (source) => EditorState.create({ doc: source }); +const extract = (source) => sourceHeadings(stateFor(source)); +const sourceEntry = (source, raw, level, text, offset = source.indexOf(raw)) => ({ + level, text, pos: offset, end: offset + raw.length, +}); + +test('document headings are semantic descendants with exact node extents and plain marked text', () => { + const intro = paragraph('lead'); + const formatted = schema.node('heading', { level: 2 }, [ + schema.text('日本語😀', [schema.mark('strong')]), schema.text(' same', [schema.mark('em')]), + ]); + const quotedHeading = heading('same', 3); + const quote = schema.node('blockquote', null, quotedHeading); + const listedHeading = heading('same', 6); + const list = schema.node('bullet_list', null, schema.node('list_item', null, [paragraph('item'), listedHeading])); + const empty = heading('', 1); + const code = schema.node('code_block', null, schema.text('# not a heading')); + const doc = schema.node('doc', null, [intro, formatted, quote, list, empty, code, paragraph('# also not a heading')]); + const formattedPos = intro.nodeSize; + const quotePos = formattedPos + formatted.nodeSize; + const listPos = quotePos + quote.nodeSize; + const listedPos = listPos + 2 + paragraph('item').nodeSize; + const emptyPos = listPos + list.nodeSize; + assert.deepEqual(documentHeadings(doc), [ + { level: 2, text: '日本語😀 same', pos: formattedPos, end: formattedPos + formatted.nodeSize }, + { level: 3, text: 'same', pos: quotePos + 1, end: quotePos + 1 + quotedHeading.nodeSize }, + { level: 6, text: 'same', pos: listedPos, end: listedPos + listedHeading.nodeSize }, + { level: 1, text: '', pos: emptyPos, end: emptyPos + empty.nodeSize }, + ]); +}); + +test('source ATX levels use UTF-16 offsets and full heading extents', () => { + const source = 'intro 😀\n\n' + [1, 2, 3, 4, 5, 6].map((level) => `${'#'.repeat(level)} 日本語😀 ${level}`).join('\n'); + assert.deepEqual(extract(source), [1, 2, 3, 4, 5, 6].map((level) => { + const raw = `${'#'.repeat(level)} 日本語😀 ${level}`; + return sourceEntry(source, raw, level, `日本語😀 ${level}`); + })); +}); + +test('source labels render inline formatting, entities, links, image alt text and escaped markers', () => { + const source = '# **Bold** *日本語😀* [Link](https://example.test) ![Alt *text*](image.png) `a_*b` & \\*literal\\* HTML ==mark== H~2~ x^2^ ###'; + assert.deepEqual(extract(source), [{ + level: 1, text: 'Bold 日本語😀 Link Alt text a_*b & *literal* HTML mark H2 x2', pos: 0, end: source.length, + }]); +}); + +test('reference links and images use their visible labels rather than Markdown syntax', () => { + const source = '# [Reference][ref] and ![Image][img]\n\n[ref]: https://example.org\n[img]: image.png\n'; + assert.equal(extract(source)[0].text, 'Reference and Image'); +}); + +test('empty labels and duplicate labels remain distinct navigable headings', () => { + const source = '#\n## \n### ###\n# same\n# same'; + const raws = ['#', '## ', '### ###', '# same', '# same']; + let pos = 0; + const expected = raws.map((raw, index) => { + const entry = sourceEntry(source, raw, [1, 2, 3, 1, 1][index], index < 3 ? '' : 'same', pos); + pos += raw.length + 1; + return entry; + }); + assert.deepEqual(extract(source), expected); +}); + +test('fenced and indented code, escaped markers, and invalid ATX markers are excluded', () => { + const source = [ + '# Real', '', '```md', '# fenced', 'Setext', '------', '```', '', + '~~~', '## tilde fenced', '~~~', '', ' # indented', ' underline', ' =====', '', + '\\# escaped', '', '#no-space', '', '####### too many', '', + '> ```', '> # quoted code', '> ```', '', '## Last', + ].join('\n'); + assert.deepEqual(extract(source), [ + sourceEntry(source, '# Real', 1, 'Real'), sourceEntry(source, '## Last', 2, 'Last'), + ]); +}); + +test('closed display math never creates phantom ATX or Setext headings and preserves following offsets', () => { + const source = '$$\nx\n---\n# math heading\n$$\n\n# Real\n\n$$\n## More math\n$$\n\n## Last'; + assert.deepEqual(extract(source), [ + sourceEntry(source, '# Real', 1, 'Real'), sourceEntry(source, '## Last', 2, 'Last'), + ]); +}); + +test('math delimiters in fenced code and unclosed display math do not hide real Markdown headings', () => { + const source = '```tex\n$$\n# code\n```\n\n# Real\n\n$$\n# Unclosed'; + assert.deepEqual(extract(source), [ + sourceEntry(source, '# Real', 1, 'Real'), sourceEntry(source, '# Unclosed', 1, 'Unclosed'), + ]); +}); + +test('Setext headings and headings nested in quotes and lists retain semantic starts', () => { + const source = [ + 'Title 日本語😀', '====', '', 'Sub *title*', '----', '', + '> ## Quote', '', '- ### Listed', '', + '> Multi **line**', '> continued', '> ====', '', + '- List setext', ' -----', + ].join('\n'); + assert.deepEqual(extract(source), [ + sourceEntry(source, 'Title 日本語😀\n====', 1, 'Title 日本語😀'), + sourceEntry(source, 'Sub *title*\n----', 2, 'Sub title'), + sourceEntry(source, '## Quote', 2, 'Quote'), + sourceEntry(source, '### Listed', 3, 'Listed'), + sourceEntry(source, 'Multi **line**\n> continued\n> ====', 1, 'Multi line continued'), + sourceEntry(source, 'List setext\n -----', 2, 'List setext'), + ]); +}); + +test('source extraction fully parses headings beyond an unconfigured or partial language tree', () => { + const source = 'ordinary text\n\n'.repeat(2000) + '# Far heading'; + assert.deepEqual(extract(source), [sourceEntry(source, '# Far heading', 1, 'Far heading')]); +}); + +test('raw HTML blocks do not create Markdown outline entries or expose heading-looking text', () => { + const source = '

HTML title

\n\n
\n# HTML block content\n
\n\n# Markdown'; + assert.deepEqual(extract(source), [sourceEntry(source, '# Markdown', 1, 'Markdown')]); +}); + +test('active heading uses the latest start, including boundaries and body content', () => { + const headings = [ + { level: 1, text: 'same', pos: 5, end: 10 }, + { level: 2, text: 'same', pos: 20, end: 25 }, + { level: 1, text: '', pos: 35, end: 36 }, + ]; + for (const [pos, index] of [[0, -1], [4, -1], [5, 0], [10, 0], [19, 0], [20, 1], [34, 1], [35, 2], [100, 2]]) { + assert.equal(activeHeadingIndex(headings, pos), index); + } + assert.equal(activeHeadingIndex([], 0), -1); + assert.equal(activeHeadingIndex([{ ...headings[0], pos: 0 }], 0), 0); +}); + +test('source anchors map insertions before/at the heading start without absorbing end insertions', () => { + const state = stateFor('lead\n# One\n\n## Two'); + const original = sourceHeadings(state); + const before = state.update({ changes: { from: original[0].pos, insert: 'prefix\n' } }); + const shifted = mapSourceHeadings(original, before.changes.desc); + assert.deepEqual(shifted, sourceHeadings(before.state)); + assert.deepEqual(original, sourceHeadings(state)); + const atEnd = before.state.update({ changes: { from: shifted[0].end, insert: '\nnew body' } }); + assert.deepEqual(mapSourceHeadings(shifted, atEnd.changes.desc), sourceHeadings(atEnd.state)); + const inLabel = atEnd.state.update({ changes: { from: shifted[0].pos + 2, insert: 'New ' } }); + const mapped = mapSourceHeadings(mapSourceHeadings(shifted, atEnd.changes.desc), inLabel.changes.desc); + assert.equal(mapped[0].pos, sourceHeadings(inLabel.state)[0].pos); + assert.equal(mapped[0].end, sourceHeadings(inLabel.state)[0].end); + assert.equal(mapped[0].text, 'One'); // Labels refresh with the debounced extraction. +}); + +test('source anchors discard deletion/replacement instead of jumping to a duplicate or unrelated heading', () => { + const state = stateFor('# same\n\n# same\n\n# last'); + const original = sourceHeadings(state); + for (const insert of ['', '# unrelated']) { + const transaction = state.update({ changes: { from: original[0].pos, to: original[0].end, insert } }); + const mapped = mapSourceHeadings(original, transaction.changes.desc); + assert.deepEqual(mapped, sourceHeadings(transaction.state).slice(insert ? 1 : 0)); + } + const markerDeleted = state.update({ changes: { from: 0, to: 1 } }); + assert.deepEqual(mapSourceHeadings(original, markerDeleted.changes.desc), sourceHeadings(markerDeleted.state)); + const covering = state.update({ changes: { from: 0, to: original[1].end, insert: '# replacement' } }); + assert.deepEqual(mapSourceHeadings(original, covering.changes.desc), sourceHeadings(covering.state).slice(1)); +}); + +test('source mapping handles simultaneous and composed edits while preserving surviving anchors', () => { + const state = stateFor('# first\n\n# second\n\n# third'); + const original = sourceHeadings(state); + const first = state.update({ changes: [ + { from: 0, insert: 'intro\n\n' }, + { from: original[1].pos, to: original[1].end, insert: 'body' }, + ] }); + const second = first.state.update({ changes: { from: 0, insert: '😀\n' } }); + const composed = first.changes.desc.composeDesc(second.changes.desc); + assert.deepEqual(mapSourceHeadings(original, composed), sourceHeadings(second.state)); + assert.deepEqual(mapSourceHeadings(original, ChangeSet.empty(state.doc.length)), original); +}); + +test('document anchors map transactions before debounce and exclude insertions at the end', () => { + const doc = schema.node('doc', null, [paragraph('lead'), heading('one'), heading('two', 2)]); + const original = documentHeadings(doc); + const transform = new Transform(doc).insert(original[0].pos, paragraph('intro')); + let mapped = mapDocumentHeadings(original, transform.mapping); + assert.deepEqual(mapped, documentHeadings(transform.doc)); + const inserted = new Transform(transform.doc).insert(mapped[0].end, paragraph('body')); + mapped = mapDocumentHeadings(mapped, inserted.mapping); + assert.deepEqual(mapped, documentHeadings(inserted.doc)); + const edited = new Transform(inserted.doc).insert(mapped[0].pos + 1, schema.text('new ')); + const editedMapped = mapDocumentHeadings(mapped, edited.mapping); + assert.equal(editedMapped[0].pos, documentHeadings(edited.doc)[0].pos); + assert.equal(editedMapped[0].end, documentHeadings(edited.doc)[0].end); + assert.deepEqual(original, documentHeadings(doc)); + assert.deepEqual(mapDocumentHeadings(original, new Mapping()), original); +}); + +test('document deletion and replacement never reuse an obsolete heading opening token', () => { + const doc = schema.node('doc', null, [heading('same'), heading('same'), heading('last')]); + const original = documentHeadings(doc); + for (const replacement of [null, paragraph('body'), heading('unrelated')]) { + const transform = new Transform(doc); + if (replacement) transform.replaceWith(original[0].pos, original[0].end, replacement); + else transform.delete(original[0].pos, original[0].end); + const fresh = documentHeadings(transform.doc); + assert.deepEqual(mapDocumentHeadings(original, transform.mapping), replacement?.type.name === 'heading' ? fresh.slice(1) : fresh); + } + const transform = new Transform(doc).insert(0, paragraph('intro')); + const shifted = documentHeadings(transform.doc); + transform.replaceWith(shifted[1].pos, shifted[1].end, paragraph('body')); + assert.deepEqual(mapDocumentHeadings(original, transform.mapping), documentHeadings(transform.doc)); +}); + +test('document mappings respect their selected step range', () => { + const headings = [{ level: 1, text: 'one', pos: 5, end: 10 }]; + const mapping = new Mapping([new StepMap([0, 0, 3]), new StepMap([0, 0, 2])]); + assert.deepEqual(mapDocumentHeadings(headings, mapping.slice(1)), [{ ...headings[0], pos: 7, end: 12 }]); +}); + +test('math delimiters in tilde fences cannot mask later real headings', () => { + const source = '~~~tex\n$$\n$x$\n~~~\n\n# Real\n\n$$\nunclosed'; + assert.deepEqual(extract(source), [sourceEntry(source, '# Real', 1, 'Real')]); +}); + +test('masking display math retains list and quote context for following headings', () => { + const source = '- Outer\n - $$\n x\n ---\n $$\n\n ## Nested\n\n> $$\n> y\n> ---\n> $$\n> ## Quoted'; + assert.deepEqual(extract(source), [ + sourceEntry(source, '## Nested', 2, 'Nested'), sourceEntry(source, '## Quoted', 2, 'Quoted'), + ]); +}); diff --git a/tests/viewer-import.test.mjs b/tests/viewer-import.test.mjs new file mode 100644 index 0000000..b323773 --- /dev/null +++ b/tests/viewer-import.test.mjs @@ -0,0 +1,109 @@ +import assert from 'node:assert/strict'; +import { readFile } from 'node:fs/promises'; +import test from 'node:test'; +import { createContext, Script } from 'node:vm'; +import { parse } from 'svelte/compiler'; +import { transformWithEsbuild } from 'vite'; + +const source = await readFile(new URL('../src/lib/components/Editor.svelte', import.meta.url), 'utf8'); +const declaration = parse(source, { modern: true }).instance.content.body.find(node => + node.type === 'FunctionDeclaration' && node.id.name === 'viewerImportTo' +); +const { code } = await transformWithEsbuild(source.slice(declaration.start, declaration.end), 'viewer-import.ts', { + loader: 'ts', format: 'esm', target: 'esnext' +}); +const importScript = new Script(`${code}\nviewerImportTo;`); + +function fixture(path = '/external/External.md') { + const original = { path, meta: { title: 'External' }, + content: '![Image](images/photo.png)', raw: '---\ncustom: keep\n---\n![Image](images/photo.png)' }; + const imported = { ...original, path: '/vault/External.md', + content: '![Image](.helixnotes/attachments/unique-photo.png)', + raw: '---\ncustom: keep\n---\n![Image](.helixnotes/attachments/unique-photo.png)' }; + let current = true; + const calls = []; + const loaded = []; + const messages = []; + const files = new Map([[path, original]]); + const context = createContext({ + beginEditorNoteNavigation: () => () => current, + $viewerNote: { path, content: original.content }, $activeNote: original, + $appConfig: { active_vault: '/vault' }, $readOnly: true, $focusMode: true, + $activeNotePath: path, $editorDirty: false, viewerImportBusy: false, viewerImportPickerOpen: true, + readNote: async (_vault, target) => { + if (!files.has(target)) throw new Error('Not found'); + return files.get(target); + }, + copyFileTo: async (origin, target) => files.set(target, { ...files.get(origin), path: target }), + externalNoteReferences: () => [{ from: 24, to: 40, href: 'images/photo.png', html: false }], + importNote: async (...args) => { calls.push(args); files.set(imported.path, imported); return imported; }, + loadNote: (target, content) => loaded.push({ target, content }), + viewerFlash: message => messages.push(message), console: { error() {} } + }); + return { context, original, imported, calls, loaded, messages, + cancel: () => { current = false; }, run: importScript.runInContext(context) }; +} + +test('notebook destinations use portable relative paths rather than duplicating the vault path', async () => { + const ast = parse(source, { modern: true }); + const declaration = ast.instance.content.body.flatMap(node => node.declarations ?? []).find(node => node.id.name === 'viewerFlatNotebooks'); + const expression = declaration.init.arguments[0]; + const transformed = await transformWithEsbuild(source.slice(expression.start, expression.end), 'viewer-notebooks.ts', { loader: 'ts', format: 'esm' }); + const list = new Script(`(${transformed.code.replace(/;\s*$/, '')})()`).runInNewContext({ + viewerImportPickerOpen: true, + $notebooks: [{ path: 'C:\\Vault\\Projects', relative_path: 'Projects', name: 'Projects', children: [ + { path: 'C:\\Vault\\Projects\\Nested', relative_path: 'Projects\\Nested', name: 'Nested', children: [] } + ] }] + }); + assert.deepEqual(Array.from(list, entry => entry.path), ['Projects', 'Projects/Nested']); +}); + +test('Windows viewer imports use the native filename instead of treating a full path as a filename', async () => { + const f = fixture('C:\\Users\\writer\\External.md'); + await f.run(''); + assert.equal(f.context.$activeNotePath, '/vault/External.md'); + assert.equal(f.context.$viewerNote, null); +}); + +test('viewer import uses the accepted raw note and semantic attachment references in one native operation', async () => { + const f = fixture(); + await f.run('Projects'); + assert.equal(f.calls.length, 1, 'copying Markdown alone must not bypass attachment import'); + const [vault, path, notebook, raw, references] = f.calls[0]; + assert.equal(vault, '/vault'); + assert.equal(path, f.original.path); + assert.equal(notebook, 'Projects'); + assert.equal(raw, f.original.raw); + assert.equal(references[0].href, 'images/photo.png'); + assert.equal(f.loaded[0].content, f.imported.content); + assert.equal(f.original.content, '![Image](images/photo.png)'); +}); + +test('failed native import leaves the original read-only viewer selected and allows retry', async () => { + const f = fixture(); + f.context.importNote = async () => { throw new Error('Attachment permission denied'); }; + await f.run(''); + assert.equal(f.context.$activeNote, f.original); + assert.equal(f.context.$viewerNote.path, f.original.path); + assert.equal(f.context.$readOnly, true); + assert.equal(f.context.viewerImportBusy, false); + assert.match(f.messages[0], /Attachment permission denied/); +}); + +test('an import completion cannot steal selection after the viewer is closed or navigation changes', async () => { + const f = fixture(); + let finish; + f.context.importNote = () => new Promise(resolve => { finish = resolve; }); + const importing = f.run(''); + await new Promise(resolve => setImmediate(resolve)); + f.cancel(); + const newer = { path: '/vault/Newer.md', content: 'Unsaved draft' }; + f.context.$activeNote = newer; + f.context.$activeNotePath = newer.path; + f.context.$viewerNote = null; + if (finish) finish(f.imported); + await importing; + assert.equal(typeof finish, 'function', 'the viewer must delegate to the native import'); + assert.equal(f.context.$activeNote, newer); + assert.equal(f.loaded.length, 0); +});