From f0be2759bd76e9a1b5da4cf617860d20725f1120 Mon Sep 17 00:00:00 2001 From: Steve Layton Date: Sat, 6 Jul 2024 23:04:54 -0700 Subject: [PATCH] adding two new options, custom filename and filename in output dir (#86) * adding two new options, custom filename and filename in output dir --- src/config.ts | 4 ++++ src/main.ts | 35 ++++++++++++++++++++++++++++++++++- src/utils.ts | 33 +++++++++++++++++++++++++++++---- 3 files changed, 67 insertions(+), 5 deletions(-) diff --git a/src/config.ts b/src/config.ts index 2aea5d3..75583cf 100644 --- a/src/config.ts +++ b/src/config.ts @@ -16,6 +16,8 @@ export interface MarkdownExportPluginSettings { GFM: boolean; fileNameEncode: boolean; removeOutgoingLinkBrackets: boolean; + includeFileName: boolean; + customFileName: string; } export const DEFAULT_SETTINGS: MarkdownExportPluginSettings = { @@ -25,4 +27,6 @@ export const DEFAULT_SETTINGS: MarkdownExportPluginSettings = { GFM: true, fileNameEncode: true, removeOutgoingLinkBrackets: false, + includeFileName: false, + customFileName: "", }; diff --git a/src/main.ts b/src/main.ts index 15f2f9f..1b16a04 100644 --- a/src/main.ts +++ b/src/main.ts @@ -72,10 +72,16 @@ export default class MarkdownExportPlugin extends Plugin { file: TAbstractFile, outputFormat: string, ) { + + let dir = "" + if (this.settings.includeFileName == true) { + dir = file.name.replace(".md", "") + } + // try create attachment directory await tryCreateFolder( this, - path.join(this.settings.output, this.settings.attachment), + path.join(this.settings.output, dir, this.settings.attachment), ); // run @@ -84,6 +90,7 @@ export default class MarkdownExportPlugin extends Plugin { new Notice( `Exporting ${file.path} to ${path.join( this.settings.output, + dir, file.name, )}`, ); @@ -200,5 +207,31 @@ class MarkdownExportSettingTab extends PluginSettingTab { await this.plugin.saveSettings(); }), ); + + new Setting(containerEl) + .setName("Include filename in output path") + .setDesc( + "false default, if you want to include the filename (without extension) in the output path set this to true", + ) + .addToggle((toggle) => + toggle + .setValue(this.plugin.settings.includeFileName) + .onChange(async (value: boolean) => { + this.plugin.settings.includeFileName = value; + await this.plugin.saveSettings(); + }), + ); + new Setting(containerEl) + .setName("Custom filename") + .setDesc("update if you want a custom filename, leave off extension") + .addText((text) => + text + .setPlaceholder("Enter custom filename") + .setValue(this.plugin.settings.customFileName) + .onChange(async (value) => { + this.plugin.settings.customFileName = value; + await this.plugin.saveSettings(); + }), + ); } } diff --git a/src/utils.ts b/src/utils.ts index 532284e..8895af2 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -197,6 +197,7 @@ export async function tryCreate( export async function tryCopyImage( plugin: MarkdownExportPlugin, + filename: string, contentPath: string, ) { try { @@ -237,9 +238,15 @@ export async function tryCopyImage( continue; } + let dir = "" + if (plugin.settings.includeFileName == true) { + dir = filename.replace(".md", "") + } + const targetPath = path .join( plugin.settings.output, + dir, plugin.settings.attachment, imageLinkMd5.concat(imageExt), ) @@ -418,16 +425,28 @@ export async function tryCopyMarkdownByRead( } } - await tryCopyImage(plugin, file.path); + let dir = "" + if (plugin.settings.includeFileName == true) { + dir = file.name.replace(".md", "") + } + + await tryCopyImage(plugin, file.name, file.path); + + const outDir = path.join(plugin.settings.output, dir, outputSubPath); - const outDir = path.join(plugin.settings.output, outputSubPath); await tryCreateFolder(plugin, outDir); switch (outputFormat) { case "HTML": { + let filename + if (plugin.settings.customFileName) { + filename = plugin.settings.customFileName + ".md" + } else { + filename = file.name + } const targetFile = path.join( outDir, - file.name.replace(".md", ".html"), + filename.replace(".md", ".html"), ); const { html } = await markdownToHTML( plugin, @@ -438,7 +457,13 @@ export async function tryCopyMarkdownByRead( break; } case "markdown": { - const targetFile = path.join(outDir, file.name); + let filename + if (plugin.settings.customFileName) { + filename = plugin.settings.customFileName + ".md" + } else { + filename = file.name + } + const targetFile = path.join(outDir, filename); await tryCreate(plugin, targetFile, content); break; }