Skip to content

Commit

Permalink
Index wikilinks in frontmatter strings (#1000) (#1066)
Browse files Browse the repository at this point in the history
  • Loading branch information
Maarrk authored Oct 11, 2024
1 parent 64e398f commit 7b955ef
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
37 changes: 37 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,42 @@ 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]*$/, "");
// Make sure we search from the beginning, when reusing a Regex object with global flag
wikiLinkRegex.lastIndex = 0;
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: false,
};
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 in YAML as well. So an unquoted link is parsed as list inside a list:
```yaml
some page: [[Pages]]
equivalent yaml: [
[ "Pages" ]
]
```

0 comments on commit 7b955ef

Please sign in to comment.