Skip to content

Commit

Permalink
Added settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Developer-Mike committed Jan 10, 2024
1 parent fadf384 commit 8824656
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/file-preview.ts → src/file-previews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const FILETYPE_MAP: {[key: string]: typeof ConvertableFileView} = {
export function registerFilePreviews(plugin: Plugin) {
for (const [filetype, view] of Object.entries(FILETYPE_MAP)) {
// @ts-ignore
plugin.registerView(view.VIEW_TYPE, (leaf) => new view(leaf, this));
plugin.registerView(view.VIEW_TYPE, (leaf) => new view(leaf, plugin));

// @ts-ignore
plugin.registerExtensions([filetype], view.VIEW_TYPE);
Expand Down
18 changes: 15 additions & 3 deletions src/filetypes/convertable-file-view.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { Notice, TFile, TextFileView, WorkspaceLeaf } from "obsidian";
import * as path from "path";
import DocxerPlugin from "src/main";

export default abstract class ConvertableFileView extends TextFileView {
plugin: DocxerPlugin;
fileContent: string;
header: HTMLElement|null = null;
content: HTMLElement|null = null;

constructor(leaf: WorkspaceLeaf) {
constructor(leaf: WorkspaceLeaf, plugin: DocxerPlugin) {
super(leaf);
this.plugin = plugin;
}

getDisplayText(): string {
Expand Down Expand Up @@ -70,14 +73,23 @@ export default abstract class ConvertableFileView extends TextFileView {
if (!this.file) return;

const targetFilepath = path.join(path.dirname(this.file.path), path.basename(this.file.path, path.extname(this.file.path)) + ".md");
const attachmentsDirectory = path.dirname(targetFilepath);
const attachmentsDirectory = {
"vault": "",
"custom": this.plugin.settings.customAttachmentsFolder,
"same": path.dirname(targetFilepath),
"subfolder": path.join(path.dirname(targetFilepath), this.plugin.settings.customAttachmentsFolder)
}[this.plugin.settings.attachmentsFolder];

const markdown = await this.toMarkdown(attachmentsDirectory);
if (!markdown) {
new Notice("Error converting file to markdown.");
return;
}

this.app.vault.create(targetFilepath, markdown);
const convertedFile = await this.app.vault.create(targetFilepath, markdown);
this.leaf.openFile(convertedFile);

if (this.plugin.settings.deleteFileAfterConversion)
this.app.vault.delete(this.file);
}
}
5 changes: 3 additions & 2 deletions src/filetypes/docx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as mammoth from "mammoth";
import { NodeHtmlMarkdown } from 'node-html-markdown'
import { renderAsync } from 'docx-preview'
import * as path from "path";
import { toValidFilename } from "src/utils";
import { createMissingFolders, toObsidianPath, toValidFilename } from "src/utils";

export default class DocxFileView extends ConvertableFileView {
static readonly VIEW_TYPE = "docx-view";
Expand All @@ -30,8 +30,9 @@ export default class DocxFileView extends ConvertableFileView {
convertImage: mammoth.images.imgElement((image: any) => {
return image.read().then((imageBinary: any) => {
const filename = toValidFilename(image.altText) + "." + image.contentType.split("/")[1];
const filepath = path.join(attachmentsDirectory, filename);
const filepath = toObsidianPath(path.join(attachmentsDirectory, filename));

createMissingFolders(this.app, filepath);
this.app.vault.createBinary(filepath, imageBinary);
return { alt: image.altText, src: filepath };
});
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Plugin } from 'obsidian';
import { DocxerPluginSettings, DocxerSettingTab, DEFAULT_SETTINGS } from './settings';
import { registerFilePreviews } from './file-preview';
import { registerFilePreviews } from './file-previews';

export default class DocxerPlugin extends Plugin {
settings: DocxerPluginSettings;
Expand Down
47 changes: 41 additions & 6 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ import DocxerPlugin from "./main";
import { App, PluginSettingTab, Setting } from "obsidian";

export interface DocxerPluginSettings {
deleteDocxAfterConversion: boolean;
deleteFileAfterConversion: boolean;
attachmentsFolder: "vault" | "custom" | "same" | "subfolder";
customAttachmentsFolder: string;
}

export const DEFAULT_SETTINGS: Partial<DocxerPluginSettings> = {
deleteDocxAfterConversion: false,
deleteFileAfterConversion: false,
attachmentsFolder: "subfolder",
customAttachmentsFolder: "Attachments"
};

export class DocxerSettingTab extends PluginSettingTab {
Expand All @@ -22,13 +26,44 @@ export class DocxerSettingTab extends PluginSettingTab {
containerEl.empty();

new Setting(containerEl)
.setName("Delete .docx after conversion")
.setDesc("Delete .docx file after pressing the conversion button.")
.setName("Delete file after conversion")
.setDesc("Delete file after pressing the conversion button.")
.addToggle((toggle) =>
toggle
.setValue(this.plugin.settings.deleteDocxAfterConversion)
.setValue(this.plugin.settings.deleteFileAfterConversion)
.onChange(async (value) => {
this.plugin.settings.deleteDocxAfterConversion = value;
this.plugin.settings.deleteFileAfterConversion = value;
await this.plugin.saveSettings();
})
);

new Setting(containerEl)
.setName("Attachments Folder")
.setDesc("Specify the destination for attachments extracted during file conversion.")
.addDropdown((dropdown) =>
dropdown
.addOptions({
"vault": "Vault folder",
"custom": "In the folder specified below",
"same": "Same folder as current file",
"subfolder": "In subfolder under current folder"
})
.setValue(this.plugin.settings.attachmentsFolder)
.onChange(async (value) => {
this.plugin.settings.attachmentsFolder = value as any;
await this.plugin.saveSettings();
})
);

new Setting(containerEl)
.setName("Custom Attachments Folder")
.setDesc("Specify the name of the folder where attachments will be extracted.")
.addText((text) =>
text
.setPlaceholder("Attachments")
.setValue(this.plugin.settings.customAttachmentsFolder)
.onChange(async (value) => {
this.plugin.settings.customAttachmentsFolder = value;
await this.plugin.saveSettings();
})
);
Expand Down
20 changes: 20 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
import { App } from "obsidian";
import * as path from "path";

export function toValidFilename(filename: string): string {
let validFilename = filename.replace(/[^a-zA-Z0-9öüäÖÜÄ.\-]/g, "");
return validFilename;
}

export function toObsidianPath(path: string): string {
return path.replace(/\\/g, "/");
}

export function createMissingFolders(app: App, filepath: string) {
const folder = path.dirname(filepath);
const folders = folder.contains("\\") ? folder.split("\\") : folder.split("/");

let currentFolder = "";
for (const folder of folders) {
currentFolder = path.join(currentFolder, folder);

if (!app.vault.getAbstractFileByPath(currentFolder))
app.vault.createFolder(currentFolder);
}
}
1 change: 1 addition & 0 deletions styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
}

.docx-wrapper {
padding: 0 !important;
background-color: var(--background-primary) !important;
}

0 comments on commit 8824656

Please sign in to comment.