on master help sign in
build: index headings in search cache

Transforms them into an AST, filters to just heading strings.

commit3cb5c5fa7f97342058103bca9171268fe3e9a3d3 authorMatilde Park <matilde.park@urbit.org> dateTue 26 Jul 2022, 14:38 (4 years ago) parent775ca66
1 file changed+8
lib/cache.js+8
lib/cache.js+8
@@ -2,6 +2,7 @@ const fs = require("fs");
22const path = require("path");
33const matter = require("gray-matter");
44const toml = require("@iarna/toml");
5+const markdoc = require("@markdoc/markdoc");
56
67const options = {
78 engines: {
@@ -37,6 +38,8 @@ function buildSearchIndex(dir) {
3738 fs.readFileSync(path.join(dir, page.name)),
3839 options
3940 );
41+ const parsed = markdoc.parse(pageContent);
42+ const transform = markdoc.transform(parsed);
4043 return {
4144 title: pageData.title,
4245 base: page.name,
@@ -46,6 +49,11 @@ function buildSearchIndex(dir) {
4649 page.name.replace(/.md$/, "")
4750 ),
4851 content: pageData.description || "",
52+ headings: transform.children
53+ .filter((e) => e.name === "h2" || e.name === "h3")
54+ .map((e) => e.children)
55+ .flat()
56+ .filter((e) => typeof e === "string"),
4957 parent: metadata.data.title,
5058 };
5159 })