Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

js-solr-highlighter

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

js-solr-highlighter - npm Package Compare versions

Comparing version 0.6.3 to 0.6.5

27

build/index.js

@@ -19,6 +19,11 @@ "use strict";

return STOP_WORDS.includes(string);
}
} // validFields are those parsed as fields. If undefined, all will be parsed as fields if they are like x:x
// highlightedFields are those among validFields whose values will be highlighted. If undefined, the values of all valid fields will be highlighted
function highlightByQuery(query, content, options = {}) {
const excludedFields = options.excludedFields === undefined ? [] : options.excludedFields;
const {
validFields,
highlightedFields
} = options;
let words = [];

@@ -55,3 +60,3 @@

if (excludedFields.includes(field)) {
if (validFields === undefined || validFields.includes(field)) {
// remove invalid "

@@ -104,12 +109,14 @@ if (res[2].startsWith('"') && !res[2].endsWith('"')) {

} else {
const highlightedFields = ['TITLE', '<implicit>'];
const allParentheses = astString.match(/"parenthesized":true/g); // not an elegant solution
const allParentheses = astString.match(/"parenthesized":true/g);
if (!highlightedFields.includes(left.field) && operator === '<implicit>' && right && right.field === '<implicit>' && !allParentheses) {
const canHighlight = field => highlightedFields === undefined ? field : highlightedFields.includes(field); // not an elegant solution
if (!canHighlight(left.field) && operator === '<implicit>' && right && right.field === '<implicit>' && !allParentheses) {
words = addTerm(words, q, true);
} else {
if (start !== 'NOT') {
if (highlightedFields.includes(left.field)) {
if (canHighlight(left.field)) {
words = addTerm(words, left.term, left.quoted);
} else if (left.left && highlightedFields.includes(left.left.field)) {
} else if (left.left && canHighlight(left.left.field)) {
words = addTerm(words, left.left.term, left.left.quoted);

@@ -120,5 +127,5 @@ }

if (operator !== 'NOT' && right) {
if (highlightedFields.includes(right.field)) {
if (canHighlight(right.field)) {
words = addTerm(words, right.term, right.quoted);
} else if ((!right.right || !highlightedFields.includes(right.right.field)) && right.left && highlightedFields.includes(right.left.field)) {
} else if ((!right.right || !canHighlight(right.right.field)) && right.left && canHighlight(right.left.field)) {
words = addTerm(words, right.left.term, right.left.quoted);

@@ -125,0 +132,0 @@ }

@@ -45,5 +45,6 @@ import TextAnnotator from 'text-annotator'

// validFields are those parsed as fields. If undefined, all will be parsed as fields if they are like x:x
// highlightedFields are those among validFields whose values will be highlighted. If undefined, the values of all valid fields will be highlighted
function highlightByQuery(query, content, options = {}) {
const excludedFields =
options.excludedFields === undefined ? [] : options.excludedFields
const { validFields, highlightedFields } = options

@@ -78,3 +79,3 @@ let words = []

const fieldVal = res[1] + ':' + res[2]
if (excludedFields.includes(field)) {
if (validFields === undefined || validFields.includes(field)) {
// remove invalid "

@@ -135,7 +136,11 @@ if (res[2].startsWith('"') && !res[2].endsWith('"')) {

} else {
const highlightedFields = ['TITLE', '<implicit>']
const allParentheses = astString.match(/"parenthesized":true/g)
const canHighlight = field =>
highlightedFields === undefined
? field
: highlightedFields.includes(field)
// not an elegant solution
if (
!highlightedFields.includes(left.field) &&
!canHighlight(left.field) &&
operator === '<implicit>' &&

@@ -149,5 +154,5 @@ right &&

if (start !== 'NOT') {
if (highlightedFields.includes(left.field)) {
if (canHighlight(left.field)) {
words = addTerm(words, left.term, left.quoted)
} else if (left.left && highlightedFields.includes(left.left.field)) {
} else if (left.left && canHighlight(left.left.field)) {
words = addTerm(words, left.left.term, left.left.quoted)

@@ -157,8 +162,8 @@ }

if (operator !== 'NOT' && right) {
if (highlightedFields.includes(right.field)) {
if (canHighlight(right.field)) {
words = addTerm(words, right.term, right.quoted)
} else if (
(!right.right || !highlightedFields.includes(right.right.field)) &&
(!right.right || !canHighlight(right.right.field)) &&
right.left &&
highlightedFields.includes(right.left.field)
canHighlight(right.left.field)
) {

@@ -165,0 +170,0 @@ words = addTerm(words, right.left.term, right.left.quoted)

import { highlightByQuery } from './index.js'
// test with options
const options = {
excludedFields: [
validFields: [
'ABBR',

@@ -153,3 +154,4 @@ 'ABSTRACT',

'text_synonyms'
]
],
highlightedFields: ['TITLE', '<implicit>']
}

@@ -421,1 +423,17 @@

})
// test('TITLE:blood AND CONTENT:cell', () => {
// const query = 'TITLE:blood AND CONTENT:cell'
// const content = 'A molecular map of lymph node blood vascular endothelium at single cell resolution'
// const received = highlightByQuery(query, content, { validFields: ['TITLE'] })
// const expected = 'A molecular map of lymph node <span id=\"highlight-0\" class=\"extra-bold\">blood</span> vascular endothelium at single cell resolution'
// expect(received).toBe(expected)
// })
test('TITLE:blood OR CONTENT:cell', () => {
const query = 'TITLE:blood OR CONTENT:cell'
const content = 'A molecular map of lymph node blood vascular endothelium at single cell resolution'
const received = highlightByQuery(query, content, { validFields: ['TITLE', 'CONTENT'], highlightedFields: ['CONTENT'] })
const expected = 'A molecular map of lymph node blood vascular endothelium at single <span id="highlight-0" class="extra-bold">cell</span> resolution'
expect(received).toBe(expected)
})
{
"name": "js-solr-highlighter",
"version": "0.6.3",
"version": "0.6.5",
"description": "A JavaScript library for highlighting HTML text based on the query in the lucene/solr query syntax",

@@ -80,3 +80,5 @@ "main": "build/index.js",

"Highlighter",
"JavaScript"
"JavaScript",
"browser",
"Node.js"
],

@@ -83,0 +85,0 @@ "author": "Zhan Huang",

# js-solr-highlighter
A JavaScript library for highlighting HTML text based on the query in the lucene/solr query syntax
## basic usage
### no options
```bash
var query = 'cancer AND blood'
var content = 'Platelet Volume Is Reduced In Metastasing Breast Cancer: Blood Profiles Reveal Significant Shifts.'
var highlightedContent = highlightByQuery(query, content)
// 'Platelet Volume Is Reduced In Metastasing Breast <span id="highlight-0" class="extra-bold">Cancer</span>: <span id="highlight-1" class="extra-bold">Blood</span> Profiles Reveal Significant Shifts.'
```
### with the validFields options that specify the fields valid in the query syntax. If not specified, all like X:X will be valid fields
```bash
var query = 'TITLE:blood AND CONTENT:cell'
var content = 'A molecular map of lymph node blood vascular endothelium at single cell resolution'
var options = { validFields: ['TITLE'] }
var highlightedContent = highlightByQuery(query, content, options)
// 'A molecular map of lymph node <span id="highlight-0" class="extra-bold">blood</span> vascular endothelium at single cell resolution'
// "cell" will not be highlighted
```
### with the highlightedFields options that specify the valid fields whose values will be highlighted. If not specified, the values of all valid fields will be highlighted
```bash
var query = 'TITLE:blood OR CONTENT:cell'
var content = 'A molecular map of lymph node blood vascular endothelium at single cell resolution'
var options = { validFields: ['TITLE', 'CONTENT'], highlightedFields: ['CONTENT'] }
var highlightedContent = highlightByQuery(query, content, options)
// 'A molecular map of lymph node blood vascular endothelium at single <span id="highlight-0" class="extra-bold">cell</span> resolution'
// "blood" will not be highlighted
```
## options
## highlighting rules
## contact
Zhan Huang<z2hm@outlook.com>
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc