From 36aa0cc187f05a02e2b6679aff91416ed834d1b2 Mon Sep 17 00:00:00 2001 From: Greg Richardson Date: Thu, 11 May 2023 12:26:27 -0600 Subject: [PATCH 1/2] fix(embeddings): custom heading slugs --- .github/workflows/search.yml | 15 +++++++++++++-- apps/docs/scripts/search/sources/markdown.ts | 19 ++++++++++++++++++- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/.github/workflows/search.yml b/.github/workflows/search.yml index b3d60d06629..b4631ceca5d 100644 --- a/.github/workflows/search.yml +++ b/.github/workflows/search.yml @@ -9,7 +9,12 @@ on: - 'supabase/migrations/**' - 'apps/docs/**' - 'spec/**' - workflow_dispatch: {} + workflow_dispatch: + inputs: + refresh: + description: 'Refresh all pages' + required: false + type: boolean schedule: - cron: '0 0 * * *' @@ -46,6 +51,12 @@ jobs: - name: Run migrations run: npx supabase db push + - name: Update embeddings + working-directory: ./apps/docs + if: ${{ !inputs.refresh }} + run: npm run embeddings + - name: Refresh embeddings working-directory: ./apps/docs - run: npm run embeddings + if: ${{ inputs.refresh }} + run: npm run embeddings:refresh diff --git a/apps/docs/scripts/search/sources/markdown.ts b/apps/docs/scripts/search/sources/markdown.ts index 851839a772b..ef51f95e593 100644 --- a/apps/docs/scripts/search/sources/markdown.ts +++ b/apps/docs/scripts/search/sources/markdown.ts @@ -96,6 +96,23 @@ export function splitTreeBy(tree: Root, predicate: (node: Content) => boolean) { }, []) } +/** + * Wrapped slug generator that also accounts for + * custom anchors in the format: + * + * ```markdown + * ### My Heading [#my-custom-anchor] + * ``` + */ +export function generateSlug(slugger: GithubSlugger, heading: string) { + const match = heading.match(/\[#(.*)\]/) + if (match) { + const [, customAnchor] = match + return slugger.slug(customAnchor) + } + return slugger.slug(heading) +} + /** * Processes MDX content for search indexing. * It extracts metadata, strips it of all JSX, @@ -142,7 +159,7 @@ export function processMdxForSearch(content: string): ProcessedMdx { const [firstNode] = tree.children const heading = firstNode.type === 'heading' ? toString(firstNode) : undefined - const slug = heading ? slugger.slug(heading) : undefined + const slug = heading ? generateSlug(slugger, heading) : undefined return { content: toMarkdown(tree), From 1c0da103c94ae8beafd713354d0f7174f717e703 Mon Sep 17 00:00:00 2001 From: Greg Richardson Date: Thu, 11 May 2023 13:24:18 -0600 Subject: [PATCH 2/2] fix(embeddings): remove custom anchor from heading --- apps/docs/scripts/search/sources/markdown.ts | 28 +++++++++++++------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/apps/docs/scripts/search/sources/markdown.ts b/apps/docs/scripts/search/sources/markdown.ts index ef51f95e593..91beb6cc01a 100644 --- a/apps/docs/scripts/search/sources/markdown.ts +++ b/apps/docs/scripts/search/sources/markdown.ts @@ -97,20 +97,20 @@ export function splitTreeBy(tree: Root, predicate: (node: Content) => boolean) { } /** - * Wrapped slug generator that also accounts for - * custom anchors in the format: + * Parses a markdown heading which can optionally + * contain a custom anchor in the format: * * ```markdown * ### My Heading [#my-custom-anchor] * ``` */ -export function generateSlug(slugger: GithubSlugger, heading: string) { - const match = heading.match(/\[#(.*)\]/) +export function parseHeading(heading: string): { heading: string; customAnchor?: string } { + const match = heading.match(/(.*) *\[#(.*)\]/) if (match) { - const [, customAnchor] = match - return slugger.slug(customAnchor) + const [, heading, customAnchor] = match + return { heading, customAnchor } } - return slugger.slug(heading) + return { heading } } /** @@ -157,12 +157,20 @@ export function processMdxForSearch(content: string): ProcessedMdx { const sections = sectionTrees.map((tree) => { const [firstNode] = tree.children + const content = toMarkdown(tree) - const heading = firstNode.type === 'heading' ? toString(firstNode) : undefined - const slug = heading ? generateSlug(slugger, heading) : undefined + const rawHeading: string = firstNode.type === 'heading' ? toString(firstNode) : undefined + + if (!rawHeading) { + return { content } + } + + const { heading, customAnchor } = parseHeading(rawHeading) + + const slug = slugger.slug(customAnchor ?? heading) return { - content: toMarkdown(tree), + content, heading, slug, }