From 5b4d3f71f915d079cfb0f0ed088149bf15289394 Mon Sep 17 00:00:00 2001 From: advplyr Date: Sun, 26 Jun 2022 15:46:16 -0500 Subject: [PATCH] Update:Global library search strips periods, commas and other characters when matching #750 --- client/components/cards/ItemSearchCard.vue | 17 +++-------------- client/plugins/init.client.js | 1 - server/objects/LibraryItem.js | 4 ++-- server/objects/mediaTypes/Book.js | 4 ++-- server/objects/metadata/BookMetadata.js | 8 ++++---- server/objects/metadata/PodcastMetadata.js | 4 ++-- server/utils/fileUtils.js | 2 +- server/utils/index.js | 6 ++++++ 8 files changed, 20 insertions(+), 26 deletions(-) diff --git a/client/components/cards/ItemSearchCard.vue b/client/components/cards/ItemSearchCard.vue index 83d22f17..a09b75c0 100644 --- a/client/components/cards/ItemSearchCard.vue +++ b/client/components/cards/ItemSearchCard.vue @@ -62,21 +62,10 @@ export default { matchHtml() { if (!this.matchText || !this.search) return '' if (this.matchKey === 'subtitle') return '' - var matchSplit = this.matchText.toLowerCase().split(this.search.toLowerCase().trim()) - if (matchSplit.length < 2) return '' - var html = '' - var totalLenSoFar = 0 - for (let i = 0; i < matchSplit.length - 1; i++) { - var indexOf = matchSplit[i].length - var firstPart = this.matchText.substr(totalLenSoFar, indexOf) - var actualWasThere = this.matchText.substr(totalLenSoFar + indexOf, this.search.length) - totalLenSoFar += indexOf + this.search.length - - html += `${firstPart}${actualWasThere}` - } - var lastPart = this.matchText.substr(totalLenSoFar) - html += lastPart + // This used to highlight the part of the search found + // but with removing commas periods etc this is no longer plausible + const html = this.matchText if (this.matchKey === 'tags') return `

Tags: ${html}

` if (this.matchKey === 'authors') return `by ${html}` diff --git a/client/plugins/init.client.js b/client/plugins/init.client.js index 4e39ef51..a2c9ceb6 100644 --- a/client/plugins/init.client.js +++ b/client/plugins/init.client.js @@ -204,7 +204,6 @@ Vue.prototype.$copyToClipboard = (str, ctx) => { }) } - function xmlToJson(xml) { const json = {}; for (const res of xml.matchAll(/(?:<(\w*)(?:\s[^>]*)*>)((?:(?!<\1).)*)(?:<\/\1>)|<(\w*)(?:\s*)*\/>/gm)) { diff --git a/server/objects/LibraryItem.js b/server/objects/LibraryItem.js index f125be29..7e371ac9 100644 --- a/server/objects/LibraryItem.js +++ b/server/objects/LibraryItem.js @@ -7,7 +7,7 @@ const LibraryFile = require('./files/LibraryFile') const Book = require('./mediaTypes/Book') const Podcast = require('./mediaTypes/Podcast') const Video = require('./mediaTypes/Video') -const { areEquivalent, copyValue, getId } = require('../utils/index') +const { areEquivalent, copyValue, getId, cleanStringForSearch } = require('../utils/index') class LibraryItem { constructor(libraryItem = null) { @@ -451,7 +451,7 @@ class LibraryItem { } searchQuery(query) { - query = query.toLowerCase() + query = cleanStringForSearch(query) return this.media.searchQuery(query) } diff --git a/server/objects/mediaTypes/Book.js b/server/objects/mediaTypes/Book.js index e84980d0..ec4e88fc 100644 --- a/server/objects/mediaTypes/Book.js +++ b/server/objects/mediaTypes/Book.js @@ -1,7 +1,7 @@ const Path = require('path') const Logger = require('../../Logger') const BookMetadata = require('../metadata/BookMetadata') -const { areEquivalent, copyValue } = require('../../utils/index') +const { areEquivalent, copyValue, cleanStringForSearch } = require('../../utils/index') const { parseOpfMetadataXML } = require('../../utils/parsers/parseOpfMetadata') const { overdriveMediaMarkersExist, parseOverdriveMediaMarkersAsChapters } = require('../../utils/parsers/parseOverdriveMediaMarkers') const abmetadataGenerator = require('../../utils/abmetadataGenerator') @@ -304,7 +304,7 @@ class Book { searchQuery(query) { var payload = { - tags: this.tags.filter(t => t.toLowerCase().includes(query)), + tags: this.tags.filter(t => cleanStringForSearch(t).includes(query)), series: this.metadata.searchSeries(query), authors: this.metadata.searchAuthors(query), matchKey: null, diff --git a/server/objects/metadata/BookMetadata.js b/server/objects/metadata/BookMetadata.js index 4cedd1cb..19ee3aa6 100644 --- a/server/objects/metadata/BookMetadata.js +++ b/server/objects/metadata/BookMetadata.js @@ -1,5 +1,5 @@ const Logger = require('../../Logger') -const { areEquivalent, copyValue } = require('../../utils/index') +const { areEquivalent, copyValue, cleanStringForSearch } = require('../../utils/index') const parseNameString = require('../../utils/parsers/parseNameString') class BookMetadata { constructor(metadata) { @@ -342,15 +342,15 @@ class BookMetadata { } searchSeries(query) { - return this.series.filter(se => se.name.toLowerCase().includes(query)) + return this.series.filter(se => cleanStringForSearch(se.name).includes(query)) } searchAuthors(query) { - return this.authors.filter(se => se.name.toLowerCase().includes(query)) + return this.authors.filter(au => cleanStringForSearch(au.name).includes(query)) } searchQuery(query) { // Returns key if match is found var keysToCheck = ['title', 'asin', 'isbn'] for (var key of keysToCheck) { - if (this[key] && this[key].toLowerCase().includes(query)) { + if (this[key] && cleanStringForSearch(String(this[key])).includes(query)) { return { matchKey: key, matchText: this[key] diff --git a/server/objects/metadata/PodcastMetadata.js b/server/objects/metadata/PodcastMetadata.js index a645d7cb..3bbd7f2c 100644 --- a/server/objects/metadata/PodcastMetadata.js +++ b/server/objects/metadata/PodcastMetadata.js @@ -1,5 +1,5 @@ const Logger = require('../../Logger') -const { areEquivalent, copyValue } = require('../../utils/index') +const { areEquivalent, copyValue, cleanStringForSearch } = require('../../utils/index') class PodcastMetadata { constructor(metadata) { @@ -94,7 +94,7 @@ class PodcastMetadata { searchQuery(query) { // Returns key if match is found var keysToCheck = ['title', 'author', 'itunesId', 'itunesArtistId'] for (var key of keysToCheck) { - if (this[key] && String(this[key]).toLowerCase().includes(query)) { + if (this[key] && cleanStringForSearch(String(this[key])).includes(query)) { return { matchKey: key, matchText: this[key] diff --git a/server/utils/fileUtils.js b/server/utils/fileUtils.js index b2b0a85b..d76adb70 100644 --- a/server/utils/fileUtils.js +++ b/server/utils/fileUtils.js @@ -210,4 +210,4 @@ module.exports.sanitizeFilename = (filename, colonReplacement = ' - ') => { } return sanitized -} \ No newline at end of file +} diff --git a/server/utils/index.js b/server/utils/index.js index c48ce28a..9ff14198 100644 --- a/server/utils/index.js +++ b/server/utils/index.js @@ -129,4 +129,10 @@ module.exports.encodeUriPath = (path) => { module.exports.toNumber = (val, fallback = 0) => { if (isNaN(val) || val === null) return fallback return Number(val) +} + +module.exports.cleanStringForSearch = (str) => { + if (!str) return '' + // Remove ' . ` " , + return str.toLowerCase().replace(/[\'\.\`\",]/g, '').trim() } \ No newline at end of file