From b1bad4392e5ecb0a99e999cd1343385d7eee9ca8 Mon Sep 17 00:00:00 2001 From: Mara Date: Fri, 5 Aug 2022 23:23:17 +0000 Subject: [PATCH] feat: Add using frontmatter title field for generate filename --- package-lock.json | 4 ++-- plugin/i18n/locales/en-us.ts | 2 ++ plugin/i18n/locales/fr-fr.ts | 3 ++- plugin/settings.ts | 12 +++++++++++- plugin/settings/interface.ts | 2 ++ plugin/src/filePathConvertor.ts | 16 ++++++++++++---- 6 files changed, 31 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index 4038a04f..746e721e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "obsidian-mkdocs-publication", - "version": "3.14.0-2", + "version": "3.14.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "obsidian-mkdocs-publication", - "version": "3.14.0-2", + "version": "3.14.0", "license": "AGPL-3.0", "dependencies": { "@octokit/core": "^3.5.1", diff --git a/plugin/i18n/locales/en-us.ts b/plugin/i18n/locales/en-us.ts index a3447679..7b933788 100644 --- a/plugin/i18n/locales/en-us.ts +++ b/plugin/i18n/locales/en-us.ts @@ -59,6 +59,8 @@ export default { hardBreakDesc: "Add a markdown hard line break (double whitespace) after each line.", headerDataview: "Dataview", headerDataviewDesc: "Convert dataview to markdown.", + useFrontmatterTitle: "Use frontmatter title", + useFrontmatterTitleDesc: "Use frontmatter \"title\" field instead of the file name.", // --- // # Embed # // diff --git a/plugin/i18n/locales/fr-fr.ts b/plugin/i18n/locales/fr-fr.ts index fff8188b..4a8128ef 100644 --- a/plugin/i18n/locales/fr-fr.ts +++ b/plugin/i18n/locales/fr-fr.ts @@ -61,7 +61,8 @@ export default { hardBreakDesc: "Ajoutez un retour à la ligne Markdown (double espace) après chaque ligne.", headerDataview: "Dataview", headerDataviewDesc: "Convertir dataview en markdown.", - + useFrontmatterTitle: "Utiliser la clé frontmatter \"title\"", + useFrontmatterTitleDesc: "Utilisez le champ \"title\" du frontmatter à la place du nom du fichier.", // --- // # Embed # // diff --git a/plugin/settings.ts b/plugin/settings.ts index 14dc2551..d131b1e9 100644 --- a/plugin/settings.ts +++ b/plugin/settings.ts @@ -161,7 +161,17 @@ export class MkdocsSettingsTab extends PluginSettingTab { await this.plugin.saveSettings(); }); }); - + new Setting(this.containerEl) + .setName(t('useFrontmatterTitle') as string) + .setDesc(t('useFrontmatterTitleDesc') as string) + .addToggle((toggle) => { + toggle + .setValue(this.plugin.settings.useFrontmatterTitle) + .onChange(async (value) => { + this.plugin.settings.useFrontmatterTitle = value; + await this.plugin.saveSettings(); + }); + }); /* ------------------------------ * * Text conversion * * ------------------------------ */ diff --git a/plugin/settings/interface.ts b/plugin/settings/interface.ts index a7dd4c33..44ceb034 100644 --- a/plugin/settings/interface.ts +++ b/plugin/settings/interface.ts @@ -26,6 +26,7 @@ export interface MkdocsPublicationSettings { hardBreak: boolean; logNotice: boolean; convertDataview: boolean; + useFrontmatterTitle: boolean; } export enum folderSettings { @@ -65,4 +66,5 @@ export const DEFAULT_SETTINGS: MkdocsPublicationSettings = { hardBreak: false, logNotice: false, convertDataview: true, + useFrontmatterTitle: false, } diff --git a/plugin/src/filePathConvertor.ts b/plugin/src/filePathConvertor.ts index 91c4973b..06f92e7a 100644 --- a/plugin/src/filePathConvertor.ts +++ b/plugin/src/filePathConvertor.ts @@ -87,15 +87,23 @@ function folderNoteIndex( function createObsidianPath( file: TFile, settings:MkdocsPublicationSettings, - vault: Vault) { + vault: Vault, + metadataCache: MetadataCache) { /** * Create link path based on settings and file path * @param file : TFile - Image TFile * @param settings : MkdocsPublicationSettings - Settings * @returns string - Link path */ + const frontmatter = metadataCache.getCache(file.path).frontmatter + if (!frontmatter || !frontmatter[settings.shareKey]) { + return file.name + } const folderDefault = settings.folderDefaultName; - const fileName = folderNoteIndex(file, vault, settings); + let fileName = folderNoteIndex(file, vault, settings); + if (fileName === file.name && settings.useFrontmatterTitle && frontmatter['title']) { + fileName = frontmatter['title']; + } const rootFolder = folderDefault.length > 0 ? folderDefault + "/" : '' const path = rootFolder + file.path.replace(file.name, fileName); if (settings.subFolder.length > 0) { @@ -135,9 +143,9 @@ function getReceiptFolder( let path = settings.folderDefaultName.length > 0 ? settings.folderDefaultName + "/" + file.name : file.name; if (settings.downloadedFolder === folderSettings.yaml) { - path = createFrontmatterPath(file, settings, metadataCache) + path = createFrontmatterPath(file, settings, metadataCache); } else if (settings.downloadedFolder === folderSettings.obsidian) { - path = createObsidianPath(file, settings, vault) + path = createObsidianPath(file, settings, vault, metadataCache); } return path }