-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
64 lines (59 loc) · 1.95 KB
/
index.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
// import the dependency
const algoliaSitemap = require('../../build');
// set up your API keys
// make sure the key has "browse" capability
/**
* @type {algoliaConfig}
*/
const algoliaConfig = {
appId: 'APP_ID',
apiKey: 'API_KEY',
indexName: 'NAME',
};
/**
* @typedef {Object} Params
* @property {string} loc the link of this hit
* @property {string} [lastmod] the last time this link was modified (ISO8601)
* @property {number} [priority] the priority you give to this link (between 0 and 1)
* @property {Object} [alternates] alternative versions of this link (useful for multi-language)
* @property {Array} [alternates.languages] list of languages that are enabled
* @property {function} [alternates.hitToURL] function to transform a language into a url of this object
*/
/**
* Function to transform a hit into its link
*
* @param {Object} hit a hit to transform
* @returns {Params} the parameters for this hit
*/
function hitToParams(hit) {
const url = ({ lang, objectID }) =>
`https://${lang}.yoursite.com/detail/${objectID}`;
const loc = url({ lang: 'en', objectID: hit.objectID });
const lastmod = new Date().toISOString();
const priority = Math.random();
return {
loc,
lastmod,
priority,
alternates: {
languages: ['fr', 'pt-BR', 'zh-Hans'],
hitToURL: lang => url({ lang, objectID: hit.objectID }),
},
};
}
/**
* @param {algoliaConfig} algoliaConfig configuration for Algolia
* @param {string} sitemapLoc href of your sitemap, used to build the sitemap index
* @param {string} outputFolder relative path where your sitemaps will be outputed
* @param {function} hitToParams function to transform a hit into Params
*/
algoliaSitemap({
algoliaConfig,
sitemapLoc: 'https://yoursite.com/sitemaps',
outputFolder: `${__dirname}/sitemaps`,
hitToParams,
})
.then(() => {
console.log('Done generating sitemaps'); // eslint-disable-line no-console
})
.catch(console.error); // eslint-disable-line no-console