Skip to content

getLinkAtPosition

Finds the link at a position in a note’s source text.

In the body:

  1. A link is hit from the column of its first character to the column just past its last, both included. 2. When two links touch ([a](x){cursor}[b](y)), the one the cursor is before wins. 3. A line inside a fenced code block, or a fence line itself, holds no link. Indentation is ignored, so a nested list item is not read as an indented code block.

In the frontmatter:

  1. The quotes belong to the link: key: {cursor}"[[note]]" and key: "[[note]]"{cursor} both hit. So do unquoted values, flow sequences, block sequence items and Markdown links. 2. A position on the key, the sequence dash, the indentation or the gap before the value hits only when the value holds exactly one link, as {cursor}key: "[[note]]" does. A key with an empty value counts the links on its nested lines. Several links give null, since nothing says which one is meant. 3. The commas and spaces between two entries (["[[a]]",{cursor} "[[b]]"]) hit the entry after them. 4. Nothing in a comment is a link, and the delimiter lines hold none.

Positions are those of the YAML source text. The Properties view is a widget, not editor text, so it has no positions to look up.

Import:

import { getLinkAtPosition } from '@mnaoumov/obsidian-lib/link-at-position';

Signature:

function getLinkAtPosition(params: GetLinkAtPositionParams): LinkAtPosition | null

Parameters:

Parameter Type Description
params GetLinkAtPositionParams The lines and the position.

Returns: LinkAtPosition | null — The link, or null when the position is not on one.


Links to this page: