From b8c0d4e3ba1780418125004e8472c3f655b5bc2d Mon Sep 17 00:00:00 2001 From: dphuang2 Date: Mon, 16 Oct 2023 10:00:27 -0700 Subject: [PATCH] save --- generator/konfig-next-app/package.json | 2 +- .../src/utils/compute-search-index.ts | 14 ++++++++++---- .../src/utils/generate-props-for-markdown-page.ts | 15 +++++++++++++++ generator/konfig-next-app/yarn.lock | 15 ++++++++++----- 4 files changed, 36 insertions(+), 10 deletions(-) diff --git a/generator/konfig-next-app/package.json b/generator/konfig-next-app/package.json index 084fb86308..6515423829 100644 --- a/generator/konfig-next-app/package.json +++ b/generator/konfig-next-app/package.json @@ -46,7 +46,7 @@ "deepmerge": "^4.3.1", "eslint": "8.41.0", "eslint-config-next": "13.4.4", - "flexsearch": "^0.7.31", + "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 index 2b69c0b2fd..335485bd59 100644 --- a/generator/konfig-next-app/src/utils/compute-search-index.ts +++ b/generator/konfig-next-app/src/utils/compute-search-index.ts @@ -1,13 +1,19 @@ import { OpenAPIV3_XDocument } from 'konfig-lib' -import flexsearch from 'flexsearch' +import Fuse from 'fuse.js' /** * Computes a search index using flexsearch */ -export function computeSearchIndex({}: { +export function computeSearchIndex({ + markdown, + openapi, +}: { markdown: { id: string; content: string }[] openapi: OpenAPIV3_XDocument }) { - const index = flexsearch.create() - // index.add() + 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 b8d921d7dc..a540426f17 100644 --- a/generator/konfig-next-app/yarn.lock +++ b/generator/konfig-next-app/yarn.lock @@ -1692,6 +1692,11 @@ dependencies: "@types/ms" "*" +"@types/flexsearch@^0.7.4": + version "0.7.4" + resolved "https://registry.yarnpkg.com/@types/flexsearch/-/flexsearch-0.7.4.tgz#6e72bf21a8cc838d6d3449ddadc2745bcee3e239" + integrity sha512-R5ZZmhxOBquntySny8dWYLDGdPJlX9K6p6vcj1w5NWGQ33X4V8MEtdQXK5KFFR1j6o0RhbPMdLSVg5XULVn3oA== + "@types/graceful-fs@^4.1.3": version "4.1.6" resolved "https://registry.yarnpkg.com/@types/graceful-fs/-/graceful-fs-4.1.6.tgz#e14b2576a1c25026b7f02ede1de3b84c3a1efeae" @@ -3476,11 +3481,6 @@ flatted@^3.1.0: resolved "https://registry.yarnpkg.com/flatted/-/flatted-3.2.7.tgz#609f39207cb614b89d0765b477cb2d437fbf9787" integrity sha512-5nqDSxl8nn5BSNxyR3n4I6eDmbolI6WT+QqR547RwxQapgjQBmtktdP+HTBb/a/zLsbzERTONyUB5pefh5TtjQ== -flexsearch@^0.7.31: - version "0.7.31" - resolved "https://registry.yarnpkg.com/flexsearch/-/flexsearch-0.7.31.tgz#065d4110b95083110b9b6c762a71a77cc52e4702" - integrity sha512-XGozTsMPYkm+6b5QL3Z9wQcJjNYxp0CYn3U1gO7dwD6PAqU1SVWZxI9CCg3z+ml3YfqdPnrBehaBrnH2AGKbNA== - flux@^4.0.1: version "4.0.4" resolved "https://registry.yarnpkg.com/flux/-/flux-4.0.4.tgz#9661182ea81d161ee1a6a6af10d20485ef2ac572" @@ -3569,6 +3569,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"