Skip to content

Commit d5bc9cc

Browse files
author
Ozan Tellioglu
committed
Conversion Under Certain Folder
1 parent 317f08a commit d5bc9cc

File tree

4 files changed

+99
-8
lines changed

4 files changed

+99
-8
lines changed

src/converter.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import LinkConverterPlugin from 'main';
2-
import { App, TFile, Notice, normalizePath } from 'obsidian';
2+
import { App, TFile, Notice, normalizePath, TFolder } from 'obsidian';
3+
import { getFilesUnderPath } from './utils';
34

45
/* -------------------- LINK DETECTOR -------------------- */
56

@@ -126,16 +127,16 @@ export const convertLinksInActiveFile = async (plugin: LinkConverterPlugin, fina
126127
}
127128
};
128129

129-
// --> Command Function: Converts All Links in All Files in Vault and Save in Corresponding Files
130-
export const convertLinksInVault = async (plugin: LinkConverterPlugin, finalFormat: 'markdown' | 'wiki') => {
131-
let mdFiles: TFile[] = plugin.app.vault.getMarkdownFiles();
132-
let notice = new Notice('Starting link conversion in your vault ', 0);
130+
// --> Convert Links under Files under a Certain Folder
131+
export const convertLinksUnderFolder = async (folder: TFolder, plugin: LinkConverterPlugin, finalFormat: 'markdown' | 'wiki') => {
132+
let mdFiles: TFile[] = getFilesUnderPath(folder.path, plugin);
133+
let notice = new Notice('Starting link conversion', 0);
133134
try {
134135
let totalCount = mdFiles.length;
135136
let counter = 0;
136137
for (let mdFile of mdFiles) {
137138
counter++;
138-
notice.setMessage(`Converting the links in your vault ${counter}/${totalCount}.`);
139+
notice.setMessage(`Converting the links in notes ${counter}/${totalCount}.`);
139140
// --> Skip Excalidraw and Kanban Files
140141
if (hasFrontmatter(plugin.app, mdFile.path, 'excalidraw-plugin') || hasFrontmatter(plugin.app, mdFile.path, 'kanban-plugin')) {
141142
continue;
@@ -149,6 +150,11 @@ export const convertLinksInVault = async (plugin: LinkConverterPlugin, finalForm
149150
}
150151
};
151152

153+
// --> Command Function: Converts All Links in All Files in Vault and Save in Corresponding Files
154+
export const convertLinksInVault = async (plugin: LinkConverterPlugin, finalFormat: 'markdown' | 'wiki') => {
155+
convertLinksUnderFolder(plugin.app.vault.getRoot(), plugin, finalFormat);
156+
};
157+
152158
const hasFrontmatter = (app: App, filePath: string, keyToCheck: string) => {
153159
let metaCache = app.metadataCache.getCache(filePath);
154160
return metaCache.frontmatter && metaCache.frontmatter[keyToCheck];

src/main.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Menu, Plugin, TFile, addIcon } from 'obsidian';
22
import { LinkConverterSettingsTab, LinkConverterPluginSettings, DEFAULT_SETTINGS } from './settings';
3-
import { ConfirmationModal } from 'modals';
3+
import { ConfirmationModal, FolderSuggestionModal } from 'modals';
44
import * as Converter from 'converter';
55
import * as Icons from './icons';
66

@@ -53,6 +53,24 @@ export default class LinkConverterPlugin extends Plugin {
5353
},
5454
});
5555

56+
this.addCommand({
57+
id: 'convert-wikis-to-mdlink-under-folder',
58+
name: 'Certain Folder: Links to Markdown',
59+
callback: () => {
60+
let fileMoveSuggester = new FolderSuggestionModal(this, 'markdown');
61+
fileMoveSuggester.open();
62+
},
63+
});
64+
65+
this.addCommand({
66+
id: 'convert-mdlinks-to-wikis-under-folder',
67+
name: 'Certain Folder: Links to Wiki',
68+
callback: () => {
69+
let fileMoveSuggester = new FolderSuggestionModal(this, 'wiki');
70+
fileMoveSuggester.open();
71+
},
72+
});
73+
5674
if (this.settings.contextMenu) this.app.workspace.on('file-menu', this.addFileMenuItems);
5775
}
5876

src/modals.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { App, Modal } from 'obsidian';
1+
import LinkConverterPlugin from 'main';
2+
import { App, FuzzySuggestModal, Modal, TFolder } from 'obsidian';
3+
import * as Converter from './converter';
24

35
export class ConfirmationModal extends Modal {
46
callback: Function;
@@ -33,3 +35,50 @@ export class ConfirmationModal extends Modal {
3335
cancelButton.addEventListener('click', () => this.close());
3436
}
3537
}
38+
39+
type FinalFormat = 'markdown' | 'wiki';
40+
41+
export class FolderSuggestionModal extends FuzzySuggestModal<TFolder> {
42+
app: App;
43+
plugin: LinkConverterPlugin;
44+
finalFormat: FinalFormat;
45+
46+
constructor(plugin: LinkConverterPlugin, finalFormat: FinalFormat) {
47+
super(plugin.app);
48+
this.plugin = plugin;
49+
this.finalFormat = finalFormat;
50+
}
51+
52+
getItemText(item: TFolder): string {
53+
return item.path;
54+
}
55+
56+
getItems(): TFolder[] {
57+
return getAllFoldersInVault(this.app);
58+
}
59+
60+
onChooseItem(folder: TFolder, evt: MouseEvent | KeyboardEvent) {
61+
let infoText = `Are you sure you want to convert all
62+
${this.finalFormat === 'wiki' ? 'Markdown Links to Wikilinks' : 'Wikilinks to Markdown Links'}
63+
under ${folder.name}?`;
64+
let modal = new ConfirmationModal(this.app, infoText, () => Converter.convertLinksUnderFolder(folder, this.plugin, this.finalFormat));
65+
modal.open();
66+
}
67+
}
68+
69+
function getAllFoldersInVault(app: App): TFolder[] {
70+
let folders: TFolder[] = [];
71+
let rootFolder = app.vault.getRoot();
72+
folders.push(rootFolder);
73+
function recursiveFx(folder: TFolder) {
74+
for (let child of folder.children) {
75+
if (child instanceof TFolder) {
76+
let childFolder: TFolder = child as TFolder;
77+
folders.push(childFolder);
78+
if (childFolder.children) recursiveFx(childFolder);
79+
}
80+
}
81+
}
82+
recursiveFx(rootFolder);
83+
return folders;
84+
}

src/utils.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import LinkConverterPlugin from 'main';
2+
import { TFile, TFolder, App } from 'obsidian';
3+
4+
// Helper Function To Get List of Files
5+
export const getFilesUnderPath = (path: string, plugin: LinkConverterPlugin): TFile[] => {
6+
var filesUnderPath: TFile[] = [];
7+
recursiveFx(path, plugin.app);
8+
function recursiveFx(path: string, app: App) {
9+
var folderObj = app.vault.getAbstractFileByPath(path);
10+
if (folderObj instanceof TFolder && folderObj.children) {
11+
for (let child of folderObj.children) {
12+
if (child instanceof TFile) filesUnderPath.push(child);
13+
if (child instanceof TFolder) recursiveFx(child.path, app);
14+
}
15+
}
16+
}
17+
return filesUnderPath;
18+
};

0 commit comments

Comments
 (0)