-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcontainer.js
More file actions
43 lines (38 loc) · 1.2 KB
/
container.js
File metadata and controls
43 lines (38 loc) · 1.2 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
/**
* Generate container representation as JSON-LD
*/
const LDP = 'http://www.w3.org/ns/ldp#';
/**
* Generate JSON-LD representation of a container
* @param {string} containerUrl - Full URL of the container
* @param {Array<{name: string, isDirectory: boolean}>} entries - Container contents
* @returns {object} - JSON-LD representation
*/
export function generateContainerJsonLd(containerUrl, entries) {
// Ensure container URL ends with /
const baseUrl = containerUrl.endsWith('/') ? containerUrl : containerUrl + '/';
const contains = entries.map(entry => {
const childUrl = baseUrl + entry.name + (entry.isDirectory ? '/' : '');
return {
'@id': childUrl,
'@type': entry.isDirectory ? [`${LDP}Container`, `${LDP}BasicContainer`, `${LDP}Resource`] : [`${LDP}Resource`]
};
});
return {
'@context': {
'ldp': LDP,
'contains': { '@id': 'ldp:contains', '@type': '@id' }
},
'@id': baseUrl,
'@type': ['ldp:Container', 'ldp:BasicContainer', 'ldp:Resource'],
'contains': contains
};
}
/**
* Convert JSON-LD to string
* @param {object} jsonLd
* @returns {string}
*/
export function serializeJsonLd(jsonLd) {
return JSON.stringify(jsonLd, null, 2);
}