> ## Documentation Index
> Fetch the complete documentation index at: https://www.algolia.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Snippet

> Shortens attributes from hits for display in search results.

```ts Signature theme={"system"}
components.Snippet({
  attribute: string,
  hit: object,
  // Optional parameters
  highlightedTagName?: string,
  nonHighlightedTagName?: string,
  separator?: string,
  cssClasses?: object,
});
```

## About this widget

The `snippet` function returns attributes in your search results in a shorter form (a snippet).
Snippeted attributes are also [highlighted](https://www.algolia.com/doc/api-reference/widgets/highlight/js.md).

This function uses Algolia's [snippeting feature](https://www.algolia.com/doc/guides/building-search-ui/ui-and-ux-patterns/highlighting-snippeting/js.md#snippeting)
in combination with the [`hits`](https://www.algolia.com/doc/api-reference/widgets/hits/js)
or [`infiniteHits`](https://www.algolia.com/doc/api-reference/widgets/infinite-hits/js) widgets.

To determine which [attributes](#param-attribute) should be snippeted,
set them from [the Algolia dashboard](https://dashboard.algolia.com/explorer/configuration/snippeting),
[the CLI](https://www.algolia.com/doc/tools/cli/commands/objects/browse.md#highlighting-and-snippeting),
or with the API (using the [`attributesToSnippet`](https://www.algolia.com/doc/api-reference/api-parameters/attributesToSnippet) parameter):

```js JavaScript icon=code theme={"system"}
search.addWidgets([
  configure({
    attributesToSnippet: ["description"],
  }),
]);
```

With `attributesToSnippet`, you can also set the snippet's size to a specific number of words (it defaults to 10).
For example, `attributesToSnippet: ['description:5']`.

## Examples

```js JavaScript icon=code theme={"system"}
hits({
  container: "#hits",
  templates: {
    item(hit, { html, components }) {
      return html`
        <h2>${hit.name}</h2>
        <p>${components.Snippet({ attribute: "description", hit })}</p>
      `;
    },
  },
});
```

## Options

**attribute** `string` *required*

The attribute of the record to snippet.
For deeply nested objects, specify a dot-separated value like `actor.bio`.

```js JavaScript icon=code theme={"system"}
components.Snippet({
  // ...
  attribute: "description.full",
});
```

**hit** `object` *required*

Original `hit` object provided to the function.
The value is already bound to the function inside a string template,
so you don't have to provide it.
For this to work,
the provided object must have a `_snippetResult[attribute].value` property.

```js JavaScript icon=code theme={"system"}
components.Snippet({
  // ...
  hit: item,
});
```

**highlightedTagName** `string` (default: mark)

The name of the HTML element to wrap the snippeted parts of the string.

```js JavaScript icon=code theme={"system"}
components.Snippet({
  // ...
  highlightedTagName: "em",
});
```

**nonHighlightedTagName** `string` (default: span)

The HTML element that wraps the non-highlighted parts of the string.

```js JavaScript icon=code theme={"system"}
components.Snippet({
  // ...
  nonHighlightedTagName: "div",
});
```

**separator** `string` (default: , )

The character between each item when the attribute to highlight is an array.

```js JavaScript icon=code theme={"system"}
components.Snippet({
  // ...
  separator: " - ",
});
```

**cssClasses** `object` (default: \{})

The [CSS classes you can override](https://www.algolia.com/doc/guides/building-search-ui/widgets/customize-an-existing-widget/js.md#style-your-widgets):

* `root`. The component's root element.
* `highlighted`. The highlighted parts.
* `nonHighlighted`. The non-highlighted parts.
* `separator`. The separator elements between highlighted parts.

```js JavaScript icon=code theme={"system"}
components.Snippet({
  // ...
  cssClasses: {
    root: "MyCustomSnippet",
    highlighted: ["MyCustomSnippetPart", "MyCustomSnippetPart--subclass"],
  },
});
```

## HTML output

```html HTML icon=code-xml theme={"system"}
<span>This is the <mark class="ais-Snippet-highlighted">highlighted text</mark></span>
```
