Merge branch 'develop'

# Conflicts:
#	manifest-beta.json
#	src/settings.ts
#	versions.json
This commit is contained in:
Simon Cambier
2023-07-22 23:17:43 +02:00
9 changed files with 215 additions and 90 deletions

View File

@@ -10,7 +10,9 @@ import {
getAliasesFromMetadata,
getTagsFromMetadata,
isFileCanvas,
isFileFromDataloomPlugin,
isFilePlaintext,
isFilenameIndexable,
logDebug,
makeMD5,
removeDiacritics,
@@ -18,6 +20,7 @@ import {
import type { CanvasData } from 'obsidian/canvas'
import type { AsPlainObject } from 'minisearch'
import type MiniSearch from 'minisearch'
import { settings } from './settings'
/**
* This function is responsible for extracting the text from a file and
@@ -58,11 +61,38 @@ async function getAndMapIndexedDocument(
content = texts.join('\r\n')
}
// ** Dataloom plugin **
else if (settings.dataloomIndexing && isFileFromDataloomPlugin(path)) {
try {
const data = JSON.parse(await app.vault.cachedRead(file))
// data is a json object, we recursively iterate the keys
// and concatenate the values if the key is "markdown"
const texts: string[] = []
const iterate = (obj: any) => {
for (const key in obj) {
if (typeof obj[key] === 'object') {
iterate(obj[key])
} else if (key === 'markdown') {
texts.push(obj[key])
}
}
}
iterate(data)
content = texts.join('\r\n')
} catch (e) {
console.error('Omnisearch: Error while parsing Dataloom file', path)
console.error(e)
}
}
// ** Image or PDF **
else if (extractor?.canFileBeExtracted(path)) {
content = await extractor.extractText(file)
} else {
throw new Error(`Unsupported file type: "${path}"`)
}
// ** Unsupported files **
else if (isFilenameIndexable(path)) {
content = file.path
}
if (content === null || content === undefined) {

View File

@@ -18,7 +18,6 @@
import { Query } from 'src/search/query'
import { openNote } from 'src/tools/notes'
import { searchEngine } from 'src/search/omnisearch'
import { cacheManager } from 'src/cache-manager'
export let modal: OmnisearchInFileModal
export let parent: OmnisearchVaultModal | null = null

View File

@@ -1,50 +0,0 @@
import { isFileImage, isFilePDF, isFilePlaintext } from './tools/utils'
import type { TFile } from 'obsidian'
import type { IndexedDocument } from './globals'
import { cacheManager } from './cache-manager'
/**
* Return all plaintext files as IndexedDocuments
*/
export async function getPlainTextFiles(): Promise<IndexedDocument[]> {
const allFiles = app.vault.getFiles().filter(f => isFilePlaintext(f.path))
const data: IndexedDocument[] = []
for (const file of allFiles) {
const doc = await cacheManager.getDocument(file.path)
data.push(doc)
// await cacheManager.updateLiveDocument(file.path, doc)
}
return data
}
/**
* Return all PDFs as IndexedDocuments.
*/
export async function getPDFAsDocuments(): Promise<IndexedDocument[]> {
const files = app.vault.getFiles().filter(f => isFilePDF(f.path))
return await getBinaryFiles(files)
}
/**
* Return all imageas as IndexedDocuments.
*/
export async function getImagesAsDocuments(): Promise<IndexedDocument[]> {
const files = app.vault.getFiles().filter(f => isFileImage(f.path))
return await getBinaryFiles(files)
}
async function getBinaryFiles(files: TFile[]): Promise<IndexedDocument[]> {
const data: IndexedDocument[] = []
const input = []
for (const file of files) {
input.push(
new Promise(async (resolve, _reject) => {
const doc = await cacheManager.getDocument(file.path)
data.push(doc)
return resolve(null)
})
)
}
await Promise.all(input)
return data
}

View File

@@ -107,4 +107,4 @@ export function isCacheEnabled(): boolean {
}
export const SPACE_OR_PUNCTUATION =
/[|\n\r -#%-*,-/:;?@[-\]_{}\u00A0\u00A1\u00A7\u00AB\u00B6\u00B7\u00BB\u00BF\u037E\u0387\u055A-\u055F\u0589\u058A\u05BE\u05C0\u05C3\u05C6\u05F3\u05F4\u0609\u060A\u060C\u060D\u061B\u061E\u061F\u066A-\u066D\u06D4\u0700-\u070D\u07F7-\u07F9\u0830-\u083E\u085E\u0964\u0965\u0970\u09FD\u0A76\u0AF0\u0C77\u0C84\u0DF4\u0E4F\u0E5A\u0E5B\u0F04-\u0F12\u0F14\u0F3A-\u0F3D\u0F85\u0FD0-\u0FD4\u0FD9\u0FDA\u104A-\u104F\u10FB\u1360-\u1368\u1400\u166E\u1680\u169B\u169C\u16EB-\u16ED\u1735\u1736\u17D4-\u17D6\u17D8-\u17DA\u1800-\u180A\u1944\u1945\u1A1E\u1A1F\u1AA0-\u1AA6\u1AA8-\u1AAD\u1B5A-\u1B60\u1BFC-\u1BFF\u1C3B-\u1C3F\u1C7E\u1C7F\u1CC0-\u1CC7\u1CD3\u2000-\u200A\u2010-\u2029\u202F-\u2043\u2045-\u2051\u2053-\u205F\u207D\u207E\u208D\u208E\u2308-\u230B\u2329\u232A\u2768-\u2775\u27C5\u27C6\u27E6-\u27EF\u2983-\u2998\u29D8-\u29DB\u29FC\u29FD\u2CF9-\u2CFC\u2CFE\u2CFF\u2D70\u2E00-\u2E2E\u2E30-\u2E4F\u3000-\u3003\u3008-\u3011\u3014-\u301F\u3030\u303D\u30A0\u30FB\uA4FE\uA4FF\uA60D-\uA60F\uA673\uA67E\uA6F2-\uA6F7\uA874-\uA877\uA8CE\uA8CF\uA8F8-\uA8FA\uA8FC\uA92E\uA92F\uA95F\uA9C1-\uA9CD\uA9DE\uA9DF\uAA5C-\uAA5F\uAADE\uAADF\uAAF0\uAAF1\uABEB\uFD3E\uFD3F\uFE10-\uFE19\uFE30-\uFE52\uFE54-\uFE61\uFE63\uFE68\uFE6A\uFE6B\uFF01-\uFF03\uFF05-\uFF0A\uFF0C-\uFF0F\uFF1A\uFF1B\uFF1F\uFF20\uFF3B-\uFF3D\uFF3F\uFF5B\uFF5D\uFF5F-\uFF65]+/u
/[|\n\r -#%-*,.\/:;?@[-\]_{}\u00A0\u00A1\u00A7\u00AB\u00B6\u00B7\u00BB\u00BF\u037E\u0387\u055A-\u055F\u0589\u058A\u05BE\u05C0\u05C3\u05C6\u05F3\u05F4\u0609\u060A\u060C\u060D\u061B\u061E\u061F\u066A-\u066D\u06D4\u0700-\u070D\u07F7-\u07F9\u0830-\u083E\u085E\u0964\u0965\u0970\u09FD\u0A76\u0AF0\u0C77\u0C84\u0DF4\u0E4F\u0E5A\u0E5B\u0F04-\u0F12\u0F14\u0F3A-\u0F3D\u0F85\u0FD0-\u0FD4\u0FD9\u0FDA\u104A-\u104F\u10FB\u1360-\u1368\u1400\u166E\u1680\u169B\u169C\u16EB-\u16ED\u1735\u1736\u17D4-\u17D6\u17D8-\u17DA\u1800-\u180A\u1944\u1945\u1A1E\u1A1F\u1AA0-\u1AA6\u1AA8-\u1AAD\u1B5A-\u1B60\u1BFC-\u1BFF\u1C3B-\u1C3F\u1C7E\u1C7F\u1CC0-\u1CC7\u1CD3\u2000-\u200A\u2010-\u2029\u202F-\u2043\u2045-\u2051\u2053-\u205F\u207D\u207E\u208D\u208E\u2308-\u230B\u2329\u232A\u2768-\u2775\u27C5\u27C6\u27E6-\u27EF\u2983-\u2998\u29D8-\u29DB\u29FC\u29FD\u2CF9-\u2CFC\u2CFE\u2CFF\u2D70\u2E00-\u2E2E\u2E30-\u2E4F\u3000-\u3003\u3008-\u3011\u3014-\u301F\u3030\u303D\u30A0\u30FB\uA4FE\uA4FF\uA60D-\uA60F\uA673\uA67E\uA6F2-\uA6F7\uA874-\uA877\uA8CE\uA8CF\uA8F8-\uA8FA\uA8FC\uA92E\uA92F\uA95F\uA9C1-\uA9CD\uA9DE\uA9DF\uAA5C-\uAA5F\uAADE\uAADF\uAAF0\uAAF1\uABEB\uFD3E\uFD3F\uFE10-\uFE19\uFE30-\uFE52\uFE54-\uFE61\uFE63\uFE68\uFE6A\uFE6B\uFF01-\uFF03\uFF05-\uFF0A\uFF0C-\uFF0F\uFF1A\uFF1B\uFF1F\uFF20\uFF3B-\uFF3D\uFF3F\uFF5B\uFF5D\uFF5F-\uFF65]+/u

View File

@@ -12,6 +12,7 @@ import {
logDebug,
removeDiacritics,
splitCamelCase,
splitHyphens,
stringsToRegex,
stripMarkdownCharacters,
warnDebug,
@@ -22,17 +23,22 @@ import { cacheManager } from '../cache-manager'
import { sortBy } from 'lodash-es'
const tokenize = (text: string): string[] => {
const tokens = text.split(SPACE_OR_PUNCTUATION)
let tokens = text.split(SPACE_OR_PUNCTUATION)
// When enabled, we only use the chsSegmenter,
// and not the other custom tokenizers
const chsSegmenter = getChsSegmenter()
if (chsSegmenter) {
return tokens.flatMap(word =>
tokens = tokens.flatMap(word =>
chsRegex.test(word) ? chsSegmenter.cut(word) : [word]
)
} else {
if (settings.splitCamelCase)
return [...tokens, ...tokens.flatMap(splitCamelCase)]
return tokens
// Split camelCase tokens into "camel" and "case
tokens = [...tokens, ...tokens.flatMap(splitCamelCase)]
// Split hyphenated tokens
tokens = [...tokens, ...tokens.flatMap(splitHyphens)]
}
return tokens
}
export class Omnisearch {
@@ -182,12 +188,26 @@ export class Omnisearch {
logDebug('Starting search for', query)
let fuzziness: number
switch (settings.fuzziness) {
case '0':
fuzziness = 0
break
case '1':
fuzziness = 0.1
break
default:
fuzziness = 0.2
break
}
let results = this.minisearch.search(query.segmentsToStr(), {
prefix: term => term.length >= options.prefixLength,
// length <= 3: no fuzziness
// length <= 5: fuzziness of 10%
// length > 5: fuzziness of 20%
fuzzy: term => (term.length <= 3 ? 0 : term.length <= 5 ? 0.1 : 0.2),
fuzzy: term =>
term.length <= 3 ? 0 : term.length <= 5 ? fuzziness / 2 : fuzziness,
combineWith: 'AND',
boost: {
basename: settings.weightBasename,

View File

@@ -9,11 +9,10 @@ type Keywords = {
} & { text: string[] }
export class Query {
query: Keywords & { exclude: Keywords }
/**
* @deprecated
*/
extensions: string[] = []
query: Keywords & {
exclude: Keywords
}
#inQuotes: string[]
constructor(text = '') {
if (settings.ignoreDiacritics) {
@@ -44,7 +43,10 @@ export class Query {
}
}
this.query = parsed
this.extensions = this.query.ext ?? []
// Get strings in quotes, and remove the quotes
this.#inQuotes =
text.match(/"([^"]+)"/g)?.map(o => o.replace(/"/g, '')) ?? []
}
public isEmpty(): boolean {
@@ -62,7 +64,7 @@ export class Query {
public segmentsToStr(): string {
return this.query.text.join(' ')
}
public getTags(): string[] {
return this.query.text.filter(o => o.startsWith('#'))
}
@@ -72,6 +74,11 @@ export class Query {
}
public getExactTerms(): string[] {
return this.query.text.filter(o => o.split(' ').length > 1)
return [
...new Set([
...this.query.text.filter(o => o.split(' ').length > 1),
...this.#inQuotes,
]),
]
}
}

View File

@@ -34,8 +34,12 @@ export interface OmnisearchSettings extends WeightingSettings {
indexedFileTypes: string[]
/** Enable PDF indexing */
PDFIndexing: boolean
/** Enable PDF indexing */
/** Enable Images indexing */
imagesIndexing: boolean
/** Enable Dataloom indexing */
dataloomIndexing: boolean
/** Enable indexing of unknown files */
unsupportedFilesIndexing: 'yes' | 'no' | 'default'
/** Activate the small 🔍 button on Obsidian's ribbon */
ribbonIcon: boolean
/** Display the small contextual excerpt in search results */
@@ -54,6 +58,7 @@ export interface OmnisearchSettings extends WeightingSettings {
splitCamelCase: boolean
openInNewPane: boolean
verboseLogging: boolean
fuzziness: '0' | '1' | '2'
}
/**
@@ -144,28 +149,66 @@ export class SettingsTab extends PluginSettingTab {
)
.setDisabled(!getTextExtractor())
// Additional files to index
// Dataloom Indexing
const indexDataLoomDesc = new DocumentFragment()
indexDataLoomDesc.createSpan({}, span => {
span.innerHTML = `Include <a href="https://github.com/trey-wallis/obsidian-dataloom">DataLoom</a> <pre style="display:inline">.loom</pre> files in search results
<br/>${needsARestart}`
})
new Setting(containerEl)
.setName('DataLoom indexing (beta)')
.setDesc(indexDataLoomDesc)
.addToggle(toggle =>
toggle.setValue(settings.dataloomIndexing).onChange(async v => {
settings.dataloomIndexing = v
await saveSettings(this.plugin)
})
)
.setDisabled(!getTextExtractor())
// Additional text files to index
const indexedFileTypesDesc = new DocumentFragment()
indexedFileTypesDesc.createSpan({}, span => {
span.innerHTML = `In addition to standard <code>md</code> files, Omnisearch can also index other <strong style="color: var(--text-accent)">plaintext</strong> files.<br/>
Add extensions separated by a space, without the dot. Example: "<code>txt org</code>".<br />
span.innerHTML = `In addition to standard <code>md</code> files, Omnisearch can also index other <strong style="color: var(--text-accent)">PLAINTEXT</strong> files.<br/>
Add extensions separated by a space, without the dot. Example: "<code>txt org csv</code>".<br />
⚠️ <span style="color: var(--text-accent)">Using extensions of non-plaintext files (like .docx or .pptx) WILL cause crashes,
because Omnisearch will try to index their content.</span><br />
${needsARestart}`
})
new Setting(containerEl)
.setName('Additional files to index')
.setName('Additional TEXT files to index')
.setDesc(indexedFileTypesDesc)
.addText(component => {
component
.setValue(settings.indexedFileTypes.join(' '))
.setPlaceholder('Example: txt org')
.setPlaceholder('Example: txt org csv')
.onChange(async v => {
settings.indexedFileTypes = v.split(' ')
await saveSettings(this.plugin)
})
})
// Unsupported files
const indexUnsupportedDesc = new DocumentFragment()
indexUnsupportedDesc.createSpan({}, span => {
span.innerHTML = `
Omnisearch can index file<strong>names</strong> of "unsupported" files, such as e.g. <pre style="display:inline">.mp4</pre> or <pre style="display:inline">.xlsx</pre>.<br/>
"Obsidian setting" will respect the value of "<em>Files & Links > Detect all file extensions</em>".
<br />${needsARestart}`
})
new Setting(containerEl)
.setName('Index unsupported files (beta)')
.setDesc(indexUnsupportedDesc)
.addDropdown(dropdown => {
dropdown
.addOptions({ yes: 'Yes', no: 'No', default: 'Obsidian setting' })
.setValue(settings.unsupportedFilesIndexing)
.onChange(async v => {
;(settings.unsupportedFilesIndexing as any) = v
await saveSettings(this.plugin)
})
})
//#endregion Indexing
//#region Behavior
@@ -264,6 +307,29 @@ export class SettingsTab extends PluginSettingTab {
})
)
// Fuzziness
new Setting(containerEl)
.setName('Fuzziness')
.setDesc(
"Define the level of fuzziness for the search. The higher the fuzziness, the more results you'll get."
)
.addDropdown(dropdown =>
dropdown
.addOptions({
0: 'Exact match',
1: 'Not too fuzzy',
2: 'Fuzzy enough',
})
.setValue(settings.fuzziness)
.onChange(async v => {
if (!['0', '1', '2'].includes(v)) {
v = '2'
}
settings.fuzziness = v as '0' | '1' | '2'
await saveSettings(this.plugin)
})
)
//#endregion Behavior
//#region User Interface
@@ -466,6 +532,8 @@ export const DEFAULT_SETTINGS: OmnisearchSettings = {
indexedFileTypes: [] as string[],
PDFIndexing: false,
imagesIndexing: false,
dataloomIndexing: false,
unsupportedFilesIndexing: 'no',
splitCamelCase: false,
openInNewPane: false,
@@ -476,6 +544,7 @@ export const DEFAULT_SETTINGS: OmnisearchSettings = {
highlight: true,
showPreviousQueryResults: true,
simpleSearch: false,
fuzziness: '0',
weightBasename: 3,
weightDirectory: 2,
@@ -500,5 +569,13 @@ export async function saveSettings(plugin: Plugin): Promise<void> {
}
export function isPluginDisabled(): boolean {
return app.loadLocalStorage(K_DISABLE_OMNISEARCH) == '1'
return app.loadLocalStorage(K_DISABLE_OMNISEARCH) === '1'
}
export function canIndexUnsupportedFiles(): boolean {
return (
settings.unsupportedFilesIndexing === 'yes' ||
(settings.unsupportedFilesIndexing === 'default' &&
!!app.vault.getConfig('showUnsupportedFiles'))
)
}

View File

@@ -18,7 +18,7 @@ import {
SPACE_OR_PUNCTUATION,
type SearchMatch,
} from '../globals'
import { settings } from '../settings'
import { canIndexUnsupportedFiles, settings } from '../settings'
import { type BinaryLike, createHash } from 'crypto'
import { md5 } from 'pure-md5'
@@ -26,14 +26,10 @@ export function highlighter(str: string): string {
return `<span class="${highlightClass}">${str}</span>`
}
export function highlighterGroups(...args: any[]) {
if (
args[1] !== null &&
args[1] !== undefined &&
args[2] !== null &&
args[2] !== undefined
)
return `<span>${args[1]}</span><span class="${highlightClass}">${args[2]}</span>`
export function highlighterGroups(_substring: string, ...args: any[]) {
// args[0] is the single char preceding args[1], which is the word we want to highlight
if (!!args[1].trim())
return `<span>${args[0]}</span><span class="${highlightClass}">${args[1]}</span>`
return '&lt;no content&gt;'
}
@@ -91,15 +87,20 @@ export function getAllIndices(text: string, regex: RegExp): SearchMatch[] {
*/
export function stringsToRegex(strings: string[]): RegExp {
if (!strings.length) return /^$/g
// sort strings by decreasing length, so that longer strings are matched first
strings.sort((a, b) => b.length - a.length)
const joined =
'(' +
// Default word split is not applied if the user uses the cm-chs-patch plugin
(getChsSegmenter()
? ''
: // Split on start of line, spaces, punctuation, or capital letters (for camelCase)
// We also add the hyphen to the list of characters that can split words
settings.splitCamelCase
? `^|${SPACE_OR_PUNCTUATION.source}|[A-Z]`
: `^|${SPACE_OR_PUNCTUATION.source}`) +
? `^|${SPACE_OR_PUNCTUATION.source}|\-|[A-Z]`
: `^|${SPACE_OR_PUNCTUATION.source}|\-`) +
')' +
`(${strings.map(s => escapeRegex(s)).join('|')})`
@@ -262,18 +263,34 @@ export function getCtrlKeyLabel(): 'ctrl' | '⌘' {
return Platform.isMacOS ? '⌘' : 'ctrl'
}
export function isFileIndexable(path: string): boolean {
export function isContentIndexable(path: string): boolean {
const hasTextExtractor = !!getTextExtractor()
const canIndexPDF = hasTextExtractor && settings.PDFIndexing
const canIndexImages = hasTextExtractor && settings.imagesIndexing
return (
isFilePlaintext(path) ||
isFileCanvas(path) ||
isFileFromDataloomPlugin(path) ||
(canIndexPDF && isFilePDF(path)) ||
(canIndexImages && isFileImage(path))
)
}
export function isFilenameIndexable(path: string): boolean {
return (
canIndexUnsupportedFiles() ||
isFilePlaintext(path) ||
isFileCanvas(path) ||
isFileFromDataloomPlugin(path) ||
isFilePDF(path) ||
isFileImage(path)
)
}
export function isFileIndexable(path: string): boolean {
return isFilenameIndexable(path) || isContentIndexable(path)
}
export function isFileImage(path: string): boolean {
const ext = getExtension(path)
return ext === 'png' || ext === 'jpg' || ext === 'jpeg' || ext === 'webp'
@@ -291,6 +308,10 @@ export function isFileCanvas(path: string): boolean {
return path.endsWith('.canvas')
}
export function isFileFromDataloomPlugin(path: string): boolean {
return path.endsWith('.loom') || path.endsWith('.dashboard')
}
export function getExtension(path: string): string {
const split = path.split('.')
return split[split.length - 1] ?? ''
@@ -318,11 +339,32 @@ export function chunkArray<T>(arr: T[], len: number): T[][] {
}
/**
* Converts a 'fooBarBAZLorem' into ['foo', 'Bar', 'BAZ', 'Lorem]
* Converts a 'fooBarBAZLorem' into ['foo', 'Bar', 'BAZ', 'Lorem']
* If the string isn't camelCase, returns an empty array
* @param text
*/
export function splitCamelCase(text: string): string[] {
return text.replace(/([a-z](?=[A-Z]))/g, '$1 ').split(' ')
const split = text
.replace(/([a-z](?=[A-Z]))/g, '$1 ')
.split(' ')
.filter(t => t)
if (split.length > 1) {
return split
}
return []
}
/**
* Converts a 'foo-bar-baz' into ['foo', 'bar', 'baz']
* If the string isn't hyphenated, returns an empty array
* @param text
*/
export function splitHyphens(text: string): string[] {
const split = text.split('-').filter(t => t)
if (split.length > 1) {
return split
}
return []
}
export function logDebug(...args: any[]): void {