Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

Update gatsby local search to also include headers #32

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions config/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,40 @@ const searchPluginConfig = {
fields {
path
}
tableOfContents
}
}
}
`,
index: ["title"],
normalizer: ({ data }) =>
data.allMdx.nodes.map((node) => ({
id: node.id,
title: node.frontmatter.title,
description: node.frontmatter.description,
url: node.fields.path,
isExternal: false,
})),
data.allMdx.nodes.flatMap((node) => {
// return at least the main page ("node")
const returnVal = [
{
id: node.id,
title: node.frontmatter.title,
description: node.frontmatter.description,
url: node.fields.path,
isExternal: false,
toc: node.tableOfContents,
},
];

// add the headers too if they're in ToC
if (node.tableOfContents.items != null) {
node.tableOfContents.items.map((header) =>
returnVal.push({
id: node.id.concat(header.url),
title: node.frontmatter.title.concat(" - ", header.title),
url: node.fields.path.concat(header.url),
isExternal: false,
})
);
}

return returnVal;
}),
},
};

Expand Down