WIP refactor to take advantage of minisearch 6.0
This commit is contained in:
@@ -4,14 +4,13 @@ import {
|
||||
getAliasesFromMetadata,
|
||||
getTagsFromMetadata,
|
||||
isFileImage,
|
||||
isFilePDF,
|
||||
isFilePlaintext,
|
||||
removeDiacritics,
|
||||
} from './tools/utils'
|
||||
import * as NotesIndex from './notes-index'
|
||||
import type { TFile } from 'obsidian'
|
||||
import type { IndexedDocument } from './globals'
|
||||
import { getNonExistingNotes } from './tools/notes'
|
||||
import { getPdfText, getImageText } from 'obsidian-text-extract'
|
||||
import { getImageText, getPdfText } from 'obsidian-text-extract'
|
||||
|
||||
/**
|
||||
* Return all plaintext files as IndexedDocuments
|
||||
@@ -20,9 +19,9 @@ 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 fileToIndexedDocument(file)
|
||||
const doc = await getIndexedDocument(file.path)
|
||||
data.push(doc)
|
||||
await cacheManager.updateLiveDocument(file.path, doc)
|
||||
// await cacheManager.updateLiveDocument(file.path, doc)
|
||||
}
|
||||
return data
|
||||
}
|
||||
@@ -31,7 +30,7 @@ export async function getPlainTextFiles(): Promise<IndexedDocument[]> {
|
||||
* Return all PDFs as IndexedDocuments.
|
||||
*/
|
||||
export async function getPDFAsDocuments(): Promise<IndexedDocument[]> {
|
||||
const files = app.vault.getFiles().filter(f => f.path.endsWith('.pdf'))
|
||||
const files = app.vault.getFiles().filter(f => isFilePDF(f.path))
|
||||
return await getBinaryFiles(files)
|
||||
}
|
||||
|
||||
@@ -49,8 +48,8 @@ async function getBinaryFiles(files: TFile[]): Promise<IndexedDocument[]> {
|
||||
for (const file of files) {
|
||||
input.push(
|
||||
new Promise(async (resolve, reject) => {
|
||||
const doc = await fileToIndexedDocument(file)
|
||||
await cacheManager.updateLiveDocument(file.path, doc)
|
||||
const doc = await getIndexedDocument(file.path)
|
||||
// await cacheManager.updateLiveDocument(file.path, doc)
|
||||
data.push(doc)
|
||||
return resolve(null)
|
||||
})
|
||||
@@ -60,37 +59,34 @@ async function getBinaryFiles(files: TFile[]): Promise<IndexedDocument[]> {
|
||||
return data
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a file into an IndexedDocument.
|
||||
* Will use the cache if possible.
|
||||
*/
|
||||
export async function fileToIndexedDocument(
|
||||
file: TFile
|
||||
export async function getIndexedDocument(
|
||||
path: string
|
||||
): Promise<IndexedDocument> {
|
||||
const file = app.vault.getFiles().find(f => f.path === path)
|
||||
if (!file) throw new Error(`Invalid file path: "${path}"`)
|
||||
let content: string
|
||||
if (isFilePlaintext(file.path)) {
|
||||
if (isFilePlaintext(path)) {
|
||||
content = await app.vault.cachedRead(file)
|
||||
} else if (file.path.endsWith('.pdf')) {
|
||||
} else if (isFilePDF(path)) {
|
||||
content = await getPdfText(file)
|
||||
} else if (isFileImage(file.path)) {
|
||||
content = await getImageText(file)
|
||||
} else {
|
||||
throw new Error('Invalid file: ' + file.path)
|
||||
throw new Error('Invalid file format: ' + file.path)
|
||||
}
|
||||
|
||||
content = removeDiacritics(content)
|
||||
const metadata = app.metadataCache.getFileCache(file)
|
||||
|
||||
// Look for links that lead to non-existing files,
|
||||
// and add them to the index.
|
||||
if (metadata) {
|
||||
// FIXME: https://github.com/scambier/obsidian-omnisearch/issues/129
|
||||
const nonExisting = getNonExistingNotes(file, metadata)
|
||||
for (const name of nonExisting.filter(
|
||||
o => !cacheManager.getLiveDocument(o)
|
||||
)) {
|
||||
NotesIndex.addNonExistingToIndex(name, file.path)
|
||||
}
|
||||
// // FIXME: https://github.com/scambier/obsidian-omnisearch/issues/129
|
||||
// const nonExisting = getNonExistingNotes(file, metadata)
|
||||
// for (const name of nonExisting.filter(
|
||||
// o => !cacheManager.getLiveDocument(o)
|
||||
// )) {
|
||||
// NotesIndex.addNonExistingToIndex(name, file.path)
|
||||
// }
|
||||
|
||||
// EXCALIDRAW
|
||||
// Remove the json code
|
||||
@@ -117,3 +113,61 @@ export async function fileToIndexedDocument(
|
||||
headings3: metadata ? extractHeadingsFromCache(metadata, 3).join(' ') : '',
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a file into an IndexedDocument.
|
||||
* Will use the cache if possible.
|
||||
*/
|
||||
// async function fileToIndexedDocument(
|
||||
// file: TFile
|
||||
// ): Promise<IndexedDocument> {
|
||||
// let content: string
|
||||
// if (isFilePlaintext(file.path)) {
|
||||
// content = await app.vault.cachedRead(file)
|
||||
// } else if (isFilePDF(file.path)) {
|
||||
// content = await getPdfText(file)
|
||||
// } else if (isFileImage(file.path)) {
|
||||
// content = await getImageText(file)
|
||||
// } else {
|
||||
// throw new Error('Invalid file: ' + file.path)
|
||||
// }
|
||||
//
|
||||
// content = removeDiacritics(content)
|
||||
// const metadata = app.metadataCache.getFileCache(file)
|
||||
//
|
||||
// // Look for links that lead to non-existing files,
|
||||
// // and add them to the index.
|
||||
// if (metadata) {
|
||||
// // FIXME: https://github.com/scambier/obsidian-omnisearch/issues/129
|
||||
// const nonExisting = getNonExistingNotes(file, metadata)
|
||||
// for (const name of nonExisting.filter(
|
||||
// o => !cacheManager.getLiveDocument(o)
|
||||
// )) {
|
||||
// NotesIndex.addNonExistingToIndex(name, file.path)
|
||||
// }
|
||||
//
|
||||
// // EXCALIDRAW
|
||||
// // Remove the json code
|
||||
// if (metadata.frontmatter?.['excalidraw-plugin']) {
|
||||
// const comments =
|
||||
// metadata.sections?.filter(s => s.type === 'comment') ?? []
|
||||
// for (const { start, end } of comments.map(c => c.position)) {
|
||||
// content =
|
||||
// content.substring(0, start.offset - 1) + content.substring(end.offset)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// return {
|
||||
// basename: removeDiacritics(file.basename),
|
||||
// content,
|
||||
// path: file.path,
|
||||
// mtime: file.stat.mtime,
|
||||
//
|
||||
// tags: getTagsFromMetadata(metadata),
|
||||
// aliases: getAliasesFromMetadata(metadata).join(''),
|
||||
// headings1: metadata ? extractHeadingsFromCache(metadata, 1).join(' ') : '',
|
||||
// headings2: metadata ? extractHeadingsFromCache(metadata, 2).join(' ') : '',
|
||||
// headings3: metadata ? extractHeadingsFromCache(metadata, 3).join(' ') : '',
|
||||
// }
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user