Zeta & UI: Update search utilities import paths

This commit is contained in:
SonMooSans
2023-10-18 19:09:10 +08:00
parent b7a0ad52c1
commit a4a8120e44
18 changed files with 82 additions and 65 deletions
+14
View File
@@ -0,0 +1,14 @@
---
'next-docs-zeta': major
'next-docs-ui': patch
---
Update search utilities import paths.
Search Utilities in `next-docs-zeta/server` is now moved to `next-docs-zeta/search` and `next-docs-zeta/server-algolia`.
Client-side Changes:
`next-docs-zeta/search` -> `next-docs-zeta/search/client`
`next-docs-zeta/search-algolia` -> `next-docs-zeta/search-algolia/client`
If you're using Next Docs UI, make sure to import the correct path.
+1 -1
View File
@@ -7,7 +7,7 @@ import {
SearchDialog,
type SharedProps
} from 'next-docs-ui/components/dialog/search'
import { useAlgoliaSearch } from 'next-docs-zeta/search-algolia'
import { useAlgoliaSearch } from 'next-docs-zeta/search-algolia/client'
import { useParams } from 'next/navigation'
import { useEffect, useState } from 'react'
+19 -7
View File
@@ -1,15 +1,11 @@
import path from 'path'
import algosearch from 'algoliasearch'
import { config } from 'dotenv'
import { sync } from 'next-docs-zeta/algolia'
import { createUtils, loadContext } from 'next-docs-zeta/contentlayer'
import { allDocs, allMeta } from './.contentlayer/generated/index.mjs'
import { sync } from 'next-docs-zeta/search-algolia/server'
import { allDocs } from './.contentlayer/generated/index.mjs'
config({ path: path.resolve(process.cwd(), '.env.local') })
const ctx = loadContext(allMeta, allDocs)
const { getPageUrl } = createUtils(ctx)
const client = algosearch(
process.env.ALGOLIA_APP_ID,
process.env.ALGOLIA_API_KEY
@@ -18,7 +14,7 @@ const client = algosearch(
sync(client, {
documents: allDocs.map(docs => ({
...docs,
url: getPageUrl(docs.slug),
url: getPageUrl(['docs', ...docs.slug.split('/')]),
structured: docs.structuredData,
extra_data: {
tag: docs._raw.flattenedPath.startsWith('docs/ui') ? 'ui' : 'headless'
@@ -27,3 +23,19 @@ sync(client, {
}).then(() => {
console.log('search updated')
})
/**
* @param {Array<string>} slug
* @returns {string}
*/
function getPageUrl(slug) {
const url = slug
.filter(segment => segment != null && segment.length > 0)
.join('/')
if (url.startsWith('//')) {
return url.slice(1)
}
return url
}
+1 -1
View File
@@ -1,6 +1,6 @@
import { getPageUrl } from '@/app/source'
import { allDocs } from 'contentlayer/generated'
import { createSearchAPI } from 'next-docs-zeta/server'
import { createSearchAPI } from 'next-docs-zeta/search/server'
export const { GET } = createSearchAPI('advanced', {
indexes: allDocs.map(page => ({
+1 -1
View File
@@ -1,6 +1,6 @@
import { getPageUrl } from '@/app/source'
import { allDocs } from 'contentlayer/generated'
import { createSearchAPI } from 'next-docs-zeta/server'
import { createSearchAPI } from 'next-docs-zeta/search/server'
export const { GET } = createSearchAPI('simple', {
indexes: allDocs.map(page => ({
@@ -1,7 +1,7 @@
'use client'
import type { SearchIndex } from 'algoliasearch/lite'
import { useAlgoliaSearch } from 'next-docs-zeta/search-algolia'
import { useAlgoliaSearch } from 'next-docs-zeta/search-algolia/client'
import dynamic from 'next/dynamic'
import { createContext, useContext, type ReactNode } from 'react'
import { type SharedProps } from './search'
@@ -3,7 +3,7 @@
import { CommandGroup, CommandItem } from '@/components/ui/command'
import { I18nContext } from '@/contexts/i18n'
import { ExternalLinkIcon } from 'lucide-react'
import { useDocsSearch } from 'next-docs-zeta/search'
import { useDocsSearch } from 'next-docs-zeta/search/client'
import { useRouter } from 'next/navigation'
import { useContext, type ReactNode } from 'react'
import { SearchDialog, type SharedProps } from './search'
@@ -8,7 +8,7 @@ import {
} from '@/components/ui/command'
import { I18nContext } from '@/contexts/i18n'
import { FileTextIcon, HashIcon, TextIcon } from 'lucide-react'
import type { SortedResult } from 'next-docs-zeta/server'
import type { SortedResult } from 'next-docs-zeta/search/shared'
import { useRouter } from 'next/navigation'
import { useContext, type ReactNode } from 'react'
@@ -1,9 +1,9 @@
import type { BaseIndex } from '@/algolia'
import type { SortedResult } from '@/server/flexsearch-api'
import type { SortedResult } from '@/search/shared'
import type { Hit, SearchOptions } from '@algolia/client-search'
import type { SearchIndex } from 'algoliasearch/lite'
import { useState } from 'react'
import useSWR from 'swr'
import type { BaseIndex } from './shared'
export function groupResults(hits: Hit<BaseIndex>[]): SortedResult[] {
const grouped: SortedResult[] = []
@@ -2,6 +2,7 @@ import { randomUUID } from 'crypto'
import type { DocsPageBase } from '@/contentlayer/types'
import type { StructuredData } from '@/mdx-plugins/search-structure'
import type { SearchClient, SearchIndex } from 'algoliasearch'
import type { BaseIndex } from './shared'
type DocumentRecord = DocsPageBase & {
/**
@@ -38,26 +39,6 @@ export async function setIndexSettings(index: SearchIndex): Promise<void> {
})
}
export type BaseIndex = {
objectID: string
title: string
url: string
section?: string
/**
* The anchor id
*/
section_id?: string
/**
* The id of page, used for distinct
*/
page_id: string
content: string
}
type Section = {
section?: string
section_id?: string
@@ -0,0 +1,19 @@
export type BaseIndex = {
objectID: string
title: string
url: string
section?: string
/**
* The anchor id
*/
section_id?: string
/**
* The id of page, used for distinct
*/
page_id: string
content: string
}
@@ -1,6 +1,6 @@
import type { SortedResult } from '@/server/flexsearch-api'
import { useEffect, useState } from 'react'
import useSWR from 'swr'
import type { SortedResult } from './shared'
export function useDocsSearch<Result = SortedResult[]>(
locale?: string,
@@ -1,6 +1,7 @@
import { structure, type StructuredData } from '@/mdx-plugins/search-structure'
import FlexSearch from 'flexsearch'
import { NextResponse, type NextRequest } from 'next/server'
import type { SortedResult } from './shared'
type SearchAPI = {
GET: (
@@ -164,13 +165,6 @@ type InternalIndex = {
content: string
}
export type SortedResult = {
id: string
url: string
type: 'page' | 'heading' | 'text'
content: string
}
export function initSearchAPIAdvanced({
indexes,
language,
+6
View File
@@ -0,0 +1,6 @@
export type SortedResult = {
id: string
url: string
type: 'page' | 'heading' | 'text'
content: string
}
-1
View File
@@ -1,4 +1,3 @@
export * from './flexsearch-api'
export * from './get-toc'
export * from './types'
export * from './get-toc-sanity'
+9 -2
View File
@@ -1,6 +1,5 @@
import type { WithAs } from '@/types'
import { usePathname } from 'next/navigation'
import type { ElementType, ReactNode } from 'react'
import type { ComponentPropsWithoutRef, ElementType, ReactNode } from 'react'
import { createContext, useContext, useEffect, useState } from 'react'
import { RemoveScroll } from 'react-remove-scroll'
@@ -31,6 +30,14 @@ export function SidebarProvider(props: SidebarProviderProps) {
)
}
type WithAs<T extends ElementType, Extend = object> = Omit<
ComponentPropsWithoutRef<T>,
'as' | keyof Extend
> &
Extend & {
as?: T
}
export type SidebarTriggerProps<T extends ElementType> = WithAs<T>
export function SidebarTrigger<T extends ElementType = 'button'>({
-9
View File
@@ -1,9 +0,0 @@
import type { ComponentPropsWithoutRef, ElementType } from 'react'
export type WithAs<T extends ElementType, Extend = object> = Omit<
ComponentPropsWithoutRef<T>,
'as' | keyof Extend
> &
Extend & {
as?: T
}
+4 -10
View File
@@ -3,18 +3,12 @@ import tsconfig from './tsconfig.json'
export default defineConfig({
entry: [
'src/{server,breadcrumb,sidebar,toc,search,link,contentlayer,middleware,mdx-plugins,algolia,search-algolia}/index.{ts,tsx}',
'src/{server,breadcrumb,sidebar,toc,link,contentlayer,middleware,mdx-plugins}/index.{ts,tsx}',
'src/search/{client,server,shared}.ts',
'src/search-algolia/{client,server,shared}.ts',
'src/contentlayer/configuration.ts'
],
external: [
'@algolia/client-search',
'@types/react',
'@types/react-dom',
'algoliasearch',
'contentlayer',
'next',
'unified'
],
external: ['@algolia/client-search', 'algoliasearch', 'contentlayer', 'next'],
format: 'esm',
dts: true,
target: tsconfig.compilerOptions.target as 'es2016'