|
| 1 | +require('dotenv').config() |
| 2 | + |
| 3 | +const algoliasearch = require('algoliasearch') |
| 4 | +const glob = require('glob') |
| 5 | +const matter = require('gray-matter') |
| 6 | +const path = require('path') |
| 7 | + |
| 8 | +// In addition to the content of the page, |
| 9 | +// define additional front matter attributes that will be search-indexable |
| 10 | +const SEARCH_DIMENSIONS = ['page_title', 'description'] |
| 11 | + |
| 12 | +main() |
| 13 | + |
| 14 | +async function main() { |
| 15 | + const pagesFolder = path.join(__dirname, '../pages') |
| 16 | + |
| 17 | + // Grab all search-indexable content and format for Algolia |
| 18 | + const searchObjects = glob |
| 19 | + .sync(path.join(pagesFolder, '**/*.mdx')) |
| 20 | + .map((fullPath) => { |
| 21 | + const { content, data } = matter.read(fullPath) |
| 22 | + |
| 23 | + // Get path relative to `pages` |
| 24 | + const __resourcePath = fullPath.replace(`${pagesFolder}/`, '') |
| 25 | + |
| 26 | + // Use clean URL for Algolia id |
| 27 | + const objectID = __resourcePath.replace('.mdx', '') |
| 28 | + |
| 29 | + const searchableDimensions = Object.keys(data) |
| 30 | + .filter((key) => SEARCH_DIMENSIONS.includes(key)) |
| 31 | + .map((dimension) => ({ |
| 32 | + [dimension]: data[dimension], |
| 33 | + })) |
| 34 | + |
| 35 | + return { |
| 36 | + ...searchableDimensions, |
| 37 | + content, |
| 38 | + __resourcePath, |
| 39 | + objectID, |
| 40 | + } |
| 41 | + }) |
| 42 | + |
| 43 | + try { |
| 44 | + await indexSearchContent(searchObjects) |
| 45 | + } catch (e) { |
| 46 | + console.error(e) |
| 47 | + process.exit(1) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +async function indexSearchContent(objects) { |
| 52 | + const { |
| 53 | + NEXT_PUBLIC_ALGOLIA_APP_ID: appId, |
| 54 | + NEXT_PUBLIC_ALGOLIA_INDEX: index, |
| 55 | + ALGOLIA_API_KEY: apiKey, |
| 56 | + } = process.env |
| 57 | + |
| 58 | + if (!apiKey || !appId || !index) { |
| 59 | + throw new Error( |
| 60 | + `[*** Algolia Search Indexing Error ***] Received: ALGOLIA_API_KEY=${apiKey} ALGOLIA_APP_ID=${appId} ALGOLIA_INDEX=${index} \n Please ensure all Algolia Search-related environment vars are set in CI settings.` |
| 61 | + ) |
| 62 | + } |
| 63 | + |
| 64 | + console.log(`updating ${objects.length} indices...`) |
| 65 | + |
| 66 | + try { |
| 67 | + const searchClient = algoliasearch(appId, apiKey) |
| 68 | + const searchIndex = searchClient.initIndex(index) |
| 69 | + |
| 70 | + await searchIndex.partialUpdateObjects(objects, { |
| 71 | + createIfNotExists: true, |
| 72 | + }) |
| 73 | + |
| 74 | + // Remove indices for items that aren't included in the new batch |
| 75 | + const newObjectIds = objects.map(({ objectID }) => objectID) |
| 76 | + let staleObjects = [] |
| 77 | + |
| 78 | + await searchIndex.browseObjects({ |
| 79 | + query: '', |
| 80 | + batch: (batch) => { |
| 81 | + staleObjects = staleObjects.concat( |
| 82 | + batch.filter(({ objectID }) => !newObjectIds.includes(objectID)) |
| 83 | + ) |
| 84 | + }, |
| 85 | + }) |
| 86 | + |
| 87 | + const staleIds = staleObjects.map(({ objectID }) => objectID) |
| 88 | + |
| 89 | + if (staleIds.length > 0) { |
| 90 | + console.log(`deleting ${staleIds.length} stale indices:`) |
| 91 | + console.log(staleIds) |
| 92 | + |
| 93 | + await searchIndex.deleteObjects(staleIds) |
| 94 | + } |
| 95 | + |
| 96 | + console.log('done') |
| 97 | + process.exit(0) |
| 98 | + } catch (error) { |
| 99 | + throw new Error(error) |
| 100 | + } |
| 101 | +} |
0 commit comments