-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselectFile.ts
32 lines (30 loc) · 1.26 KB
/
selectFile.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { Placeholder, PlaceholderCategory, PlaceholderType } from "../types";
import { addFileToSelection } from "../scripts";
/**
* Directive to select a file. The placeholder will always be replaced with an empty string.
*
* Syntax: `{{selectFile:...}}`, where `...` is the POSIX path to the file to select.
*/
const SelectFileDirective: Placeholder = {
name: "selectFile",
regex: /{{(selectFile)(:[\s\S]*?)?}}/g,
apply: async (str: string) => {
const file = str.match(/(?<=(selectFiles:))[\s\S]*?(?=}})/)?.[0];
if (!file) return { result: "" };
await addFileToSelection(file);
return { result: "" };
},
constant: false,
fn: async (path: unknown) => {
const pathText = typeof path === "function" ? await Promise.resolve(path()) : path;
return (await SelectFileDirective.apply(`{{selectFile:${pathText}}}`)).result
},
example: "{{selectFile:/Users/username/Desktop/file.txt}}",
description:
"Directive to a select file. The placeholder will always be replaced with an empty string.",
hintRepresentation: "{{selectFile:...}}",
fullRepresentation: "Select File At Path",
type: PlaceholderType.InteractiveDirective,
categories: [PlaceholderCategory.Files, PlaceholderCategory.Applications],
};
export default SelectFileDirective;