Skip to content

Commit

Permalink
feat: add index generation workflow on main update
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenFluin committed Nov 3, 2023
1 parent b5dd1e0 commit 1f6540e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/update-search.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Update Search Index
on:
push:
branches:
- main
jobs:
update:
runs-on: ubuntu-latest
steps:
- name: Make sure we have node and dependencies
run: npm i
- name: Process and send index updates to Algolia
run: npm run build-index
15 changes: 8 additions & 7 deletions generate-search-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ import matter from "gray-matter";
import { remark } from "remark";
import remarkMdx from "remark-mdx";
// Using strip because stripping via remarkMdx rendering wasn't working
import strip from 'strip-markdown'
import strip from "strip-markdown";
import { exit } from "process";

const client = algoliasearch("ECUG3H1E0M", process.env.ALGOLIA_KEY);

if (!process.env.ALGOLIA_KEY) {
console.error("ALGOLIA_KEY not set");
exit(1);
}

const index = client.initIndex("documentation");

const sitemap = [];
Expand Down Expand Up @@ -53,8 +58,6 @@ function walk(dir) {
content = fileContents;
}



// Get rid of markdown
if (filepath.indexOf(".mdx") > -1 || filepath.indexOf(".md") > -1) {
content = String(remark().use(strip).processSync(content));
Expand All @@ -73,11 +76,10 @@ function walk(dir) {
// Fix newlines
content = content.replace(/\n/g, " ");


// Shorten to fit in Algolia Index
if (content) {
content = content.substring(0, 8000);
}
}

let title = data?.title;
const pattern = /^# (.*)$/;
Expand All @@ -96,7 +98,6 @@ function walk(dir) {
}
}


sitemap.push({
url: url,
lastmod: mtime,
Expand Down Expand Up @@ -129,7 +130,7 @@ try {
}

function stripTags(html) {
if(!html || !html.replace) {
if (!html || !html.replace) {
return html;
}
// Create a regular expression to match all HTML tags.
Expand Down
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"start": "astro dev",
"build": "astro build",
"preview": "astro preview",
"astro": "astro"
"astro": "astro",
"build-index": "node ./generate-search-index.js"
},
"dependencies": {
"@astrojs/markdown-remark": "^3.2.0",
Expand Down

1 comment on commit 1f6540e

@vercel
Copy link

@vercel vercel bot commented on 1f6540e Nov 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.