fix: search file collections using top level file fields (#3948)
This commit is contained in:
parent
c95437790f
commit
5d710dbd69
@ -2,6 +2,7 @@ import { resolveBackend, Backend, extractSearchFields } from '../backend';
|
|||||||
import registry from 'Lib/registry';
|
import registry from 'Lib/registry';
|
||||||
import { FOLDER } from 'Constants/collectionTypes';
|
import { FOLDER } from 'Constants/collectionTypes';
|
||||||
import { Map, List, fromJS } from 'immutable';
|
import { Map, List, fromJS } from 'immutable';
|
||||||
|
import { FILES } from '../constants/collectionTypes';
|
||||||
|
|
||||||
jest.mock('Lib/registry');
|
jest.mock('Lib/registry');
|
||||||
jest.mock('netlify-cms-lib-util');
|
jest.mock('netlify-cms-lib-util');
|
||||||
@ -555,6 +556,23 @@ describe('Backend', () => {
|
|||||||
{ path: 'pages/not-me.md', slug: 'not-me', data: { title: 'not me' } },
|
{ path: 'pages/not-me.md', slug: 'not-me', data: { title: 'not me' } },
|
||||||
];
|
];
|
||||||
|
|
||||||
|
const files = [
|
||||||
|
{
|
||||||
|
path: 'files/file1.md',
|
||||||
|
slug: 'file1',
|
||||||
|
data: {
|
||||||
|
author: 'find me by author',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: 'files/file2.md',
|
||||||
|
slug: 'file2',
|
||||||
|
data: {
|
||||||
|
other: 'find me by other',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
const implementation = {
|
const implementation = {
|
||||||
init: jest.fn(() => implementation),
|
init: jest.fn(() => implementation),
|
||||||
};
|
};
|
||||||
@ -570,6 +588,9 @@ describe('Backend', () => {
|
|||||||
if (collection.get('name') === 'pages') {
|
if (collection.get('name') === 'pages') {
|
||||||
return Promise.resolve(pages);
|
return Promise.resolve(pages);
|
||||||
}
|
}
|
||||||
|
if (collection.get('name') === 'files') {
|
||||||
|
return Promise.resolve(files);
|
||||||
|
}
|
||||||
return Promise.resolve([]);
|
return Promise.resolve([]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -609,6 +630,32 @@ describe('Backend', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should search in file collection using top level fields', async () => {
|
||||||
|
const collections = [
|
||||||
|
fromJS({
|
||||||
|
name: 'files',
|
||||||
|
files: [
|
||||||
|
{
|
||||||
|
name: 'file1',
|
||||||
|
fields: [{ name: 'author', widget: 'string' }],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'file2',
|
||||||
|
fields: [{ name: 'other', widget: 'string' }],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
type: FILES,
|
||||||
|
}),
|
||||||
|
];
|
||||||
|
|
||||||
|
expect(await backend.search(collections, 'find me by author')).toEqual({
|
||||||
|
entries: [files[0]],
|
||||||
|
});
|
||||||
|
expect(await backend.search(collections, 'find me by other')).toEqual({
|
||||||
|
entries: [files[1]],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('should query collections by title', async () => {
|
it('should query collections by title', async () => {
|
||||||
const results = await backend.query(collections[0], ['title'], 'find me by title');
|
const results = await backend.query(collections[0], ['title'], 'find me by title');
|
||||||
|
|
||||||
|
@ -36,6 +36,7 @@ import {
|
|||||||
blobToFileObj,
|
blobToFileObj,
|
||||||
asyncLock,
|
asyncLock,
|
||||||
AsyncLock,
|
AsyncLock,
|
||||||
|
UnpublishedEntry,
|
||||||
} from 'netlify-cms-lib-util';
|
} from 'netlify-cms-lib-util';
|
||||||
import { basename, join, extname, dirname } from 'path';
|
import { basename, join, extname, dirname } from 'path';
|
||||||
import { status } from './constants/publishModes';
|
import { status } from './constants/publishModes';
|
||||||
@ -54,7 +55,6 @@ import {
|
|||||||
import AssetProxy from './valueObjects/AssetProxy';
|
import AssetProxy from './valueObjects/AssetProxy';
|
||||||
import { FOLDER, FILES } from './constants/collectionTypes';
|
import { FOLDER, FILES } from './constants/collectionTypes';
|
||||||
import { selectCustomPath } from './reducers/entryDraft';
|
import { selectCustomPath } from './reducers/entryDraft';
|
||||||
import { UnpublishedEntry } from 'netlify-cms-lib-util/src/implementation';
|
|
||||||
|
|
||||||
const { extractTemplateVars, dateParsers } = stringTemplate;
|
const { extractTemplateVars, dateParsers } = stringTemplate;
|
||||||
|
|
||||||
@ -446,20 +446,33 @@ export class Backend {
|
|||||||
const summaryFields = extractTemplateVars(summary);
|
const summaryFields = extractTemplateVars(summary);
|
||||||
|
|
||||||
// TODO: pass search fields in as an argument
|
// TODO: pass search fields in as an argument
|
||||||
const searchFields = [
|
let searchFields: (string | null | undefined)[] = [];
|
||||||
selectInferedField(collection, 'title'),
|
|
||||||
selectInferedField(collection, 'shortTitle'),
|
if (collection.get('type') === FILES) {
|
||||||
selectInferedField(collection, 'author'),
|
collection.get('files')?.forEach(f => {
|
||||||
...summaryFields.map(elem => {
|
const topLevelFields = f!
|
||||||
if (dateParsers[elem]) {
|
.get('fields')
|
||||||
return selectInferedField(collection, 'date');
|
.map(f => f!.get('name'))
|
||||||
}
|
.toArray();
|
||||||
return elem;
|
searchFields = [...searchFields, ...topLevelFields];
|
||||||
}),
|
});
|
||||||
].filter(Boolean) as string[];
|
} else {
|
||||||
|
searchFields = [
|
||||||
|
selectInferedField(collection, 'title'),
|
||||||
|
selectInferedField(collection, 'shortTitle'),
|
||||||
|
selectInferedField(collection, 'author'),
|
||||||
|
...summaryFields.map(elem => {
|
||||||
|
if (dateParsers[elem]) {
|
||||||
|
return selectInferedField(collection, 'date');
|
||||||
|
}
|
||||||
|
return elem;
|
||||||
|
}),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
const filteredSearchFields = searchFields.filter(Boolean) as string[];
|
||||||
const collectionEntries = await this.listAllEntries(collection);
|
const collectionEntries = await this.listAllEntries(collection);
|
||||||
return fuzzy.filter(searchTerm, collectionEntries, {
|
return fuzzy.filter(searchTerm, collectionEntries, {
|
||||||
extract: extractSearchFields(uniq(searchFields)),
|
extract: extractSearchFields(uniq(filteredSearchFields)),
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.map(p =>
|
.map(p =>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user