| 
1 | 1 | import LinkConverterPlugin from 'main';  | 
2 |  | -import { App, TFile, Notice, normalizePath, TFolder } from 'obsidian';  | 
 | 2 | +import { App, TFile, Notice, normalizePath, TFolder, MarkdownView } from 'obsidian';  | 
3 | 3 | import { getFilesUnderPath } from './utils';  | 
4 | 4 | 
 
  | 
5 | 5 | /* -------------------- LINK DETECTOR -------------------- */  | 
@@ -150,6 +150,29 @@ export const convertLinksUnderFolder = async (folder: TFolder, plugin: LinkConve  | 
150 | 150 |     }  | 
151 | 151 | };  | 
152 | 152 | 
 
  | 
 | 153 | +// --> Convert Links within editor Selection  | 
 | 154 | +export const convertLinksWithinSelection = async (finalFormat: 'markdown' | 'wiki', plugin: LinkConverterPlugin) => {  | 
 | 155 | +    let activeLeaf = plugin.app.workspace.getActiveViewOfType(MarkdownView);  | 
 | 156 | +    if (activeLeaf) {  | 
 | 157 | +        let editor = activeLeaf.editor;  | 
 | 158 | +        let selection = editor.getSelection();  | 
 | 159 | +        let sourceFile = activeLeaf.file;  | 
 | 160 | +        if (selection !== '') {  | 
 | 161 | +            let newText: string;  | 
 | 162 | +            if (finalFormat === 'markdown') {  | 
 | 163 | +                newText = await convertWikiLinksToMarkdown(selection, sourceFile, plugin);  | 
 | 164 | +            } else if (finalFormat === 'wiki') {  | 
 | 165 | +                newText = await convertMarkdownLinksToWikiLinks(selection, sourceFile, plugin);  | 
 | 166 | +            }  | 
 | 167 | +            editor.replaceSelection(newText);  | 
 | 168 | +        } else {  | 
 | 169 | +            new Notice("You didn't select any text.");  | 
 | 170 | +        }  | 
 | 171 | +    } else {  | 
 | 172 | +        new Notice('There is no active leaf open.', 3000);  | 
 | 173 | +    }  | 
 | 174 | +};  | 
 | 175 | + | 
153 | 176 | // --> Command Function: Converts All Links in All Files in Vault and Save in Corresponding Files  | 
154 | 177 | export const convertLinksInVault = async (plugin: LinkConverterPlugin, finalFormat: 'markdown' | 'wiki') => {  | 
155 | 178 |     convertLinksUnderFolder(plugin.app.vault.getRoot(), plugin, finalFormat);  | 
 | 
0 commit comments