-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApplyTagsFromURL.js
46 lines (36 loc) · 1.31 KB
/
ApplyTagsFromURL.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
let table = base.getTable('Content Map');
let result = await table.selectRecordsAsync({fields: ['URL', 'Tags']});
function getTags(url) {
let tokens = url.split("/");
const firstPathToken = 3;
return tokens.slice(firstPathToken, tokens.length - 2);
}
function getSlugTitle(url) {
let tokens = url.split("/").filter(e => e);
return tokens.length > 3 ? tokens[tokens.length - 1] : '';
}
function doesTagsFieldContain(tagName) {
let existingTags = table.getField("Tags").options.choices.map(o => o.name);
return existingTags.includes(tagName);
}
async function addOptionToTags(tag) {
console.log("Adding " + tag);
const field = table.getField('Tags');
await field.updateOptionsAsync({
choices: [
...field.options.choices,
{name: tag},
],
});
}
let records = result.records;
for(let record of records.filter(r => r.getCellValue("URL"))) {
let url = record.getCellValue("URL");
let tags = getTags(url);
let slugTitle = getSlugTitle(url);
console.log("Tags is " + tags);
for(const tag of tags.filter(t => !doesTagsFieldContain(t))) {
await addOptionToTags(tag);
}
await table.updateRecordAsync(record, {"Tags": tags.map(t => {return {name:t}}) , "Slug Title": slugTitle});
}