// The rules behind the note editor's wiki-link autocomplete (type `[[` while editing a note and the // campaign's notes, actors, items and maps are offered to link to). These helpers hold no state, // touch no DOM and import nothing, so tests can load them straight from source. Marker.js renders // `[[target]]` as a link, so the target written here is the entry's label. // Accent- and case-insensitive, the way the search palette compares names. function Normalize(text) { return String(text ?? '') .normalize('NFD') .replace(/[\u0300-\u036f]/g, '') .toLowerCase() .trim(); } // The `[[target` the caret is sitting in. `[[The Wr` answers `{ start, query: 'The Wr' }`. // // A link is a single line: a `]]` or a newline between the `[[` and the caret closes the token, so // the menu only follows the link currently being typed. Returns null when there is none. function FindLinkTrigger(text, caret) { if (typeof text !== 'string') return null; const position = Math.max(0, Math.min(Number(caret) || 0, text.length)); const before = text.slice(0, position); const start = before.lastIndexOf('[['); if (start === -1) return null; const query = before.slice(start + 2); if (query.includes(']]') || query.includes('\n')) return null; return { start, query }; } // One `{ id, label, type }` per linkable entry, from the campaign's four lists. Entries without a // name are left out (they would show as an empty menu row), the rest are sorted by label so the menu // reads alphabetically whatever order the API answered with. function BuildLinkSuggestions({ notes, actors, items, maps } = {}) { const suggestions = []; const seen = new Set(); const push = (type, list, labelOf) => { for (const entry of list ?? []) { const label = String(labelOf(entry) ?? '').trim(); if (!entry?._id || !label) continue; const key = `${type}:${label}`; if (seen.has(key)) continue; seen.add(key); suggestions.push({ id: String(entry._id), label, type }); } }; push('note', notes, (note) => note.title); push('actor', actors, (actor) => actor.name); push('item', items, (item) => item.name); push('map', maps, (map) => map.name); return suggestions.sort((a, b) => a.label.localeCompare(b.label)); } // The suggestions matching what has been typed. An empty query offers them all; the ones whose label // starts with it come first, and a match further inside still shows, the way search does it. The list // is capped so a large campaign cannot open a menu taller than the note. const MAX_SUGGESTIONS = 50; function FilterLinkSuggestions(suggestions, query) { const needle = Normalize(query); const all = suggestions ?? []; if (!needle) return all.slice(0, MAX_SUGGESTIONS); return all .filter((suggestion) => Normalize(suggestion.label).includes(needle)) .sort((a, b) => { const startsA = Normalize(a.label).startsWith(needle) ? 0 : 1; const startsB = Normalize(b.label).startsWith(needle) ? 0 : 1; return startsA - startsB || a.label.localeCompare(b.label); }) .slice(0, MAX_SUGGESTIONS); } // The entry a written `[[label]]` points at. Links store the label (not the id), so the note looks // the target up again when it is rendered; the comparison is the same case- and accent-insensitive // one the menu uses. Returns null when nothing in the campaign carries that label any more. function FindLinkTarget(suggestions, label) { const needle = Normalize(label); if (!needle) return null; return (suggestions ?? []).find((suggestion) => Normalize(suggestion.label) === needle) ?? null; } // Replaces the typed `[[query` with `[[label]]` and returns where the caret goes: just after the // closing brackets, ready to carry on with the note. function InsertLinkSuggestion(text, trigger, suggestion) { const source = String(text ?? ''); const start = trigger?.start ?? 0; const query = trigger?.query ?? ''; const before = source.slice(0, start); const after = source.slice(start + 2 + query.length); const snippet = `[[${suggestion?.label ?? ''}]]`; return { text: before + snippet + after, caret: start + snippet.length }; } export { BuildLinkSuggestions, FilterLinkSuggestions, FindLinkTarget, FindLinkTrigger, InsertLinkSuggestion };