WIP document mapping

This commit is contained in:
Simon Cambier
2022-11-26 18:03:06 +01:00
parent c6cee62214
commit d4519b57c2
4 changed files with 100 additions and 69 deletions

View File

@@ -10,6 +10,7 @@ import {
import type { TFile } from 'obsidian'
import type { IndexedDocument } from './globals'
import { getImageText, getPdfText } from 'obsidian-text-extract'
import { cacheManager } from "./cache-manager";
/**
* Return all plaintext files as IndexedDocuments
@@ -18,7 +19,7 @@ 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 getIndexedDocument(file.path)
const doc = await cacheManager.getDocument(file.path)
data.push(doc)
// await cacheManager.updateLiveDocument(file.path, doc)
}
@@ -47,7 +48,7 @@ async function getBinaryFiles(files: TFile[]): Promise<IndexedDocument[]> {
for (const file of files) {
input.push(
new Promise(async (resolve, _reject) => {
const doc = await getIndexedDocument(file.path)
const doc = await cacheManager.getDocument(file.path)
data.push(doc)
return resolve(null)
})
@@ -57,61 +58,6 @@ async function getBinaryFiles(files: TFile[]): Promise<IndexedDocument[]> {
return data
}
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(path)) {
content = await app.vault.cachedRead(file)
} else if (isFilePDF(path)) {
content = await getPdfText(file)
} else if (isFileImage(file.path)) {
content = await getImageText(file)
} else {
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)
// }
// 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(' ') : '',
}
}
/**
* Convert a file into an IndexedDocument.
* Will use the cache if possible.