Skip to content

Commit 8ca9cd7

Browse files
committed
Index wikilinks in frontmatter strings (#1000)
1 parent 4bc6ed7 commit 8ca9cd7

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

plugs/index/page_links.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
collectNodesOfType,
23
findNodeOfType,
34
renderToText,
45
traverseTree,
@@ -206,6 +207,42 @@ export async function indexLinks({ name, tree }: IndexTreeEvent) {
206207
}
207208
}
208209
}
210+
211+
// Also index links used inside quoted frontmatter strings like "[[Page]]"
212+
// must match the full string node, only allowing for quotes and whitespace around it
213+
if (n.type === "FrontMatter") {
214+
// The YAML in frontmatter is parsed by CodeMirror itself
215+
for (const textNode of collectNodesOfType(n, "string")) {
216+
const text = textNode.children![0].text!;
217+
const trimmed = text.replace(/^["'\s]*/, "").replace(/["'\s]*$/, "");
218+
// Make sure we search from the beginning, when reusing a Regex object with global flag
219+
wikiLinkRegex.lastIndex = 0;
220+
const match = wikiLinkRegex.exec(text);
221+
// Search in entire node text to get correct position, but check for full match against trimmed
222+
if (match && match[0] === trimmed) {
223+
const [_fullMatch, firstMark, url, alias, _lastMark] = match;
224+
const pos = textNode.from! + match.index! + firstMark.length;
225+
const link: any = {
226+
ref: `${name}@${pos}`,
227+
tag: "link",
228+
page: name,
229+
snippet: extractSnippetAroundIndex(pageText, pos),
230+
pos: pos,
231+
asTemplate: false,
232+
};
233+
if (looksLikePathWithExtension(url)) {
234+
link.toFile = resolvePath(name, url);
235+
} else {
236+
link.toPage = resolvePath(name, parsePageRef(url).page);
237+
}
238+
if (alias) {
239+
link.alias = alias;
240+
}
241+
updateITags(link, frontmatter);
242+
links.push(link);
243+
}
244+
}
245+
}
209246
return false;
210247
});
211248

website/YAML.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
11
YAML stands for “YAML Ain’t Markup Language.” More information can be found at [the YAML website](https://yaml.org/).
22

3-
SilverBullet uses YAML in various contexts, specifically [[Frontmatter]].
3+
SilverBullet uses YAML in various contexts, specifically [[Frontmatter]] and [[Space Config]]
4+
5+
# Internal links
6+
Many string values can be written directly in YAML without any quoting, like:
7+
```yaml
8+
property: value
9+
```
10+
11+
However when you want to reference [[Links|a page]] or [[Command links|command]] you will need to quote the full link:
12+
```yaml
13+
some page: "[[Pages]]"
14+
list of pages:
15+
- "[[Pages]]"
16+
- "[[Links]]"
17+
```
18+
19+
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:
20+
```yaml
21+
some page: [[Pages]]
22+
equivalent yaml: [
23+
[ "Pages" ]
24+
]
25+
```

0 commit comments

Comments
 (0)