mirror of
https://github.com/khoj-ai/khoj.git
synced 2024-12-17 18:17:10 +00:00
Set File Types to Sync from Obsidian via Khoj Plugin Settings Page
Useful to limit file types to sync with Khoj. Avoids hitting indexed data limits, especially for users on the Khoj cloud free tier Closes #893
This commit is contained in:
parent
895f1c8e9e
commit
19efc83455
2 changed files with 67 additions and 4 deletions
|
@ -10,6 +10,11 @@ export interface UserInfo {
|
||||||
email?: string;
|
email?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface SyncFileTypes {
|
||||||
|
markdown: boolean;
|
||||||
|
images: boolean;
|
||||||
|
pdf: boolean;
|
||||||
|
}
|
||||||
export interface KhojSetting {
|
export interface KhojSetting {
|
||||||
resultsCount: number;
|
resultsCount: number;
|
||||||
khojUrl: string;
|
khojUrl: string;
|
||||||
|
@ -17,6 +22,7 @@ export interface KhojSetting {
|
||||||
connectedToBackend: boolean;
|
connectedToBackend: boolean;
|
||||||
autoConfigure: boolean;
|
autoConfigure: boolean;
|
||||||
lastSync: Map<TFile, number>;
|
lastSync: Map<TFile, number>;
|
||||||
|
syncFileType: SyncFileTypes;
|
||||||
userInfo: UserInfo | null;
|
userInfo: UserInfo | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,6 +33,11 @@ export const DEFAULT_SETTINGS: KhojSetting = {
|
||||||
connectedToBackend: false,
|
connectedToBackend: false,
|
||||||
autoConfigure: true,
|
autoConfigure: true,
|
||||||
lastSync: new Map(),
|
lastSync: new Map(),
|
||||||
|
syncFileType: {
|
||||||
|
markdown: true,
|
||||||
|
images: true,
|
||||||
|
pdf: true,
|
||||||
|
},
|
||||||
userInfo: null,
|
userInfo: null,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -96,6 +107,43 @@ export class KhojSettingTab extends PluginSettingTab {
|
||||||
this.plugin.settings.resultsCount = value;
|
this.plugin.settings.resultsCount = value;
|
||||||
await this.plugin.saveSettings();
|
await this.plugin.saveSettings();
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
// Add new "Sync" heading
|
||||||
|
containerEl.createEl('h3', {text: 'Sync'});
|
||||||
|
|
||||||
|
// Add setting to sync markdown notes
|
||||||
|
new Setting(containerEl)
|
||||||
|
.setName('Sync Notes')
|
||||||
|
.setDesc('Index Markdown files in your vault with Khoj.')
|
||||||
|
.addToggle(toggle => toggle
|
||||||
|
.setValue(this.plugin.settings.syncFileType.markdown)
|
||||||
|
.onChange(async (value) => {
|
||||||
|
this.plugin.settings.syncFileType.markdown = value;
|
||||||
|
await this.plugin.saveSettings();
|
||||||
|
}));
|
||||||
|
|
||||||
|
// Add setting to sync images
|
||||||
|
new Setting(containerEl)
|
||||||
|
.setName('Sync Images')
|
||||||
|
.setDesc('Index images in your vault with Khoj.')
|
||||||
|
.addToggle(toggle => toggle
|
||||||
|
.setValue(this.plugin.settings.syncFileType.images)
|
||||||
|
.onChange(async (value) => {
|
||||||
|
this.plugin.settings.syncFileType.images = value;
|
||||||
|
await this.plugin.saveSettings();
|
||||||
|
}));
|
||||||
|
|
||||||
|
// Add setting to sync PDFs
|
||||||
|
new Setting(containerEl)
|
||||||
|
.setName('Sync PDFs')
|
||||||
|
.setDesc('Index PDF files in your vault with Khoj.')
|
||||||
|
.addToggle(toggle => toggle
|
||||||
|
.setValue(this.plugin.settings.syncFileType.pdf)
|
||||||
|
.onChange(async (value) => {
|
||||||
|
this.plugin.settings.syncFileType.pdf = value;
|
||||||
|
await this.plugin.saveSettings();
|
||||||
|
}));
|
||||||
|
|
||||||
new Setting(containerEl)
|
new Setting(containerEl)
|
||||||
.setName('Auto Sync')
|
.setName('Auto Sync')
|
||||||
.setDesc('Automatically index your vault with Khoj.')
|
.setDesc('Automatically index your vault with Khoj.')
|
||||||
|
|
|
@ -48,14 +48,29 @@ function filenameToMimeType (filename: TFile): string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const supportedImageFilesTypes = ['png', 'jpg', 'jpeg'];
|
export const fileTypeToExtension = {
|
||||||
export const supportedBinaryFileTypes = ['pdf'].concat(supportedImageFilesTypes);
|
'pdf': ['pdf'],
|
||||||
export const supportedFileTypes = ['md', 'markdown'].concat(supportedBinaryFileTypes);
|
'image': ['png', 'jpg', 'jpeg'],
|
||||||
|
'markdown': ['md', 'markdown'],
|
||||||
|
};
|
||||||
|
export const supportedImageFilesTypes = fileTypeToExtension.image;
|
||||||
|
export const supportedBinaryFileTypes = fileTypeToExtension.pdf.concat(supportedImageFilesTypes);
|
||||||
|
export const supportedFileTypes = fileTypeToExtension.markdown.concat(supportedBinaryFileTypes);
|
||||||
|
|
||||||
export async function updateContentIndex(vault: Vault, setting: KhojSetting, lastSync: Map<TFile, number>, regenerate: boolean = false): Promise<Map<TFile, number>> {
|
export async function updateContentIndex(vault: Vault, setting: KhojSetting, lastSync: Map<TFile, number>, regenerate: boolean = false): Promise<Map<TFile, number>> {
|
||||||
// Get all markdown, pdf files in the vault
|
// Get all markdown, pdf files in the vault
|
||||||
console.log(`Khoj: Updating Khoj content index...`)
|
console.log(`Khoj: Updating Khoj content index...`)
|
||||||
const files = vault.getFiles().filter(file => supportedFileTypes.includes(file.extension));
|
const files = vault.getFiles()
|
||||||
|
// Filter supported file types for syncing
|
||||||
|
.filter(file => supportedFileTypes.includes(file.extension))
|
||||||
|
// Filter user configured file types for syncing
|
||||||
|
.filter(file => {
|
||||||
|
if (fileTypeToExtension.markdown.includes(file.extension)) return setting.syncFileType.markdown;
|
||||||
|
if (fileTypeToExtension.pdf.includes(file.extension)) return setting.syncFileType.pdf;
|
||||||
|
if (fileTypeToExtension.image.includes(file.extension)) return setting.syncFileType.images;
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
|
||||||
let countOfFilesToIndex = 0;
|
let countOfFilesToIndex = 0;
|
||||||
let countOfFilesToDelete = 0;
|
let countOfFilesToDelete = 0;
|
||||||
lastSync = lastSync.size > 0 ? lastSync : new Map<TFile, number>();
|
lastSync = lastSync.size > 0 ? lastSync : new Map<TFile, number>();
|
||||||
|
|
Loading…
Reference in a new issue