2021-12-31 08:27:13 +01:00
|
|
|
/*
|
|
|
|
* This will update (replace really) the Algolia index with the
|
|
|
|
* current website contents. Or at least the markdown and Strapi
|
|
|
|
* content
|
|
|
|
*
|
|
|
|
* It expects the following environment vars to be set in a
|
2022-08-10 14:03:18 +02:00
|
|
|
* .env file in the 'sites/dev' folder:
|
2021-12-31 08:27:13 +01:00
|
|
|
*
|
2022-08-10 14:03:18 +02:00
|
|
|
* ALGOLIA_API_WRITE_KEY -> Needs permission to index/create/delete
|
2021-12-31 08:27:13 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
import dotenv from 'dotenv'
|
|
|
|
import fs from 'fs'
|
|
|
|
import path from 'path'
|
|
|
|
import algoliasearch from 'algoliasearch'
|
|
|
|
import { unified } from 'unified'
|
|
|
|
import remarkParser from 'remark-parse'
|
|
|
|
import remarkCompiler from 'remark-stringify'
|
|
|
|
import remarkFrontmatter from 'remark-frontmatter'
|
|
|
|
import remarkFrontmatterExtractor from 'remark-extract-frontmatter'
|
|
|
|
import remarkRehype from 'remark-rehype'
|
|
|
|
import rehypeSanitize from 'rehype-sanitize'
|
|
|
|
import rehypeStringify from 'rehype-stringify'
|
|
|
|
import yaml from 'yaml'
|
2022-08-10 14:03:18 +02:00
|
|
|
import { getMdxFileList } from '../../shared/prebuild/mdx.mjs'
|
|
|
|
import config from '../algolia.config.mjs'
|
2021-12-31 08:27:13 +01:00
|
|
|
dotenv.config()
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize Algolia client
|
|
|
|
*/
|
2022-10-02 21:58:34 +02:00
|
|
|
const client = algoliasearch(config.algolia.app, process.env.ALGOLIA_API_WRITE_KEY)
|
2022-08-10 11:50:21 +02:00
|
|
|
const index = client.initIndex(config.algolia.index)
|
2021-12-31 08:27:13 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Loads markdown from disk and compiles it into HTML for indexing
|
|
|
|
*/
|
2022-10-02 21:58:34 +02:00
|
|
|
const markdownLoader = async (file) => {
|
2021-12-31 08:27:13 +01:00
|
|
|
const md = await fs.promises.readFile(file, 'utf-8')
|
|
|
|
|
|
|
|
const page = await unified()
|
|
|
|
.use(remarkParser)
|
|
|
|
.use(remarkCompiler)
|
|
|
|
.use(remarkFrontmatter)
|
|
|
|
.use(remarkFrontmatterExtractor, { yaml: yaml.parse })
|
|
|
|
.use(remarkRehype)
|
|
|
|
.use(rehypeSanitize)
|
|
|
|
.use(rehypeStringify)
|
|
|
|
.process(md)
|
|
|
|
const id = file.split('freesewing/markdown/dev').pop().slice(0, -6)
|
|
|
|
|
|
|
|
return {
|
|
|
|
objectID: id,
|
|
|
|
page: id,
|
|
|
|
title: page.data.title,
|
|
|
|
body: page.value,
|
|
|
|
type: 'docs',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-10 14:03:18 +02:00
|
|
|
/*
|
|
|
|
* Clear the index to scrub old pages
|
|
|
|
*/
|
|
|
|
const clearIndex = async () => {
|
|
|
|
console.log(`🗑️ Clearing index`)
|
|
|
|
await index.clearObjects()
|
|
|
|
}
|
2021-12-31 08:27:13 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get and index markdown content
|
|
|
|
*/
|
|
|
|
const indexMarkdownContent = async () => {
|
|
|
|
// Say hi
|
2022-08-10 14:03:18 +02:00
|
|
|
console.log(`🗂️ Indexing Markdown content to Algolia`)
|
2021-12-31 08:27:13 +01:00
|
|
|
|
|
|
|
// Setup MDX root path
|
|
|
|
const mdxRoot = path.resolve('..', '..', 'markdown', 'dev')
|
|
|
|
|
|
|
|
// Get list of filenames
|
|
|
|
const list = await getMdxFileList(mdxRoot, 'en')
|
|
|
|
const pages = []
|
2022-08-10 14:03:18 +02:00
|
|
|
|
2021-12-31 08:27:13 +01:00
|
|
|
for (const file of list) pages.push(await markdownLoader(file))
|
|
|
|
// Index markdown to Algolia
|
2022-08-10 14:03:18 +02:00
|
|
|
await index.clearObjects()
|
|
|
|
await index
|
2021-12-31 08:27:13 +01:00
|
|
|
.saveObjects(pages)
|
2022-10-02 21:58:34 +02:00
|
|
|
.then(() => null)
|
|
|
|
.catch((err) => console.log(err))
|
2021-12-31 08:27:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const run = async () => {
|
2022-10-02 21:58:34 +02:00
|
|
|
if (process.env.VERCEL_ENV === 'production' || process.env.FORCE_ALGOLIA) {
|
2022-08-10 14:03:18 +02:00
|
|
|
console.log()
|
|
|
|
await clearIndex()
|
2022-02-05 19:40:44 +01:00
|
|
|
await indexMarkdownContent()
|
|
|
|
console.log()
|
|
|
|
} else {
|
|
|
|
console.log()
|
|
|
|
console.log('Not a producion deploy. Not indexing to Algolia.')
|
|
|
|
console.log('To force indexing, set the FORCE_ALGOLIA environment variable')
|
|
|
|
console.log()
|
|
|
|
}
|
2021-12-31 08:27:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
run()
|