Compare commits
8
Commits
d821344ade
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
921a72b015 | ||
|
|
92a7af6ec5 | ||
|
|
52ada90150 | ||
|
|
d3c7e7ea8e | ||
|
|
99fd363c22 | ||
|
|
dd7d7a367d | ||
|
|
282fd1be13 | ||
|
|
46a5d3d114 |
No files matched your search
@@ -1,10 +1,13 @@
|
||||
> [!CAUTION]
|
||||
> This project is a heavy work-in-progress, there are bugs, and features may change or disappear.
|
||||
|
||||
# Locator for Obsidian
|
||||
|
||||
[](https://github.com/sponsors/scambier)
|
||||
|
||||
---
|
||||
|
||||
**Locator** is an Obsidian plugin that lets you locate your files in a few keystrokes. It is a lightweight, experimental version of Omnisearch.
|
||||
**Locator** is an Obsidian plugin that lets you locate your files in a few keystrokes. It is an experimental fork of Omnisearch.
|
||||
|
||||
It always instantly shows you the most relevant results, thanks to its smart weighting algorithm.
|
||||
|
||||
@@ -39,15 +42,13 @@ You can check the [CHANGELOG](./CHANGELOG.md) for more information on the differ
|
||||
- Directly Insert a `[[link]]` from the search results
|
||||
- Supports Vim navigation keys
|
||||
|
||||
**Note:** support of Chinese depends
|
||||
on [this additional plugin](https://github.com/aidenlx/cm-chs-patch) (also you may need to clear search cache data to apply new Chinese index). Please read its documentation for more
|
||||
information.
|
||||
|
||||
## Issues & Feature Requests
|
||||
|
||||
If you're reading this README from the [repository's page](https://git.scambier.xyz/scambier/obsidian-locator), you'll notice it is hosted on a private forge without account registration.
|
||||
Locator is a personal hobby project, tailored for my own needs. It is publicly available because you may find it useful too, but I'm not interested in your issues or pull requests.
|
||||
|
||||
If you'd like to participate in a similar project (thank you!), I'll be glad to review your pull requests for [**Omnisearch**](https://github.com/scambier/obsidian-omnisearch)
|
||||
|
||||
Thank you for your understanding.
|
||||
|
||||
## LICENSE
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
<script lang="ts">
|
||||
import type { ResultNote } from '../globals'
|
||||
import ResultItemContainer from './ResultItemContainer.svelte'
|
||||
import type LocatorPlugin from '../main'
|
||||
|
||||
export let plugin: LocatorPlugin
|
||||
export let offset: number
|
||||
export let note: ResultNote
|
||||
export let index = 0
|
||||
export let selected = false
|
||||
|
||||
$: cleanedContent = plugin.textProcessor.makeExcerpt(note?.content ?? '', offset)
|
||||
</script>
|
||||
|
||||
<ResultItemContainer
|
||||
id="{index.toString()}"
|
||||
on:auxclick
|
||||
on:click
|
||||
on:mousemove
|
||||
selected="{selected}">
|
||||
<div class="omnisearch-result__body">
|
||||
{@html plugin.textProcessor.highlightText(cleanedContent, note.matches)}
|
||||
</div>
|
||||
</ResultItemContainer>
|
||||
@@ -1,8 +1,10 @@
|
||||
<script lang="ts">
|
||||
import { setIcon, TFile } from 'obsidian'
|
||||
import { showExcerpt } from 'src/settings/utils'
|
||||
import { onMount } from 'svelte'
|
||||
import type { ResultNote } from '../globals'
|
||||
import type LocatorPlugin from '../main'
|
||||
import { getDefaultIconSVG } from '../tools/icon-utils'
|
||||
import {
|
||||
getExtension,
|
||||
isFileCanvas,
|
||||
@@ -12,15 +14,6 @@
|
||||
pathWithoutFilename,
|
||||
} from '../tools/utils'
|
||||
import ResultItemContainer from './ResultItemContainer.svelte'
|
||||
// Import icon utility functions
|
||||
import { showExcerpt } from 'src/settings/utils'
|
||||
import {
|
||||
getDefaultIconSVG,
|
||||
getIconNameForPath,
|
||||
initializeIconPacks,
|
||||
loadIconData,
|
||||
loadIconSVG,
|
||||
} from '../tools/icon-utils'
|
||||
|
||||
export let selected = false
|
||||
export let note: ResultNote
|
||||
@@ -29,19 +22,12 @@
|
||||
let imagePath: string | null = null
|
||||
let title = ''
|
||||
let notePath = ''
|
||||
let iconData = {}
|
||||
let folderIconSVG: string | null = null
|
||||
let fileIconSVG: string | null = null
|
||||
let prefixToIconPack: { [prefix: string]: string } = {}
|
||||
let iconsPath: string
|
||||
const folderIconSVG = getDefaultIconSVG('folder')
|
||||
const fileIconSVG = getDefaultIconSVG(note.path)
|
||||
let iconDataLoaded = false // Flag to indicate iconData is loaded
|
||||
|
||||
// Initialize icon data and icon packs once when the component mounts
|
||||
onMount(async () => {
|
||||
iconData = await loadIconData(plugin)
|
||||
const iconPacks = await initializeIconPacks(plugin)
|
||||
prefixToIconPack = iconPacks.prefixToIconPack
|
||||
iconsPath = iconPacks.iconsPath
|
||||
iconDataLoaded = true // Set the flag after iconData is loaded
|
||||
})
|
||||
|
||||
@@ -51,40 +37,9 @@
|
||||
// Update title and notePath before loading icons
|
||||
title = note.displayTitle || note.basename
|
||||
notePath = pathWithoutFilename(note.path)
|
||||
await loadIcons()
|
||||
})()
|
||||
}
|
||||
|
||||
async function loadIcons() {
|
||||
// Load folder icon
|
||||
const folderIconName = getIconNameForPath(notePath, iconData)
|
||||
if (folderIconName) {
|
||||
folderIconSVG = await loadIconSVG(
|
||||
folderIconName,
|
||||
plugin,
|
||||
iconsPath,
|
||||
prefixToIconPack
|
||||
)
|
||||
} else {
|
||||
// Fallback to default folder icon
|
||||
folderIconSVG = getDefaultIconSVG('folder', plugin)
|
||||
}
|
||||
|
||||
// Load file icon
|
||||
const fileIconName = getIconNameForPath(note.path, iconData)
|
||||
if (fileIconName) {
|
||||
fileIconSVG = await loadIconSVG(
|
||||
fileIconName,
|
||||
plugin,
|
||||
iconsPath,
|
||||
prefixToIconPack
|
||||
)
|
||||
} else {
|
||||
// Fallback to default icons based on file type
|
||||
fileIconSVG = getDefaultIconSVG(note.path, plugin)
|
||||
}
|
||||
}
|
||||
|
||||
// Svelte action to render SVG content with dynamic updates
|
||||
function renderSVG(node: HTMLElement, svgContent: string) {
|
||||
node.innerHTML = svgContent
|
||||
@@ -163,10 +118,8 @@
|
||||
title="The document above is embedded in this note" />
|
||||
{:else}
|
||||
<!-- File Icon -->
|
||||
{#if fileIconSVG}
|
||||
<span class="omnisearch-result__icon" use:renderSVG={fileIconSVG} />
|
||||
{/if}
|
||||
{/if}
|
||||
<span>
|
||||
{@html plugin.textProcessor.highlightText(title, matchesTitle)}
|
||||
</span>
|
||||
@@ -191,9 +144,8 @@
|
||||
{#if notePath}
|
||||
<div class="omnisearch-result__folder-path">
|
||||
<!-- Folder Icon -->
|
||||
{#if folderIconSVG}
|
||||
<span class="omnisearch-result__icon" use:renderSVG={folderIconSVG} />
|
||||
{/if}
|
||||
|
||||
<span>
|
||||
{@html plugin.textProcessor.highlightText(notePath, matchesNotePath)}
|
||||
</span>
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@ import { Notice } from 'obsidian'
|
||||
import type LocatorPlugin from './main'
|
||||
|
||||
export class Database extends Dexie {
|
||||
public static readonly dbVersion = 10
|
||||
public static readonly dbVersion = 1
|
||||
searchHistory!: Dexie.Table<{ id?: number; query: string }, number>
|
||||
minisearch!: Dexie.Table<
|
||||
{
|
||||
|
||||
@@ -190,15 +190,16 @@ export class SearchEngine {
|
||||
}
|
||||
const mtime = storedFields?.mtime as number
|
||||
const now = new Date().valueOf()
|
||||
const daysElapsed = (now - mtime) / (24 * 3600)
|
||||
|
||||
const daysElapsed = (now - mtime) / (24 * 3600_000)
|
||||
// Documents boost
|
||||
const cutoff = {
|
||||
[RecencyCutoff.Day]: -3,
|
||||
[RecencyCutoff.Week]: -0.3,
|
||||
[RecencyCutoff.Month]: -0.1,
|
||||
} as const
|
||||
return 1 + Math.exp(cutoff[settings.recencyBoost] * daysElapsed)
|
||||
return (
|
||||
1 + Math.exp(cutoff[settings.recencyBoost] * (daysElapsed / 1000))
|
||||
)
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
import { App, Plugin, PluginSettingTab, Setting } from 'obsidian'
|
||||
import { RecencyCutoff } from '../globals'
|
||||
import type LocatorPlugin from '../main'
|
||||
import { enableVerboseLogging } from '../tools/utils'
|
||||
import { injectSettingsBehavior } from './settings-behavior'
|
||||
import { injectSettingsDanger } from './settings-danger'
|
||||
import { injectSettingsHttp } from './settings-http'
|
||||
@@ -64,7 +63,6 @@ export class SettingsTab extends PluginSettingTab {
|
||||
.addToggle(toggle =>
|
||||
toggle.setValue(settings.verboseLogging).onChange(async v => {
|
||||
settings.verboseLogging = v
|
||||
enableVerboseLogging(v)
|
||||
await saveSettings(this.plugin)
|
||||
})
|
||||
)
|
||||
@@ -137,6 +135,5 @@ export async function loadSettings(plugin: Plugin): Promise<LocatorSettings> {
|
||||
await plugin.loadData()
|
||||
)
|
||||
showExcerpt.set(settings.showExcerpt)
|
||||
enableVerboseLogging(settings.verboseLogging)
|
||||
return settings
|
||||
}
|
||||
+8
-177
@@ -1,183 +1,13 @@
|
||||
import { getIcon, normalizePath } from 'obsidian'
|
||||
import type LocatorPlugin from '../main'
|
||||
import {
|
||||
isFileImage,
|
||||
isFilePDF,
|
||||
isFileCanvas,
|
||||
isFileExcalidraw,
|
||||
warnVerbose,
|
||||
} from './utils'
|
||||
import { escapeHTML } from './text-processing'
|
||||
import { getIcon } from 'obsidian'
|
||||
import { isFileCanvas, isFileExcalidraw, isFileImage, isFilePDF } from './utils'
|
||||
|
||||
export interface IconPacks {
|
||||
prefixToIconPack: { [prefix: string]: string }
|
||||
iconsPath: string
|
||||
}
|
||||
|
||||
export async function loadIconData(plugin: LocatorPlugin): Promise<any> {
|
||||
const app = plugin.app
|
||||
|
||||
// Check if the 'obsidian-icon-folder' plugin is installed and enabled
|
||||
// Casting 'app' to 'any' here to avoid TypeScript errors since 'plugins' might not be defined on 'App'
|
||||
const iconFolderPlugin = (app as any).plugins.getPlugin(
|
||||
'obsidian-icon-folder'
|
||||
)
|
||||
if (!iconFolderPlugin) {
|
||||
return {}
|
||||
}
|
||||
|
||||
const dataJsonPath = `${app.vault.configDir}/plugins/obsidian-icon-folder/data.json`
|
||||
try {
|
||||
const dataJsonContent = await app.vault.adapter.read(dataJsonPath)
|
||||
const rawIconData = JSON.parse(dataJsonContent)
|
||||
// Normalize keys
|
||||
const iconData: any = {}
|
||||
for (const key in rawIconData) {
|
||||
const normalizedKey = normalizePath(key)
|
||||
iconData[normalizedKey] = rawIconData[key]
|
||||
}
|
||||
return iconData
|
||||
} catch (e) {
|
||||
warnVerbose('Failed to read data.json:', e)
|
||||
return {}
|
||||
}
|
||||
}
|
||||
|
||||
export async function initializeIconPacks(
|
||||
plugin: LocatorPlugin
|
||||
): Promise<IconPacks> {
|
||||
// Add 'Li' prefix for Lucide icons
|
||||
const prefixToIconPack: { [prefix: string]: string } = { Li: 'lucide-icons' }
|
||||
let iconsPath = 'icons'
|
||||
|
||||
const app = plugin.app
|
||||
|
||||
// Access the obsidian-icon-folder plugin
|
||||
const iconFolderPlugin = (app as any).plugins.getPlugin(
|
||||
'obsidian-icon-folder'
|
||||
)
|
||||
|
||||
if (iconFolderPlugin) {
|
||||
// Get the icons path from the plugin's settings
|
||||
const iconFolderSettings = iconFolderPlugin.settings
|
||||
iconsPath = iconFolderSettings?.iconPacksPath || 'icons'
|
||||
const iconsDir = `${app.vault.configDir}/${iconsPath}`
|
||||
|
||||
try {
|
||||
const iconPackDirs = await app.vault.adapter.list(iconsDir)
|
||||
if (iconPackDirs.folders && iconPackDirs.folders.length > 0) {
|
||||
for (const folderPath of iconPackDirs.folders) {
|
||||
const pathParts = folderPath.split('/')
|
||||
const iconPackName = pathParts[pathParts.length - 1]
|
||||
const prefix = createIconPackPrefix(iconPackName)
|
||||
prefixToIconPack[prefix] = iconPackName
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
warnVerbose('Failed to list icon packs:', e)
|
||||
}
|
||||
}
|
||||
|
||||
return { prefixToIconPack, iconsPath }
|
||||
}
|
||||
|
||||
function createIconPackPrefix(iconPackName: string): string {
|
||||
if (iconPackName.includes('-')) {
|
||||
const splitted = iconPackName.split('-')
|
||||
let result = splitted[0].charAt(0).toUpperCase()
|
||||
for (let i = 1; i < splitted.length; i++) {
|
||||
result += splitted[i].charAt(0).toLowerCase()
|
||||
}
|
||||
return result
|
||||
}
|
||||
return (
|
||||
iconPackName.charAt(0).toUpperCase() + iconPackName.charAt(1).toLowerCase()
|
||||
)
|
||||
}
|
||||
|
||||
export function getIconNameForPath(path: string, iconData: any): string | null {
|
||||
const normalizedPath = normalizePath(path)
|
||||
const iconEntry = iconData[normalizedPath]
|
||||
if (iconEntry) {
|
||||
if (typeof iconEntry === 'string') {
|
||||
return iconEntry
|
||||
} else if (typeof iconEntry === 'object' && iconEntry.iconName) {
|
||||
return iconEntry.iconName
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
export function parseIconName(iconName: string): {
|
||||
prefix: string
|
||||
name: string
|
||||
} {
|
||||
const prefixMatch = iconName.match(/^[A-Z][a-z]*/)
|
||||
if (prefixMatch) {
|
||||
const prefix = prefixMatch[0]
|
||||
const name = iconName.substring(prefix.length)
|
||||
return { prefix, name }
|
||||
} else {
|
||||
// No prefix, treat the entire iconName as the name
|
||||
return { prefix: '', name: iconName }
|
||||
}
|
||||
}
|
||||
|
||||
export async function loadIconSVG(
|
||||
iconName: string,
|
||||
plugin: LocatorPlugin,
|
||||
iconsPath: string,
|
||||
prefixToIconPack: { [prefix: string]: string }
|
||||
): Promise<string | null> {
|
||||
const parsed = parseIconName(iconName)
|
||||
const { prefix, name } = parsed
|
||||
|
||||
if (!prefix) {
|
||||
// No prefix, assume it's an emoji or text
|
||||
return `<span class="locator-result__icon--emoji">${escapeHTML(
|
||||
name
|
||||
)}</span>`
|
||||
}
|
||||
|
||||
const iconPackName = prefixToIconPack[prefix]
|
||||
|
||||
if (!iconPackName) {
|
||||
warnVerbose(`No icon pack found for prefix: ${prefix}`)
|
||||
return null
|
||||
}
|
||||
|
||||
if (iconPackName === 'lucide-icons') {
|
||||
// Convert CamelCase to dash-case for Lucide icons
|
||||
const dashedName = name.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase()
|
||||
const iconEl = getIcon(dashedName)
|
||||
if (iconEl) {
|
||||
return iconEl.outerHTML
|
||||
} else {
|
||||
warnVerbose(`Lucide icon not found: ${dashedName}`)
|
||||
return null
|
||||
}
|
||||
} else {
|
||||
if (!iconsPath) {
|
||||
warnVerbose('Icons path is not set. Cannot load icon SVG.')
|
||||
return null
|
||||
}
|
||||
const iconPath = `${plugin.app.vault.configDir}/${iconsPath}/${iconPackName}/${name}.svg`
|
||||
try {
|
||||
const svgContent = await plugin.app.vault.adapter.read(iconPath)
|
||||
return svgContent
|
||||
} catch (e) {
|
||||
warnVerbose(`Failed to load icon SVG for ${iconName} at ${iconPath}:`, e)
|
||||
return null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function getDefaultIconSVG(
|
||||
notePath: string,
|
||||
plugin: LocatorPlugin
|
||||
): string {
|
||||
export function getDefaultIconSVG(notePath: string): string {
|
||||
// Return SVG content for default icons based on file type
|
||||
let iconName = 'file'
|
||||
|
||||
if (notePath === 'folder') {
|
||||
iconName = 'folder'
|
||||
} else {
|
||||
if (isFileImage(notePath)) {
|
||||
iconName = 'image'
|
||||
} else if (isFilePDF(notePath)) {
|
||||
@@ -185,6 +15,7 @@ export function getDefaultIconSVG(
|
||||
} else if (isFileCanvas(notePath) || isFileExcalidraw(notePath)) {
|
||||
iconName = 'layout-dashboard'
|
||||
}
|
||||
}
|
||||
const iconEl = getIcon(iconName)
|
||||
return iconEl ? iconEl.outerHTML : ''
|
||||
}
|
||||
+5
-7
@@ -9,6 +9,7 @@ import {
|
||||
import { md5 } from 'pure-md5'
|
||||
import { eng, fra } from 'stopword'
|
||||
import { isSearchMatch, type SearchMatch } from '../globals'
|
||||
import { settings } from 'src/settings'
|
||||
|
||||
export function pathWithoutFilename(path: string): string {
|
||||
const split = path.split('/')
|
||||
@@ -121,11 +122,12 @@ export function removeDiacritics(str: string, arabic = true): string {
|
||||
return ''
|
||||
}
|
||||
|
||||
// TODO: add a global setting to toggle this, this impacts performances
|
||||
if (arabic) {
|
||||
// Arabic diacritics
|
||||
// https://stackoverflow.com/a/40959537
|
||||
str = str
|
||||
.replace(/([^\u0621-\u063A\u0641-\u064A\u0660-\u0669a-zA-Z 0-9])/g, '')
|
||||
// .replace(/([^\u0621-\u063A\u0641-\u064A\u0660-\u0669a-zA-Z 0-9])/g, '')
|
||||
.replace(/(آ|إ|أ)/g, 'ا')
|
||||
.replace(/(ة)/g, 'ه')
|
||||
.replace(/(ئ|ؤ)/g, 'ء')
|
||||
@@ -144,6 +146,7 @@ export function removeDiacritics(str: string, arabic = true): string {
|
||||
str = str.normalize('NFD').replace(diacriticsRegex, '').normalize('NFC')
|
||||
str = str.replaceAll('[__locator__backtick__]', '`')
|
||||
str = str.replaceAll('[__locator__caret__]', '^')
|
||||
|
||||
return str
|
||||
}
|
||||
|
||||
@@ -250,13 +253,8 @@ export function warnVerbose(...args: any[]): void {
|
||||
printVerbose(console.warn, ...args)
|
||||
}
|
||||
|
||||
let verboseLoggingEnabled = false
|
||||
export function enableVerboseLogging(enable: boolean): void {
|
||||
verboseLoggingEnabled = enable
|
||||
}
|
||||
|
||||
function printVerbose(fn: (...args: any[]) => any, ...args: any[]): void {
|
||||
if (verboseLoggingEnabled) {
|
||||
if (settings.verboseLogging) {
|
||||
fn(...args)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user