From 6be174eeb9372604549fe82877face6227fecd79 Mon Sep 17 00:00:00 2001 From: Simon Cambier Date: Wed, 9 Nov 2022 22:01:02 +0100 Subject: [PATCH] #109 - opt-out to keep line returns in excerpts And removed the "show notice" setting --- src/cache-manager.ts | 8 +- src/components/ModalVault.svelte | 2 +- src/globals.ts | 2 +- src/notes-index.ts | 4 +- src/settings.ts | 170 ++++++++++++++++--------------- src/tools/utils.ts | 29 +++++- 6 files changed, 121 insertions(+), 94 deletions(-) diff --git a/src/cache-manager.ts b/src/cache-manager.ts index 0de4642..b287d68 100644 --- a/src/cache-manager.ts +++ b/src/cache-manager.ts @@ -103,11 +103,9 @@ class CacheManager { try { return MiniSearch.loadJS(cachedIndex.data, minisearchOptions) } catch (e) { - if (settings.showIndexingNotices) { - new Notice( - 'Omnisearch - Cache missing or invalid. Some freezes may occur while Omnisearch indexes your vault.' - ) - } + new Notice( + 'Omnisearch - Cache missing or invalid. Some freezes may occur while Omnisearch indexes your vault.' + ) console.error('Omnisearch - Error while loading Minisearch cache') console.error(e) return null diff --git a/src/components/ModalVault.svelte b/src/components/ModalVault.svelte index 8482be3..32e660f 100644 --- a/src/components/ModalVault.svelte +++ b/src/components/ModalVault.svelte @@ -296,7 +296,7 @@
ctrl+h - to toggle excerpt + to toggle excerpts
escto close diff --git a/src/globals.ts b/src/globals.ts index dc457da..8bee954 100644 --- a/src/globals.ts +++ b/src/globals.ts @@ -6,7 +6,7 @@ export const regexStripQuotes = /^"|"$|^'|'$/g export const chsRegex = /[\u4e00-\u9fa5]/ export const excerptBefore = 100 -export const excerptAfter = 180 +export const excerptAfter = 300 export const highlightClass = 'suggestion-highlight omnisearch-highlight' diff --git a/src/notes-index.ts b/src/notes-index.ts index b99a1a1..f4e3e8d 100644 --- a/src/notes-index.ts +++ b/src/notes-index.ts @@ -107,9 +107,7 @@ export function markNoteForReindex(note: TAbstractFile): void { export async function refreshIndex(): Promise { if (notesToReindex.size > 0) { - if (settings.showIndexingNotices) { - new Notice(`Omnisearch - Reindexing ${notesToReindex.size} notes`, 2000) - } + console.info(`Omnisearch - Reindexing ${notesToReindex.size} notes`) for (const note of notesToReindex) { removeFromIndex(note.path) await addToIndexAndMemCache(note) diff --git a/src/settings.ts b/src/settings.ts index fc441f6..9360e68 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -27,14 +27,14 @@ export interface OmnisearchSettings extends WeightingSettings { PDFIndexing: boolean /** Enable PDF indexing */ imagesIndexing: boolean - /** Display Omnisearch popup notices over Obsidian */ - showIndexingNotices: boolean /** Activate the small 🔍 button on Obsidian's ribbon */ ribbonIcon: boolean /** Display short filenames in search results, instead of the full path */ showShortName: boolean /** Display the small contextual excerpt in search results */ showExcerpt: boolean + /** Render line returns with
in excerpts */ + renderLineReturnInExcerpts: boolean /** Enable a "create note" button in the Vault Search modal */ showCreateButton: boolean /** Re-execute the last query when opening Omnisearch */ @@ -77,6 +77,74 @@ export class SettingsTab extends PluginSettingTab { Buy Me a Coffee at ko-fi.com ` + //#region Indexing + + new Setting(containerEl).setName('Indexing').setHeading() + + // PDF Indexing + const indexPDFsDesc = new DocumentFragment() + indexPDFsDesc.createSpan({}, span => { + span.innerHTML = `Omnisearch will include PDFs in search results. +
    +
  • ⚠️ Each PDF can take anywhere from a few seconds to 2 minutes to be processed.
  • +
  • ⚠️ Texts extracted from PDFs may contain errors such as missing spaces, or spaces in the middle of words.
  • +
  • ⚠️ Some PDFs can't be processed correctly and will return an empty text.
  • +
  • This feature is currently a work-in-progress, please report issues that you might experience.
  • +
+ Needs a restart to fully take effect.` + }) + new Setting(containerEl) + .setName('BETA - PDF Indexing') + .setDesc(indexPDFsDesc) + .addToggle(toggle => + toggle.setValue(settings.PDFIndexing).onChange(async v => { + settings.PDFIndexing = v + await saveSettings(this.plugin) + }) + ) + + // Images Indexing + const indexImagesDesc = new DocumentFragment() + indexImagesDesc.createSpan({}, span => { + span.innerHTML = `Omnisearch will use Tesseract to index images from their text. +
    +
  • Only English is supported at the moment.
  • +
  • Not all images can be correctly read by the OCR, this feature works best with scanned documents.
  • +
+ Needs a restart to fully take effect.` + }) + new Setting(containerEl) + .setName('BETA - Images Indexing') + .setDesc(indexImagesDesc) + .addToggle(toggle => + toggle.setValue(settings.imagesIndexing).onChange(async v => { + settings.imagesIndexing = v + await saveSettings(this.plugin) + }) + ) + + // Additional files to index + const indexedFileTypesDesc = new DocumentFragment() + indexedFileTypesDesc.createSpan({}, span => { + span.innerHTML = `In addition to standard md files, Omnisearch can also index other plain text files.
+ Add extensions separated by a space, without the dot. Example: "txt org".
+ Needs a restart to fully take effect.` + }) + new Setting(containerEl) + .setName('Additional files to index') + .setDesc(indexedFileTypesDesc) + .addText(component => { + component + .setValue(settings.indexedFileTypes.join(' ')) + .setPlaceholder('Example: txt org') + .onChange(async v => { + settings.indexedFileTypes = v.split(' ') + await saveSettings(this.plugin) + }) + }) + + //#endregion Indexing + // #region Behavior new Setting(containerEl).setName('Behavior').setHeading() @@ -112,27 +180,7 @@ export class SettingsTab extends PluginSettingTab { }) ) - // Additional files to index - const indexedFileTypesDesc = new DocumentFragment() - indexedFileTypesDesc.createSpan({}, span => { - span.innerHTML = `In addition to standard md files, Omnisearch can also index other plain text files.
- Add extensions separated by a space, without the dot. Example: "txt org".
- Needs a restart to fully take effect.` - }) - new Setting(containerEl) - .setName('Additional files to index') - .setDesc(indexedFileTypesDesc) - .addText(component => { - component - .setValue(settings.indexedFileTypes.join(' ')) - .setPlaceholder('Example: txt org') - .onChange(async v => { - settings.indexedFileTypes = v.split(' ') - await saveSettings(this.plugin) - }) - }) - - // Ignore diacritics + // Simpler search new Setting(containerEl) .setName('Simpler search') .setDesc( @@ -146,48 +194,6 @@ export class SettingsTab extends PluginSettingTab { }) ) - // PDF Indexing - const indexPDFsDesc = new DocumentFragment() - indexPDFsDesc.createSpan({}, span => { - span.innerHTML = `Omnisearch will include PDFs in search results. -
    -
  • ⚠️ Each PDF can take anywhere from a few seconds to 2 minutes to be processed.
  • -
  • ⚠️ Texts extracted from PDFs may contain errors such as missing spaces, or spaces in the middle of words.
  • -
  • ⚠️ Some PDFs can't be processed correctly and will return an empty text.
  • -
  • This feature is currently a work-in-progress, please report issues that you might experience.
  • -
- Needs a restart to fully take effect.` - }) - new Setting(containerEl) - .setName('BETA - PDF Indexing') - .setDesc(indexPDFsDesc) - .addToggle(toggle => - toggle.setValue(settings.PDFIndexing).onChange(async v => { - settings.PDFIndexing = v - await saveSettings(this.plugin) - }) - ) - - // PDF Indexing - const indexImagesDesc = new DocumentFragment() - indexImagesDesc.createSpan({}, span => { - span.innerHTML = `Omnisearch will use Tesseract to index images from their text. -
    -
  • Only English is supported at the moment.
  • -
  • Not all images can be correctly read by the OCR, this feature works best with scanned documents.
  • -
- Needs a restart to fully take effect.` - }) - new Setting(containerEl) - .setName('BETA - Images Indexing') - .setDesc(indexImagesDesc) - .addToggle(toggle => - toggle.setValue(settings.imagesIndexing).onChange(async v => { - settings.imagesIndexing = v - await saveSettings(this.plugin) - }) - ) - // #endregion Behavior // #region User Interface @@ -212,9 +218,9 @@ export class SettingsTab extends PluginSettingTab { // Show context excerpt new Setting(containerEl) - .setName('Show excerpt') + .setName('Show excerpts') .setDesc( - 'Shows the part of the note that matches the search. Disable this to only show filenames in results.' + 'Shows the contextual part of the note that matches the search. Disable this to only show filenames in results.' ) .addToggle(toggle => toggle.setValue(settings.showExcerpt).onChange(async v => { @@ -222,10 +228,23 @@ export class SettingsTab extends PluginSettingTab { }) ) - // Show context excerpt + // Keep line returns in excerpts + new Setting(containerEl) + .setName('Render line return in excerpts') + .setDesc('Activate this option render line returns in result excerpts.') + .addToggle(toggle => + toggle + .setValue(settings.renderLineReturnInExcerpts) + .onChange(async v => { + settings.renderLineReturnInExcerpts = v + await saveSettings(this.plugin) + }) + ) + + // Show previous query results new Setting(containerEl) .setName('Show previous query results') - .setDesc('Re-executes the previous query when opening Omnisearch') + .setDesc('Re-executes the previous query when opening Omnisearch.') .addToggle(toggle => toggle.setValue(settings.showPreviousQueryResults).onChange(async v => { settings.showPreviousQueryResults = v @@ -249,17 +268,6 @@ export class SettingsTab extends PluginSettingTab { }) ) - // Show notices - new Setting(containerEl) - .setName('Show indexing notices') - .setDesc('Shows a notice when indexing is done, usually at startup.') - .addToggle(toggle => - toggle.setValue(settings.showIndexingNotices).onChange(async v => { - settings.showIndexingNotices = v - await saveSettings(this.plugin) - }) - ) - // Display note names without the full path new Setting(containerEl) .setName('Hide full path in results list') @@ -341,10 +349,10 @@ export const DEFAULT_SETTINGS: OmnisearchSettings = { PDFIndexing: false, imagesIndexing: false, - showIndexingNotices: false, showShortName: false, ribbonIcon: true, showExcerpt: true, + renderLineReturnInExcerpts: true, showCreateButton: false, showPreviousQueryResults: true, simpleSearch: false, diff --git a/src/tools/utils.ts b/src/tools/utils.ts index 602b640..1339d3e 100644 --- a/src/tools/utils.ts +++ b/src/tools/utils.ts @@ -92,15 +92,38 @@ export function loopIndex(index: number, nbItems: number): number { export function makeExcerpt(content: string, offset: number): string { try { const pos = offset ?? -1 + const from = Math.max(0, pos - excerptBefore) + const to = Math.min(content.length, pos + excerptAfter) if (pos > -1) { - const from = Math.max(0, pos - excerptBefore) - const to = Math.min(content.length, pos + excerptAfter) content = (from > 0 ? '…' : '') + content.slice(from, to).trim() + (to < content.length - 1 ? '…' : '') + } else { + content = content.slice(0, excerptAfter) } - return escapeHTML(content) + if (settings.renderLineReturnInExcerpts) { + const lineReturn = new RegExp(/(?:\r\n|\r|\n)/g) + // Remove multiple line returns + content = content + .split(lineReturn) + .filter(l => l) + .join('\n') + + const last = content.lastIndexOf('\n', pos - from) + + if (last > 0) { + content = content.slice(last) + } + } + + content = escapeHTML(content) + + if (settings.renderLineReturnInExcerpts) { + content = content.trim().replaceAll('\n', '
') + } + + return content } catch (e) { new Notice( 'Omnisearch - Error while creating excerpt, see developer console'