Initial commit

This commit is contained in:
Timothy DeHerrera
2022-11-01 15:18:00 -06:00
committed by GitHub
commit 14079f0511
90 changed files with 7473 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
{
"name": "search",
"version": "1.0.0",
"description": "",
"main": "prepare-index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Aftab Alam",
"license": "ISC"
}

View File

@@ -0,0 +1,37 @@
import path from 'path'
import { promises as fs } from 'fs'
import { globby } from 'globby'
import grayMatter from 'gray-matter'
(async function () {
// prepare the dirs
const srcDir = path.join(process.cwd(), 'src')
const publicDir = path.join(process.cwd(), 'public')
const contentDir = path.join(srcDir, 'pages', 'blog')
const contentFilePattern = path.join(contentDir, '*.md')
const indexFile = path.join(publicDir, 'search-index.json')
const getSlugFromPathname = (pathname) => path.basename(pathname, path.extname(pathname))
const contentFilePaths = await globby([ contentFilePattern ])
if(contentFilePaths.length) {
const files = contentFilePaths.map(async(filePath) => await fs.readFile(filePath, 'utf8'))
const index = []
let i = 0
for await (let file of files){
const { data: { title, description, tags }, content } = grayMatter(file)
index.push({
slug: getSlugFromPathname(contentFilePaths[i]),
category: 'blog',
title,
description,
tags,
body: content
})
i++
}
await fs.writeFile(indexFile, JSON.stringify(index))
console.log(`Indexed ${index.length} documents from ${contentDir} to ${indexFile}`)
}
})();