Skip to content
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

Index wikilinks in frontmatter strings #1066

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions plugs/index/page_links.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
collectNodesOfType,
findNodeOfType,
renderToText,
traverseTree,
Expand Down Expand Up @@ -206,6 +207,40 @@ export async function indexLinks({ name, tree }: IndexTreeEvent) {
}
}
}

// Also index links used inside quoted frontmatter strings like "[[Page]]"
// must match the full string node, only allowing for quotes and whitespace around it
if (n.type === "FrontMatter") {
// The YAML in frontmatter is parsed by CodeMirror itself
for (const textNode of collectNodesOfType(n, "string")) {
const text = textNode.children![0].text!;
const trimmed = text.replace(/^["'\s]*/, "").replace(/["'\s]*$/, "");
const match = wikiLinkRegex.exec(text);
// search in entire node text to get correct position, but check for full match against trimmed
if (match && match[0] === trimmed) {
const [_fullMatch, firstMark, url, alias, _lastMark] = match;
const pos = textNode.from! + match.index! + firstMark.length;
const link: any = {
ref: `${name}@${pos}`,
tag: "link",
page: name,
snippet: extractSnippetAroundIndex(pageText, pos),
pos: pos,
asTemplate: true,
};
if (looksLikePathWithExtension(url)) {
link.toFile = resolvePath(name, url);
} else {
link.toPage = resolvePath(name, parsePageRef(url).page);
}
if (alias) {
link.alias = alias;
}
updateITags(link, frontmatter);
links.push(link);
}
}
}
return false;
});

Expand Down
24 changes: 23 additions & 1 deletion website/YAML.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
YAML stands for “YAML Ain’t Markup Language.” More information can be found at [the YAML website](https://yaml.org/).

SilverBullet uses YAML in various contexts, specifically [[Frontmatter]].
SilverBullet uses YAML in various contexts, specifically [[Frontmatter]] and [[Space Config]]

# Internal links
Many string values can be written directly in YAML without any quoting, like:
```yaml
property: value
```

However when you want to reference [[Links|a page]] or [[Command links|command]] you will need to quote the full link:
```yaml
some page: "[[Pages]]"
list of pages:
- "[[Pages]]"
- "[[Links]]"
```

This is because the square brackets used in the internal link format have a meaning YAML as well. So an unquoted link is parsed as list inside a list:
```yaml
some page: [[Pages]]
equivalent yaml: [
[ "Pages" ]
]
```
Loading