From 11ff99db869df9f4e067108f6c25a357d2acb8c1 Mon Sep 17 00:00:00 2001 From: Joshen Lim Date: Mon, 21 Nov 2022 00:49:05 +0800 Subject: [PATCH] Fix algolia search postbuild script --- apps/docs/scripts/build-search.js | 23 ++++++++++++++++++++++- apps/docs/styles/docsearch.scss | 6 ++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/apps/docs/scripts/build-search.js b/apps/docs/scripts/build-search.js index a5c06f08157..91e69c3fae8 100644 --- a/apps/docs/scripts/build-search.js +++ b/apps/docs/scripts/build-search.js @@ -4,6 +4,7 @@ const path = require('path') const matter = require('gray-matter') const dotenv = require('dotenv') const algoliasearch = require('algoliasearch/lite') +const { isEmpty } = require('lodash') // [Joshen] We initially thought of building out our search using Algolia directly, // but eventually decided to just use DocSearch since it provides us a UI and some @@ -83,10 +84,30 @@ async function walk(dir) { const searchObjects = allPages .map((slug) => { + let id, title, description const fileContents = fs.readFileSync(slug, 'utf8') const { data, content } = matter(fileContents) - const { id, title, description } = data + if (isEmpty(data)) { + // Guide pages do not have front-matter meta, unlike reference pages, have to manually extract + const metaIndex = fileContents.indexOf('export const meta = {') + if (metaIndex !== -1) { + const metaString = + fileContents + .slice(metaIndex + 20, fileContents.indexOf('}') + 1) + .replace(/\n/g, '') + .slice(0, -2) + '}' + const meta = eval(`(${metaString})`) + id = meta.id + title = meta.title + description = meta.description + } + } else { + id = data.id + title = data.title + description = data.description + } + const url = (slug.includes('/generated') ? slug.replace('/generated', '') : slug) .replace('docs', '') .replace('pages', '') diff --git a/apps/docs/styles/docsearch.scss b/apps/docs/styles/docsearch.scss index e9b3e1050a2..a9ca46569c5 100644 --- a/apps/docs/styles/docsearch.scss +++ b/apps/docs/styles/docsearch.scss @@ -390,6 +390,12 @@ } } +.DocSearch-StartScreen { + .DocSearch-Help { + @apply text-center py-16; + } +} + .DocSearch-Help { @apply text-scale-1100; font-size: 0.875rem !important;