-
-
Notifications
You must be signed in to change notification settings - Fork 604
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Farnabaz <farnabaz@gmail.com>
- Loading branch information
1 parent
3cea513
commit f40e1a1
Showing
19 changed files
with
1,064 additions
and
916 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# Search | ||
|
||
With some simple configuration and a composable, you can add search to your Nuxt Content site. | ||
|
||
## Usage | ||
|
||
First, you need to enable search in the `nuxt.config.js` file: | ||
|
||
```js [nuxt.config.js] | ||
export default defineNuxtConfig({ | ||
content: { | ||
experimental: { | ||
search: true | ||
} | ||
} | ||
}) | ||
``` | ||
|
||
::alert{type="info"} | ||
You can use the search feature with any [Nuxt Content source](/api/configuration#sources). You can also enable or not the [document-driven feature](/guide/writing/document-driven). | ||
:: | ||
|
||
## Search | ||
|
||
You can use the `searchContent` composable to search in your `content` directory: | ||
|
||
```vue | ||
<script lang="ts" setup> | ||
const search = ref('') | ||
const results = searchContent(search) | ||
</script> | ||
<template> | ||
<main> | ||
<input v-model="search"> | ||
<pre>{{ result }} </pre> | ||
</main> | ||
</template> | ||
``` | ||
|
||
By default, the `searchContent` composable will used an indexed search. This mean that the search will be faster and the API response smaller and optimized **only usable by [`miniSearch`](https://lucaong.github.io/minisearch/)**. | ||
|
||
::alert{type="info"} | ||
Internally, the search feature, both `searchContent` and `/api/indexed-search.ts` endpoint, use [`miniSearch`](https://lucaong.github.io/minisearch/). | ||
:: | ||
|
||
## Simple Search | ||
|
||
If needed, you can disable the indexed search and use a simple search instead: | ||
|
||
```js [nuxt.config.js] | ||
export default defineNuxtConfig({ | ||
content: { | ||
experimental: { | ||
search: { | ||
indexed: false | ||
} | ||
} | ||
} | ||
}) | ||
``` | ||
|
||
Remember that the simple search is **slower** and the API response **bigger**. You can still use the `searchContent` composable which is agnostic of the search type. | ||
|
||
## Custom Search | ||
|
||
Using the simple search, aka non-indexed search, you can use the tool of your choice to search in the API response. For example, you can use [`fuse.js`](https://fusejs.io/) with the integration of [`@vueuse/integrations`](https://vueuse.org/integrations/useFuse/#usefuse): | ||
|
||
```ts | ||
export default async function customSearchContent(search: Ref<string>) { | ||
const runtimeConfig = useRuntimeConfig() | ||
const { integrity, api } = runtimeConfig.public.content | ||
|
||
const { data } = await useFetch(`${api.baseURL}/search${integrity ? '.' + integrity : ''}.json`) | ||
|
||
const { results } = useFuse(search, data) | ||
|
||
return results | ||
} | ||
``` | ||
|
||
## Configuration | ||
|
||
Please, read the [configuration section](../../4.api/3.configuration.md#search) to learn more about the search configuration. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# searchContent() | ||
|
||
Used to search in your `content` directory. | ||
|
||
```ts | ||
const input = ref('') | ||
const results = searchContent(input) | ||
``` | ||
|
||
## Parameters | ||
|
||
| Key | Type | Default | Description | | ||
|----------------------|:------------------------------------------------:|---------|----------------------------------------------------------------------------------------------| | ||
| `search` | `MaybeRefOrGetter<string>` | | The search input | | ||
| `options` | `SearchContentOptions` | `{}` | The options | | ||
| `options.miniSearch` | `MaybeRefOrGetter<MiniSearchOptions<T>>` | | The options passed to [`miniSearch`](https://lucaong.github.io/minisearch/) | | ||
| `options.fetch` | `MaybeRefOrGetter<UseFetchOptions<string \| T>>` | | The options passed to [`useFetch`](https://nuxt.com/docs/api/composables/use-fetch#usefetch) | | ||
|
||
## MiniSearchOptions | ||
|
||
You can easily define the miniSearch options by using the `defineMiniSearchOptions` composable: | ||
|
||
```ts | ||
const miniSearchOptions = defineMiniSearchOptions({ | ||
fields: ['title', 'description', 'body'] | ||
}) | ||
``` | ||
|
||
Using these options allows you to modify how the search is performed. You can change the [fields that are searched, the weight of each field, and more](https://lucaong.github.io/minisearch/#search-options). | ||
|
||
::alert{type="info"} | ||
Only available when using the simple search, aka non-indexed search. | ||
:: | ||
|
||
## UseFetchOptions | ||
|
||
An option is provided to customize the behavior of the `useFetch` composable used internally to only fetch the search content on client and lazy. This could avoid fetching the content on SSR and adding content to the `_payload.josn` file improving the performance of your app since `_payload` is loaded for hydration. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<script lang="ts" setup> | ||
const search = ref('') | ||
// Usage for non indexed search | ||
// const miniSearch = defineMiniSearchOptions({ | ||
// fields: ['title', 'content', 'titles'], | ||
// storeFields: ['title', 'content', 'titles'], | ||
// searchOptions: { | ||
// prefix: true, | ||
// fuzzy: 0.2, | ||
// boost: { | ||
// title: 4, | ||
// content: 2, | ||
// titles: 1 | ||
// } | ||
// } | ||
// }) | ||
// const result = await searchContent(search, { miniSearch }) | ||
// Usage for indexed search | ||
const result = await searchContent(search) | ||
</script> | ||
|
||
<template> | ||
<input v-model="search"> | ||
|
||
<pre>{{ result }}</pre> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Partial file | ||
|
||
This is a partial file. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
_draft: true | ||
--- | ||
|
||
# Draft file |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
--- | ||
description: JavaScript, a programming language! | ||
--- | ||
|
||
# JavaScript | ||
|
||
JavaScript (/ˈdʒɑːvəskrɪpt/), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2023, 98.7% of websites use JavaScript on the client side for webpage behavior, often incorporating third-party libraries. All major web browsers have a dedicated JavaScript engine to execute the code on users' devices. | ||
|
||
Another article. | ||
|
||
For more information, see [Wikipedia](https://en.wikipedia.org/wiki/JavaScript). | ||
|
||
## Hello World | ||
|
||
```javascript | ||
console.log("Hello World!"); | ||
``` | ||
|
||
::alert{type="info"} | ||
This is a JavaScript alert! | ||
:: | ||
|
||
## History | ||
|
||
Hello from history, this is a test! | ||
|
||
### Creation | ||
|
||
Creation is another test! | ||
|
||
### Adoption | ||
|
||
## Other usage | ||
|
||
### Server-side | ||
|
||
This is a section about server-side. | ||
|
||
#### Node.js | ||
|
||
##### Hello World | ||
|
||
### Desktop applications | ||
|
||
## See also |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
description: PHP, a programming language! | ||
--- | ||
|
||
# PHP | ||
|
||
PHP is a general-purpose scripting language geared towards web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1993 and released in 1995. The PHP reference implementation is now produced by the PHP Group. PHP was originally an abbreviation of Personal Home Page, but it now stands for the recursive initialism PHP: Hypertext Preprocessor. | ||
|
||
For more information, see [Wikipedia](https://en.wikipedia.org/wiki/PHP). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"languages": [ | ||
"javascript" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export default defineNuxtConfig({ | ||
ssr: false, | ||
|
||
extends: ['../shared'], | ||
|
||
content: { | ||
experimental: { | ||
search: { | ||
mode: 'full-text', | ||
indexed: true, | ||
filterQuery: { _draft: false, _partial: false } | ||
} | ||
} | ||
}, | ||
|
||
typescript: { | ||
includeWorkspace: true | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<script lang="ts" setup> | ||
const open = ref(false) | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<h1>Search</h1> | ||
|
||
<button @click="open = !open"> | ||
{{ open ? 'Close' : 'Open' }} Search | ||
</button> | ||
|
||
<Search v-if="open" /> | ||
|
||
<Search /> | ||
</div> | ||
</template> |
Oops, something went wrong.