Repository navigation
Fix notebook folder selection on Android and the Linux default #729
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
DWAA1660
merged 1 commit into
AlienTechSoftware:main
from
DWAA1660:fix/notebook-folder-defaults
Dec 5, 2026
+275
−21
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| //! Linux's per-user notebook default (issue #705). | ||
| use std::path::{Path, PathBuf}; | ||
|
|
||
| pub(super) fn notebooks_dir(home: &Path, config_home: Option<&Path>) -> PathBuf { | ||
| let config = config_home.filter(|path| path.is_absolute()).map_or_else(|| home.join(".config"), Path::to_path_buf); | ||
| std::fs::read_to_string(config.join("user-dirs.dirs")) | ||
| .ok() | ||
| .and_then(|settings| documents_dir(&settings, home)) | ||
| .map_or_else(|| super::default_notebooks_dir_for_home(home), |documents| documents.join("NoteCraft")) | ||
| } | ||
|
|
||
| fn documents_dir(settings: &str, home: &Path) -> Option<PathBuf> { | ||
| let value = settings.lines().find_map(|line| { | ||
| let (key, value) = line.split_once('=')?; | ||
| (key.trim() == "XDG_DOCUMENTS_DIR").then_some(value.trim()) | ||
| })?; | ||
| let quoted = value.strip_prefix('"')?; | ||
| let relative = quoted.strip_prefix("$HOME/").or_else(|| quoted.strip_prefix("$HOME").filter(|rest| rest.starts_with('"'))); | ||
| let path = quoted_path(relative.unwrap_or(quoted))?; | ||
| if relative.is_some() { | ||
| return Some(home.join(path.trim_start_matches('/'))); | ||
| } | ||
| path.starts_with('/').then(|| PathBuf::from(path)) | ||
| } | ||
|
|
||
| // XDG writes double-quoted shell assignments. Decode the path as data and expand only the | ||
| // documented leading $HOME, without running the file or expanding other shell expressions. | ||
| fn quoted_path(value: &str) -> Option<String> { | ||
| let mut chars = value.chars(); | ||
| let mut path = String::new(); | ||
| while let Some(c) = chars.next() { | ||
| match c { | ||
| '"' => { | ||
| let tail = chars.as_str().trim_start(); | ||
| return (tail.is_empty() || tail.starts_with('#')).then_some(path); | ||
| } | ||
| '\\' => { | ||
| let escaped = chars.next()?; | ||
| if !matches!(escaped, '\\' | '"' | '$' | '`') { | ||
| path.push('\\'); | ||
| } | ||
| path.push(escaped); | ||
| } | ||
| '$' | '`' => return None, | ||
| _ => path.push(c), | ||
| } | ||
| } | ||
| None | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn linux_notebook_default_falls_back_for_missing_or_invalid_config() { | ||
| let tmp = crate::testing::TempDir::new("xdg-fallback"); | ||
| let home = tmp.path(); | ||
| let config = home.join(".config"); | ||
| assert_eq!(notebooks_dir(home, None), home.join("Documents").join("NoteCraft")); | ||
| std::fs::create_dir(&config).expect("config folder"); | ||
| let file = config.join("user-dirs.dirs"); | ||
| std::fs::write(&file, "XDG_DOCUMENTS_DIR=\"relative/folder\"\n").expect("invalid value"); | ||
| assert_eq!(notebooks_dir(home, None), home.join("Documents").join("NoteCraft")); | ||
| std::fs::write(&file, "XDG_DOCUMENTS_DIR=\"$HOME/Docs\"\n").expect("documents folder"); | ||
| assert_eq!(notebooks_dir(home, Some(Path::new("relative"))), home.join("Docs").join("NoteCraft")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn xdg_value_accepts_absolute_paths_and_literals() { | ||
| let home = Path::new("/home/user"); | ||
| let settings = r#"XDG_DOCUMENTS_DIR="/media/My \$avings" # mounted disk"#; | ||
| assert_eq!(documents_dir(settings, home), Some(PathBuf::from("/media/My $avings"))); | ||
| assert_eq!(documents_dir(r#"XDG_DOCUMENTS_DIR="$HOME""#, home), Some(home.to_path_buf())); | ||
| assert_eq!(documents_dir(r#"XDG_DOCUMENTS_DIR="$HOME/$(other)""#, home), None); | ||
| } | ||
|
|
||
| #[test] | ||
| fn linux_notebook_default_uses_xdg_documents() { | ||
| let tmp = crate::testing::TempDir::new("xdg-notebooks"); | ||
| let home = tmp.path(); | ||
| let config = home.join("config"); | ||
| std::fs::create_dir(&config).expect("config folder"); | ||
| std::fs::write(config.join("user-dirs.dirs"), "XDG_DOCUMENTS_DIR=\"$HOME/Documentos compartidos\"\n").expect("XDG config"); | ||
| assert_eq!(notebooks_dir(home, Some(&config)), home.join("Documentos compartidos").join("NoteCraft")); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Duplicate
XDG_DOCUMENTS_DIRentries resolve to the first valid value, not the last.documents_dirreturns the first validXDG_DOCUMENTS_DIRassignment. The canonicalxdg-user-dir-lookupreader and shellsourceboth give the last valid assignment precedence. For a file that containsXDG_DOCUMENTS_DIR="$HOME/Documents"followed byXDG_DOCUMENTS_DIR="/mnt/Documents", NoteCraft stores notebooks under$HOME/Documents/NoteCraft, while other XDG tools use/mnt/Documents.Select the last valid
XDG_DOCUMENTS_DIRassignment. Collect the valid matches and take the last one, for example withfilter_mapfollowed bylast()in place offind_map. Keep skipping invalid lines, so an invalid later line does not replace an earlier valid value.