& { position: Position }
+import { Result } from "@orama/orama"
+import { SectionSchema } from "../../../server/types.js"
interface SearchResultProps {
- hit: Hit
-}
-
-function snippet(hit: Hit): JSX.Element {
- const PADDING = 20
- const PADDING_MARKER = '...'
- const isBeginning = hit.position.start < PADDING
- const isEnd = hit.position.start + hit.position.length > (hit.document.sectionContent as string).length - PADDING
- const preMatch = (hit.document.sectionContent as string).substring(
- isBeginning ? 0 : hit.position.start - PADDING,
- hit.position.start
- )
- const match = (hit.document.sectionContent as string).substring(
- hit.position.start,
- hit.position.start + hit.position.length
- )
- const postMatch = (hit.document.sectionContent as string).substring(
- hit.position.start + hit.position.length,
- hit.position.start + hit.position.length + PADDING
- )
- return (
-
- {isBeginning ? '' : PADDING_MARKER}
- {preMatch}
- {match}
- {postMatch}
- {isEnd ? '' : PADDING_MARKER}
-
- )
+ hit: Result
}
export function SearchResult({ hit }: SearchResultProps): JSX.Element {
@@ -44,7 +13,8 @@ export function SearchResult({ hit }: SearchResultProps): JSX.Element {
{hit.document.sectionTitle as string}
- {snippet(hit)}
+
diff --git a/packages/plugin-docusaurus/src/server/index.ts b/packages/plugin-docusaurus/src/server/index.ts
index 43518f5b9..20c33bf19 100644
--- a/packages/plugin-docusaurus/src/server/index.ts
+++ b/packages/plugin-docusaurus/src/server/index.ts
@@ -1,7 +1,6 @@
import type { LoadedContent, LoadedVersion } from '@docusaurus/plugin-content-docs'
import type { LoadContext, Plugin } from '@docusaurus/types'
import { create, insertMultiple, save } from '@orama/orama'
-import { OramaWithHighlight, afterInsert as highlightAfterInsert } from '@orama/plugin-match-highlight'
import type { DefaultSchemaElement, NodeContent, PopulateFnContext } from '@orama/plugin-parsedoc'
import { defaultHtmlSchema, populate } from '@orama/plugin-parsedoc'
import * as githubSlugger from 'github-slugger'
@@ -13,9 +12,9 @@ import { gzip as gzipCB } from 'node:zlib'
import type { Configuration as WebpackConfiguration } from 'webpack'
import { retrieveTranslationMessages } from './translationMessages.js'
-import { INDEX_FILE, PLUGIN_NAME, PluginOptions, RawDataWithPositions, SectionSchema, schema } from './types.js'
+import { INDEX_FILE, PLUGIN_NAME, PluginOptions, SectionSchema, schema } from './types.js'
-export type { PluginData, PluginOptions, RawDataWithPositions, SectionSchema } from './types.js'
+export type { PluginData, PluginOptions, SectionSchema } from './types.js'
const gzip = promisify(gzipCB)
@@ -146,20 +145,12 @@ async function buildDevSearchData(siteDir: string, outDir: string, allContent: a
// Create the Orama database and then serialize it
const _db = await create({
- schema,
- plugins: [
- {
- name: 'highlight',
- afterInsert: highlightAfterInsert
- }
- ]
+ schema
})
- const db = _db as OramaWithHighlight
- await insertMultiple(db, documents)
+ await insertMultiple(_db, documents)
- const serialized = (await save(db)) as RawDataWithPositions
- serialized.positions = db.data.positions
+ const serialized = (await save(_db))
await writeFile(indexPath(outDir, version), await gzip(JSON.stringify(serialized)))
}
diff --git a/packages/plugin-docusaurus/src/server/types.ts b/packages/plugin-docusaurus/src/server/types.ts
index cf89302c3..48a7b5e5b 100644
--- a/packages/plugin-docusaurus/src/server/types.ts
+++ b/packages/plugin-docusaurus/src/server/types.ts
@@ -1,5 +1,5 @@
import { Result } from '@orama/orama'
-import type { Position } from '@orama/plugin-match-highlight'
+import type { Position } from '@orama/highlight'
import type { AnyDocument, AnySchema, RawData } from '@orama/orama'
diff --git a/packages/plugin-docusaurus/test/integration.ts b/packages/plugin-docusaurus/test/integration.ts
index 5f5a88055..216eefe0a 100644
--- a/packages/plugin-docusaurus/test/integration.ts
+++ b/packages/plugin-docusaurus/test/integration.ts
@@ -1,5 +1,4 @@
-import { AnyOrama, TypedDocument, create, load } from '@orama/orama'
-import { OramaWithHighlight, SearchResultWithHighlight, searchWithHighlight } from '@orama/plugin-match-highlight'
+import { create, load, search } from "@orama/orama"
import assert from 'node:assert'
import { exec, ExecException } from 'node:child_process'
import { existsSync } from 'node:fs'
@@ -28,10 +27,6 @@ async function cleanup(): Promise {
await rm(sandbox, { force: true, recursive: true })
}
-function search>(database: OramaWithHighlight, term: string): Promise> {
- return searchWithHighlight(database, { term, properties: ['sectionTitle', 'sectionContent', 'type'] })
-}
-
async function execute(command: string, cwd?: string): Promise {
const { HOME, PATH } = process.env
const env = cwd ? { HOME, PATH } : process.env
@@ -94,31 +89,29 @@ await test('generated DBs have indexed pages content', async () => {
const rawData = gunzipSync(rawCompressedData).toString('utf-8')
const data = JSON.parse(rawData)
- const _database = await create({ schema })
- const database = _database as OramaWithHighlight;
+ const database = await create({ schema })
await load(database, data)
- database.data.positions = data.positions
// Search results seem reasonable
- const indexSearchResult = await search(database, 'index')
+ const indexSearchResult = await search(database, { term: 'index', properties: ['sectionTitle', 'sectionContent', 'type'] })
assert.ok(indexSearchResult.count === 1)
assert.ok(indexSearchResult.hits[0].document.pageRoute === '/#main')
- const catSearchResult = await search(database, 'cat')
+ const catSearchResult = await search(database, { term: 'cat', properties: ['sectionTitle', 'sectionContent', 'type'] })
assert.ok(catSearchResult.count === 1)
assert.ok(catSearchResult.hits[0].document.pageRoute === '/animals_cat')
- const dogSearchResult = await search(database, 'dog')
+ const dogSearchResult = await search(database, { term: 'dog', properties: ['sectionTitle', 'sectionContent', 'type'] })
assert.ok(dogSearchResult.count === 2)
assert.ok(dogSearchResult.hits[0].document.pageRoute === '/animals_dog#dog')
- const domesticSearchResult = await search(database, 'domestic')
+ const domesticSearchResult = await search(database, { term: 'domestic', properties: ['sectionTitle', 'sectionContent', 'type'] })
assert.ok(domesticSearchResult.count === 2)
assert.ok(domesticSearchResult.hits[0].document.pageRoute === '/animals_cat')
assert.ok(domesticSearchResult.hits[1].document.pageRoute === '/animals_dog#dog')
// We do not have content about turtles
- const turtleSearchResult = await search(database, 'turtle')
+ const turtleSearchResult = await search(database, { term: 'turtle', properties: ['sectionTitle', 'sectionContent', 'type'] })
assert.ok(turtleSearchResult.count === 0)
})
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index bb74fa220..f3df32760 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -325,12 +325,12 @@ importers:
'@docusaurus/theme-common':
specifier: ^2.4.3
version: 2.4.3(@docusaurus/types@2.4.3)(@swc/core@1.3.27)(eslint@8.48.0)(react-dom@17.0.2)(react@17.0.2)(typescript@5.0.3)
+ '@orama/highlight':
+ specifier: ^0.1.2
+ version: 0.1.2
'@orama/orama':
specifier: workspace:*
version: link:../orama
- '@orama/plugin-match-highlight':
- specifier: workspace:*
- version: link:../plugin-match-highlight
'@orama/plugin-parsedoc':
specifier: workspace:*
version: link:../plugin-parsedoc
@@ -3924,6 +3924,17 @@ packages:
'@nodelib/fs.scandir': 2.1.5
fastq: 1.15.0
+ /@orama/highlight@0.1.2:
+ resolution: {integrity: sha512-B48PnxFwRRHBeEIkmKI38tZmpQDWdt6o4bch5dZaChdZh0pwPHtostMv++eVlNv3/qLtfcdLoSYHWvoN9Mp0Lw==}
+ dependencies:
+ '@orama/orama': 2.0.0-beta.4
+ dev: false
+
+ /@orama/orama@2.0.0-beta.4:
+ resolution: {integrity: sha512-hPg18sBo8YtZ1vebGwKKcvsLr3EVNR7FoqfDnj7BgGdO5eKsEDINjTGaQIJpAQRNqmSZnGW5N+soXa3Br6GkEA==}
+ engines: {node: '>= 16.0.0'}
+ dev: false
+
/@pkgr/utils@2.4.1:
resolution: {integrity: sha512-JOqwkgFEyi+OROIyq7l4Jy28h/WwhDnG/cPkXG2Z1iFbubB6jsHW1NDvmyOzTBxHr3yg68YGirmh1JUgMqa+9w==}
engines: {node: ^12.20.0 || ^14.18.0 || >=16.0.0}