mirror of
https://github.com/unarray/file-tree-generator.git
synced 2026-07-22 08:40:29 +00:00
Create GenerateTree command
This commit is contained in:
parent
3a62bfb151
commit
34f5bfbe2f
1 changed files with 35 additions and 0 deletions
35
src/commands/GenerateTree.ts
Normal file
35
src/commands/GenerateTree.ts
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
import type { Command, Editor } from "obsidian";
|
||||
import { dialog } from "@electron/remote";
|
||||
import { getFiles } from "#/utils/path";
|
||||
import { explorerEntityToCallout, filesToExplorerEntity } from "#/utils/parser";
|
||||
import { beginningString } from "#/utils/regex";
|
||||
import { sep as separator } from "path";
|
||||
|
||||
export class GenerateTree implements Command {
|
||||
|
||||
public readonly id = "generate-tree";
|
||||
|
||||
public readonly name = "Generate a File Tree";
|
||||
|
||||
public editorCallback = async(editor: Editor): Promise<void> => {
|
||||
const dialogResponse = await dialog.showOpenDialog({
|
||||
properties: ["openDirectory"]
|
||||
});
|
||||
|
||||
if (dialogResponse.canceled) return;
|
||||
|
||||
const selectedPath = dialogResponse.filePaths[0];
|
||||
const removePath = selectedPath.substring(0, selectedPath.lastIndexOf(separator) + separator.length);
|
||||
const regex = beginningString(removePath);
|
||||
const files = (await getFiles(selectedPath)).map(file => file.replace(regex, ""));
|
||||
const structure = filesToExplorerEntity(files);
|
||||
const callouts = explorerEntityToCallout(structure);
|
||||
const cursorLine = editor.getCursor("to").line;
|
||||
|
||||
editor.setLine(
|
||||
cursorLine,
|
||||
`${editor.getLine(cursorLine)}\n${callouts}`
|
||||
);
|
||||
};
|
||||
|
||||
}
|
||||
Loading…
Reference in a new issue