Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

Commit

Permalink
feat(snippet): add a Snippet component
Browse files Browse the repository at this point in the history
Closes: #52
  • Loading branch information
rayrutjes committed Apr 4, 2017
1 parent fa377c6 commit f62c78c
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
35 changes: 35 additions & 0 deletions packages/vue-instantsearch-snippet/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Vue InstantSearch Snippet
---

A functional component that helps you safely display snippeted attributes of your search results.

This component leverages the [snippeting feature of Algolia](https://www.algolia.com/doc/faq/searching/what-is-attributes-to-snippet-how-does-it-work/#faq-section)
but adds some sugar on top of it to prevent XSS attacks.

This component will override the following settings of your index at query time:
- highlightPreTag
- highlightPostTag

You will then be able to choose your custom highlighting tag on a per component basis for a given snippet.

## Usage

Basic usage:

```html
<ais-snippet :result="result" attribute-name="description"></ais-snippet>
```

Changing the highlighting tag:

```html
<ais-snippet :result="result" attribute-name="description" tag-name="mark"></ais-snippet>
```

**Note that the tag name has to be passed without carets.**

## CSS Classes

| ClassName | Description |
|-----------------------|-------------------|
| ais-snippet | Container class |
25 changes: 25 additions & 0 deletions packages/vue-instantsearch-snippet/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "vue-instantsearch-snippet",
"version": "0.0.0",
"files": [
"dist"
],
"main": "dist/vue-instantsearch-snippet.common.js",
"module": "dist/vue-instantsearch-snippet.esm.js",
"jsnext:main": "dist/vue-instantsearch-snippet.esm.js",
"unpkg": "dist/vue-instantsearch-snippet.js",
"scripts": {
"build": "yarn build:umd && yarn build:es && yarn build:cjs",
"build:umd": "rollup -c ../../build/rollup.umd.config.js",
"build:es": "rollup -c ../../build/rollup.es.config.js",
"build:cjs": "rollup -c ../../build/rollup.cjs.config.js"
},
"dependencies": {
"escape-html": "^1.0.3",
"instantsearch-store": "*",
"vue-instantsearch-component": "*"
},
"peerDependencies": {
"vue": "^2.2.2"
}
}
43 changes: 43 additions & 0 deletions packages/vue-instantsearch-snippet/src/Snippet.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<script>
import {HIGHLIGHT_PRE_TAG, HIGHLIGHT_POST_TAG} from 'instantsearch-store'
import escapeHtml from 'escape-html'
export default {
functional: true,
props: {
result: {
type: Object,
required: true
},
attributeName: {
type: String,
required: true
},
tagName: {
type: String,
default: 'em'
}
},
render (h, ctx) {
const result = ctx.props.result
const attributeName = ctx.props.attributeName
const tagName = ctx.props.tagName
if (!result._snippetResult || !result._snippetResult[attributeName]) {
throw new Error(`Attribute ${attributeName} has no snippet.`)
}
return h('span', {
'class': {
'ais-snippet': true
},
domProps: {
innerHTML: escapeHtml(result._snippetResult[attributeName].value)
.replace(HIGHLIGHT_PRE_TAG, `<${tagName}>`)
.replace(HIGHLIGHT_POST_TAG, `</${tagName}>`)
}
}
)
}
}
</script>
1 change: 1 addition & 0 deletions packages/vue-instantsearch-snippet/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {default} from './Snippet.vue'

0 comments on commit f62c78c

Please sign in to comment.