Skip to content

Commit

Permalink
fix: the noArticleMessage is no more displayed when there is no resul…
Browse files Browse the repository at this point in the history
…t to the search
  • Loading branch information
TayzenDev committed Nov 26, 2024
1 parent 3196735 commit e4828dd
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- noArticlesMessage is no more displayed when the user has no result on his "enter-triggered search", "No results" instead

### Removed

## 1.1.5 - 2024-11-24
Expand Down
12 changes: 12 additions & 0 deletions blog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ function getMarkdown(fileName: string, postsFolder: string) {
return content;
}

export function isPostsFolderEmpty(folder: string) {
try {
for (const _ of fs.expandGlobSync(path.join(folder, "[!_]*.md"))) {
return false;
}
return true;
} catch (e) {
console.error("Error while opening folder:", folder, ":", e);
return true;
}
}

export function getArticles(
postsFolder: string,
routeBase?: string,
Expand Down
2 changes: 2 additions & 0 deletions server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
getArticles,
getRSS,
getSitemap,
isPostsFolderEmpty,
} from "./blog.ts";
import { isDirectoryEmpty, isUrl } from "./utils.ts";
import { ArticlePage } from "./templates/article.tsx";
Expand Down Expand Up @@ -88,6 +89,7 @@ export function createServer(config: SmallblogConfig) {
locale={config.locale}
description={config.siteDescription}
noArticlesMessage={await getNoArticlesMessage(config)}
noPosts={isPostsFolderEmpty(config.postsFolder)}
bodyScript={config.customBodyScript}
headScript={config.customHeaderScript}
favicon={!!config.favicon}
Expand Down
5 changes: 5 additions & 0 deletions templates/components/articles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ export const Articles: FC<{
</div>
);
})}
{nb_posts === 0 && props.search && (
<div class="no-articles">
No results
</div>
)}

{nb_posts > itemsPerPage && (
<nav class="pagination">
Expand Down
6 changes: 4 additions & 2 deletions templates/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type IndexProps = {
locale?: string;
description?: string;
noArticlesMessage?: string;
noPosts?: boolean;
bodyScript?: string;
headScript?: string;
favicon: boolean;
Expand All @@ -36,6 +37,7 @@ export const Index: FC<IndexProps> = (props: IndexProps) => {
locale,
description,
noArticlesMessage,
noPosts,
favicon = false,
faviconLink,
customPages = [],
Expand Down Expand Up @@ -71,15 +73,15 @@ export const Index: FC<IndexProps> = (props: IndexProps) => {
value={props.search}
/>
</form>
{posts && posts.length > 0 && (
{!noPosts && (
<Articles
posts={posts}
search={props.search}
page={props.page}
itemsPerPage={props.itemsPerPage}
/>
)}
{(!posts || posts.length === 0) && (
{noPosts && (
<div
class="no-articles"
dangerouslySetInnerHTML={{ __html: noArticlesMessage || "" }}
Expand Down

0 comments on commit e4828dd

Please sign in to comment.