-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmakeNoteWithPrompting.js
More file actions
65 lines (53 loc) · 2.17 KB
/
Copy pathmakeNoteWithPrompting.js
File metadata and controls
65 lines (53 loc) · 2.17 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Validate the required config elements.
function validateNoteCreationConfigElements(config) {
// Ensure the config has the required properties.
if (!config.hasOwnProperty("path")) {
// Throw.
throw new Error("Missing required 'path' configuration element.");
}
// Ensure the config has the required properties.
if (!config.hasOwnProperty("filename")) {
// Throw.
throw new Error("Missing required 'filename' configuration element.");
}
}
/**
* Create a new note from template with automatic prompting.
*
* @param {object} tp - The `Templater` object.
* @param {object} config - A configuration object containing objects used in the template.
* @param {string} ext - The file extension. Defaults to `.md`.
*
* @returns {Promise<TFile|undefined>} - A promise that resolves to the newly created note `TFile` object if `Templater` was invoked to create the note. Otherwise, `undefined`.
*
* @throws {Error} - Throws a errors for various reasons. Error handling in the template is recommended.
*/
async function makeNoteWithPrompting(tp, config, ext = ".md") {
// If the invocation mode is creating a new note.
if (tp.config.run_mode === 0) {
// Validate the config elements required for note creation.
validateNoteCreationConfigElements(config);
}
// Prompt the user.
await tp.user.prompt(tp, config);
// If the invocation mode is creating a new note.
if (tp.config.run_mode === 0) {
// Check if the file exists.
const fileExists = await tp.file.exists(
`${config.path.value}/${config.filename.value}${ext}`
);
// If the file exists.
if (fileExists) {
// Throw.
throw new Error(`Note '${config.filename.value}' already exists.`);
}
// Get a file object for the temporary note created.
const file = await tp.file.find_tfile(tp.file.path(true));
// Move the note to the desired location.
await tp.file.move(`${config.path.value}/${config.filename.value}`, file);
// Return the file object.
return file;
}
}
// Export the function.
module.exports = makeNoteWithPrompting