Repository navigation
Expand file tree
/
Copy pathlibrary-create-library.js
More file actions
128 lines (114 loc) · 3.75 KB
/
Copy pathlibrary-create-library.js
File metadata and controls
128 lines (114 loc) · 3.75 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { Library } from '../.codex/library/objects/Library.js';
import { Glossary } from '../.codex/library/objects/Glossary.js';
import { LibraryModal } from '../.codex/library/modals/LibraryModal.js';
import { buildBibliographyBase, buildGlossaryBaseContent } from '../.codex/library/base-builders'
import { Log } from '../.codex/utils/logger.js';
/**
* checkCallback (not callback) so the command is only enabled from within a
* resolvable folder -- checking=true just resolves the folder and returns a
* boolean; checking=false does that same resolution again (cheap) and then
* fires the actual async modal + scaffold work below.
*/
export function buildInvokeCommand(app) {
return {
name: 'Create Library',
id: 'library-create-library',
checkCallback(checking) {
const folder = app.workspace.getActiveFile()?.parent?.path ?? '';
if (!folder) {
return false;
}
if (!checking) {
runCreateLibrary(app, folder);
}
return true;
}
};
};
async function runCreateLibrary(app, folder) {
new LibraryModal(app, async (result) => {
try {
// libraryName names the generated bibliography file -- LibraryModal
// has no built-in way to block an empty submit, so the actual
// enforcement lives here.
if (!result.libraryName) {
const error = new Error('Library name is required -- it names the generated bibliography file.');
Log.error(
'create-library',
error.message,
error
);
throw error;
}
const worksFolderName = result.worksFolderName || 'works';
const tags = Array.from(
new Set([...(result.tags ?? []), result.libraryTag].filter(Boolean))
);
const library = new Library(app, {
folder,
...result,
tags,
});
for (const subfolder of [
'authors',
'collections',
worksFolderName,
'glossary',
'ARCHIVE',
]) {
await app.vault.createFolder(`${ folder }/${ subfolder }`);
// Empty folders don't survive a git commit on their own.
await app.vault.create(`${ folder }/${ subfolder }/.gitkeep`, '');
}
const bibliographyFilename = `${ result.libraryName } Bibliography.base`;
await app.vault.create(
`${ folder }/${ bibliographyFilename }`,
buildBibliographyBase(result.libraryTag)
);
Log.log('create-library', `${ bibliographyFilename } created.`);
const glossaryFolder = `${ folder }/glossary`;
const glossaryBaseFilename = `${ result.libraryName } Glossary.base`;
await app.vault.create(
`${ glossaryFolder }/${ glossaryBaseFilename }`,
buildGlossaryBaseContent(result.libraryTag)
);
Log.log('create-library', `${ glossaryBaseFilename } created.`);
const glossary = new Glossary(app, {
folder: glossaryFolder,
libraryName: result.libraryName,
libraryTag: result.libraryTag,
tags,
});
await glossary.create();
Log.log('create-library', `Glossary note created: ${ glossary.path }`);
const libraryFile = await library.create();
await app.fileManager.processFrontMatter(libraryFile, (frontmatter) => {
frontmatter['sorting-spec'] = buildSortingSpec(bibliographyFilename, worksFolderName);
});
await app.workspace.getLeaf(false).openFile(libraryFile);
Log.log('create-library', `Library created: ${ libraryFile.path }`);
} catch (error) {
Log.error(
'create-library',
'Library scaffold failed partway through -- check the vault for partially-created folders/notes before retrying.',
error
);
throw error;
}
}).open();
}
/**
* <works> in the sorting-spec listing below is whichever name the curator
* gave works-folder-name (or the works fallback, if left blank).
*/
function buildSortingSpec(bibliographyFilename, worksFolderName) {
return [
bibliographyFilename,
worksFolderName,
'authors',
'collections',
'glossary',
'ARCHIVE',
'',
].join('\n');
}