Expand description
Parsing and resolution of Markdown and wiki-style links.
Finds [[wikilinks]], ![[embeds]] and relative markdown links in a note
body or its frontmatter, and resolves a link target to a file path with
LinkIndex, using a path-suffix index.
use markdown_link::{LinkIndex, body_links};
let links = body_links("See [[Target#Heading|alias]].");
assert_eq!(links[0].target, "Target");
let index = LinkIndex::build(&["notes/Target.md".to_string()]);
assert_eq!(index.resolve("Target"), Some("notes/Target.md"));Two details that look arbitrary and are not:
- Code is stripped before matching. A fenced block full of
[[example]]would otherwise manufacture links that do not exist, and the link graph would grow edges nobody wrote. - A pathed miss falls back to its basename. Notes often write typed
links like
[[muscle-groups/glutes]]whose prefix is a collection alias, not a real folder. Dropping the prefix on a miss is what makes those resolve; without it the graph is full of ghost nodes for links that a human would call obviously correct.
Re-exports§
pub use code::strip_code;
Modules§
- code
- Code fences and inline code spans: finding them, and blanking them so a lexer never harvests out of them.
Structs§
- Body
Link - One outgoing reference found in a body, with its span, before resolution.
- Link
Index - A suffix index: every file is reachable by any suffix of its path.
- RawLink
- One reference found in a note, before it is resolved.
- Scan
Options - What a body scan counts as a link.
ScanOptions::defaultis whatbody_links_spannedandcrate::body_linksuse. - Size
- An embed’s display size,
|300(width) or|300x200(width and height). - Typed
Ref - A typed reference,
[[type/slug]]or[[type/slug|Alias]]: a frontmatter value naming one note of a known type, such aslocation: "[[location/home]]". - Wiki
Link - A parsed
[[target#heading|alias]].
Enums§
- Body
Link Kind - What syntax produced a
BodyLink. - Link
Kind - What kind of reference produced a
RawLink. - Resolve
Error - Why
LinkIndex::resolve_strictdid not resolve a target.
Constants§
- ATTACHMENT_
EXTENSIONS - Every non-markdown extension that counts as a vault ATTACHMENT (lowercase, no leading dot).
Functions§
- body_
links - Every wikilink, embed and relative markdown link in a note body, in the order they appear.
- body_
links_ spanned - Every wikilink, embed and relative markdown link in a body, in encounter order, with byte spans.
- body_
links_ spanned_ with body_links_spannedunderoptions: which code rules apply, whether HTML comments hide links, and whether[[#Heading]]is a link.- display_
text - Replace each wikilink and embed with what a reader sees for it, for
snippets and search: its alias, or else its target. An embed’s trailing
|300or|300x200is a size, not an alias, so![[img.png|300]]showsimg.png. An empty alias shows the target. Code is not skipped. - encode_
uri_ component - JavaScript’s
encodeURIComponent: every byte of the UTF-8 text exceptA-Z a-z 0-9 - _ . ! ~ * ' ( )is percent-encoded. - encode_
uri_ fragment - JavaScript’s
encodeURI, with#also encoded, for a fragment. URI structure characters (/ ? : @ & = + $ ; ,) are kept, so a fragment such aspage=2reads back as written. - escape_
label - Markdown link text with
\,[and]escaped, so a label cannot end or nest the link it sits in. - frontmatter_
links - Wikilinks hiding in frontmatter values, tagged with their top-level key.
- frontmatter_
paths - Frontmatter string values that name a file by path (
cover: img/a.png), tagged with their top-level key. A value counts when its last segment has an extension; a URL, a wikilink or text with a line break does not. Whether the path exists is the resolver’s question. - is_
size_ spec - Whether
specis an image size,^\d+(x\d+)?$:300or300x200. - is_
vault_ link_ url - Whether a markdown link’s URL is one this dialect harvests: a note, or a vault attachment.
- join_
relative targetjoined ontofrom’s folder when it starts with./or../, with the dot segments collapsed.Nonefor any other target, and for one that climbs above the vault root.- may_
hold_ frontmatter_ paths - A cheap test of raw frontmatter text: whether any token on any line could
be a path
frontmatter_pathskeeps. False means it would find none, so a caller can skip the YAML parse. It errs toward true: a false positive costs a parse, a false negative a missed link. - md_
link_ target - The resolution target of a markdown link’s URL: percent-decoded, minus its
trailing
.md. - normalize_
target - Normalise a link target to an index key.
- parse_
inner - Parse a single wikilink body (the text between the brackets).
- parse_
inner_ self parse_inner, except that a link with no target and a#fragment, such as#Headingor#^block, is a link to the note it is written in: its target is empty. This is how Obsidian reads[[#Heading]].- parse_
typed_ ref - Parse a whole value as a typed reference, or
None. - percent_
decode - Percent-decode a markdown link URL. Public for the same reason
strip_md_extis. - prefer_
in_ folder - Pick the winner from a shortest-first candidate field, as written in
from. - relative_
link - The relative URL from the file
fromto the fileto, both vault-relative and/-separated, with an optional#fragment. - strip_
md_ ext - Drop a trailing
.md, case-insensitively. - suffixes
a/b/c->["c", "b/c", "a/b/c"].- target_
range - Byte range of
parse_inner’s target withininner, for a caller that rewrites the target in place and must leave the rest of the link as written. - wikilink_
targets - Every reference in one piece of markdown, in the order it appears, with
any
#headingand|aliasstripped.