|
1 | 1 | import {
|
| 2 | + collectNodesOfType, |
2 | 3 | findNodeOfType,
|
3 | 4 | renderToText,
|
4 | 5 | traverseTree,
|
@@ -206,6 +207,42 @@ export async function indexLinks({ name, tree }: IndexTreeEvent) {
|
206 | 207 | }
|
207 | 208 | }
|
208 | 209 | }
|
| 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 | + } |
209 | 246 | return false;
|
210 | 247 | });
|
211 | 248 |
|
|
0 commit comments