-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.js
69 lines (56 loc) · 2.46 KB
/
routes.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
import fs from 'fs';
import path from 'path';
import { cwd } from 'process';
import { linkResolver } from './src/js/utils/linkResolver.js';
const excerptLength = 300;
// https://stackoverflow.com/questions/18749591/encode-html-entities-in-javascript/18750001#18750001
const encodeHTMLEntities = s => s.replace(/[\u00A0-\u9999<>\&]/g, i => `&#${i.charCodeAt(0)};`);
const build = async () => {
const query = await (await fetch('https://53goq129.api.sanity.io/v2024-10-27/data/query/production?query=*')).json();
const index = query.result.findIndex(({ _type }) => _type === 'settings');
let settings = query.result.splice(index, 1)[0];
settings = {
title: encodeHTMLEntities(settings.title),
description: encodeHTMLEntities(settings.description),
favicon: query.result.find(({ _id }) => _id === settings.favicon?.asset._ref)?.url,
shareImage: query.result.find(({ _id }) => _id === settings.shareImage?.asset._ref)?.url
};
const result = query.result.filter(({ _type, name, slug }) => {
return (_type === 'page' || _type === 'article') && (
(name !== 'Home' && slug) ||
name === 'Home'
);
}).map(doc => {
let type = '';
let title = '';
let description = '';
let shareImage = '';
if (doc._type === 'page') {
type = doc.name.toLowerCase();
title = doc.title;
description = doc.description;
}
if (doc._type === 'article') {
type = 'project';
title = doc.title;
description = doc.description;
shareImage = query.result.find(({ _id }) => _id === doc.featuredImage?.image?.asset._ref)?.url;
}
if (description && description.length > excerptLength) {
description = `${description.substring(0, excerptLength - 3)}...`;
}
return {
type,
title: title ? encodeHTMLEntities(title) : settings.title,
description: description ? encodeHTMLEntities(description.replace(/\n/g, ' ').trim()) : settings.description,
favicon: `${settings.favicon}?w=512&h=512&fit=crop&crop=center`,
shareImage: `${shareImage ? shareImage : settings.shareImage}?w=1200&h=630&fit=crop&crop=center`,
fullPath: linkResolver(doc)
};
});
fs.writeFileSync(
path.join(cwd(), './routes.json'),
JSON.stringify({ settings, result }, null, '\t')
);
}
build();