Skip to main content

LinkIndex

Struct LinkIndex 

Source
pub struct LinkIndex {
    pub collisions: usize,
    /* private fields */
}
Expand description

A suffix index: every file is reachable by any suffix of its path.

§Examples

use markdown_link::LinkIndex;

let index = LinkIndex::build(&["notes/a.md".to_string(), "deep/er/b.md".to_string()]);
assert_eq!(index.resolve("a"), Some("notes/a.md"));
assert_eq!(index.resolve("er/b"), Some("deep/er/b.md"));
assert_eq!(index.resolve("missing"), None);

Fields§

§collisions: usize

How many times a path claimed a key that a different path already held while the index was built.

Implementations§

Source§

impl LinkIndex

Source

pub fn build(paths: &[String]) -> LinkIndex

Build from vault-relative paths (with extensions).

Shortest path first, measured in CHARACTERS (not depth), then deterministic collation. Character length is the observed tie-break: x/y/tie.md (10 chars, 3 segments) beats length-versus-depth-long-folder/tie.md (38 chars, 2 segments).

§Examples
use markdown_link::LinkIndex;

let index = LinkIndex::build(&["x/y/tie.md".to_string(), "long-folder/tie.md".to_string()]);
// The shorter path wins the bare name.
assert_eq!(index.resolve("tie"), Some("x/y/tie.md"));
assert_eq!(index.resolve("long-folder/tie"), Some("long-folder/tie.md"));
assert!(index.collisions > 0);
Source

pub fn resolve(&self, target: &str) -> Option<&str>

Resolve one target to a vault-relative path, or None.

Source-independent, and therefore the same answer everywhere in the vault. Equivalent to LinkIndex::resolve_from called from the vault root, where the folder preference cannot apply.

§Examples
use markdown_link::LinkIndex;

let index = LinkIndex::build(&["notes/glutes.md".to_string()]);
assert_eq!(index.resolve("glutes"), Some("notes/glutes.md"));
// A pathed miss falls back to its basename.
assert_eq!(index.resolve("muscle-groups/glutes"), Some("notes/glutes.md"));
assert_eq!(index.resolve("nothing"), None);
Source

pub fn resolve_from(&self, target: &str, from: &str) -> Option<&str>

Resolve one target as it was written in from, a vault-relative path.

Differs from LinkIndex::resolve in two cases, the only ones where where the link sits can change the answer. A ./ or ../ target names the file at that path from the linking note’s folder (see join_relative). An AMBIGUOUS name — several files answer to it — takes two observed behaviours, in order: a candidate inside the linking note’s own folder beats one outside it, and otherwise the shorter full path wins.

The folder test is a plain string prefix, not a segment-aligned one, which is what the application does: a note in notes/ also prefers a candidate under notes-archive/. Small, real, and deliberately matched — a reader seeing one file while a rename rewrites a different one is worse than either rule alone.

§Examples
use markdown_link::LinkIndex;

let index = LinkIndex::build(&["b/a.md".to_string(), "notes/a.md".to_string(), "notes/x.md".to_string()]);
// Ambiguous name: the candidate in the linking note's folder wins.
assert_eq!(index.resolve_from("a", "notes/x.md"), Some("notes/a.md"));
assert_eq!(index.resolve_from("a", "other.md"), Some("b/a.md"));
// A `./` target is relative to the linking note.
assert_eq!(index.resolve_from("./a", "notes/x.md"), Some("notes/a.md"));
Source

pub fn len(&self) -> usize

The number of distinct keys (normalised path suffixes) in the index. This is not the number of files.

§Examples
use markdown_link::LinkIndex;

let index = LinkIndex::build(&["a/b.md".to_string()]);
assert_eq!(index.len(), 2); // "b" and "a/b"
Source

pub fn is_empty(&self) -> bool

Whether the index holds no keys.

§Examples
use markdown_link::LinkIndex;

assert!(LinkIndex::build(&[]).is_empty());
assert!(!LinkIndex::build(&["a.md".to_string()]).is_empty());

Trait Implementations§

Source§

impl Clone for LinkIndex

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for LinkIndex

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for LinkIndex

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.