mirror of
https://github.com/stracker-phil/obsidian-mention-things.git
synced 2026-07-22 05:43:27 +00:00
Refactor suggestion handling and file indexing
- Updated the `shouldShowSuggestions` method in `suggestion.ts` to improve query handling by trimming and checking for closing brackets and max length against the trimmed name. - Moved the logic for handling new or updated files in `file-indexer.ts` to ensure it checks for file existence before adding to the map, and added logic to remove files from the map if they no longer exist. - Simplified the `getLinkFromPath` and `getLinkFromAlias` functions in `link-utils.ts` by directly using the mention type sign in regex checks, improving readability and maintainability.
This commit is contained in:
parent
f807959d56
commit
ed42e09458
3 changed files with 26 additions and 26 deletions
|
|
@ -86,18 +86,19 @@ export class SuggestionProvider extends EditorSuggest<MentionSuggestion> {
|
|||
* Determine if suggestions should be shown based on various conditions
|
||||
*/
|
||||
private shouldShowSuggestions(query: string, signIndex: number, charsLeftOfCursor: string): boolean {
|
||||
if (!query) {
|
||||
const name = query?.substring(1).trim();
|
||||
if (!name) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if query includes closing brackets
|
||||
if (query.includes(']]')) {
|
||||
if (name.includes(']]')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if query exceeds max length
|
||||
const maxMatchLength = this.settings.maxMatchLength ?? DEFAULT_SETTINGS.maxMatchLength;
|
||||
if (maxMatchLength && query.length > maxMatchLength) {
|
||||
if (maxMatchLength && name.length > maxMatchLength) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
@ -105,7 +106,7 @@ export class SuggestionProvider extends EditorSuggest<MentionSuggestion> {
|
|||
const stopCharacters = this.settings.stopCharacters ?? DEFAULT_SETTINGS.stopCharacters;
|
||||
if (stopCharacters) {
|
||||
for (const char of stopCharacters) {
|
||||
if (query.includes(char)) {
|
||||
if (name.includes(char)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -60,15 +60,15 @@ export class FileIndexer {
|
|||
updateIndex(path: string, originalPath?: string): boolean {
|
||||
let needsUpdate = false;
|
||||
|
||||
// Handle new or updated file
|
||||
const addItem = getLinkFromPath(path, this.settings);
|
||||
if (addItem) {
|
||||
this.addFileToMap(addItem);
|
||||
needsUpdate = true;
|
||||
}
|
||||
|
||||
const file = this.app.vault.getFileByPath(path);
|
||||
if (file) {
|
||||
// Handle new or updated file
|
||||
const addItem = getLinkFromPath(path, this.settings);
|
||||
if (addItem) {
|
||||
this.addFileToMap(addItem);
|
||||
needsUpdate = true;
|
||||
}
|
||||
|
||||
const fileAliases: string[] = this.app.metadataCache.getFileCache(file)?.frontmatter?.aliases;
|
||||
if (fileAliases) {
|
||||
fileAliases.forEach(alias => {
|
||||
|
|
@ -79,6 +79,13 @@ export class FileIndexer {
|
|||
}
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// If the file doesn't exist, it might have been deleted
|
||||
const removeItem = getLinkFromPath(path, this.settings);
|
||||
if (removeItem) {
|
||||
this.removeFileFromMap(removeItem);
|
||||
needsUpdate = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Handle renamed or deleted file
|
||||
|
|
|
|||
|
|
@ -13,22 +13,16 @@ export function getLinkFromPath(path: string, settings: MentionSettings): Mentio
|
|||
}
|
||||
|
||||
for (const sign in settings.mentionTypes) {
|
||||
const type = settings.mentionTypes[sign];
|
||||
|
||||
if (!type?.sign) {
|
||||
if (!path.includes('/' + sign)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!path.includes('/' + type.sign)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const regex = new RegExp(`/(${type.sign}([^/]+))\\.md$`);
|
||||
const regex = new RegExp(`/([${sign}]([^/]+))\\.md$`);
|
||||
const result = regex.exec(path);
|
||||
|
||||
if (result?.[2]) {
|
||||
return {
|
||||
type: type,
|
||||
type: settings.mentionTypes[sign],
|
||||
name: result[2],
|
||||
fileName: result[1],
|
||||
path,
|
||||
|
|
@ -45,21 +39,19 @@ export function getLinkFromAlias(alias: string, file: TFile, settings: MentionSe
|
|||
}
|
||||
|
||||
for (const sign in settings.mentionTypes) {
|
||||
const type = settings.mentionTypes[sign];
|
||||
|
||||
if (!type?.sign || !alias.startsWith(type.sign)) {
|
||||
if (!alias.startsWith(sign)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const aliasRegex = new RegExp(`${type.sign}([^/]+)$`);
|
||||
const aliasRegex = new RegExp(`[${sign}]([^/]+)$`);
|
||||
const aliasRegexResult = aliasRegex.exec(alias);
|
||||
|
||||
const fileNameRegex = new RegExp(`(${type.sign}([^/]+))\\.md$`);
|
||||
const fileNameRegex = new RegExp(`([${sign}]([^/]+))\\.md$`);
|
||||
const fileNameRegexResult = fileNameRegex.exec(file.name);
|
||||
|
||||
if (aliasRegexResult?.[1] && fileNameRegexResult?.[1]) {
|
||||
return {
|
||||
type: type,
|
||||
type: settings.mentionTypes[sign],
|
||||
name: aliasRegexResult[1],
|
||||
fileName: fileNameRegexResult[1],
|
||||
path: file.path,
|
||||
|
|
|
|||
Loading…
Reference in a new issue