Fixed internal linking not correctly filtering to published-only (#20054)

no issue

- updated search to add `status` to the search results
- added filtering to the editor's `searchLinks()` method
- prevented TaskCancellation errors being thrown from the search task being cast to a Promise
This commit is contained in:
Kevin Ansfield 2024-04-18 18:18:37 +01:00 committed by GitHub
parent a10b13916a
commit 7132619115
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 6 deletions

View File

@ -4,9 +4,9 @@ import React, {Suspense} from 'react';
import fetch from 'fetch';
import ghostPaths from 'ghost-admin/utils/ghost-paths';
import {action} from '@ember/object';
import {didCancel, task} from 'ember-concurrency';
import {inject} from 'ghost-admin/decorators/inject';
import {inject as service} from '@ember/service';
import {task} from 'ember-concurrency';
export const fileTypes = {
image: {
@ -311,14 +311,23 @@ export default class KoenigLexicalEditor extends Component {
return this.defaultLinks;
}
const results = await this.search.searchTask.perform(term);
let results = [];
try {
results = await this.search.searchTask.perform(term);
} catch (error) {
// don't surface task cancellation errors
if (!didCancel(error)) {
throw error;
}
}
// TODO: Add grouped results support to Koenig
const flattenedResults = [];
results.forEach(group => flattenedResults.push(...group.options));
// only published posts/pages have URLs
const filteredResults = flattenedResults.filter(result => result.groupName === 'Posts');
const filteredResults = flattenedResults.filter(result => (result.groupName === 'Posts' || result.groupName === 'Pages') && result.status === 'published');
return filteredResults;
};

View File

@ -18,14 +18,14 @@ export default class SearchService extends Service {
{
name: 'Posts',
model: 'post',
fields: ['id', 'url', 'title'],
fields: ['id', 'url', 'title', 'status'],
idField: 'id',
titleField: 'title'
},
{
name: 'Pages',
model: 'page',
fields: ['id', 'url', 'title'],
fields: ['id', 'url', 'title', 'status'],
idField: 'id',
titleField: 'title'
},
@ -127,7 +127,8 @@ export default class SearchService extends Service {
id: `${searchable.model}.${item[searchable.idField]}`,
url: item.url,
title: item[searchable.titleField],
groupName: searchable.name
groupName: searchable.name,
status: item.status
})
);