@operad-io/memory 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,224 @@
1
+ /**
2
+ * The link check the nightly tidy reads: broken links with their source line,
3
+ * notes nothing links to, targets that several notes already name, and cheap
4
+ * suggestions where a note mentions another note's title without linking it.
5
+ * It answers from the link table, so a caller refreshes the index first; it
6
+ * writes nothing and keeps no state of its own.
7
+ */
8
+
9
+ import { parseNoteLinksWithPositions, withoutCode } from "./links.ts";
10
+ import { untrustedFields, type MemorySearch } from "./search/service.ts";
11
+ import type { DocumentKey, IndexedDocument, LinkQuery } from "./search/types.ts";
12
+
13
+ /** How many unlinked mentions one report suggests at most. */
14
+ export const UNLINKED_MENTION_LIMIT = 20;
15
+
16
+ /** A link whose target no note has, with the line that writes it. */
17
+ export interface BrokenNoteLink {
18
+ /** The note that writes the link, inside its scope. */
19
+ readonly path: string;
20
+ /** The 1-based line in the source note. */
21
+ readonly line: number;
22
+ /** The target as the note wrote it. */
23
+ readonly target: string;
24
+ }
25
+
26
+ /** A target several notes link to while no note has that name. */
27
+ export interface WantedTarget {
28
+ readonly target: string;
29
+ /** How many different notes link to it. */
30
+ readonly notes: number;
31
+ }
32
+
33
+ /** A source note that names another note's title without linking it. */
34
+ export interface UnlinkedMention {
35
+ /** The note whose body contains the title. */
36
+ readonly path: string;
37
+ /** The title of the note that is not linked. */
38
+ readonly title: string;
39
+ /** The path of the note that is not linked. */
40
+ readonly target: string;
41
+ }
42
+
43
+ /** The four lists a tidy reads about the links in one scope. */
44
+ export interface LinkReport {
45
+ readonly broken: readonly BrokenNoteLink[];
46
+ readonly orphans: readonly string[];
47
+ readonly wanted: readonly WantedTarget[];
48
+ readonly unlinked: readonly UnlinkedMention[];
49
+ }
50
+
51
+ /** A report with nothing to look at, for a tidy with no link table to read. */
52
+ export const EMPTY_LINK_REPORT: LinkReport = {
53
+ broken: [],
54
+ orphans: [],
55
+ wanted: [],
56
+ unlinked: [],
57
+ };
58
+
59
+ function inQuery(document: DocumentKey, query: LinkQuery): boolean {
60
+ if (document.scope !== "wiki" && document.scope !== "memory") return false;
61
+ if (query.scope !== undefined && document.scope !== query.scope) return false;
62
+ if (query.owner !== undefined && document.owner !== query.owner) return false;
63
+ return true;
64
+ }
65
+
66
+ /** Whether a note is a structural page rather than one an orphan check names. */
67
+ function structural(path: string): boolean {
68
+ const name = path.slice(path.lastIndexOf("/") + 1);
69
+ return (
70
+ name === "index.md" ||
71
+ name === "instructions.md" ||
72
+ name === "_about.md" ||
73
+ path.startsWith("log/")
74
+ );
75
+ }
76
+
77
+ /** The 1-based line of an offset in a note's body. */
78
+ function lineAt(text: string, at: number): number {
79
+ let line = 1;
80
+ for (let index = 0; index < at; index += 1) {
81
+ if (text[index] === "\n") line += 1;
82
+ }
83
+ return line;
84
+ }
85
+
86
+ /** Whether a character belongs to a word, so a title does not match inside one. */
87
+ function wordCharacter(character: string | undefined): boolean {
88
+ return character !== undefined && /[\p{L}\p{N}_]/u.test(character);
89
+ }
90
+
91
+ /** Whether a body names a title as a word rather than as part of a longer word. */
92
+ function mentions(body: string, title: string): boolean {
93
+ let at = body.indexOf(title);
94
+ while (at >= 0) {
95
+ const before = at === 0 ? undefined : body[at - 1];
96
+ const after = at + title.length >= body.length ? undefined : body[at + title.length];
97
+ if (!wordCharacter(before) && !wordCharacter(after)) return true;
98
+ at = body.indexOf(title, at + 1);
99
+ }
100
+ return false;
101
+ }
102
+
103
+ /**
104
+ * The link lists for one scope, or one owner's memory. The index must have
105
+ * been refreshed; links stored before a refresh still name their old targets.
106
+ */
107
+ export function linkReport(
108
+ search: MemorySearch,
109
+ query: LinkQuery = {},
110
+ unlinkedLimit = UNLINKED_MENTION_LIMIT,
111
+ ): LinkReport {
112
+ const notes = search.backend
113
+ .keys()
114
+ .filter((key) => key.file !== undefined && key.path !== undefined && inQuery(key, query))
115
+ .sort((left, right) => (left.path ?? "").localeCompare(right.path ?? ""));
116
+ const byId = new Map(notes.map((note) => [note.id, note]));
117
+ const documents = new Map<string, IndexedDocument>();
118
+ for (const note of notes) {
119
+ const document = search.backend.get(note.id);
120
+ if (document !== undefined) documents.set(note.id, document);
121
+ }
122
+ // An untrusted note is never pointed at, as a source or as a target.
123
+ const trusted = new Set(
124
+ notes
125
+ .filter((note) => {
126
+ const document = documents.get(note.id);
127
+ return document !== undefined && !untrustedFields(document.fields);
128
+ })
129
+ .map((note) => note.id),
130
+ );
131
+ const links = search.backend.allLinks().filter((link) => trusted.has(link.docId));
132
+
133
+ const linked = new Set<string>();
134
+ const linkedBySource = new Map<string, Set<string>>();
135
+ const sourcesByTarget = new Map<string, Set<string>>();
136
+ const broken: BrokenNoteLink[] = [];
137
+ const lines = new Map<string, readonly number[]>();
138
+
139
+ for (const link of links) {
140
+ if (link.targetId !== undefined) {
141
+ if (link.targetId !== link.docId) linked.add(link.targetId);
142
+ const targets = linkedBySource.get(link.docId) ?? new Set<string>();
143
+ targets.add(link.targetId);
144
+ linkedBySource.set(link.docId, targets);
145
+ continue;
146
+ }
147
+ const source = byId.get(link.docId);
148
+ if (source?.path === undefined) continue;
149
+
150
+ let sourceLines = lines.get(link.docId);
151
+ if (sourceLines === undefined) {
152
+ const document = documents.get(link.docId);
153
+ sourceLines =
154
+ document === undefined
155
+ ? []
156
+ : parseNoteLinksWithPositions(document.text).map((place) =>
157
+ lineAt(document.text, place.at),
158
+ );
159
+ lines.set(link.docId, sourceLines);
160
+ }
161
+ broken.push({
162
+ path: source.path,
163
+ line: sourceLines[link.ordinal] ?? 1,
164
+ target: link.target,
165
+ });
166
+
167
+ const sources = sourcesByTarget.get(link.target) ?? new Set<string>();
168
+ sources.add(link.docId);
169
+ sourcesByTarget.set(link.target, sources);
170
+ }
171
+
172
+ broken.sort(
173
+ (left, right) =>
174
+ left.path.localeCompare(right.path) ||
175
+ left.line - right.line ||
176
+ left.target.localeCompare(right.target),
177
+ );
178
+
179
+ const orphans = notes
180
+ .filter(
181
+ (note) =>
182
+ note.path !== undefined &&
183
+ trusted.has(note.id) &&
184
+ !structural(note.path) &&
185
+ !linked.has(note.id),
186
+ )
187
+ .map((note) => note.path ?? note.id);
188
+
189
+ const wanted = [...sourcesByTarget.entries()]
190
+ .filter(([, sources]) => sources.size >= 3)
191
+ .map(([target, sources]) => ({ target, notes: sources.size }))
192
+ .sort((left, right) => right.notes - left.notes || left.target.localeCompare(right.target));
193
+
194
+ const unlinked: UnlinkedMention[] = [];
195
+ if (unlinkedLimit > 0) {
196
+ const targets = notes
197
+ .filter((note) => trusted.has(note.id))
198
+ .map((note) => ({ note, document: documents.get(note.id) }))
199
+ .filter(
200
+ (entry): entry is { note: DocumentKey; document: IndexedDocument } =>
201
+ entry.document !== undefined,
202
+ );
203
+ for (const source of targets) {
204
+ if (source.note.path === undefined) continue;
205
+ const body = withoutCode(source.document.text);
206
+ const already = linkedBySource.get(source.note.id) ?? new Set<string>();
207
+ for (const target of targets) {
208
+ if (target.note.id === source.note.id || target.note.path === undefined) continue;
209
+ if (already.has(target.note.id)) continue;
210
+ const title = target.document.title;
211
+ if (title.length === 0 || !mentions(body, title)) continue;
212
+ unlinked.push({
213
+ path: source.note.path,
214
+ title,
215
+ target: target.note.path,
216
+ });
217
+ if (unlinked.length >= unlinkedLimit) break;
218
+ }
219
+ if (unlinked.length >= unlinkedLimit) break;
220
+ }
221
+ }
222
+
223
+ return { broken, orphans, wanted, unlinked };
224
+ }
package/src/links.ts ADDED
@@ -0,0 +1,378 @@
1
+ /**
2
+ * The links a note writes: `[[target]]` and relative Markdown links, parsed
3
+ * from the note's text and resolved against the notes the index holds. A link
4
+ * table is a cache like the rest of the index: no note is written, and a link
5
+ * to a page nobody wrote yet stays in the table with no target until that page
6
+ * appears.
7
+ *
8
+ * A link resolves the way Obsidian resolves one. A target that names a path is
9
+ * taken from the scope root; a Markdown target that is not there is taken
10
+ * relative to the note itself. A bare wikilink name matches a file name with or
11
+ * without `.md`, and among files of the same name the one nearest the source
12
+ * note wins. A path is not reduced to its file name.
13
+ */
14
+
15
+ import { snippetOf } from "./search/chunks.ts";
16
+ import type { NoteLink } from "./search/types.ts";
17
+
18
+ const FENCE = /^ {0,3}(`{3,}|~{3,})(.*)$/u;
19
+ const WIKILINK = /\[\[([^[\]]+)\]\]/gu;
20
+ const MARKDOWN_LINK = /(!?)\[([^\]]*)\]\(\s*(?:<([^>\n]*)>|([^)\n]*?))\s*\)/gu;
21
+ const MARKDOWN_EXTENSION = /\.(?:md|markdown)$/iu;
22
+
23
+ /** One link a note writes, with the offset at which it starts in the body. */
24
+ export interface PositionedNoteLink extends NoteLink {
25
+ readonly at: number;
26
+ }
27
+
28
+ /** One link a note writes, with the line and excerpt a backlink will show. */
29
+ export interface PlacedNoteLink extends PositionedNoteLink {
30
+ readonly line: number;
31
+ readonly excerpt: string;
32
+ }
33
+
34
+ /** The line and excerpt around a link that starts at an offset in its note. */
35
+ export function linkPlaceAt(
36
+ text: string,
37
+ at: number,
38
+ target: string,
39
+ ): { readonly line: number; readonly excerpt: string } {
40
+ const before = text.slice(0, at);
41
+ const line = before.length === 0 ? 1 : before.split(/\r?\n/u).length;
42
+ const start = text.lastIndexOf("\n", at - 1) + 1;
43
+ const next = text.indexOf("\n", at);
44
+ const written = text.slice(start, next < 0 ? text.length : next).trim();
45
+ return { line, excerpt: snippetOf(written, [target]) };
46
+ }
47
+
48
+ /** The links a note's text writes, each with the place a backlink shows it. */
49
+ export function placedNoteLinks(text: string): readonly PlacedNoteLink[] {
50
+ return parseNoteLinksWithPositions(text).map(({ at, ...link }) => ({
51
+ ...link,
52
+ at,
53
+ ...linkPlaceAt(text, at, link.target),
54
+ }));
55
+ }
56
+
57
+ /** Removes inline code spans from one line, keeping its length and its text around them. */
58
+ function withoutInlineCode(line: string): string {
59
+ let kept = "";
60
+ let index = 0;
61
+ while (index < line.length) {
62
+ if (line[index] !== "`") {
63
+ kept += line[index];
64
+ index += 1;
65
+ continue;
66
+ }
67
+ let ticks = 0;
68
+ while (line[index + ticks] === "`") ticks += 1;
69
+ let end = -1;
70
+ let scan = index + ticks;
71
+ while (scan < line.length) {
72
+ if (line[scan] !== "`") {
73
+ scan += 1;
74
+ continue;
75
+ }
76
+ let run = 0;
77
+ while (line[scan + run] === "`") run += 1;
78
+ if (run === ticks) {
79
+ end = scan;
80
+ break;
81
+ }
82
+ scan += run;
83
+ }
84
+ if (end < 0) {
85
+ // An unmatched run of backticks is text, not a code span.
86
+ kept += "`".repeat(ticks);
87
+ index += ticks;
88
+ continue;
89
+ }
90
+ kept += " ".repeat(end + ticks - index);
91
+ index = end + ticks;
92
+ }
93
+ return kept;
94
+ }
95
+
96
+ /** A line with its text blanked, keeping its length so link offsets stay exact. */
97
+ function blankLine(line: string, carriage: boolean): string {
98
+ return " ".repeat(line.length) + (carriage ? "\r" : "");
99
+ }
100
+
101
+ /** A Markdown block that ends a paragraph, so an indented line after it is code. */
102
+ const BLOCK_START = /^ {0,3}(?:#{1,6}(?:\s|$)|>|(?:[-*_]\s*){3,}|\|?\s*:?-{3,}:?\s*\|?)/u;
103
+
104
+ /**
105
+ * The text with code left out: fenced and indented code blocks and inline code
106
+ * spans are blanked, so a link that is shown as code is not a link. Each
107
+ * blanked line keeps its length, and line breaks stay, so the text around the
108
+ * code is neither joined across a removed piece nor shifted for a reader.
109
+ */
110
+ export function withoutCode(text: string): string {
111
+ const lines: string[] = [];
112
+ let fence: { readonly character: string; readonly length: number } | undefined;
113
+ let indented = false;
114
+ // An indented code block cannot interrupt a paragraph; ordinary text does.
115
+ let paragraph = false;
116
+ for (const line of text.split("\n")) {
117
+ const carriage = line.endsWith("\r");
118
+ const content = carriage ? line.slice(0, -1) : line;
119
+ const match = FENCE.exec(content);
120
+ if (fence !== undefined) {
121
+ const closes =
122
+ match !== null &&
123
+ (match[1] ?? "").startsWith(fence.character) &&
124
+ (match[1] ?? "").length >= fence.length &&
125
+ (match[2] ?? "").trim().length === 0;
126
+ if (closes) fence = undefined;
127
+ lines.push(blankLine(content, carriage));
128
+ paragraph = false;
129
+ continue;
130
+ }
131
+ if (match !== null) {
132
+ const run = match[1] ?? "";
133
+ fence = { character: run[0] ?? "`", length: run.length };
134
+ indented = false;
135
+ lines.push(blankLine(content, carriage));
136
+ paragraph = false;
137
+ continue;
138
+ }
139
+ const isIndented = /^(?: {4}|\t)/u.test(content);
140
+ if (indented) {
141
+ if (isIndented || content.trim().length === 0) {
142
+ lines.push(blankLine(content, carriage));
143
+ paragraph = false;
144
+ continue;
145
+ }
146
+ indented = false;
147
+ }
148
+ if (isIndented && !paragraph) {
149
+ indented = true;
150
+ lines.push(blankLine(content, carriage));
151
+ paragraph = false;
152
+ continue;
153
+ }
154
+ lines.push(withoutInlineCode(content) + (carriage ? "\r" : ""));
155
+ paragraph = content.trim().length > 0 && !BLOCK_START.test(content);
156
+ }
157
+ return lines.join("\n");
158
+ }
159
+
160
+ /**
161
+ * The parts of a wikilink, split at its pipe. A `\|` is the same separator
162
+ * written so a Markdown table around the link does not split its row.
163
+ */
164
+ function splitPipe(text: string): readonly string[] {
165
+ const parts: string[] = [];
166
+ let current = "";
167
+ for (let index = 0; index < text.length; index += 1) {
168
+ const character = text[index] ?? "";
169
+ if (character === "\\" && text[index + 1] === "|") {
170
+ parts.push(current);
171
+ current = "";
172
+ index += 1;
173
+ continue;
174
+ }
175
+ if (character === "|") {
176
+ parts.push(current);
177
+ current = "";
178
+ continue;
179
+ }
180
+ current += character;
181
+ }
182
+ parts.push(current);
183
+ return parts;
184
+ }
185
+
186
+ /** A written target split at its first `#` into the note and the heading. */
187
+ function splitHeading(text: string): { readonly target: string; readonly heading?: string } {
188
+ const at = text.indexOf("#");
189
+ if (at < 0) return { target: text.trim() };
190
+ const target = text.slice(0, at).trim();
191
+ const heading = text.slice(at + 1).trim();
192
+ return heading.length === 0 ? { target } : { target, heading };
193
+ }
194
+
195
+ /**
196
+ * A percent-decoded target. A bare `%` is not an escape and must not make the
197
+ * parser throw: such a target is kept as it was written.
198
+ */
199
+ function decoded(text: string): string {
200
+ try {
201
+ return decodeURIComponent(text);
202
+ } catch {
203
+ return text;
204
+ }
205
+ }
206
+
207
+ /** Whether a Markdown target points outside the notes: a scheme or `//`. */
208
+ function external(target: string): boolean {
209
+ return /^[A-Za-z][A-Za-z0-9+.-]*:/u.test(target) || target.startsWith("//");
210
+ }
211
+
212
+ /** The links a note's text writes, with where they start, in the order they appear. */
213
+ export function parseNoteLinksWithPositions(text: string): readonly PositionedNoteLink[] {
214
+ const clean = withoutCode(text);
215
+ const found: PositionedNoteLink[] = [];
216
+
217
+ for (const match of clean.matchAll(WIKILINK)) {
218
+ const inner = match[1] ?? "";
219
+ const first = splitPipe(inner)[0] ?? "";
220
+ const { target, heading } = splitHeading(first);
221
+ // `[[#Heading]]` names a heading in the note that writes it.
222
+ if (target.length === 0 && heading === undefined) continue;
223
+ found.push({
224
+ at: match.index ?? 0,
225
+ target,
226
+ ...(heading === undefined ? {} : { heading }),
227
+ kind: "wikilink",
228
+ });
229
+ }
230
+
231
+ for (const match of clean.matchAll(MARKDOWN_LINK)) {
232
+ // `![alt](image.png)` embeds an image; it does not link to a note.
233
+ if ((match[1] ?? "") === "!") continue;
234
+ // A destination may be enclosed in angle brackets; either form may hold a space.
235
+ const written = match[4] ?? match[3] ?? "";
236
+ if (written.trim().length === 0 || external(written.trim())) continue;
237
+ const split = splitHeading(written);
238
+ const target = decoded(split.target);
239
+ if (target.length === 0) continue;
240
+ found.push({
241
+ at: match.index ?? 0,
242
+ target,
243
+ ...(split.heading === undefined ? {} : { heading: decoded(split.heading) }),
244
+ kind: "markdown",
245
+ });
246
+ }
247
+
248
+ found.sort((left, right) => left.at - right.at);
249
+ return found;
250
+ }
251
+
252
+ /** The links a note's text writes, in the order they appear. */
253
+ export function parseNoteLinks(text: string): readonly NoteLink[] {
254
+ return parseNoteLinksWithPositions(text).map(({ at: _at, ...link }) => link);
255
+ }
256
+
257
+ /** A path inside its scope root: `/` separators, no `.` or `..` parts. */
258
+ export function normaliseNotePath(path: string): string | undefined {
259
+ const parts: string[] = [];
260
+ for (const part of path.split("/")) {
261
+ if (part === "" || part === ".") continue;
262
+ if (part === "..") {
263
+ if (parts.length === 0) return undefined;
264
+ parts.pop();
265
+ continue;
266
+ }
267
+ parts.push(part);
268
+ }
269
+ return parts.length === 0 ? undefined : parts.join("/");
270
+ }
271
+
272
+ /** The paths a written note name can be: it, and the same with `.md`. */
273
+ function withExtension(path: string): readonly string[] {
274
+ return MARKDOWN_EXTENSION.test(path) ? [path] : [path, `${path}.md`];
275
+ }
276
+
277
+ /** The directory part of a note path, empty at the root. */
278
+ function directoryOf(path: string): string {
279
+ const at = path.lastIndexOf("/");
280
+ return at < 0 ? "" : path.slice(0, at);
281
+ }
282
+
283
+ /** The file part of a note path. */
284
+ function nameOf(path: string): string {
285
+ const at = path.lastIndexOf("/");
286
+ return at < 0 ? path : path.slice(at + 1);
287
+ }
288
+
289
+ /** The steps from one directory to another in the same scope tree. */
290
+ function directorySteps(from: string, to: string): number {
291
+ const source = from === "" ? [] : from.split("/");
292
+ const target = to === "" ? [] : to.split("/");
293
+ let common = 0;
294
+ while (common < source.length && common < target.length && source[common] === target[common]) {
295
+ common += 1;
296
+ }
297
+ return source.length - common + target.length - common;
298
+ }
299
+
300
+ /** A note set prepared for many resolutions: path lookup and file-name lookup. */
301
+ export interface NoteLookup {
302
+ /** Whether a path from the scope root names a note. */
303
+ has(path: string): boolean;
304
+ /** The same-named note nearest the source note, with the path breaking a tie. */
305
+ nearestByName(written: string, source: string): string | undefined;
306
+ }
307
+
308
+ /** Prepares a note set once, so resolving many links does not scan it per link. */
309
+ export function noteLookup(notes: readonly string[]): NoteLookup {
310
+ const paths = new Set(notes);
311
+ const byName = new Map<string, string[]>();
312
+ for (const note of notes) {
313
+ const name = nameOf(note);
314
+ const list = byName.get(name) ?? [];
315
+ list.push(note);
316
+ byName.set(name, list);
317
+ }
318
+ return {
319
+ has: (path) => paths.has(path),
320
+ nearestByName: (written, source) => {
321
+ const name = nameOf(written.replace(/\/+$/u, ""));
322
+ if (name.length === 0) return undefined;
323
+ const matches = withExtension(name).flatMap((form) => byName.get(form) ?? []);
324
+ if (matches.length === 0) return undefined;
325
+ const from = directoryOf(source);
326
+ return [...matches].sort(
327
+ (left, right) =>
328
+ directorySteps(from, directoryOf(left)) - directorySteps(from, directoryOf(right)) ||
329
+ left.localeCompare(right),
330
+ )[0];
331
+ },
332
+ };
333
+ }
334
+
335
+ function asLookup(notes: readonly string[] | NoteLookup): NoteLookup {
336
+ return isNoteLookup(notes) ? notes : noteLookup(notes);
337
+ }
338
+
339
+ function isNoteLookup(notes: readonly string[] | NoteLookup): notes is NoteLookup {
340
+ return !Array.isArray(notes);
341
+ }
342
+
343
+ /**
344
+ * The note a link points at, or undefined while nothing matches it. A path is
345
+ * taken from the scope root first; a Markdown target that is not there is taken
346
+ * relative to the source note. Only a bare wikilink name matches a file name. A
347
+ * path that climbs above the scope root matches nothing.
348
+ */
349
+ export function resolveNoteLink(
350
+ source: string,
351
+ link: NoteLink,
352
+ notes: readonly string[] | NoteLookup,
353
+ ): string | undefined {
354
+ const lookup = asLookup(notes);
355
+ const written = link.target.trim();
356
+ if (written.length === 0) {
357
+ return link.heading !== undefined && lookup.has(source) ? source : undefined;
358
+ }
359
+
360
+ const fromRoot = normaliseNotePath(written.replace(/^\/+/u, ""));
361
+ const candidates: string[] = [];
362
+ if (fromRoot !== undefined) candidates.push(...withExtension(fromRoot));
363
+ if (link.kind === "markdown" && !written.startsWith("/")) {
364
+ const directory = directoryOf(source);
365
+ const fromSource = normaliseNotePath(directory === "" ? written : `${directory}/${written}`);
366
+ if (fromSource !== undefined) {
367
+ for (const candidate of withExtension(fromSource)) {
368
+ if (!candidates.includes(candidate)) candidates.push(candidate);
369
+ }
370
+ }
371
+ }
372
+
373
+ const byPath = candidates.find((candidate) => lookup.has(candidate));
374
+ if (byPath !== undefined) return byPath;
375
+ // Only a bare wikilink name falls back to a file name; a path is a path.
376
+ if (link.kind !== "wikilink" || written.includes("/")) return undefined;
377
+ return lookup.nearestByName(written, source);
378
+ }