Newer
Older
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
import React from 'react';
import _ from 'lodash';
import config from 'config:jsonld-context';
import { Card, Container, Code, Heading, Stack, Text } from '@sanity/ui';
const Context = ({ types }) => {
const orderedClasses = _.orderBy(types, ['name'], ['asc']).filter(
(s) => !s.name.includes('locale') && !s.name.includes('block')
);
const base = config.prefix ? config.prefix : 'base';
const uri = config.uri ? config.uri : 'http://example.org/model/0.1/';
let context = {
'@context': {
'@version': 0.1,
_id: '@id',
_type: '@type',
[base]: uri,
},
};
const getProps = (prop) => {
let result = { '@id': `${base}:${prop.name}` };
if (prop.type === 'array') {
result['@container'] = '@list';
}
if (prop.of && prop.of.some((i) => i.type === 'reference')) {
result['@type'] = '@id';
}
if (prop.type === 'reference') {
result['@type'] = '@id';
}
return result;
};
const getFields = (fields) => {
console.log(fields);
if (!fields) return null;
const result = fields.map((field) => {
return {
[field.name]: getProps(field),
};
});
return Object.assign(...result);
};
orderedClasses.forEach((type) => {
context['@context'][type.name] = {
'@id': `${base}:${type.name}`,
'@context': getFields(type.fields),
};
});
return (
<Container width={2} padding={4}>
<Card width={3} margin={3} padding={4}>
<Stack space={3}>
<Heading as="h1" size={5}>
JSON-LD Context
</Heading>
<Text as="p">Starting point for a JSON-LD Context...</Text>
</Stack>
<Card marginTop={6}>
<Code language="json" size={1}>
{JSON.stringify(context, null, 2)}
</Code>
</Card>
</Card>
</Container>
);
};
export default Context;