diff --git a/generator/konfig-next-app/package.json b/generator/konfig-next-app/package.json index a9459f3026..ce6b9543eb 100644 --- a/generator/konfig-next-app/package.json +++ b/generator/konfig-next-app/package.json @@ -46,6 +46,7 @@ "deepmerge": "^4.3.1", "eslint": "8.41.0", "eslint-config-next": "13.4.4", + "fuse.js": "^6.6.2", "github-slugger": "^2.0.0", "hast-util-to-text": "^3.1.2", "httpsnippet": "^3.0.1", diff --git a/generator/konfig-next-app/src/utils/compute-search-index.ts b/generator/konfig-next-app/src/utils/compute-search-index.ts new file mode 100644 index 0000000000..335485bd59 --- /dev/null +++ b/generator/konfig-next-app/src/utils/compute-search-index.ts @@ -0,0 +1,19 @@ +import { OpenAPIV3_XDocument } from 'konfig-lib' +import Fuse from 'fuse.js' + +/** + * Computes a search index using flexsearch + */ +export function computeSearchIndex({ + markdown, + openapi, +}: { + markdown: { id: string; content: string }[] + openapi: OpenAPIV3_XDocument +}) { + const fuse = new Fuse(markdown, { keys: ['id', 'content'] }) + const myIndex = Fuse.createIndex(['id', 'content'], markdown) + console.log(console.log(JSON.stringify(myIndex.toJSON(), null, 2))) + const results = fuse.search('Welcome to the SnapTrade developer hub') + console.log(results.length) +} diff --git a/generator/konfig-next-app/src/utils/generate-props-for-markdown-page.ts b/generator/konfig-next-app/src/utils/generate-props-for-markdown-page.ts index a6796519d1..64d9c434d0 100644 --- a/generator/konfig-next-app/src/utils/generate-props-for-markdown-page.ts +++ b/generator/konfig-next-app/src/utils/generate-props-for-markdown-page.ts @@ -20,6 +20,7 @@ import { transformImageLinks } from './transform-image-links' import { transformInternalLinks } from './transform-internal-links' import { generateFaviconLink } from './generate-favicon-link' import { generateLogoLink } from './generate-logo-link' +import { computeSearchIndex } from './compute-search-index' export type MarkdownPageProps = { konfigYaml: KonfigYamlType @@ -178,6 +179,7 @@ export async function generatePropsForMarkdownPage({ // get all docs with collectAllDocumentation and generate a map of id to label from first heading text const docs = collectAllDocuments({ docConfig: documentationConfig }) + const idToContent: Record = {} const idToLabel: Record = {} for (const { id, path } of docs) { const content = await githubGetFileContent({ @@ -186,10 +188,23 @@ export async function generatePropsForMarkdownPage({ repo, path, }) + idToContent[id] = content const docTitle = findFirstHeadingText({ markdown: content }) idToLabel[id] = docTitle } + computeSearchIndex({ + markdown: Object.entries(idToContent).map(([id, content]) => { + if (content === undefined) + throw Error(`Couldn't find content for id: ${id}`) + return { + id, + content, + } + }), + openapi: spec.spec, + }) + const idToBreadcrumbs: Record = {} for (const { id } of docs) { // compute breadcrumb for every document in the documentation config diff --git a/generator/konfig-next-app/yarn.lock b/generator/konfig-next-app/yarn.lock index 057edf2f86..7396f862d1 100644 --- a/generator/konfig-next-app/yarn.lock +++ b/generator/konfig-next-app/yarn.lock @@ -3088,6 +3088,11 @@ functions-have-names@^1.2.2, functions-have-names@^1.2.3: resolved "https://registry.yarnpkg.com/functions-have-names/-/functions-have-names-1.2.3.tgz#0404fe4ee2ba2f607f0e0ec3c80bae994133b834" integrity sha512-xckBUXyTIqT97tq2x2AMb+g163b5JFysYk0x4qxNFwbfQkmNZoiRHb6sPzI9/QV33WeuvVYBUIiD4NzNIyqaRQ== +fuse.js@^6.6.2: + version "6.6.2" + resolved "https://registry.yarnpkg.com/fuse.js/-/fuse.js-6.6.2.tgz#fe463fed4b98c0226ac3da2856a415576dc9a111" + integrity sha512-cJaJkxCCxC8qIIcPBF9yGxY0W/tVZS3uEISDxhYIdtk8OL93pe+6Zj7LjCqVV4dzbqcriOZ+kQ/NE4RXZHsIGA== + generic-pool@3.9.0: version "3.9.0" resolved "https://registry.yarnpkg.com/generic-pool/-/generic-pool-3.9.0.tgz#36f4a678e963f4fdb8707eab050823abc4e8f5e4"