0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-27 22:49:56 -05:00

Added redirect for post on search click

- allows click on post result to open it on click when searched
- adds `slug` to content api for data
This commit is contained in:
Rishabh 2022-07-05 17:34:31 +02:00
parent 31b64c032e
commit 34a3e4ade7
4 changed files with 15 additions and 6 deletions

View file

@ -30,6 +30,7 @@ export default class App extends React.Component {
<AppContext.Provider value={{
page: 'search',
showPopup: true,
siteUrl: this.props.siteUrl,
searchIndex: this.state.searchIndex,
indexComplete: this.state.indexComplete,
searchValue: this.state.searchValue,

View file

@ -190,9 +190,14 @@ function TagResults({tags}) {
);
}
function PostListItem({title, excerpt}) {
function PostListItem({title, excerpt, slug}) {
const {siteUrl} = useContext(AppContext);
return (
<div className='py-3 -mx-7 px-7 hover:bg-neutral-100 cursor-pointer'>
<div className='py-3 -mx-7 px-7 hover:bg-neutral-100 cursor-pointer' onClick={() => {
if (slug) {
window.location.href = `${siteUrl}${slug}`;
}
}}>
<h2 className='text-[1.65rem] font-medium leading-tight text-neutral-900'>{title}</h2>
<p className='text-neutral-400 leading-normal text-sm mt-0 mb-0 truncate'>{excerpt}</p>
</div>
@ -210,6 +215,7 @@ function PostResults({posts}) {
key={d.title}
title={d.title}
excerpt={d.excerpt}
slug={d.slug}
/>
);
});
@ -288,7 +294,8 @@ function SearchResultBox() {
return {
id: d?.id,
excerpt: d?.excerpt,
title: d?.title
title: d?.title,
slug: d?.slug
};
});
console.log(filteredPosts);

View file

@ -35,7 +35,7 @@ function init() {
setup({siteUrl});
ReactDOM.render(
<React.StrictMode>
<App apiKey={apiKey} apiUrl={apiUrl} />
<App siteUrl={siteUrl} apiKey={apiKey} apiUrl={apiUrl} />
</React.StrictMode>,
document.getElementById(ROOT_DIV_ID)
);

View file

@ -17,7 +17,8 @@ export default class SearchIndex {
this.index.addDoc({
id: post.id,
title: post.title,
excerpt: post.excerpt
excerpt: post.excerpt,
slug: post.slug
});
});
@ -29,7 +30,7 @@ export default class SearchIndex {
// remove default stop words to search of *any* word
elasticlunr.clearStopWords();
const url = `${this.apiUrl}/posts/?key=${this.apiKey}&limit=all&fields=id,title,excerpt,url,updated_at,visibility&order=updated_at%20desc&formats=plaintext`;
const url = `${this.apiUrl}/posts/?key=${this.apiKey}&limit=all&fields=id,slug,title,excerpt,url,updated_at,visibility&order=updated_at%20desc&formats=plaintext`;
const indexDump = JSON.parse(this.storage.getItem('ease_search_index'));