Skip to content

Commit

Permalink
adding two new options, custom filename and filename in output dir (#86)
Browse files Browse the repository at this point in the history
* adding two new options, custom filename and filename in output dir
  • Loading branch information
shindakun authored Jul 7, 2024
1 parent e8e4fd6 commit f0be275
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export interface MarkdownExportPluginSettings {
GFM: boolean;
fileNameEncode: boolean;
removeOutgoingLinkBrackets: boolean;
includeFileName: boolean;
customFileName: string;
}

export const DEFAULT_SETTINGS: MarkdownExportPluginSettings = {
Expand All @@ -25,4 +27,6 @@ export const DEFAULT_SETTINGS: MarkdownExportPluginSettings = {
GFM: true,
fileNameEncode: true,
removeOutgoingLinkBrackets: false,
includeFileName: false,
customFileName: "",
};
35 changes: 34 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -84,6 +90,7 @@ export default class MarkdownExportPlugin extends Plugin {
new Notice(
`Exporting ${file.path} to ${path.join(
this.settings.output,
dir,
file.name,
)}`,
);
Expand Down Expand Up @@ -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();
}),
);
}
}
33 changes: 29 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ export async function tryCreate(

export async function tryCopyImage(
plugin: MarkdownExportPlugin,
filename: string,
contentPath: string,
) {
try {
Expand Down Expand Up @@ -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),
)
Expand Down Expand Up @@ -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,
Expand All @@ -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;
}
Expand Down

0 comments on commit f0be275

Please sign in to comment.