-
Notifications
You must be signed in to change notification settings - Fork 2
/
grscicoll.js
206 lines (185 loc) · 5.73 KB
/
grscicoll.js
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
const notice = (msg) => new Notice(msg, 5000);
const log = (msg) => console.log(msg);
const API_SEARCH_URL = "http://api.gbif.org/v1/grscicoll/collection";
const TEMPLATE = "Template file:";
const DESTINATION_DIR = "Destination folder:";
const FILENAME_FORMAT = "Filename format:";
const FILENAME_FORMAT_DEFAULT = "{{~code~}}({{~name~}})-{{~institutionName~}}";
const ILLEGAL_OBSIDIAN_FILE_NAME_PATTERN = /[\\,#%&\{\}\/*<>?$\'\":@]*/g;
module.exports = {
entry: start,
settings: {
name: "GRSciColl",
// author: "",
options: {
[FILENAME_FORMAT]: {
type: "text",
defaultValue: FILENAME_FORMAT_DEFAULT,
},
[TEMPLATE]: {
type: "select",
options: app.vault.getAbstractFileByPath('templates').children.map((t) => t.path),
},
[DESTINATION_DIR]: {
type: "select",
options: Object.keys(app.vault.fileMap).filter(function (filemapentry){return Object.keys(app.vault.fileMap[filemapentry]).includes('children')}),
},
},
},
};
let QuickAdd;
let Settings;
function init(params, settings) {
QuickAdd = params;
Settings = settings;
}
async function start(params, settings) {
QuickAdd = params;
Settings = settings;
var query = activeDocument.getSelection().toString();
if (typeof query != "string"){
query = null;
}
if (!query){
query = await getQuery();
query = query.searchTerms;
}
if (!query) {
notice("No query entered.");
throw new Error("No query entered.");
}
let possibleColls = await getCollectionsByQueryParams(query);
let selectedColl = await promptUserForSelectingSuggestions(possibleColls);
if (selectedColl){
// Read in template as defined in settings:
let template_file = app.vault.getAbstractFileByPath(Settings[TEMPLATE]);
let template = await app.vault.read(template_file);
// Apply template to coll object:
let rendered = params.app.plugins.getPlugin('obsidian-handlebar-helper-plugin').compileAndRender(template,selectedColl);
// Build filename and create file:
filename_template = Settings[FILENAME_FORMAT];
fileName = params.app.plugins.getPlugin('obsidian-handlebar-helper-plugin').compileAndRender(filename_template,selectedColl);
fileName = Settings[DESTINATION_DIR] + '/' + fileName.replace(ILLEGAL_OBSIDIAN_FILE_NAME_PATTERN, "") + '.md';
console.log(fileName);
try{
createdFile = await params.app.vault.create(fileName, rendered);
if (createdFile){
// Open it:
leaf = params.app.workspace.getUnpinnedLeaf();
await leaf.openFile(createdFile)
}
}
catch(Error){
notice('Could not create file / already exists');
}
}
else{
notice("No collection selected");
throw new Error("No collection selected.");
}
}
function formatTitleForSuggestionList(resultItem) {
var suggestion = `${resultItem.code} - ${resultItem.institutionName} (${resultItem.name})`;
// if (resultItem.description){
// suggestion = suggestion + ` Description: ${resultItem.description}`
// }
// if (resultItem.geography){
// suggestion = suggestion + ` Geography: ${resultItem.geography}`
// }
// if (resultItem.taxonomicCoverage){
// suggestion = suggestion + ` Taxonomic coverage: ${resultItem.taxonomicCoverage}`
// }
return decodeEntity(suggestion);
}
function decodeEntity(inputStr) {
var textarea = document.createElement("textarea");
textarea.innerHTML = inputStr;
return textarea.value;
}
async function getQuery(){
return Promise.all([promptUserForSearchTerms()]).then(
([searchTerms]) =>
new Promise((resolve, reject) => {
if (!searchTerms) {
notice("No query entered.");
reject(new Error("No query entered."));
}
resolve({ searchTerms });
})
);
}
function promptUserForSearchTerms(){
return QuickAdd.quickAddApi.inputPrompt("GRSciColl search terms:");
}
function promptUserForSelectingSuggestions(suggestions) {
return new Promise((resolve, reject) => {
QuickAdd.quickAddApi
.suggester(suggestions.map(formatTitleForSuggestionList), suggestions)
.then(async (selectedColl) => {
if (!selectedColl) {
notice("No choice selected.");
reject(new Error("No choice selected."));
}
resolve(selectedColl);
});
});
}
/**
*
* @param {object} release The collection data from searching GRSciColl
* @returns {Collection}
*/
function buildCollectionsData(collection) {
return collection;
}
/**
*
* @param {object} queryParams
* @throws Will throw Error if no collections are found
* @returns {Collection[]} The collection results from searching for a collection
*/
async function getCollectionsByQueryParams(queryParams) {
const searchResults = await fetchCollections(
API_SEARCH_URL,
queryParams
);
console.log(searchResults);
if (!searchResults || !searchResults.length) {
notice("No results found.");
throw new Error("No results found.");
}
return searchResults.map(buildCollectionsData);
}
/**
*
* @param {object} queryParams
* @returns {string} The query string for requests to grscicoll API.
*/
function buildQueryString(queryParam) {
return queryParam;
}
/**
*
* @param {string} url
* @param {object} queryParams
* @returns {object[]}
*/
async function fetchCollections(url, queryParam) {
let finalURL = new URL(url);
if (queryParam) {
finalURL.searchParams.append("q", buildQueryString(queryParam));
finalURL.searchParams.append("masterSourceType", "IH");
}
console.log(finalURL);
return JSON.parse(
await request({
url: finalURL.href,
method: "GET",
cache: "no-cache",
headers: {
"Content-Type": "application/json",
"User-Agent": "ObsidianQuickAddMacro ( [email protected] )",
},
})
)['results'];
}