mirror of
https://github.com/datahub-project/datahub.git
synced 2025-11-03 12:16:10 +00:00
Remove bold tags from search results
This commit is contained in:
parent
1bc19975ca
commit
3c200fa9c0
@ -3,6 +3,7 @@ import { isBlank } from '@ember/utils';
|
||||
import $ from 'jquery';
|
||||
import AuthenticatedRouteMixin from 'ember-simple-auth/mixins/authenticated-route-mixin';
|
||||
import buildUrl from 'wherehows-web/utils/build-url';
|
||||
import highlightResults from 'wherehows-web/utils/api/datasets/highlight-results';
|
||||
|
||||
const queryParams = ['keyword', 'category', 'page', 'source'];
|
||||
// TODO: DSS-6581 Create URL retrieval module
|
||||
@ -49,14 +50,7 @@ export default Route.extend(AuthenticatedRouteMixin, {
|
||||
if (status === 'ok') {
|
||||
const { keywords, data } = result;
|
||||
|
||||
data.forEach((datum, index, data) => {
|
||||
const { schema } = datum;
|
||||
if (schema) {
|
||||
datum.originalSchema = schema;
|
||||
// TODO: DSS-6122 refactor global reference and function
|
||||
window.highlightResults(data, index, keywords);
|
||||
}
|
||||
});
|
||||
highlightResults(data, keywords);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
55
wherehows-web/app/utils/api/datasets/highlight-results.ts
Normal file
55
wherehows-web/app/utils/api/datasets/highlight-results.ts
Normal file
@ -0,0 +1,55 @@
|
||||
import { IDataset } from 'wherehows-web/typings/api/datasets/dataset';
|
||||
|
||||
const contentLimit = 500;
|
||||
|
||||
interface ISearchDataset extends IDataset {
|
||||
originalSchema?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Refactored from legacy code. This function's purpose is to modify the given Dataset Data in place
|
||||
* and shorten the content of its schema to a specified limit number of characters as well as ensure the
|
||||
* keyword searched for appears in the preview for the schema in the search entry
|
||||
* @param {Array<IDataset>} datum - given dataset data
|
||||
* @param keyword - keyword
|
||||
*/
|
||||
const highlightResult = (datum: ISearchDataset, keyword: string): void => {
|
||||
const content = datum.schema;
|
||||
|
||||
if (content === undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
const contentLength = content.length;
|
||||
|
||||
if (keyword) {
|
||||
keyword = keyword.replace(/[+-]/g, '');
|
||||
}
|
||||
|
||||
const schemaKeywordIdx = content.indexOf(keyword);
|
||||
|
||||
let newContent: string = content;
|
||||
|
||||
if (contentLength > contentLimit) {
|
||||
// We create a substring to shorten the content, either taking the whole string if it's short enough, or taking
|
||||
// a piece of the string starting with the keyword the user searched for
|
||||
newContent =
|
||||
contentLength - 1 < contentLimit
|
||||
? content.substring(contentLength - contentLimit, contentLength)
|
||||
: content.substring(schemaKeywordIdx, contentLimit + schemaKeywordIdx);
|
||||
}
|
||||
// TODO: originalSchema may not be needed, but it appears to be a property accessed by a number of legacy functions
|
||||
datum.originalSchema = content;
|
||||
datum.schema = newContent;
|
||||
};
|
||||
|
||||
/**
|
||||
* Refactored from legacy code. This utility function's purpose is to modify the dataset response for all datasets
|
||||
* resulting from a search. It's purpose is to shorten the "search preview" content on the page and bring attention
|
||||
* to the keyword the user used in the schema
|
||||
* @param {Array<IDataset>} data - given dataset data
|
||||
* @param {string} keyword - keyword
|
||||
*/
|
||||
export default function apiDatasetsHighlightResults(data: Array<ISearchDataset>, keyword: string) {
|
||||
data.forEach(datum => highlightResult(datum, keyword));
|
||||
}
|
||||
11
wherehows-web/mirage/data/schema.js
Normal file
11
wherehows-web/mirage/data/schema.js
Normal file
@ -0,0 +1,11 @@
|
||||
/**
|
||||
* Provides a constant long string to test against for dataset schema
|
||||
*/
|
||||
export const testSchemaA =
|
||||
'It is a period of civil war. Rebel spaceships, striking from a hidden base, have won their first victory against the evil Galactic Empire.' +
|
||||
"During the battle, Rebel spies managed to steal secret plans to the Empire's ultimate weapon, the DEATH STAR, an armored space station with enough power to destroy " +
|
||||
"an entire planet.Pursued by the Empire's sinister agents, Princess Leia races home aboard her starship, custodian of the stolen plans that can save her people and " +
|
||||
'restore freedom to the galaxy... It is a dark time for the Rebellion. Although the Death Star has been destroyed, Imperial troops have driven the Rebel forces from ' +
|
||||
'their hidden base and pursued them across the galaxy. Evading the dreaded Imperial Starfleet, a group of freedom fighters led by Luke Skywalker has established a new ' +
|
||||
'secret base on the remote ice world of Hoth. The evil lord Darth Vader, obsessed with finding young Skywalker, has dispatched thousands of remote probes into the far ' +
|
||||
'reaches of space...';
|
||||
@ -1,4 +1,5 @@
|
||||
import { Factory, faker } from 'ember-cli-mirage';
|
||||
import { Factory, faker, trait } from 'ember-cli-mirage';
|
||||
import { testSchemaA } from '../data/schema';
|
||||
|
||||
export default Factory.extend({
|
||||
id: faker.random.number({ min: 10000, max: 20000 }),
|
||||
@ -30,5 +31,14 @@ export default Factory.extend({
|
||||
source: 'Hive',
|
||||
urn: faker.internet.url(),
|
||||
watchId: 0,
|
||||
owners: () => []
|
||||
owners: () => [],
|
||||
|
||||
forUnitTests: trait({
|
||||
id(id) {
|
||||
return id;
|
||||
},
|
||||
schema(id) {
|
||||
return id === 0 ? testSchemaA : 'abcd';
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
5
wherehows-web/tests/helpers/setup-mirage.js
Normal file
5
wherehows-web/tests/helpers/setup-mirage.js
Normal file
@ -0,0 +1,5 @@
|
||||
import mirageInitializer from 'wherehows-web/initializers/ember-cli-mirage';
|
||||
|
||||
export default function startMirage(container) {
|
||||
mirageInitializer.initialize(container);
|
||||
}
|
||||
@ -0,0 +1,37 @@
|
||||
import apiDatasetsHighlightResults from 'wherehows-web/utils/api/datasets/highlight-results';
|
||||
import { module, test } from 'qunit';
|
||||
import startMirage from 'wherehows-web/tests/helpers/setup-mirage';
|
||||
import { testSchemaA } from 'wherehows-web/mirage/data/schema';
|
||||
|
||||
module('Unit | Utility | api/datasets/highlight results', {
|
||||
beforeEach() {
|
||||
startMirage(this.container);
|
||||
},
|
||||
afterEach() {
|
||||
window.server.shutdown();
|
||||
}
|
||||
});
|
||||
|
||||
test('it works base case', function(assert) {
|
||||
const server = window.server;
|
||||
const model = server.createList('dataset', 1, 'forUnitTests');
|
||||
const dataset = model[0];
|
||||
|
||||
let result = apiDatasetsHighlightResults([]);
|
||||
assert.ok(!result, 'Returns without error for nothing case');
|
||||
|
||||
result = apiDatasetsHighlightResults(model);
|
||||
assert.equal(dataset.id, 0, 'Sanity check: Created model successfully');
|
||||
assert.equal(dataset.originalSchema, testSchemaA, 'Preserves original schema properly');
|
||||
assert.equal(dataset.schema, testSchemaA.slice(0, 499), 'Partial schema from beginning if no keyword found');
|
||||
});
|
||||
|
||||
test('it works for keyword cases', function(assert) {
|
||||
const server = window.server;
|
||||
const model = server.createList('dataset', 1, 'forUnitTests');
|
||||
const dataset = model[0];
|
||||
|
||||
let result = apiDatasetsHighlightResults(model, 'Rebel');
|
||||
assert.equal(dataset.id, 0, 'Sanity check: Created model successfully again');
|
||||
assert.equal(dataset.schema, testSchemaA.slice(29, 529), 'Partial schema starts from keyword index');
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user