Skip to content

Commit

Permalink
feat: Add using frontmatter title field for generate filename
Browse files Browse the repository at this point in the history
  • Loading branch information
Mara-Li committed Aug 5, 2022
1 parent 006a072 commit b1bad43
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 8 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions plugin/i18n/locales/en-us.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 # //
Expand Down
3 changes: 2 additions & 1 deletion plugin/i18n/locales/fr-fr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 # //

Expand Down
12 changes: 11 additions & 1 deletion plugin/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
* ------------------------------ */
Expand Down
2 changes: 2 additions & 0 deletions plugin/settings/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface MkdocsPublicationSettings {
hardBreak: boolean;
logNotice: boolean;
convertDataview: boolean;
useFrontmatterTitle: boolean;
}

export enum folderSettings {
Expand Down Expand Up @@ -65,4 +66,5 @@ export const DEFAULT_SETTINGS: MkdocsPublicationSettings = {
hardBreak: false,
logNotice: false,
convertDataview: true,
useFrontmatterTitle: false,
}
16 changes: 12 additions & 4 deletions plugin/src/filePathConvertor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit b1bad43

Please sign in to comment.