#157 - Basic canvas support
This commit is contained in:
@@ -1,16 +1,15 @@
|
|||||||
import { Notice } from 'obsidian'
|
import { Notice } from 'obsidian'
|
||||||
import {
|
import {
|
||||||
getTextExtractor,
|
|
||||||
type DocumentRef,
|
type DocumentRef,
|
||||||
|
getTextExtractor,
|
||||||
type IndexedDocument,
|
type IndexedDocument,
|
||||||
} from './globals'
|
} from './globals'
|
||||||
import { database } from './database'
|
import { database } from './database'
|
||||||
import type { AsPlainObject } from 'minisearch'
|
|
||||||
import type MiniSearch from 'minisearch'
|
|
||||||
import {
|
import {
|
||||||
extractHeadingsFromCache,
|
extractHeadingsFromCache,
|
||||||
getAliasesFromMetadata,
|
getAliasesFromMetadata,
|
||||||
getTagsFromMetadata,
|
getTagsFromMetadata,
|
||||||
|
isFileCanvas,
|
||||||
isFileImage,
|
isFileImage,
|
||||||
isFilePDF,
|
isFilePDF,
|
||||||
isFilePlaintext,
|
isFilePlaintext,
|
||||||
@@ -18,18 +17,50 @@ import {
|
|||||||
removeDiacritics,
|
removeDiacritics,
|
||||||
} from './tools/utils'
|
} from './tools/utils'
|
||||||
import { getImageText, getPdfText } from 'obsidian-text-extract'
|
import { getImageText, getPdfText } from 'obsidian-text-extract'
|
||||||
|
import type { CanvasData } from 'obsidian/canvas'
|
||||||
|
import type { AsPlainObject } from 'minisearch'
|
||||||
|
import type MiniSearch from 'minisearch'
|
||||||
|
|
||||||
async function getIndexedDocument(path: string): Promise<IndexedDocument> {
|
/**
|
||||||
|
* This function is responsible for extracting the text from a file and
|
||||||
|
* returning it as an `IndexedDocument` object.
|
||||||
|
* @param path
|
||||||
|
*/
|
||||||
|
async function getAndMapIndexedDocument(
|
||||||
|
path: string
|
||||||
|
): Promise<IndexedDocument> {
|
||||||
const file = app.vault.getFiles().find(f => f.path === path)
|
const file = app.vault.getFiles().find(f => f.path === path)
|
||||||
if (!file) throw new Error(`Invalid file path: "${path}"`)
|
if (!file) throw new Error(`Invalid file path: "${path}"`)
|
||||||
let content: string | null = null
|
let content: string | null = null
|
||||||
|
|
||||||
const extractor = getTextExtractor()
|
const extractor = getTextExtractor()
|
||||||
// Plain text
|
|
||||||
|
// ** Plain text **
|
||||||
|
// Just read the file content
|
||||||
if (isFilePlaintext(path)) {
|
if (isFilePlaintext(path)) {
|
||||||
content = await app.vault.cachedRead(file)
|
content = await app.vault.cachedRead(file)
|
||||||
}
|
}
|
||||||
// Image or PDF, with the text-extractor plugin
|
|
||||||
|
// ** Canvas **
|
||||||
|
// Extract the text fields from the json
|
||||||
|
else if (isFileCanvas(path)) {
|
||||||
|
const canvas = JSON.parse(await app.vault.cachedRead(file)) as CanvasData
|
||||||
|
let texts: string[] = []
|
||||||
|
// Concatenate text from the canvas fields
|
||||||
|
for (const node of canvas.nodes) {
|
||||||
|
if (node.type === 'text') {
|
||||||
|
texts.push(node.text)
|
||||||
|
} else if (node.type === 'file') {
|
||||||
|
texts.push(node.file)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (const edge of canvas.edges.filter(e => !!e.label)) {
|
||||||
|
texts.push(edge.label!)
|
||||||
|
}
|
||||||
|
content = texts.join('\r\n')
|
||||||
|
}
|
||||||
|
|
||||||
|
// a) ** Image or PDF ** with Text Extractor
|
||||||
else if (extractor) {
|
else if (extractor) {
|
||||||
if (extractor.canFileBeExtracted(path)) {
|
if (extractor.canFileBeExtracted(path)) {
|
||||||
content = await extractor.extractText(file)
|
content = await extractor.extractText(file)
|
||||||
@@ -37,7 +68,7 @@ async function getIndexedDocument(path: string): Promise<IndexedDocument> {
|
|||||||
throw new Error('Invalid file format: ' + file.path)
|
throw new Error('Invalid file format: ' + file.path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Image or PDF, without the text-extractor plugin
|
// b) ** Image or PDF ** without the text-extractor plugin
|
||||||
else {
|
else {
|
||||||
if (isFilePDF(path)) {
|
if (isFilePDF(path)) {
|
||||||
content = await getPdfText(file)
|
content = await getPdfText(file)
|
||||||
@@ -106,7 +137,7 @@ class CacheManager {
|
|||||||
|
|
||||||
public async addToLiveCache(path: string): Promise<void> {
|
public async addToLiveCache(path: string): Promise<void> {
|
||||||
try {
|
try {
|
||||||
const doc = await getIndexedDocument(path)
|
const doc = await getAndMapIndexedDocument(path)
|
||||||
this.documents.set(path, doc)
|
this.documents.set(path, doc)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.warn('Omnisearch: Error while adding to live cache', e)
|
console.warn('Omnisearch: Error while adding to live cache', e)
|
||||||
|
|||||||
@@ -7,9 +7,9 @@ import {
|
|||||||
} from 'obsidian'
|
} from 'obsidian'
|
||||||
import type { SearchMatch } from '../globals'
|
import type { SearchMatch } from '../globals'
|
||||||
import {
|
import {
|
||||||
getChsSegmenter,
|
|
||||||
excerptAfter,
|
excerptAfter,
|
||||||
excerptBefore,
|
excerptBefore,
|
||||||
|
getChsSegmenter,
|
||||||
highlightClass,
|
highlightClass,
|
||||||
isSearchMatch,
|
isSearchMatch,
|
||||||
regexLineSplit,
|
regexLineSplit,
|
||||||
@@ -215,6 +215,7 @@ export function getCtrlKeyLabel(): 'ctrl' | '⌘' {
|
|||||||
export function isFileIndexable(path: string): boolean {
|
export function isFileIndexable(path: string): boolean {
|
||||||
return (
|
return (
|
||||||
isFilePlaintext(path) ||
|
isFilePlaintext(path) ||
|
||||||
|
isFileCanvas(path) ||
|
||||||
(!Platform.isMobileApp && settings.PDFIndexing && isFilePDF(path)) ||
|
(!Platform.isMobileApp && settings.PDFIndexing && isFilePDF(path)) ||
|
||||||
(!Platform.isMobileApp && settings.imagesIndexing && isFileImage(path))
|
(!Platform.isMobileApp && settings.imagesIndexing && isFileImage(path))
|
||||||
)
|
)
|
||||||
@@ -231,11 +232,11 @@ export function isFilePDF(path: string): boolean {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function isFilePlaintext(path: string): boolean {
|
export function isFilePlaintext(path: string): boolean {
|
||||||
return getPlaintextExtensions().some(t => path.endsWith(`.${t}`))
|
return [...settings.indexedFileTypes, 'md'].some(t => path.endsWith(`.${t}`))
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getPlaintextExtensions(): string[] {
|
export function isFileCanvas(path: string): boolean {
|
||||||
return [...settings.indexedFileTypes, 'md']
|
return path.endsWith('.canvas')
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getExtension(path: string): string {
|
export function getExtension(path: string): string {
|
||||||
|
|||||||
Reference in New Issue
Block a user