Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
save

re-run yarn
  • Loading branch information
dphuang2 committed Nov 5, 2023
1 parent bc44a4e commit 50ecae0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions generator/konfig-next-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
19 changes: 19 additions & 0 deletions generator/konfig-next-app/src/utils/compute-search-index.ts
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<string, string | undefined> = {}
const idToLabel: Record<string, string | undefined> = {}
for (const { id, path } of docs) {
const content = await githubGetFileContent({
Expand All @@ -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<string, string[] | undefined> = {}
for (const { id } of docs) {
// compute breadcrumb for every document in the documentation config
Expand Down
5 changes: 5 additions & 0 deletions generator/konfig-next-app/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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==

[email protected]:
version "3.9.0"
resolved "https://registry.yarnpkg.com/generic-pool/-/generic-pool-3.9.0.tgz#36f4a678e963f4fdb8707eab050823abc4e8f5e4"
Expand Down

0 comments on commit 50ecae0

Please sign in to comment.