Skip to content

Commit

Permalink
quote function
Browse files Browse the repository at this point in the history
  • Loading branch information
Reed Nelson committed Nov 16, 2023
1 parent 726ee93 commit a832177
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/assets/quotes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
"This is the first quote.",
"This is the second quote.",
"This is the third quote."
]
29 changes: 29 additions & 0 deletions src/lib/utils/getQuote.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// src/routes/index.ts

import fs from 'fs/promises';
import path from 'path';

const getRandomQuote = (quotes: string[]): string => {
const randomIndex = Math.floor(Math.random() * quotes.length);
return quotes[randomIndex];
};

// const getWeeklyQuote = (quotes: string[]): string => {
// const today = new Date();
// const startOfYear = new Date(today.getFullYear(), 0, 1);
// const weekNumber = Math.ceil((today - startOfYear) / (7 * 24 * 60 * 60 * 1000));
// return quotes[weekNumber % quotes.length];
// };

const getQuote = async (): Promise<string> => {
try {
const jsonFilePath = path.resolve('src/assets/quotes.json');
const jsonData = await fs.readFile(jsonFilePath, 'utf-8');
const quotes = JSON.parse(jsonData);
return getRandomQuote(quotes);
} catch (error) {
throw new Error(`Error reading or processing the JSON file: ${error}`);
}
};

export default getQuote;
8 changes: 4 additions & 4 deletions src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
import Base from "@/layouts/Base.astro";
import Background from "@/components/Background.astro";
import { markdownify } from "@/lib/utils/textConverter";
import type { Button, Feature } from "@/types";
import { getEntryBySlug } from "astro:content";
import getQuote from "@/lib/utils/getQuote";
interface Homepage {
title: string;
meta_title: string;
Expand All @@ -29,8 +28,9 @@ const { title, meta_title, banner }: Homepage = homepage.data;
<div class="my-16 text-center col-10 md:col-6">
<h1 set:html={markdownify(banner.title)} class="mb-4" />
<div class="mt-20">
<h3>QotD:</h3>
<p set:html={markdownify(banner.qotd)} class="mt-4 mb-8" />
<h3>QotW:</h3>
<p set:html={ getQuote() } class="mt-4 mb-8" />
<!-- <p set:html={markdownify(banner.qotd)} class="mt-4 mb-8" /> -->
</div>
</div>
</div>
Expand Down

0 comments on commit a832177

Please sign in to comment.