diff --git a/src/i18n/locales/en.json b/src/i18n/locales/en.json index 4c8f25d..cf1e7bf 100644 --- a/src/i18n/locales/en.json +++ b/src/i18n/locales/en.json @@ -1,10 +1,16 @@ { + "command": { + "active": "Add thesaurus tags to active file" + }, "error": { "csv": { "columns": "The columns doesn't correspond with the settings.", "header": "You can have only two column in your thesaurus. You have: {{- len}} columns.", "malformed": "Your CSV is malformed: {{- len}} columns found.", "separator": "The specified separator is invalid. You seems to use \"{{- sep}}\" as separator." + }, + "file": { + "empty": "The file {{- file}} is empty!" } }, "settings": { @@ -19,11 +25,11 @@ }, "title": "Column title" }, - "excludedPath": { - "desc": "Exclude files in theses paths when using the global command. ", + "includedPaths": { + "desc": "Includes files from its paths when using the global command.", "regex": "Regex are supported.", "separate": "Separate paths with coma, semicolon, spaces or newlines.", - "title": "Excluded path" + "title": "Included paths" }, "separator": { "desc": "CSV column separator.", @@ -38,5 +44,9 @@ "title": "Thesaurus", "verify": "Verify validity of the file" } + }, + "success": { + "none": "No tag to add!", + "title": "The tags has been added:" } } \ No newline at end of file diff --git a/src/i18n/locales/fr.json b/src/i18n/locales/fr.json index d1c909d..0663e69 100644 --- a/src/i18n/locales/fr.json +++ b/src/i18n/locales/fr.json @@ -1,10 +1,16 @@ { + "command": { + "active": "Ajouter les tags du thésaurus au fichier actif" + }, "error": { "csv": { "columns": "Les colonnes du CSV fourni ne correspondent pas avec les colonnes des paramètres.", "header": "Vous ne pouvez avoir que deux colonnes dans votre thésaurus. Vous avez {{- len}} colonnes.", "malformed": "Votre CSV est mal formé : {{- len}} colonnes trouvées.", "separator": "Le séparateur spécifié n'est pas le bon. Vous semblez utiliser \"{{- sep}}\" comme séparateur." + }, + "file": { + "empty": "Le fichier {{- file}} est vide !" } }, "settings": { @@ -19,11 +25,11 @@ }, "title": "Titre des colonnes" }, - "excludedPath": { - "desc": "Exclus les fichiers depuis ses chemins lors d'utilisation de la commande globale.", + "includedPaths": { + "desc": "Inclus les fichiers depuis ses chemins lors d'utilisation de la commande globale.", "regex": "Les expressions régulières sont supportées.", - "separate": "Séparer les chemins par une virgule, point virgule, un espace ou un retour à la ligne.", - "title": "Fichier exclus" + "separate": "Séparer les chemins par une virgule, point virgule ou un retour à la ligne.", + "title": "Chemins inclus" }, "separator": { "desc": "Séparateur des colonnes du fichier CSV.", @@ -38,5 +44,9 @@ "title": "Thesaurus", "verify": "Verifier la validité du fichier" } + }, + "success": { + "none": "Aucun tag à ajouter !", + "title": "Les tags suivants ont bien été ajoutés :" } } \ No newline at end of file diff --git a/src/interfaces.ts b/src/interfaces.ts index ff59884..1324aef 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -2,7 +2,7 @@ import type { TFunction } from "i18next"; export interface MyThesaurusSettings { thesaurusPath: string; - excludedPath: string[]; + includedPaths: string[]; separator: Separator; columns: ColumnName; } @@ -19,7 +19,7 @@ export type Translation = TFunction<"translation", undefined>; export const DEFAULT_SETTINGS: MyThesaurusSettings = { thesaurusPath: "", - excludedPath: [], + includedPaths: [], separator: ";", columns: { term: "term", diff --git a/src/main.ts b/src/main.ts index 29fa9c5..098871f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,14 +1,55 @@ import i18next from "i18next"; -import { Plugin } from "obsidian"; +import { Notice, Plugin, type TFile, sanitizeHTMLToDom } from "obsidian"; import { resources, translationLanguage } from "./i18n"; import { DEFAULT_SETTINGS, type MyThesaurusSettings } from "./interfaces"; import { MyThesaurusSettingTab } from "./settings"; import "uniformize"; +import { getTags, getThesaurus } from "./utils"; export default class MyThesaurus extends Plugin { settings!: MyThesaurusSettings; + async addTagsToNote(tags: string[], file: TFile) { + const currentTags = this.app.metadataCache.getFileCache(file)?.tags || []; + const newTags = [...new Set([...currentTags, ...tags])]; + if (newTags.length === 0) return; + await this.app.fileManager.processFrontMatter(file, (frontmatter) => { + frontmatter.tags = newTags; + }); + } + + async parseFile(file: TFile) { + const contents = await this.app.vault.read(file); + const thesaurusFile = this.app.vault.getAbstractFileByPath( + this.settings.thesaurusPath + ) as TFile; + const thesaurusContents = await this.app.vault.read(thesaurusFile); + if (contents.length === 0) { + new Notice(i18next.t("error.file.empty", { file: file.name })); + } + try { + const thesaurus = getThesaurus( + thesaurusContents, + this.settings.separator, + i18next.t, + this.settings.columns + ); + const tags = getTags(contents, thesaurus); + if (tags.length > 0) { + await this.addTagsToNote(tags, file); + const successMsg = sanitizeHTMLToDom( + `
${i18next.t("success.title")}