Compare commits

...

23 Commits

Author SHA1 Message Date
Max Richter
7dda2dd60d feat: add kmenu icon for mobile 2025-11-12 16:12:57 +01:00
Max Richter
7ad08daf80 fix: make recipe crawling work 2025-11-12 15:41:30 +01:00
Max Richter
92126882b6 feat: correctly size search result items 2025-11-12 13:35:39 +01:00
Max Richter
655fc648e6 feat: fallback to unsplash cover when article contains no image 2025-11-09 23:52:53 +01:00
Max Richter
6c6b69a46a fix: make search work 2025-11-07 18:58:23 +01:00
Max Richter
97c5b7f93c fix: better position covers 2025-11-07 18:26:31 +01:00
Max Richter
bf3483019c fix: make unseen search work 2025-11-07 18:18:39 +01:00
Max Richter
5502c17c28 fix: correctly display noise gradient 2025-11-07 17:54:56 +01:00
Max Richter
581f1c1926 fix: make search usable again 2025-11-05 00:42:53 +01:00
Max Richter
7664abe089 fix: actually write image 2025-11-04 16:04:51 +01:00
Max Richter
bed7d1a11b feat: better log article creations 2025-11-04 13:57:32 +01:00
Max Richter
56a104c8b9 feat: use better names for md files 2025-11-04 13:26:49 +01:00
Max Richter
3103ed19fb fix: correctly fetch marka pi in background 2025-11-04 12:58:26 +01:00
Max Richter
fea9b69d4d feat: cache marka api responses 2025-11-04 12:09:17 +01:00
Max Richter
bb4e895770 fix: display correct name for series 2025-11-03 01:06:27 +01:00
Max Richter
28e9de4dc8 fix: make sure to only decode thumbhash once 2025-11-03 00:57:44 +01:00
Max Richter
ebb897dca4 fix: correctly embed styles.css 2025-11-03 00:46:49 +01:00
Max Richter
696082250d fix: soo many lint errors 2025-11-03 00:03:27 +01:00
Max Richter
c13420c3ab chore: deno fmt 2025-11-02 21:58:02 +01:00
Max Richter
21124dfe00 fix: remove unused imports 2025-11-02 21:56:26 +01:00
Max Richter
928782c453 fix: accessing nonexistant variable 2025-11-02 21:14:41 +01:00
Max Richter
098da12ac4 fix: make recipe ingredinets interactive 2025-11-02 20:01:01 +01:00
Max Richter
d4a7763b15 fix: accessing nonexistant variable 2025-11-02 19:19:57 +01:00
113 changed files with 1780 additions and 1193 deletions

View File

@@ -15,7 +15,7 @@ COPY . .
ENV DATA_DIR=/app/data ENV DATA_DIR=/app/data
RUN mkdir -p $DATA_DIR && \ RUN mkdir -p $DATA_DIR && \
deno install --allow-import --allow-ffi --allow-scripts=npm:sharp@0.33.5-rc.1 -e main.ts &&\ deno install --allow-import --allow-ffi --allow-scripts=npm:sharp -e main.ts &&\
sed -i -e 's/"deno"/"no-deno"/' node_modules/@libsql/client/package.json &&\ sed -i -e 's/"deno"/"no-deno"/' node_modules/@libsql/client/package.json &&\
deno task build deno task build

View File

@@ -1,9 +1,9 @@
import { isYoutubeLink } from "@lib/string.ts"; import { isYoutubeLink } from "@lib/string.ts";
import { IconBrandYoutube } from "@components/icons.tsx"; import { IconBrandYoutube } from "@components/icons.tsx";
import { GenericResource } from "@lib/types.ts";
import { SmallRating } from "@components/Rating.tsx"; import { SmallRating } from "@components/Rating.tsx";
import { Link } from "@islands/Link.tsx"; import { Link } from "@islands/Link.tsx";
import { parseRating } from "@lib/helpers.ts"; import { parseRating } from "@lib/helpers.ts";
import { GenericResource, getNameOfResource } from "@lib/marka/schema.ts";
export function Card( export function Card(
{ {
@@ -101,14 +101,16 @@ export function ResourceCard(
? `/api/images?image=${img}&width=200&height=200` ? `/api/images?image=${img}&width=200&height=200`
: "/placeholder.svg"; : "/placeholder.svg";
const rating = res.content.reviewRating?.ratingValue
? parseRating(res.content.reviewRating.ratingValue)
: undefined;
return ( return (
<Card <Card
title={res.content?.name || res.content?.itemReviewed?.name || title={getNameOfResource(res)}
res.content?.headline ||
res?.name}
backgroundColor={res.image?.average} backgroundColor={res.image?.average}
thumbhash={res.image?.blurhash} thumbhash={res.image?.thumbhash}
rating={parseRating(res.content?.reviewRating?.ratingValue)} rating={rating}
image={imageUrl} image={imageUrl}
link={`/${sublink}/${res.name.replace(/\.md$/g, "")}`} link={`/${sublink}/${res.name.replace(/\.md$/g, "")}`}
/> />

View File

@@ -10,5 +10,5 @@ export const Emoji = (props: { class?: string; name: string }) => {
/> />
) )
: <span>{props.name}</span> : <span>{props.name}</span>
: <></>; : null;
}; };

View File

@@ -1,5 +1,5 @@
import { asset } from "$fresh/runtime.ts"; import { asset } from "$fresh/runtime.ts";
import * as CSS from "https://esm.sh/csstype@3.1.2"; import * as CSS from "csstype";
interface ResponsiveAttributes { interface ResponsiveAttributes {
srcset: string; srcset: string;
@@ -47,6 +47,11 @@ const Image = (
"/api/images", "/api/images",
); );
const hasDimensions = typeof props.width === "number" &&
typeof props.height === "number";
const sizes = hasDimensions ? "" : responsiveAttributes.sizes;
const srcset = hasDimensions ? "" : responsiveAttributes.srcset;
return ( return (
<span <span
style={{ style={{
@@ -62,9 +67,10 @@ const Image = (
loading="lazy" loading="lazy"
alt={props.alt} alt={props.alt}
style={props.style} style={props.style}
srcset={responsiveAttributes.srcset} sizes={sizes}
sizes={responsiveAttributes.sizes} srcset={srcset}
src={`/api/images?image=${asset(props.src)}${props.width ? `&width=${props.width}` : "" src={`/api/images?image=${asset(props.src)}${
props.width ? `&width=${props.width}` : ""
}${props.height ? `&height=${props.height}` : ""}`} }${props.height ? `&height=${props.height}` : ""}`}
width={props.width} width={props.width}
height={props.height} height={props.height}

View File

@@ -1,5 +1,6 @@
import { GenericResource } from "@lib/types.ts";
import { Head } from "$fresh/runtime.ts"; import { Head } from "$fresh/runtime.ts";
import { GenericResource, getNameOfResource } from "@lib/marka/schema.ts";
import { formatDate } from "@lib/string.ts";
function generateJsonLd(resource: GenericResource): string { function generateJsonLd(resource: GenericResource): string {
const imageUrl = resource.content?.image const imageUrl = resource.content?.image
@@ -8,34 +9,34 @@ function generateJsonLd(resource: GenericResource): string {
const baseSchema: Record<string, unknown> = { const baseSchema: Record<string, unknown> = {
"@context": "https://schema.org", "@context": "https://schema.org",
"@type": resource.content?._type, // Converts type to PascalCase "@type": resource.content?._type,
name: resource.name, name: resource.name,
description: resource.content || resource.meta?.average || "", description: resource.content || "",
keywords: resource.tags?.join(", ") || "", keywords: resource.content.keywords?.join(", ") || "",
image: imageUrl, image: imageUrl,
}; };
if (resource.meta?.author) { if (resource.content?.author) {
baseSchema.author = { baseSchema.author = {
"@type": "Person", "@type": "Person",
name: resource.meta.author, name: resource.content.author,
}; };
} }
if (resource.meta?.date) { if (resource.content?.datePublished) {
try { try {
baseSchema.datePublished = new Date(resource.meta.date).toISOString(); baseSchema.datePublished = formatDate(
resource.content.datePublished,
);
} catch (_) { } catch (_) {
// Ignore invalid date // Ignore invalid date
} }
} }
if (resource.meta?.rating) { if (resource.content?.reviewRating) {
baseSchema.aggregateRating = { baseSchema.reviewRating = {
"@type": "AggregateRating", "@type": "Rating",
ratingValue: resource.meta.rating, ...resource.content.reviewRating,
ratingCount: 1,
bestRating: 5, // Assuming a scale of 1 to 10
}; };
} }
@@ -51,7 +52,7 @@ export function MetaTags({ resource }: { resource: GenericResource }) {
return ( return (
<> <>
<Head> <Head>
<meta property="og:title" content={resource.content?.name} /> <meta property="og:title" content={getNameOfResource(resource)} />
<meta property="og:type" content={resource.content?._type} /> <meta property="og:type" content={resource.content?._type} />
<meta <meta
property="og:image" property="og:image"

View File

@@ -20,7 +20,8 @@ function Wrapper(
) { ) {
return ( return (
<div <div
class={`flex justify-between flex-col relative w-full ${image ? "min-h-[400px]" : "min-h-[200px]" class={`flex justify-between flex-col relative w-full ${
image ? "min-h-[400px]" : "min-h-[200px]"
} rounded-3xl overflow-hidden`} } rounded-3xl overflow-hidden`}
> >
<HeroContext.Provider value={{ image }}> <HeroContext.Provider value={{ image }}>
@@ -51,7 +52,8 @@ function Title(
return ( return (
<OuterTag <OuterTag
href={link} href={link}
class={`${ctx.image ? "noisy-gradient" : "" class={`${
ctx.image ? "noisy-gradient" : ""
} after:opacity-90 flex gap-4 items-center ${ctx.image ? "pt-12" : ""}`} } after:opacity-90 flex gap-4 items-center ${ctx.image ? "pt-12" : ""}`}
> >
<h2 <h2
@@ -81,7 +83,8 @@ function EditLink({ href }: { href: string }) {
const ctx = useContext(HeroContext); const ctx = useContext(HeroContext);
return ( return (
<a <a
class={`px-4 py-2 ${ctx.image ? "bg-gray-300 text-gray-800" : "text-gray-200" class={`px-4 py-2 ${
ctx.image ? "bg-gray-300 text-gray-800" : "text-gray-200"
} rounded-lg flex gap-1 items-center`} } rounded-lg flex gap-1 items-center`}
href={href} href={href}
> >
@@ -101,7 +104,7 @@ function Header({ children }: { children: ComponentChildren }) {
function Subline( function Subline(
{ entries, children }: { { entries, children }: {
children?: ComponentChildren; children?: ComponentChildren;
entries: (string | { href: string; title: string })[]; entries: (string | undefined | { href: string; title: string })[];
}, },
) { ) {
const ctx = useContext(HeroContext); const ctx = useContext(HeroContext);
@@ -114,10 +117,11 @@ function Subline(
{entries.filter((s) => {entries.filter((s) =>
s && (typeof s === "string" ? s?.length > 1 : true) s && (typeof s === "string" ? s?.length > 1 : true)
).map((s) => { ).map((s) => {
if (!s) return;
if (typeof s === "string") { if (typeof s === "string") {
return <span>{s}</span>; return <span key={s}>{s}</span>;
} else { } else {
return <a href={s.href}>{s.title}</a>; return <a key={s.href} href={s.href}>{s.title}</a>;
} }
})} })}
</div> </div>

View File

@@ -1,5 +1,5 @@
import { IconStar, IconStarFilled } from "@components/icons.tsx"; import { IconStar, IconStarFilled } from "@components/icons.tsx";
import { useSignal } from "@preact/signals"; import { Signal, useSignal } from "@preact/signals";
import { useState } from "preact/hooks"; import { useState } from "preact/hooks";
export const SmallRating = ( export const SmallRating = (
@@ -24,27 +24,30 @@ export const SmallRating = (
}; };
export const Rating = ( export const Rating = (
props: { max?: number; rating: number }, { max, rating = useSignal(0) }: {
max?: number;
rating: Signal<number | undefined>;
},
) => { ) => {
const [rating, setRating] = useState(props.rating);
const [hover, setHover] = useState(0); const [hover, setHover] = useState(0);
const max = useSignal(props.max || 5);
const ratingValue = rating.value || 0;
return ( return (
<div <div
class="flex items-center gap-2 px-5 rounded-2xl bg-gray-200 z-10" class="flex items-center gap-2 px-5 rounded-2xl bg-gray-200 z-10 h-full"
style={{ color: "var(--foreground)", background: "var(--background)" }} style={{ color: "var(--foreground)", background: "var(--background)" }}
> >
{Array.from({ length: max.value }).map((_, i) => { {Array.from({ length: max || 5 }).map((_, i) => {
return ( return (
<span <span
class={`cursor-pointer opacity-${ class={`cursor-pointer opacity-${
(i + 1) <= rating ? 100 : (i + 1) <= hover ? 20 : 100 (i + 1) <= ratingValue ? 100 : (i + 1) <= hover ? 20 : 100
}`} }`}
onMouseOver={() => setHover(i + 1)} onMouseOver={() => setHover(i + 1)}
onClick={() => setRating(i + 1)} onClick={() => (rating.value = i + 1)}
> >
{(i + 1) <= rating || (i + 1) <= hover {(i + 1) <= ratingValue || (i + 1) <= hover
? <IconStarFilled class="w-4 h-4" /> ? <IconStarFilled class="w-4 h-4" />
: <IconStar class="w-4 h-4" />} : <IconStar class="w-4 h-4" />}
</span> </span>

View File

@@ -17,3 +17,5 @@ export { default as IconSearch } from "https://deno.land/x/tabler_icons_tsx@0.0.
export { default as IconGhost } from "https://deno.land/x/tabler_icons_tsx@0.0.3/tsx/ghost.tsx"; export { default as IconGhost } from "https://deno.land/x/tabler_icons_tsx@0.0.3/tsx/ghost.tsx";
export { default as IconBrandYoutube } from "https://deno.land/x/tabler_icons_tsx@0.0.3/tsx/brand-youtube.tsx"; export { default as IconBrandYoutube } from "https://deno.land/x/tabler_icons_tsx@0.0.3/tsx/brand-youtube.tsx";
export { default as IconWand } from "https://deno.land/x/tabler_icons_tsx@0.0.5/tsx/wand.tsx"; export { default as IconWand } from "https://deno.land/x/tabler_icons_tsx@0.0.5/tsx/wand.tsx";
export { default as IconMenu2 } from "https://deno.land/x/tabler_icons_tsx@0.0.5/tsx/menu-2.tsx";
export { default as IconAlertCircle } from "https://deno.land/x/tabler_icons_tsx@0.0.5/tsx/alert-circle.tsx";

View File

@@ -1,6 +1,6 @@
import { ComponentChildren } from "preact"; import { ComponentChildren } from "preact";
import Search from "@islands/Search.tsx"; import Search from "@islands/Search.tsx";
import { GenericResource } from "@lib/types.ts"; import { GenericResource } from "@lib/marka/schema.ts";
export type Props = { export type Props = {
children: ComponentChildren; children: ComponentChildren;
@@ -12,21 +12,29 @@ export type Props = {
searchResults?: GenericResource[]; searchResults?: GenericResource[];
}; };
function getQFromUrl(u: string | URL): string | null {
try {
const _u = typeof u === "string" ? new URL(u) : u;
return _u?.searchParams.get("q");
} catch (_e) {
return null;
}
}
export const MainLayout = ( export const MainLayout = (
{ children, url, context, searchResults }: Props, { children, url, context, searchResults }: Props,
) => { ) => {
const _url = typeof url === "string" ? new URL(url) : url; const q = getQFromUrl(url);
const hasSearch = _url?.search?.includes("q=");
if (hasSearch) { if (typeof q === "string") {
return ( return (
<Search <Search
q={_url.searchParams.get("q")}
{...context} {...context}
q={q}
results={searchResults} results={searchResults}
/> />
); );
} }
return <>{children}</>; return children;
}; };

View File

@@ -22,15 +22,19 @@
"@lib": "./lib", "@lib": "./lib",
"@lib/": "./lib/", "@lib/": "./lib/",
"@libsql/client": "npm:@libsql/client@^0.14.0", "@libsql/client": "npm:@libsql/client@^0.14.0",
"@openai/openai": "jsr:@openai/openai@^6.7.0",
"@preact/signals": "https://esm.sh/*@preact/signals@1.2.2", "@preact/signals": "https://esm.sh/*@preact/signals@1.2.2",
"@preact/signals-core": "https://esm.sh/*@preact/signals-core@1.5.1", "@preact/signals-core": "https://esm.sh/*@preact/signals-core@1.5.1",
"@std/http": "jsr:@std/http@^1.0.12", "@std/http": "jsr:@std/http@^1.0.12",
"@std/yaml": "jsr:@std/yaml@^1.0.5", "@std/yaml": "jsr:@std/yaml@^1.0.5",
"csstype": "npm:csstype@^3.1.3",
"defuddle": "npm:defuddle@^0.6.6", "defuddle": "npm:defuddle@^0.6.6",
"drizzle-kit": "npm:drizzle-kit@^0.30.1", "drizzle-kit": "npm:drizzle-kit@^0.30.1",
"drizzle-orm": "npm:drizzle-orm@^0.38.3", "drizzle-orm": "npm:drizzle-orm@^0.38.3",
"fuzzysort": "npm:fuzzysort@^3.1.0", "fuzzysort": "npm:fuzzysort@^3.1.0",
"jsdom": "npm:jsdom@^24.1.3", "jsdom": "npm:jsdom@^24.1.3",
"moviedb-promise": "npm:moviedb-promise@^4.0.7",
"parse-ingredient": "npm:parse-ingredient@^1.3.1",
"playwright": "npm:playwright@^1.49.1", "playwright": "npm:playwright@^1.49.1",
"playwright-extra": "npm:playwright-extra@^4.3.6", "playwright-extra": "npm:playwright-extra@^4.3.6",
"preact": "https://esm.sh/preact@10.22.0", "preact": "https://esm.sh/preact@10.22.0",
@@ -42,7 +46,9 @@
"tailwindcss/": "npm:/tailwindcss@^3.4.17/", "tailwindcss/": "npm:/tailwindcss@^3.4.17/",
"tailwindcss/plugin": "npm:/tailwindcss@^3.4.17/plugin.js", "tailwindcss/plugin": "npm:/tailwindcss@^3.4.17/plugin.js",
"camelcase-css": "npm:camelcase-css", "camelcase-css": "npm:camelcase-css",
"thumbhash": "npm:thumbhash@^0.1.1",
"tsx": "npm:tsx@^4.19.2", "tsx": "npm:tsx@^4.19.2",
"turndown": "npm:turndown@^7.2.2",
"yaml": "https://deno.land/std@0.197.0/yaml/mod.ts", "yaml": "https://deno.land/std@0.197.0/yaml/mod.ts",
"zod": "npm:zod@^3.24.1", "zod": "npm:zod@^3.24.1",
"fs": "https://deno.land/std/fs/mod.ts" "fs": "https://deno.land/std/fs/mod.ts"

View File

@@ -11,6 +11,7 @@ import * as $admin_log_index from "./routes/admin/log/index.tsx";
import * as $admin_performance_index from "./routes/admin/performance/index.tsx"; import * as $admin_performance_index from "./routes/admin/performance/index.tsx";
import * as $api_articles_name_ from "./routes/api/articles/[name].ts"; import * as $api_articles_name_ from "./routes/api/articles/[name].ts";
import * as $api_articles_create_index from "./routes/api/articles/create/index.ts"; import * as $api_articles_create_index from "./routes/api/articles/create/index.ts";
import * as $api_articles_enhance_name_ from "./routes/api/articles/enhance/[name].ts";
import * as $api_articles_index from "./routes/api/articles/index.ts"; import * as $api_articles_index from "./routes/api/articles/index.ts";
import * as $api_auth_callback from "./routes/api/auth/callback.ts"; import * as $api_auth_callback from "./routes/api/auth/callback.ts";
import * as $api_auth_login from "./routes/api/auth/login.ts"; import * as $api_auth_login from "./routes/api/auth/login.ts";
@@ -57,7 +58,9 @@ import * as $KMenu_commands_create_movie from "./islands/KMenu/commands/create_m
import * as $KMenu_commands_create_recipe from "./islands/KMenu/commands/create_recipe.ts"; import * as $KMenu_commands_create_recipe from "./islands/KMenu/commands/create_recipe.ts";
import * as $KMenu_commands_create_recommendations from "./islands/KMenu/commands/create_recommendations.ts"; import * as $KMenu_commands_create_recommendations from "./islands/KMenu/commands/create_recommendations.ts";
import * as $KMenu_commands_create_series from "./islands/KMenu/commands/create_series.ts"; import * as $KMenu_commands_create_series from "./islands/KMenu/commands/create_series.ts";
import * as $KMenu_commands_enhance_article_infos from "./islands/KMenu/commands/enhance_article_infos.ts";
import * as $KMenu_types from "./islands/KMenu/types.ts"; import * as $KMenu_types from "./islands/KMenu/types.ts";
import * as $KMenuButton from "./islands/KMenuButton.tsx";
import * as $Link from "./islands/Link.tsx"; import * as $Link from "./islands/Link.tsx";
import * as $Recommendations from "./islands/Recommendations.tsx"; import * as $Recommendations from "./islands/Recommendations.tsx";
import * as $Search from "./islands/Search.tsx"; import * as $Search from "./islands/Search.tsx";
@@ -74,6 +77,7 @@ const manifest = {
"./routes/admin/performance/index.tsx": $admin_performance_index, "./routes/admin/performance/index.tsx": $admin_performance_index,
"./routes/api/articles/[name].ts": $api_articles_name_, "./routes/api/articles/[name].ts": $api_articles_name_,
"./routes/api/articles/create/index.ts": $api_articles_create_index, "./routes/api/articles/create/index.ts": $api_articles_create_index,
"./routes/api/articles/enhance/[name].ts": $api_articles_enhance_name_,
"./routes/api/articles/index.ts": $api_articles_index, "./routes/api/articles/index.ts": $api_articles_index,
"./routes/api/auth/callback.ts": $api_auth_callback, "./routes/api/auth/callback.ts": $api_auth_callback,
"./routes/api/auth/login.ts": $api_auth_login, "./routes/api/auth/login.ts": $api_auth_login,
@@ -127,7 +131,10 @@ const manifest = {
"./islands/KMenu/commands/create_recommendations.ts": "./islands/KMenu/commands/create_recommendations.ts":
$KMenu_commands_create_recommendations, $KMenu_commands_create_recommendations,
"./islands/KMenu/commands/create_series.ts": $KMenu_commands_create_series, "./islands/KMenu/commands/create_series.ts": $KMenu_commands_create_series,
"./islands/KMenu/commands/enhance_article_infos.ts":
$KMenu_commands_enhance_article_infos,
"./islands/KMenu/types.ts": $KMenu_types, "./islands/KMenu/types.ts": $KMenu_types,
"./islands/KMenuButton.tsx": $KMenuButton,
"./islands/Link.tsx": $Link, "./islands/Link.tsx": $Link,
"./islands/Recommendations.tsx": $Recommendations, "./islands/Recommendations.tsx": $Recommendations,
"./islands/Search.tsx": $Search, "./islands/Search.tsx": $Search,

View File

@@ -2,7 +2,6 @@ import { Signal } from "@preact/signals";
import type { Ingredient, IngredientGroup } from "@lib/recipeSchema.ts"; import type { Ingredient, IngredientGroup } from "@lib/recipeSchema.ts";
import { FunctionalComponent } from "preact"; import { FunctionalComponent } from "preact";
import { unitsOfMeasure } from "@lib/parseIngredient.ts"; import { unitsOfMeasure } from "@lib/parseIngredient.ts";
import { renderMarkdown } from "@lib/markdown.ts";
function formatAmount(num: number) { function formatAmount(num: number) {
if (num === 0) return ""; if (num === 0) return "";
@@ -10,15 +9,12 @@ function formatAmount(num: number) {
} }
function formatUnit(unit: string, amount: number) { function formatUnit(unit: string, amount: number) {
const unitKey = unit.toLowerCase() as keyof typeof unitsOfMeasure; if (!unit) return "";
const unitKey = unit.toLowerCase() as (keyof typeof unitsOfMeasure);
if (unitKey in unitsOfMeasure) { if (unitKey in unitsOfMeasure) {
if (amount > 1 && unitsOfMeasure[unitKey].plural !== undefined) { if (amount > 1 && unitsOfMeasure[unitKey].plural !== undefined) {
return unitsOfMeasure[unitKey].plural; return unitsOfMeasure[unitKey].plural;
} }
if (unitKey !== "cup") {
return unitsOfMeasure[unitKey].short;
}
return unitKey.toString(); return unitKey.toString();
} else { } else {
return unit; return unit;
@@ -43,13 +39,13 @@ const Ingredient = (
return ( return (
<tr key={key}> <tr key={key}>
<td class="pr-4 py-2"> <td class="pr-4 py-1">
{formatAmount(finalAmount || 0)} {formatAmount(finalAmount || 0)}
<span class="ml-0.5 opacity-50"> <span class="ml-0.5 opacity-50">
{formatUnit(unit, finalAmount || 0)} {formatUnit(unit, finalAmount || 0)}
</span> </span>
</td> </td>
<td class="px-4 py-2">{name}</td> <td class="px-4 py-1">{name}</td>
</tr> </tr>
); );
}; };
@@ -66,16 +62,25 @@ export const IngredientsList: FunctionalComponent<
return ( return (
<table class="w-full border-collapse table-auto"> <table class="w-full border-collapse table-auto">
<tbody> <tbody>
{ingredients.filter((s) => !!s?.length).map((item) => { {ingredients.map((item) => {
if ("items" in item) {
return item.items.map((ing, i) => {
return ( return (
<div dangerouslySetInnerHTML={{ __html: renderMarkdown(item) }}> <Ingredient
</div> key={i}
ingredient={ing}
amount={amount}
portion={portion}
/>
); );
// return ( });
// <Ingredient ingredient={item} amount={amount} portion={portion} /> } else {
// ); return (
<Ingredient ingredient={item} amount={amount} portion={portion} />
);
}
})} })}
</tbody> </tbody>
</table> </table>
); );
}; };

View File

@@ -5,6 +5,7 @@ import { menus } from "@islands/KMenu/commands.ts";
import { MenuEntry } from "@islands/KMenu/types.ts"; import { MenuEntry } from "@islands/KMenu/types.ts";
import * as icons from "@components/icons.tsx"; import * as icons from "@components/icons.tsx";
import { IS_BROWSER } from "$fresh/runtime.ts"; import { IS_BROWSER } from "$fresh/runtime.ts";
import { isKMenuOpen } from "@lib/kmenu.ts";
const KMenuEntry = ( const KMenuEntry = (
{ entry, activeIndex, index }: { { entry, activeIndex, index }: {
entry: MenuEntry; entry: MenuEntry;
@@ -21,7 +22,7 @@ const KMenuEntry = (
: "text-gray-400" : "text-gray-400"
}`} }`}
> >
{entry?.icon && icons[entry.icon]({ class: "w-4 h-4 mr-1" })} {entry?.icon && icons[entry.icon]({ class: "min-w-4 h-4 mr-1" })}
{entry.title} {entry.title}
</div> </div>
); );
@@ -42,7 +43,7 @@ export const KMenu = (
const input = useRef<HTMLInputElement>(null); const input = useRef<HTMLInputElement>(null);
const commandInput = useSignal(""); const commandInput = useSignal("");
const visible = useSignal(false); const visible = isKMenuOpen;
if (visible.value === false) { if (visible.value === false) {
setTimeout(() => { setTimeout(() => {
activeMenuType.value = "main"; activeMenuType.value = "main";
@@ -103,8 +104,9 @@ export const KMenu = (
} }
useEventListener("keydown", (ev: KeyboardEvent) => { useEventListener("keydown", (ev: KeyboardEvent) => {
const target = ev.target as HTMLElement;
if (ev.key === "k") { if (ev.key === "k") {
if (ev?.target?.nodeName == "INPUT") { if (target.nodeName == "INPUT") {
return; return;
} }
@@ -167,11 +169,13 @@ export const KMenu = (
style={{ background: "#2B2930", color: "#818181" }} style={{ background: "#2B2930", color: "#818181" }}
> >
<div <div
class={`grid h-12 text-gray-400 ${ class={`grid min-h-12 text-gray-400 ${
activeState.value !== "loading" && "border-b" (activeState.value === "normal" || activeState.value === "input") &&
"border-b"
} border-gray-500 `} } border-gray-500 `}
style={{ style={{
gridTemplateColumns: activeState.value !== "loading" gridTemplateColumns:
(activeState.value === "normal" || activeState.value === "input")
? "auto 1fr" ? "auto 1fr"
: "1fr", : "1fr",
}} }}
@@ -197,12 +201,18 @@ export const KMenu = (
)} )}
{activeState.value === "loading" && ( {activeState.value === "loading" && (
<div class="py-3 px-4 flex items-center gap-2"> <div class="py-3 px-4 flex items-center gap-2">
<icons.IconLoader2 class="animate-spin w-4 h-4" /> <icons.IconLoader2 class="animate-spin min-w-4 h-4" />
{loadingText.value || "Loading..."} {loadingText.value || "Loading..."}
</div> </div>
)} )}
{activeState.value === "error" && (
<div class="py-3 px-4 flex items-center gap-2 text-red-400">
<icons.IconAlertCircle class="min-w-4 h-4" />
{loadingText.value || "An error occurred"}
</div> </div>
{activeState.value === "normal" && )}
</div>
{(activeState.value === "normal" || activeState.value === "input") &&
( (
<div <div
class="" class=""

View File

@@ -7,6 +7,7 @@ import { addSeriesInfo } from "@islands/KMenu/commands/add_series_infos.ts";
import { createNewSeries } from "@islands/KMenu/commands/create_series.ts"; import { createNewSeries } from "@islands/KMenu/commands/create_series.ts";
import { updateAllRecommendations } from "@islands/KMenu/commands/create_recommendations.ts"; import { updateAllRecommendations } from "@islands/KMenu/commands/create_recommendations.ts";
import { createNewRecipe } from "@islands/KMenu/commands/create_recipe.ts"; import { createNewRecipe } from "@islands/KMenu/commands/create_recipe.ts";
import { enhanceArticleInfo } from "@islands/KMenu/commands/enhance_article_infos.ts";
export const menus: Record<string, Menu> = { export const menus: Record<string, Menu> = {
main: { main: {
@@ -77,6 +78,7 @@ export const menus: Record<string, Menu> = {
createNewSeries, createNewSeries,
createNewRecipe, createNewRecipe,
addMovieInfos, addMovieInfos,
enhanceArticleInfo,
// updateAllRecommendations, // updateAllRecommendations,
], ],
}, },

View File

@@ -8,6 +8,7 @@ export const addMovieInfos: MenuEntry = {
meta: "", meta: "",
icon: "IconReportSearch", icon: "IconReportSearch",
cb: async (state, context) => { cb: async (state, context) => {
try {
state.activeState.value = "loading"; state.activeState.value = "loading";
const movie = context as ReviewResource; const movie = context as ReviewResource;
@@ -17,6 +18,10 @@ export const addMovieInfos: MenuEntry = {
`/api/tmdb/query?q=${encodeURIComponent(query)}`, `/api/tmdb/query?q=${encodeURIComponent(query)}`,
); );
if (!response.ok) {
throw new Error(await response.text());
}
const json = await response.json() as TMDBMovie[]; const json = await response.json() as TMDBMovie[];
const menuID = `result/${movie.name}`; const menuID = `result/${movie.name}`;
@@ -26,6 +31,7 @@ export const addMovieInfos: MenuEntry = {
entries: json.map((m) => ({ entries: json.map((m) => ({
title: `${m.title} released ${m.release_date}`, title: `${m.title} released ${m.release_date}`,
cb: async () => { cb: async () => {
try {
state.activeState.value = "loading"; state.activeState.value = "loading";
await fetch(`/api/movies/enhance/${movie.name}/`, { await fetch(`/api/movies/enhance/${movie.name}/`, {
method: "POST", method: "POST",
@@ -34,6 +40,10 @@ export const addMovieInfos: MenuEntry = {
state.visible.value = false; state.visible.value = false;
state.activeState.value = "normal"; state.activeState.value = "normal";
globalThis.location.reload(); globalThis.location.reload();
} catch (e) {
state.activeState.value = "error";
state.loadingText.value = e.message;
}
}, },
})), })),
}; };
@@ -41,6 +51,10 @@ export const addMovieInfos: MenuEntry = {
state.activeMenu.value = menuID; state.activeMenu.value = menuID;
state.commandInput.value = ""; state.commandInput.value = "";
state.activeState.value = "normal"; state.activeState.value = "normal";
} catch (e) {
state.activeState.value = "error";
state.loadingText.value = e.message;
}
}, },
visible: () => { visible: () => {
const loc = globalThis["location"]; const loc = globalThis["location"];

View File

@@ -8,6 +8,7 @@ export const addSeriesInfo: MenuEntry = {
meta: "", meta: "",
icon: "IconReportSearch", icon: "IconReportSearch",
cb: async (state, context) => { cb: async (state, context) => {
try {
state.activeState.value = "loading"; state.activeState.value = "loading";
const series = context as ReviewResource; const series = context as ReviewResource;
@@ -17,6 +18,10 @@ export const addSeriesInfo: MenuEntry = {
`/api/tmdb/query?q=${encodeURIComponent(query)}&type=serie`, `/api/tmdb/query?q=${encodeURIComponent(query)}&type=serie`,
); );
if (!response.ok) {
throw new Error(await response.text());
}
const json = await response.json() as TMDBSeries[]; const json = await response.json() as TMDBSeries[];
const menuID = `result/${series.name}`; const menuID = `result/${series.name}`;
@@ -26,6 +31,7 @@ export const addSeriesInfo: MenuEntry = {
entries: json.map((m) => ({ entries: json.map((m) => ({
title: `${m.name || m.original_name} released ${m.first_air_date}`, title: `${m.name || m.original_name} released ${m.first_air_date}`,
cb: async () => { cb: async () => {
try {
state.activeState.value = "loading"; state.activeState.value = "loading";
await fetch(`/api/series/enhance/${series.name}/`, { await fetch(`/api/series/enhance/${series.name}/`, {
method: "POST", method: "POST",
@@ -34,6 +40,10 @@ export const addSeriesInfo: MenuEntry = {
state.visible.value = false; state.visible.value = false;
state.activeState.value = "normal"; state.activeState.value = "normal";
//window.location.reload(); //window.location.reload();
} catch (e) {
state.activeState.value = "error";
state.loadingText.value = e.message;
}
}, },
})), })),
}; };
@@ -41,6 +51,10 @@ export const addSeriesInfo: MenuEntry = {
state.commandInput.value = ""; state.commandInput.value = "";
state.activeMenu.value = menuID; state.activeMenu.value = menuID;
state.activeState.value = "normal"; state.activeState.value = "normal";
} catch (e) {
state.activeState.value = "error";
state.loadingText.value = e.message;
}
}, },
visible: () => { visible: () => {
const loc = globalThis["location"]; const loc = globalThis["location"];

View File

@@ -22,14 +22,16 @@ export const createNewArticle: MenuEntry = {
state.activeState.value = "loading"; state.activeState.value = "loading";
fetchStream("/api/articles/create?url=" + value, (chunk) => { fetchStream("/api/articles/create?url=" + value, (chunk) => {
if (chunk.startsWith("id:")) { if (chunk.type === "error") {
state.activeState.value = "error";
state.loadingText.value = chunk.message;
} else if (chunk.type === "finished") {
state.loadingText.value = "Finished"; state.loadingText.value = "Finished";
setTimeout(() => { setTimeout(() => {
window.location.href = "/articles/" + globalThis.location.href = "/articles/" + chunk.url;
chunk.replace("id:", "").trim();
}, 500); }, 500);
} else { } else {
state.loadingText.value = chunk; state.loadingText.value = chunk.message;
} }
}); });
} }

View File

@@ -31,6 +31,7 @@ export const createNewMovie: MenuEntry = {
let currentQuery: string; let currentQuery: string;
const search = debounce(async function search(query: string) { const search = debounce(async function search(query: string) {
try {
currentQuery = query; currentQuery = query;
if (query.length < 2) { if (query.length < 2) {
return; return;
@@ -38,6 +39,10 @@ export const createNewMovie: MenuEntry = {
const response = await fetch("/api/tmdb/query?q=" + query); const response = await fetch("/api/tmdb/query?q=" + query);
if (!response.ok) {
throw new Error(await response.text());
}
const movies = await response.json() as TMDBMovie[]; const movies = await response.json() as TMDBMovie[];
if (query !== currentQuery) return; if (query !== currentQuery) return;
@@ -48,18 +53,30 @@ export const createNewMovie: MenuEntry = {
return { return {
title: `${r.title} - ${r.release_date}`, title: `${r.title} - ${r.release_date}`,
cb: async () => { cb: async () => {
try {
state.activeState.value = "loading"; state.activeState.value = "loading";
const response = await fetch("/api/movies/" + r.id, { const response = await fetch("/api/movies/" + r.id, {
method: "POST", method: "POST",
}); });
if (!response.ok) {
throw new Error(await response.text());
}
const movie = await response.json() as ReviewResource; const movie = await response.json() as ReviewResource;
unsub(); unsub();
globalThis.location.href = "/movies/" + movie.name; globalThis.location.href = "/movies/" + movie.name;
} catch (e) {
state.activeState.value = "error";
state.loadingText.value = e.message;
}
}, },
}; };
}), }),
}; };
state.activeMenu.value = "input_link"; state.activeMenu.value = "input_link";
} catch (e) {
state.activeState.value = "error";
state.loadingText.value = e.message;
}
}, 500); }, 500);
const unsub = state.commandInput.subscribe((value) => { const unsub = state.commandInput.subscribe((value) => {

View File

@@ -21,15 +21,17 @@ export const createNewRecipe: MenuEntry = {
state.activeState.value = "loading"; state.activeState.value = "loading";
fetchStream("/api/recipes/create?url=" + value, (chunk) => { fetchStream("/api/recipes/create?url=" + value, (msg) => {
if (chunk.startsWith("id:")) { if (msg.type === "error") {
state.activeState.value = "error";
state.loadingText.value = msg.message;
} else if (msg.type === "finished") {
state.loadingText.value = "Finished"; state.loadingText.value = "Finished";
setTimeout(() => { setTimeout(() => {
globalThis.location.href = "/recipes/" + globalThis.location.href = "/recipes/" + msg.url;
chunk.replace("id:", "").trim();
}, 500); }, 500);
} else { } else {
state.loadingText.value = chunk; state.loadingText.value = msg.message;
} }
}); });
} }

View File

@@ -10,12 +10,15 @@ export const updateAllRecommendations: MenuEntry = {
state.activeState.value = "loading"; state.activeState.value = "loading";
fetchStream("/api/recommendation/all", (chunk) => { fetchStream("/api/recommendation/all", (chunk) => {
if (chunk.toLowerCase().includes("finish")) { if (chunk.type === "error") {
state.activeState.value = "error";
state.loadingText.value = chunk.message;
} else if (chunk.type === "finished") {
setTimeout(() => { setTimeout(() => {
window.location.reload(); globalThis.location.reload();
}, 500); }, 500);
} else { } else {
state.loadingText.value = chunk; state.loadingText.value = chunk.message;
} }
}); });
}, },

View File

@@ -31,6 +31,7 @@ export const createNewSeries: MenuEntry = {
let currentQuery: string; let currentQuery: string;
const search = debounce(async function search(query: string) { const search = debounce(async function search(query: string) {
try {
currentQuery = query; currentQuery = query;
if (query.length < 2) { if (query.length < 2) {
return; return;
@@ -40,6 +41,10 @@ export const createNewSeries: MenuEntry = {
"/api/tmdb/query?q=" + query + "&type=series", "/api/tmdb/query?q=" + query + "&type=series",
); );
if (!response.ok) {
throw new Error(await response.text());
}
const series = await response.json() as TMDBSeries[]; const series = await response.json() as TMDBSeries[];
if (query !== currentQuery) return; if (query !== currentQuery) return;
@@ -50,19 +55,31 @@ export const createNewSeries: MenuEntry = {
return { return {
title: `${r.name} - ${r.first_air_date}`, title: `${r.name} - ${r.first_air_date}`,
cb: async () => { cb: async () => {
try {
state.activeState.value = "loading"; state.activeState.value = "loading";
const response = await fetch("/api/series/" + r.id, { const response = await fetch("/api/series/" + r.id, {
method: "POST", method: "POST",
}); });
if (!response.ok) {
throw new Error(await response.text());
}
const series = await response.json() as ReviewResource; const series = await response.json() as ReviewResource;
unsub(); unsub();
globalThis.location.href = "/series/" + series.name; globalThis.location.href = "/series/" + series.name;
} catch (e) {
state.activeState.value = "error";
state.loadingText.value = e.message;
}
}, },
}; };
}), }),
}; };
state.commandInput.value = ""; state.commandInput.value = "";
state.activeMenu.value = "input_link"; state.activeMenu.value = "input_link";
} catch (e) {
state.activeState.value = "error";
state.loadingText.value = e.message;
}
}, 500); }, 500);
const unsub = state.commandInput.subscribe((value) => { const unsub = state.commandInput.subscribe((value) => {

View File

@@ -0,0 +1,41 @@
import { getCookie } from "@lib/string.ts";
import { MenuEntry } from "../types.ts";
import { ArticleResource } from "@lib/marka/schema.ts";
import { fetchStream } from "@lib/helpers.ts";
export const enhanceArticleInfo: MenuEntry = {
title: "Enhance Article Info",
meta: "Update metadata and content from source url",
icon: "IconReportSearch",
cb: (state, context) => {
state.activeState.value = "loading";
const article = context as ArticleResource;
fetchStream(
`/api/articles/enhance/${article.name}/`,
(chunk) => {
if (chunk.type === "error") {
state.activeState.value = "error";
state.loadingText.value = chunk.message;
} else if (chunk.type == "finished") {
state.loadingText.value = "Finished";
setTimeout(() => {
state.visible.value = false;
state.activeState.value = "normal";
globalThis.location.reload();
}, 500);
} else {
state.loadingText.value = chunk.message;
}
},
{ method: "POST" },
);
},
visible: () => {
const loc = globalThis["location"];
if (!getCookie("session_cookie")) return false;
return (loc?.pathname?.includes("article") &&
!loc.pathname.endsWith("articles"));
},
};

View File

@@ -6,7 +6,7 @@ type IconKey = keyof typeof icons;
export type MenuState = { export type MenuState = {
activeMenu: Signal<string>; activeMenu: Signal<string>;
activeState: Signal<"input" | "error" | "normal" | "loading">; activeState: Signal<"input" | "error" | "normal" | "loading">;
loadingText:Signal<string>; loadingText: Signal<string>;
commandInput: Signal<string>; commandInput: Signal<string>;
visible: Signal<boolean>; visible: Signal<boolean>;
menus: Record<string, Menu>; menus: Record<string, Menu>;

14
islands/KMenuButton.tsx Normal file
View File

@@ -0,0 +1,14 @@
import { Button } from "@components/Button.tsx";
import { IconMenu2 } from "@components/icons.tsx";
import { isKMenuOpen } from "@lib/kmenu.ts";
export default function KMenuButton() {
return (
<Button
class="fixed bottom-4 right-4 md:hidden bg-gray-800 text-white p-3 rounded-full shadow-lg z-50"
onClick={() => (isKMenuOpen.value = true)}
>
<IconMenu2 class="w-6 h-6" />
</Button>
);
}

View File

@@ -1,7 +1,6 @@
import { useEffect } from "preact/hooks"; import { useEffect } from "preact/hooks";
declare global { declare global {
// deno-lint-ignore no-var
var loadingTimeout: ReturnType<typeof setTimeout> | undefined; var loadingTimeout: ReturnType<typeof setTimeout> | undefined;
} }

View File

@@ -1,5 +1,6 @@
import { useCallback, useState } from "preact/hooks"; import { useCallback, useState } from "preact/hooks";
import { IconWand } from "@components/icons.tsx"; import { IconWand } from "@components/icons.tsx";
import { RecommendationResource } from "@lib/recommendation.ts";
type RecommendationState = "disabled" | "loading"; type RecommendationState = "disabled" | "loading";
@@ -11,7 +12,7 @@ export function Recommendations(
}, },
) { ) {
const [state, setState] = useState<RecommendationState>("disabled"); const [state, setState] = useState<RecommendationState>("disabled");
const [results, setResults] = useState(); const [results, setResults] = useState<RecommendationResource[]>();
const startFetch = useCallback( const startFetch = useCallback(
async () => { async () => {
@@ -44,9 +45,9 @@ export function Recommendations(
<div class="flex gap-5 items-center mb-4"> <div class="flex gap-5 items-center mb-4">
<img <img
class="w-12 h-12 rounded-full object-cover" class="w-12 h-12 rounded-full object-cover"
src={`https://image.tmdb.org/t/p/original${res.poster_path}`} src={`https://image.tmdb.org/t/p/original${res.id}`}
/> />
<p>{res.title}</p> <p>{res.id}</p>
</div> </div>
); );
})} })}
@@ -66,6 +67,7 @@ export function Recommendations(
{!results && state === "disabled" && {!results && state === "disabled" &&
( (
<button <button
type="submit"
onClick={startFetch} onClick={startFetch}
class="ml-8 mt-2 font-thin flex items-center gap-2 text-white my-2" class="ml-8 mt-2 font-thin flex items-center gap-2 text-white my-2"
> >

View File

@@ -10,7 +10,7 @@ import { Rating } from "@components/Rating.tsx";
import { useSignal } from "@preact/signals"; import { useSignal } from "@preact/signals";
import Image from "@components/Image.tsx"; import Image from "@components/Image.tsx";
import { Emoji } from "@components/Emoji.tsx"; import { Emoji } from "@components/Emoji.tsx";
import { GenericResource } from "@lib/marka/schema.ts"; import { GenericResource, getNameOfResource } from "@lib/marka/schema.ts";
export async function fetchQueryResource(url: URL, type = "") { export async function fetchQueryResource(url: URL, type = "") {
const query = url.searchParams.get("q"); const query = url.searchParams.get("q");
@@ -23,7 +23,7 @@ export async function fetchQueryResource(url: URL, type = "") {
url.searchParams.set("status", "not-seen"); url.searchParams.set("status", "not-seen");
} }
if (type) { if (type) {
url.searchParams.set("types", type); url.searchParams.set("type", type);
} }
const response = await fetch(url); const response = await fetch(url);
const jsonData = await response.json(); const jsonData = await response.json();
@@ -33,28 +33,30 @@ export async function fetchQueryResource(url: URL, type = "") {
} }
} }
export const RedirectSearchHandler = () => { export function RedirectSearchHandler() {
if (getCookie("session_cookie")) {
useEventListener("keydown", (e: KeyboardEvent) => { useEventListener("keydown", (e: KeyboardEvent) => {
if (e?.target?.nodeName == "INPUT") return; if (getCookie("session_cookie")) {
const target = e.target as HTMLInputElement;
if (target.nodeName == "INPUT") return;
if ( if (
e.key === "?" && e.key === "?" &&
globalThis.location.search === "" globalThis.location.search === ""
) { ) {
globalThis.location.href += "?q="; globalThis.location.href += "?q=";
} }
}, IS_BROWSER ? document?.body : undefined);
} }
}, IS_BROWSER ? document?.body : undefined);
return; // deno-lint-ignore jsx-no-useless-fragment
}; return <></>;
}
const SearchResultImage = ({ src }: { src: string }) => { const SearchResultImage = ({ src }: { src: string }) => {
return ( return (
<Image <Image
class="object-cover w-12 h-12 rounded-full" class="object-cover w-12 h-12 rounded-full"
width="50" width={100}
height="50" height={100}
src={src} src={src}
alt="preview image" alt="preview image"
/> />
@@ -67,9 +69,9 @@ export const SearchResultItem = (
showEmoji?: boolean; showEmoji?: boolean;
}, },
) => { ) => {
const resourceType = resources[item?.content._type]; const resourceType =
resources[item.content._type.toLowerCase() as keyof typeof resources];
const href = item?.path.replace("/resources", "").replace(/\.md$/, ""); const href = item?.path.replace("/resources", "").replace(/\.md$/, "");
console.log({ item, href });
return ( return (
<a <a
href={href} href={href}
@@ -79,8 +81,7 @@ export const SearchResultItem = (
? <Emoji class="w-7 h-7" name={resourceType.emoji} /> ? <Emoji class="w-7 h-7" name={resourceType.emoji} />
: ""} : ""}
{item.image && <SearchResultImage src={item.image?.url} />} {item.image && <SearchResultImage src={item.image?.url} />}
{item.content?.headline || item.content?.name || {getNameOfResource(item)}
item.content?.itemReviewed.name || item?.name}
</a> </a>
); );
}; };
@@ -113,6 +114,7 @@ const Search = (
const searchQuery = useSignal(q); const searchQuery = useSignal(q);
const data = useSignal<GenericResource[] | undefined>(results); const data = useSignal<GenericResource[] | undefined>(results);
const isLoading = useSignal(false); const isLoading = useSignal(false);
const rating = useSignal<number | undefined>(undefined);
const showSeenStatus = useSignal(false); const showSeenStatus = useSignal(false);
const inputRef = useRef<HTMLInputElement>(null); const inputRef = useRef<HTMLInputElement>(null);
@@ -121,8 +123,10 @@ const Search = (
if (u.searchParams.get("q") !== searchQuery.value) { if (u.searchParams.get("q") !== searchQuery.value) {
u.searchParams.set("q", searchQuery.value); u.searchParams.set("q", searchQuery.value);
} }
if (showSeenStatus.value) { if (showSeenStatus.value === true) {
u.searchParams.set("rating", "0"); u.searchParams.set("rating", "0");
} else if (rating.value) {
u.searchParams.set("rating", rating.value.toString());
} else { } else {
u.searchParams.delete("rating"); u.searchParams.delete("rating");
} }
@@ -161,7 +165,7 @@ const Search = (
useEffect(() => { useEffect(() => {
debouncedFetchData(); // Call the debounced fetch function with the updated search query debouncedFetchData(); // Call the debounced fetch function with the updated search query
}, [searchQuery.value, showSeenStatus.value]); }, [searchQuery.value, showSeenStatus.value, rating.value]);
useEffect(() => { useEffect(() => {
debouncedFetchData(); debouncedFetchData();
@@ -186,8 +190,12 @@ const Search = (
onInput={handleInputChange} onInput={handleInputChange}
/> />
</div> </div>
<Checkbox label="seen" checked={showSeenStatus} /> <Checkbox label="unrated" checked={showSeenStatus} />
<Rating rating={4} /> <div
class={showSeenStatus.value ? "opacity-10" : ""}
>
<Rating rating={rating} />
</div>
</header> </header>
{data.value?.length && !isLoading.value {data.value?.length && !isLoading.value
? <SearchResultList showEmoji={!type} result={data.value} /> ? <SearchResultList showEmoji={!type} result={data.value} />

View File

@@ -7,6 +7,7 @@ export const PROXY_PASSWORD = Deno.env.get("PROXY_PASSWORD");
export const TMDB_API_KEY = Deno.env.get("TMDB_API_KEY"); export const TMDB_API_KEY = Deno.env.get("TMDB_API_KEY");
export const OPENAI_API_KEY = Deno.env.get("OPENAI_API_KEY"); export const OPENAI_API_KEY = Deno.env.get("OPENAI_API_KEY");
export const YOUTUBE_API_KEY = Deno.env.get("YOUTUBE_API_KEY"); export const YOUTUBE_API_KEY = Deno.env.get("YOUTUBE_API_KEY");
export const UNSPLASH_API_KEY = Deno.env.get("UNSPLASH_API_KEY");
export const TELEGRAM_API_KEY = Deno.env.get("TELEGRAM_API_KEY")!; export const TELEGRAM_API_KEY = Deno.env.get("TELEGRAM_API_KEY")!;
export const GITEA_SERVER = Deno.env.get("GITEA_SERVER"); export const GITEA_SERVER = Deno.env.get("GITEA_SERVER");
@@ -18,6 +19,7 @@ const duration = Deno.env.get("SESSION_DURATION");
export const SESSION_DURATION = duration ? +duration : (60 * 60 * 24); export const SESSION_DURATION = duration ? +duration : (60 * 60 * 24);
export const MARKA_API_KEY = Deno.env.get("MARKA_API_KEY"); export const MARKA_API_KEY = Deno.env.get("MARKA_API_KEY");
export const MARKA_API_URL = Deno.env.get("MARKA_API_URL");
export const JWT_SECRET = Deno.env.get("JWT_SECRET"); export const JWT_SECRET = Deno.env.get("JWT_SECRET");

View File

@@ -31,19 +31,54 @@ export const fixRenderedMarkdown = (content: string) => {
}); });
}; };
export async function fetchStream(url: string, cb: (chunk: string) => void) { type StreamMessage = {
const response = await fetch(url); type: "info";
const reader = response?.body?.getReader(); message: string;
if (reader) { } | {
type: "error";
message: string;
} | {
type: "warning";
message: string;
} | {
type: "finished";
url: string;
};
export async function fetchStream(
url: string,
cb: (chunk: StreamMessage) => void,
init?: RequestInit,
) {
const res = await fetch(url, init);
if (!res.body) return;
let buffer = "";
const reader = res.body
.pipeThrough(new TextDecoderStream())
.pipeThrough(
new TransformStream<string, string>({
transform(chunk, controller) {
buffer += chunk;
let idx;
while ((idx = buffer.indexOf("\n")) >= 0) {
const line = buffer.slice(0, idx).trim();
buffer = buffer.slice(idx + 1);
if (line) controller.enqueue(line);
}
},
flush(controller) {
const line = buffer.trim();
if (line) controller.enqueue(line);
},
}),
)
.getReader();
while (true) { while (true) {
const { done, value } = await reader.read(); const { done, value } = await reader.read();
if (done) return; if (done) break;
const data = new TextDecoder().decode(value); cb(JSON.parse(value));
data
.split("$")
.filter((d) => d && d.length)
.map((d) => cb(Array.isArray(d) ? d[0] : d));
}
} }
} }
@@ -58,32 +93,53 @@ export function hashString(message: string) {
} }
export const createStreamResponse = () => { export const createStreamResponse = () => {
let controller: ReadableStreamController<ArrayBufferView>; const encoder = new TextEncoder();
const body = new ReadableStream({ let controller: ReadableStreamDefaultController<Uint8Array>;
start(cont) {
controller = cont; const body = new ReadableStream<Uint8Array>({
start(c) {
controller = c;
}, },
}); });
const response = new Response(body, { const response = new Response(body, {
headers: { headers: {
"content-type": "text/plain", // newline-delimited JSON
"content-type": "application/x-ndjson; charset=utf-8",
// prevent intermediaries from buffering/transforming
"cache-control": "no-cache, no-transform",
"x-content-type-options": "nosniff", "x-content-type-options": "nosniff",
// nginx hint to disable proxy buffering
"x-accel-buffering": "no",
// if you control compression, keep it off for streams
// "content-encoding": "identity",
}, },
}); });
function cancel() { const send = (obj: unknown) => {
controller.close(); controller.enqueue(encoder.encode(JSON.stringify(obj) + "\n")); // ← delimiter
};
const cancel = () => controller.close();
function info(message: string) {
return send({ type: "info", message });
} }
function enqueue(chunk: string) { function error(message: string) {
controller?.enqueue(new TextEncoder().encode("$" + chunk)); return send({ type: "error", message });
}
function warning(message: string) {
return send({ type: "warning", message });
} }
return { return {
response, response,
cancel, cancel,
enqueue, send,
info,
error,
warning,
}; };
}; };
@@ -102,17 +158,14 @@ export function debounce<T extends (...args: Parameters<T>) => void>(
} }
export function parseRating(rating: string | number) { export function parseRating(rating: string | number) {
if (typeof rating === "string") { if (typeof rating == "number") return rating;
try { try {
const res = parseInt(rating); const res = parseInt(rating);
if (!Number.isNaN(res)) return res; if (!Number.isNaN(res)) return res;
} catch (_e) { } catch (_e) {
// This is okay // This is okay
} }
return rating.length / 2; return rating.length / 2;
}
return rating;
} }
export async function convertOggToMp3( export async function convertOggToMp3(

View File

@@ -1,286 +1,93 @@
import { useEffect, useMemo, useRef } from "preact/hooks"; import { useEffect, useMemo, useRef } from "preact/hooks";
export interface CallOptions { type Debounced<T extends (...args: unknown[]) => unknown> =
/** & ((
* Controls if the function should be invoked on the leading edge of the timeout. ...args: Parameters<T>
*/ ) => void)
leading?: boolean; & {
/**
* Controls if the function should be invoked on the trailing edge of the timeout.
*/
trailing?: boolean;
}
export interface Options extends CallOptions {
/**
* The maximum time the given function is allowed to be delayed before it's invoked.
*/
maxWait?: number;
}
export interface ControlFunctions {
/**
* Cancel pending function invocations
*/
cancel: () => void; cancel: () => void;
/**
* Immediately invoke pending function invocations
*/
flush: () => void; flush: () => void;
/** pending: () => boolean;
* Returns `true` if there are any pending function invocations };
*/
isPending: () => boolean;
}
/**
* Subsequent calls to the debounced function `debounced.callback` return the result of the last func invocation.
* Note, that if there are no previous invocations it's mean you will get undefined. You should check it in your code properly.
*/
export interface DebouncedState<T extends (...args: any) => ReturnType<T>>
extends ControlFunctions {
(...args: Parameters<T>): ReturnType<T> | undefined;
}
/**
* Creates a debounced function that delays invoking `func` until after `wait`
* milliseconds have elapsed since the last time the debounced function was
* invoked, or until the next browser frame is drawn.
*
* The debounced function comes with a `cancel` method to cancel delayed `func`
* invocations and a `flush` method to immediately invoke them.
*
* Provide `options` to indicate whether `func` should be invoked on the leading
* and/or trailing edge of the `wait` timeout. The `func` is invoked with the
* last arguments provided to the debounced function.
*
* Subsequent calls to the debounced function return the result of the last
* `func` invocation.
*
* **Note:** If `leading` and `trailing` options are `true`, `func` is
* invoked on the trailing edge of the timeout only if the debounced function
* is invoked more than once during the `wait` timeout.
*
* If `wait` is `0` and `leading` is `false`, `func` invocation is deferred
* until the next tick, similar to `setTimeout` with a timeout of `0`.
*
* If `wait` is omitted in an environment with `requestAnimationFrame`, `func`
* invocation will be deferred until the next frame is drawn (typically about
* 16ms).
*
* See [David Corbacho's article](https://css-tricks.com/debouncing-throttling-explained-examples/)
* for details over the differences between `debounce` and `throttle`.
*
* @category Function
* @param {Function} func The function to debounce.
* @param {number} [wait=0]
* The number of milliseconds to delay; if omitted, `requestAnimationFrame` is
* used (if available, otherwise it will be setTimeout(...,0)).
* @param {Object} [options={}] The options object.
* Controls if `func` should be invoked on the leading edge of the timeout.
* @param {boolean} [options.leading=false]
* The maximum time `func` is allowed to be delayed before it's invoked.
* @param {number} [options.maxWait]
* Controls if `func` should be invoked the trailing edge of the timeout.
* @param {boolean} [options.trailing=true]
* @returns {Function} Returns the new debounced function.
* @example
*
* // Avoid costly calculations while the window size is in flux.
* const resizeHandler = useDebouncedCallback(calculateLayout, 150);
* window.addEventListener('resize', resizeHandler)
*
* // Invoke `sendMail` when clicked, debouncing subsequent calls.
* const clickHandler = useDebouncedCallback(sendMail, 300, {
* leading: true,
* trailing: false,
* })
* <button onClick={clickHandler}>click me</button>
*
* // Ensure `batchLog` is invoked once after 1 second of debounced calls.
* const debounced = useDebouncedCallback(batchLog, 250, { 'maxWait': 1000 })
* const source = new EventSource('/stream')
* source.addEventListener('message', debounced)
*
* // Cancel the trailing debounced invocation.
* window.addEventListener('popstate', debounced.cancel)
*
* // Check for pending invocations.
* const status = debounced.pending() ? "Pending..." : "Ready"
*/
export default function useDebouncedCallback< export default function useDebouncedCallback<
T extends (...args: any) => ReturnType<T>, T extends (...args: unknown[]) => unknown,
>( >(
func: T, callback: T,
wait?: number, delay: number,
options?: Options, options?: {
): DebouncedState<T> { /** Call on the leading edge. Default: false */
const lastCallTime = useRef(null); leading?: boolean;
const lastInvokeTime = useRef(0); /** Call on the trailing edge. Default: true */
const timerId = useRef(null); trailing?: boolean;
const lastArgs = useRef<unknown[]>([]); },
const lastThis = useRef<unknown>(); ): Debounced<T> {
const result = useRef<ReturnType<T>>(); const callbackRef = useRef(callback);
const funcRef = useRef(func); const timerRef = useRef<number | null>(null);
const mounted = useRef(true); const argsRef = useRef<Parameters<T> | null>(null);
// Always use the latest callback without re-creating the debounced fn
useEffect(() => { useEffect(() => {
funcRef.current = func; callbackRef.current = callback;
}, [func]); }, [callback]);
// Bypass `requestAnimationFrame` by explicitly setting `wait=0`. const leading = !!options?.leading;
const useRAF = !wait && wait !== 0 && typeof window !== "undefined"; const trailing = options?.trailing !== false; // default true
if (typeof func !== "function") { const debounced = useMemo<Debounced<T>>(() => {
throw new TypeError("Expected a function"); const clear = () => {
if (timerRef.current != null) {
clearTimeout(timerRef.current);
timerRef.current = null;
} }
wait = +wait || 0;
options = options || {};
const leading = !!options.leading;
const trailing = "trailing" in options ? !!options.trailing : true; // `true` by default
const maxing = "maxWait" in options;
const maxWait = maxing ? Math.max(+options.maxWait || 0, wait) : null;
useEffect(() => {
mounted.current = true;
return () => {
mounted.current = false;
};
}, []);
// You may have a question, why we have so many code under the useMemo definition.
//
// This was made as we want to escape from useCallback hell and
// not to initialize a number of functions each time useDebouncedCallback is called.
//
// It means that we have less garbage for our GC calls which improves performance.
// Also, it makes this library smaller.
//
// And the last reason, that the code without lots of useCallback with deps is easier to read.
// You have only one place for that.
const debounced = useMemo(() => {
const invokeFunc = (time: number) => {
const args = lastArgs.current;
const thisArg = lastThis.current;
lastArgs.current = lastThis.current = null;
lastInvokeTime.current = time;
return (result.current = funcRef.current.apply(thisArg, args));
}; };
const startTimer = (pendingFunc: () => void, wait: number) => { const invoke = () => {
if (useRAF) cancelAnimationFrame(timerId.current); const a = argsRef.current;
timerId.current = useRAF argsRef.current = null;
? requestAnimationFrame(pendingFunc) if (a) {
: setTimeout(pendingFunc, wait); callbackRef.current(...a);
};
const shouldInvoke = (time: number) => {
if (!mounted.current) return false;
const timeSinceLastCall = time - lastCallTime.current;
const timeSinceLastInvoke = time - lastInvokeTime.current;
// Either this is the first call, activity has stopped and we're at the
// trailing edge, the system time has gone backwards and we're treating
// it as the trailing edge, or we've hit the `maxWait` limit.
return (
!lastCallTime.current ||
timeSinceLastCall >= wait ||
timeSinceLastCall < 0 ||
(maxing && timeSinceLastInvoke >= maxWait)
);
};
const trailingEdge = (time: number) => {
timerId.current = null;
// Only invoke if we have `lastArgs` which means `func` has been
// debounced at least once.
if (trailing && lastArgs.current) {
return invokeFunc(time);
} }
lastArgs.current = lastThis.current = null;
return result.current;
}; };
const timerExpired = () => { const fn = ((...args: Parameters<T>) => {
const time = Date.now(); const shouldCallLeading = leading && timerRef.current == null;
if (shouldInvoke(time)) {
return trailingEdge(time);
}
// https://github.com/xnimorz/use-debounce/issues/97
if (!mounted.current) {
return;
}
// Remaining wait calculation
const timeSinceLastCall = time - lastCallTime.current;
const timeSinceLastInvoke = time - lastInvokeTime.current;
const timeWaiting = wait - timeSinceLastCall;
const remainingWait = maxing
? Math.min(timeWaiting, maxWait - timeSinceLastInvoke)
: timeWaiting;
// Restart the timer argsRef.current = args;
startTimer(timerExpired, remainingWait);
if (timerRef.current != null) clearTimeout(timerRef.current);
timerRef.current = globalThis.setTimeout(() => {
timerRef.current = null;
if (trailing) invoke();
}, delay);
if (shouldCallLeading) {
// Leading edge call happens immediately
invoke();
}
}) as Debounced<T>;
fn.cancel = () => {
argsRef.current = null;
clear();
}; };
const func: DebouncedState<T> = (...args: Parameters<T>): ReturnType<T> => { fn.flush = () => {
const time = Date.now(); if (timerRef.current != null) {
const isInvoking = shouldInvoke(time); clear();
invoke();
lastArgs.current = args;
lastThis.current = this;
lastCallTime.current = time;
if (isInvoking) {
if (!timerId.current && mounted.current) {
// Reset any `maxWait` timer.
lastInvokeTime.current = lastCallTime.current;
// Start the timer for the trailing edge.
startTimer(timerExpired, wait);
// Invoke the leading edge.
return leading ? invokeFunc(lastCallTime.current) : result.current;
} }
if (maxing) {
// Handle invocations in a tight loop.
startTimer(timerExpired, wait);
return invokeFunc(lastCallTime.current);
}
}
if (!timerId.current) {
startTimer(timerExpired, wait);
}
return result.current;
}; };
func.cancel = () => { fn.pending = () => timerRef.current != null;
if (timerId.current) {
useRAF
? cancelAnimationFrame(timerId.current)
: clearTimeout(timerId.current);
}
lastInvokeTime.current = 0;
lastArgs.current =
lastCallTime.current =
lastThis.current =
timerId.current =
null;
};
func.isPending = () => { return fn;
return !!timerId.current; // Recreate only if timing/edge behavior changes
}; }, [delay, leading, trailing]);
func.flush = () => { // Cancel on unmount
return !timerId.current ? result.current : trailingEdge(Date.now()); useEffect(() => () => debounced.cancel(), [debounced]);
};
return func;
}, [leading, maxing, wait, maxWait, trailing, useRAF]);
return debounced; return debounced;
} }

View File

@@ -6,7 +6,7 @@ export function useEventListener<T extends Event>(
element: typeof globalThis | HTMLElement = globalThis, element: typeof globalThis | HTMLElement = globalThis,
) { ) {
// Create a ref that stores handler // Create a ref that stores handler
const savedHandler = useRef<(event: Event) => void>(); const savedHandler = useRef<(event: T) => void>();
// Update ref.current value if handler changes. // Update ref.current value if handler changes.
// This allows our effect below to always get latest handler ... // This allows our effect below to always get latest handler ...
@@ -27,11 +27,11 @@ export function useEventListener<T extends Event>(
const eventListener = (event: T) => savedHandler?.current?.(event); const eventListener = (event: T) => savedHandler?.current?.(event);
// Add event listener // Add event listener
element.addEventListener(eventName, eventListener); element.addEventListener(eventName, (ev) => eventListener(ev as T));
// Remove event listener on cleanup // Remove event listener on cleanup
return () => { return () => {
element.removeEventListener(eventName, eventListener); element.removeEventListener(eventName, (ev) => eventListener(ev as T));
}; };
}, },
[eventName, element], // Re-run if eventName or element changes [eventName, element], // Re-run if eventName or element changes

View File

@@ -6,7 +6,7 @@ type ThrottleOptions = {
}; };
const useThrottledCallback = ( const useThrottledCallback = (
callback: (...args: any[]) => void, callback: (...args: unknown[]) => void,
delay: number, delay: number,
options: ThrottleOptions = {}, options: ThrottleOptions = {},
) => { ) => {
@@ -24,7 +24,7 @@ const useThrottledCallback = (
}; };
}, [timer]); }, [timer]);
const throttledCallback = (...args: any[]) => { const throttledCallback = (...args: unknown[]) => {
const now = Date.now(); const now = Date.now();
if (leading && !isLeading) { if (leading && !isLeading) {
@@ -52,4 +52,3 @@ const useThrottledCallback = (
}; };
export default useThrottledCallback; export default useThrottledCallback;

View File

@@ -1,14 +1,17 @@
import { useEffect, useRef } from "preact/hooks"; import { useEffect, useRef } from "preact/hooks";
export function useTraceUpdate(props) { export function useTraceUpdate(props: Record<string, unknown>) {
const prev = useRef(props); const prev = useRef(props);
useEffect(() => { useEffect(() => {
const changedProps = Object.entries(props).reduce((ps, [k, v]) => { const changedProps = Object.entries(props).reduce(
(ps: Record<string, unknown>, [k, v]) => {
if (prev.current[k] !== v) { if (prev.current[k] !== v) {
ps[k] = [prev.current[k], v]; ps[k] = [prev.current[k], v];
} }
return ps; return ps;
}, {}); },
{},
);
if (Object.keys(changedProps).length > 0) { if (Object.keys(changedProps).length > 0) {
console.log("Changed props:", changedProps); console.log("Changed props:", changedProps);
} }

View File

@@ -134,7 +134,7 @@ async function getLocalImage(
*/ */
async function storeLocalImage( async function storeLocalImage(
url: string, url: string,
content: ArrayBuffer, content: Uint8Array<ArrayBuffer> | ArrayBuffer,
{ width, height }: { width?: number; height?: number } = {}, { width, height }: { width?: number; height?: number } = {},
) { ) {
const isValid = await verifyImage(new Uint8Array(content)); const isValid = await verifyImage(new Uint8Array(content));
@@ -249,7 +249,7 @@ async function verifyImage(imageBuffer: Uint8Array): Promise<boolean> {
export async function getImageContent( export async function getImageContent(
url: string, url: string,
{ width, height }: { width?: number; height?: number } = {}, { width, height }: { width?: number; height?: number } = {},
): Promise<{ content: ArrayBuffer; mimeType: string }> { ): Promise<{ content: Uint8Array<ArrayBuffer>; mimeType: string }> {
log.debug("Getting image content", { url, width, height }); log.debug("Getting image content", { url, width, height });
// Check if we have the image metadata in database // Check if we have the image metadata in database
@@ -267,8 +267,8 @@ export async function getImageContent(
// Fetch and cache original if needed // Fetch and cache original if needed
if (!originalImage) { if (!originalImage) {
const fetchedImage = await getRemoteImage(url); const fetchedImage = await getRemoteImage(url);
await storeLocalImage(url, fetchedImage.buffer);
originalImage = new Uint8Array(fetchedImage.buffer); originalImage = new Uint8Array(fetchedImage.buffer);
await storeLocalImage(url, originalImage);
} }
// Resize image // Resize image

3
lib/kmenu.ts Normal file
View File

@@ -0,0 +1,3 @@
import { signal } from "@preact/signals";
export const isKMenuOpen = signal(false);

View File

@@ -41,7 +41,6 @@ export async function getLogs() {
date: new Date(date), date: new Date(date),
} as Log; } as Log;
}); });
console.log(logs);
// Return the logs sorted by date // Return the logs sorted by date
return logs.sort((a, b) => a.date.getTime() - b.date.getTime()); return logs.sort((a, b) => a.date.getTime() - b.date.getTime());

View File

@@ -38,13 +38,13 @@ export function createLogger(scope: string, _options?: LoggerOptions): Logger {
export function loggerFromStream(stream: StreamResponse) { export function loggerFromStream(stream: StreamResponse) {
return { return {
debug: (...data: unknown[]) => debug: (...data: unknown[]) =>
stream.enqueue(`${data.length > 1 ? data.join(" ") : data[0]}`), stream.info(`${data.length > 1 ? data.join(" ") : data[0]}`),
info: (...data: unknown[]) => info: (...data: unknown[]) =>
stream.enqueue(`${data.length > 1 ? data.join(" ") : data[0]}`), stream.info(`${data.length > 1 ? data.join(" ") : data[0]}`),
error: (...data: unknown[]) => error: (...data: unknown[]) =>
stream.enqueue(`[ERROR]: ${data.length > 1 ? data.join(" ") : data[0]}`), stream.error(`[ERROR]: ${data.length > 1 ? data.join(" ") : data[0]}`),
warn: (...data: unknown[]) => warn: (...data: unknown[]) =>
stream.enqueue(`[WARN]: ${data.length > 1 ? data.join(" ") : data[0]}`), stream.warning(`[WARN]: ${data.length > 1 ? data.join(" ") : data[0]}`),
}; };
} }

View File

@@ -1,10 +1,8 @@
import { MARKA_API_KEY } from "../env.ts"; import { createCache } from "../cache.ts";
import { MARKA_API_KEY, MARKA_API_URL } from "../env.ts";
import { getImage } from "../image.ts"; import { getImage } from "../image.ts";
import { GenericResource } from "./schema.ts"; import { GenericResource } from "./schema.ts";
const url = `https://marka.max-richter.dev`;
//const url = "http://localhost:8080";
async function addImageToResource<T extends GenericResource>( async function addImageToResource<T extends GenericResource>(
resource: GenericResource, resource: GenericResource,
): Promise<T> { ): Promise<T> {
@@ -14,7 +12,7 @@ async function addImageToResource<T extends GenericResource>(
const absoluteImageUrl = (imageUrl.startsWith("https://") || const absoluteImageUrl = (imageUrl.startsWith("https://") ||
imageUrl.startsWith("http://")) imageUrl.startsWith("http://"))
? imageUrl ? imageUrl
: `${url}/${imageUrl}`; : `${MARKA_API_URL}/${imageUrl}`;
const image = await getImage(absoluteImageUrl); const image = await getImage(absoluteImageUrl);
return { ...resource, image } as T; return { ...resource, image } as T;
} catch (e) { } catch (e) {
@@ -24,34 +22,68 @@ async function addImageToResource<T extends GenericResource>(
return resource as T; return resource as T;
} }
type Resource = GenericResource & {
content: GenericResource["content"] & Array<GenericResource>;
};
const fetchCache = createCache<Resource>("marka");
const cacheLock = new Map<string, Promise<Resource>>();
async function fetchAndStoreUrl(url: string) {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Failed to fetch resource: ${response.status}`);
}
const res = await response.json();
fetchCache.set(url, res);
return res;
}
async function cachedFetch(
url: string,
): Promise<Resource | undefined> {
if (fetchCache.has(url)) {
fetchAndStoreUrl(url); // Fetch the url in the background
return fetchCache.get(url);
}
if (cacheLock.has(url)) return cacheLock.get(url);
const response = fetchAndStoreUrl(url);
cacheLock.set(url, response);
const res = await response;
cacheLock.delete(url);
return res;
}
export async function fetchResource<T extends GenericResource>( export async function fetchResource<T extends GenericResource>(
resource: string, resource: string,
): Promise<T | undefined> { ): Promise<T | undefined> {
try { try {
const response = await fetch( const d = `${MARKA_API_URL}/resources/${resource}`;
`${url}/resources/${resource}`, const res = await cachedFetch(d);
); if (!res) return;
const res = await response.json();
return addImageToResource<T>(res); return addImageToResource<T>(res);
} catch (_e) { } catch (_e) {
return; return;
} }
} }
export async function listResources<T = GenericResource>( export async function listResources<T extends GenericResource>(
resource: string, resource: string,
): Promise<T[]> { ): Promise<T[]> {
try { try {
const response = await fetch( const d = `${MARKA_API_URL}/resources/${resource}`;
`${url}/resources/${resource}`, const list = await cachedFetch(d);
); if (!list) return [];
const list = await response.json();
return Promise.all( return Promise.all(
list?.content list?.content
.filter((a: GenericResource) => a?.content?._type) .filter((a) => a?.content?._type)
.map((res: GenericResource) => addImageToResource(res)), .map((res) => addImageToResource(res) as Promise<T>),
); );
} catch (_e) { } catch (_e) {
console.log(`Failed to fetch resource: ${resource}`, _e);
return []; return [];
} }
} }
@@ -62,7 +94,7 @@ export async function createResource(
) { ) {
const isJson = typeof content === "object" && const isJson = typeof content === "object" &&
!(content instanceof ArrayBuffer); !(content instanceof ArrayBuffer);
const fetchUrl = `${url}/resources/${path}`; const fetchUrl = `${MARKA_API_URL}/resources/${path}`;
const headers = new Headers(); const headers = new Headers();
headers.append("Content-Type", isJson ? "application/json" : ""); headers.append("Content-Type", isJson ? "application/json" : "");
if (MARKA_API_KEY) { if (MARKA_API_KEY) {
@@ -74,7 +106,12 @@ export async function createResource(
body: isJson ? JSON.stringify(content) : content, body: isJson ? JSON.stringify(content) : content,
}); });
if (!response.ok) { if (!response.ok) {
throw new Error(`Failed to create resource: ${response.status}`); const text = await response.text();
throw new Error(
`failed to create resource (resources/${path}): ${
text || response.status
}`,
);
} }
return response.json(); return response.json();
} }

View File

@@ -68,6 +68,7 @@ export const ReviewContentSchema = makeContentSchema("Review", {
export const RecipeContentSchema = makeContentSchema("Recipe", { export const RecipeContentSchema = makeContentSchema("Recipe", {
description: z.string().optional(), description: z.string().optional(),
name: z.string().optional(), name: z.string().optional(),
reviewRating: ReviewRatingSchema.optional(),
recipeIngredient: z.array(z.string()).optional(), recipeIngredient: z.array(z.string()).optional(),
recipeInstructions: z.array(z.string()).optional(), recipeInstructions: z.array(z.string()).optional(),
totalTime: z.string().optional(), totalTime: z.string().optional(),
@@ -124,3 +125,16 @@ export type RecipeResource = z.infer<typeof RecipeSchema> & {
export type GenericResource = z.infer<typeof GenericResourceSchema> & { export type GenericResource = z.infer<typeof GenericResourceSchema> & {
image?: typeof imageTable.$inferSelect; image?: typeof imageTable.$inferSelect;
}; };
export function getNameOfResource(res: GenericResource): string {
if (res.content?._type === "Article" && res.content.headline) {
return res.content.headline;
}
if (res.content?._type === "Review" && res.content.itemReviewed?.name) {
return res.content.itemReviewed.name;
}
if (res.content?._type === "Recipe" && res.content.name) {
return res.content.name;
}
return "Unnamed Resource";
}

View File

@@ -1,5 +1,5 @@
import OpenAI from "https://deno.land/x/openai@v4.69.0/mod.ts"; import OpenAI, { toFile } from "@openai/openai";
import { zodResponseFormat } from "https://deno.land/x/openai@v4.69.0/helpers/zod.ts"; import { zodResponseFormat } from "@openai/openai/helpers/zod";
import { OPENAI_API_KEY } from "@lib/env.ts"; import { OPENAI_API_KEY } from "@lib/env.ts";
import { hashString } from "@lib/helpers.ts"; import { hashString } from "@lib/helpers.ts";
import { createCache } from "@lib/cache.ts"; import { createCache } from "@lib/cache.ts";
@@ -35,7 +35,8 @@ export async function summarize(content: string) {
{ {
role: "user", role: "user",
content: content:
`Please summarize the article in one sentence as short as possible: ${content.slice(0, 2000) `Please summarize the article in one sentence as short as possible: ${
content.slice(0, 2000)
}`, }`,
}, },
], ],
@@ -102,7 +103,8 @@ export async function createGenres(
{ {
role: "system", role: "system",
content: content:
`you create some keywords that can be used in a recommendation system. The keywords are based on a ${type} description or title. If you do not know the title, take into account the description aswell. Create a range of keywords from very specific ones that describe the general vibe. ${title ? `The name of the ${type} is ${title}` : "" `you create some keywords that can be used in a recommendation system. The keywords are based on a ${type} description or title. If you do not know the title, take into account the description aswell. Create a range of keywords from very specific ones that describe the general vibe. ${
title ? `The name of the ${type} is ${title}` : ""
}. Return a list of around 20 keywords seperated by commas`, }. Return a list of around 20 keywords seperated by commas`,
}, },
{ {
@@ -166,7 +168,8 @@ export const getMovieRecommendations = async (
${keywords} ${keywords}
The movies should be similar to but not include ${exclude.join(", ") The movies should be similar to but not include ${
exclude.join(", ")
} or remakes of that. } or remakes of that.
respond with a plain unordered list each item starting with the year the movie was released and then the title of the movie seperated by a -`, respond with a plain unordered list each item starting with the year the movie was released and then the title of the movie seperated by a -`,
@@ -192,6 +195,23 @@ respond with a plain unordered list each item starting with the year the movie w
return recommendations; return recommendations;
}; };
export async function createUnsplashSearchTerm(content: string) {
if (!openAI) return;
const chatCompletion = await openAI.chat.completions.create({
model: model,
messages: [
{
role: "system",
content:
"Please respond with a search term for unsplash for the following article",
},
{ role: "user", content: content.slice(0, 10_000) },
],
});
return chatCompletion.choices[0].message.content?.toLowerCase();
}
export async function createTags(content: string) { export async function createTags(content: string) {
if (!openAI) return; if (!openAI) return;
const chatCompletion = await openAI.chat.completions.create({ const chatCompletion = await openAI.chat.completions.create({
@@ -213,7 +233,7 @@ export async function createTags(content: string) {
export async function extractRecipe(content: string) { export async function extractRecipe(content: string) {
if (!openAI) return; if (!openAI) return;
const completion = await openAI.beta.chat.completions.parse({ const completion = await openAI.chat.completions.parse({
model: model, model: model,
temperature: 0.1, temperature: 0.1,
messages: [ messages: [
@@ -231,7 +251,7 @@ export async function extractRecipe(content: string) {
export async function extractArticleMetadata(content: string) { export async function extractArticleMetadata(content: string) {
if (!openAI) return; if (!openAI) return;
const completion = await openAI.beta.chat.completions.parse({ const completion = await openAI.chat.completions.parse({
model: model, model: model,
temperature: 0.1, temperature: 0.1,
messages: [ messages: [
@@ -256,7 +276,7 @@ export async function transcribe(
): Promise<string | undefined> { ): Promise<string | undefined> {
if (!openAI) return; if (!openAI) return;
const file = new File([mp3Data], "audio.mp3", { const file = await toFile(mp3Data, "audio.mp3", {
type: "audio/mpeg", type: "audio/mpeg",
}); });

View File

@@ -1,7 +1,7 @@
import { import {
parseIngredient, parseIngredient,
unitsOfMeasure as _unitsOfMeasure, unitsOfMeasure as _unitsOfMeasure,
} from "https://esm.sh/parse-ingredient@1.2.1"; } from "parse-ingredient";
import { Ingredient, IngredientGroup } from "@lib/recipeSchema.ts"; import { Ingredient, IngredientGroup } from "@lib/recipeSchema.ts";
import { removeMarkdownFormatting } from "@lib/string.ts"; import { removeMarkdownFormatting } from "@lib/string.ts";
@@ -77,7 +77,7 @@ export function parseIngredients(
}; };
const unit = ingredient.unit.toLowerCase() as keyof typeof unitsOfMeasure; const unit = ingredient.unit.toLowerCase() as keyof typeof unitsOfMeasure;
if (unit in unitsOfMeasure && unit !== "cup") { if (unit in unitsOfMeasure) {
ingredient.unit = unitsOfMeasure[unit].short; ingredient.unit = unitsOfMeasure[unit].short;
} }

View File

@@ -9,7 +9,7 @@ export async function fetchHtmlWithPlaywright(
fetchUrl: string, fetchUrl: string,
streamResponse: ReturnType<typeof createStreamResponse>, streamResponse: ReturnType<typeof createStreamResponse>,
): Promise<string> { ): Promise<string> {
streamResponse.enqueue("booting up playwright"); streamResponse.info("booting up playwright");
const config: Parameters<typeof firefox.launch>[0] = {}; const config: Parameters<typeof firefox.launch>[0] = {};
if (env.PROXY_SERVER) { if (env.PROXY_SERVER) {
@@ -24,7 +24,7 @@ export async function fetchHtmlWithPlaywright(
// Launch the Playwright browser // Launch the Playwright browser
const browser = await firefox.launch(config); const browser = await firefox.launch(config);
streamResponse.enqueue("fetching html"); streamResponse.info("fetching html");
try { try {
// Open a new browser context and page // Open a new browser context and page
@@ -42,7 +42,7 @@ export async function fetchHtmlWithPlaywright(
return html; return html;
} catch (error) { } catch (error) {
streamResponse.enqueue("error fetching html"); streamResponse.error("error fetching html");
console.error(error); console.error(error);
return ""; return "";
} finally { } finally {

View File

@@ -18,8 +18,9 @@ export const IngredientGroupSchema = z.object({
export type IngredientGroup = z.infer<typeof IngredientGroupSchema>; export type IngredientGroup = z.infer<typeof IngredientGroupSchema>;
const recipeSchema = z.object({ const recipeSchema = z.object({
_type: z.literal("Recipe"),
name: z.string().describe( name: z.string().describe(
"Title of the Recipe, without the name of the website or author", "Name of the Recipe, without the name of the website or author",
), ),
description: z.string().describe( description: z.string().describe(
"Optional, short description of the recipe", "Optional, short description of the recipe",
@@ -41,11 +42,7 @@ const recipeSchema = z.object({
export type Recipe = z.infer<typeof recipeSchema>; export type Recipe = z.infer<typeof recipeSchema>;
const noRecipeSchema = z.object({ const noRecipeSchema = z.literal("none").describe("No Recipe found");
errorMessages: z.array(z.string()).describe(
"List of error messages, if no recipe was found",
),
});
export const recipeResponseSchema = z.union([recipeSchema, noRecipeSchema]); export const recipeResponseSchema = z.union([recipeSchema, noRecipeSchema]);

View File

@@ -2,9 +2,9 @@ import * as openai from "@lib/openai.ts";
import * as tmdb from "@lib/tmdb.ts"; import * as tmdb from "@lib/tmdb.ts";
import { parseRating } from "@lib/helpers.ts"; import { parseRating } from "@lib/helpers.ts";
import { createCache } from "@lib/cache.ts"; import { createCache } from "@lib/cache.ts";
import { GenericResource, ReviewResource } from "./marka/schema.ts"; import { ReviewResource } from "./marka/schema.ts";
type RecommendationResource = { export type RecommendationResource = {
id: string; id: string;
type: string; type: string;
rating: number; rating: number;
@@ -74,7 +74,7 @@ export function getRecommendation(
} }
export async function getSimilarMovies(id: string) { export async function getSimilarMovies(id: string) {
const recs = getRecommendation(id, "movie"); const recs = getRecommendation(id, "movies");
if (!recs?.keywords?.length) return; if (!recs?.keywords?.length) return;
const recommendations = await openai.getMovieRecommendations( const recommendations = await openai.getMovieRecommendations(
@@ -96,5 +96,5 @@ export async function getAllRecommendations(): Promise<
> { > {
const keys = cache.keys(); const keys = cache.keys();
const res = await Promise.all(keys.map((k) => cache.get(k))); const res = await Promise.all(keys.map((k) => cache.get(k)));
return res.map((r) => JSON.parse(r)); return res.filter((s) => !!s).map((r) => JSON.parse(r));
} }

View File

@@ -1,26 +1,26 @@
export const resources = { export const resources = {
"home": { "home": {
emoji: "House with Garden.png", emoji: "home_icon.png",
name: "Home", name: "Home",
link: "/", link: "/",
}, },
"recipe": { "recipe": {
emoji: "Fork and Knife with Plate.png", emoji: "recipes_icon.png",
name: "Recipes", name: "Recipes",
link: "/recipes", link: "/recipes",
}, },
"movie": { "movie": {
emoji: "Popcorn.png", emoji: "movies_icon.png",
name: "Movies", name: "Movies",
link: "/movies", link: "/movies",
}, },
"article": { "article": {
emoji: "Writing Hand Medium-Light Skin Tone.png", emoji: "articles_icon.png",
name: "Articles", name: "Articles",
link: "/articles", link: "/articles",
}, },
"series": { "series": {
emoji: "Television.png", emoji: "tv_series_icon.png",
name: "Series", name: "Series",
link: "/series", link: "/series",
}, },

View File

@@ -1,8 +1,9 @@
import { resources } from "@lib/resources.ts"; import { resources } from "@lib/resources.ts";
import fuzzysort from "npm:fuzzysort"; import fuzzysort from "fuzzysort";
import { extractHashTags } from "@lib/string.ts"; import { extractHashTags } from "@lib/string.ts";
import { listResources } from "./marka/index.ts"; import { listResources } from "./marka/index.ts";
import { GenericResource } from "./marka/schema.ts"; import { GenericResource } from "./marka/schema.ts";
import { parseRating } from "./helpers.ts";
type ResourceType = keyof typeof resources; type ResourceType = keyof typeof resources;
@@ -18,7 +19,7 @@ export function parseResourceUrl(_url: string | URL): SearchParams | undefined {
try { try {
const url = typeof _url === "string" ? new URL(_url) : _url; const url = typeof _url === "string" ? new URL(_url) : _url;
let query = url.searchParams.get("q") || "*"; let query = url.searchParams.get("q") || "*";
if (!query) { if (!(typeof query === "string")) {
return undefined; return undefined;
} }
query = decodeURIComponent(query); query = decodeURIComponent(query);
@@ -53,12 +54,14 @@ export async function searchResource(
{ q, tags = [], types, rating }: SearchParams, { q, tags = [], types, rating }: SearchParams,
): Promise<GenericResource[]> { ): Promise<GenericResource[]> {
const resources = (await Promise.all([ const resources = (await Promise.all([
(!types || types.includes("movie")) && listResources("movies"), (!types || types.includes("movies")) && listResources("movies"),
(!types || types.includes("series")) && listResources("series"), (!types || types.includes("series")) && listResources("series"),
(!types || types.includes("article")) && listResources("articles"), (!types || types.includes("articles")) && listResources("articles"),
(!types || types.includes("recipe")) && listResources("recipes"), (!types || types.includes("recipes")) && listResources("recipes"),
])).flat().filter(isResource); ])).flat().filter(isResource);
console.log({ types, rating, tags, q, resourceLength: resources.length });
const results: Record<string, GenericResource> = {}; const results: Record<string, GenericResource> = {};
for (const resource of resources) { for (const resource of resources) {
@@ -70,16 +73,26 @@ export async function searchResource(
results[resource.name] = resource; results[resource.name] = resource;
} }
// Select not-rated resources
if ( if (
!(resource.name in results) && rating === 0 &&
rating && resource?.content?.reviewRating && resource.content?.reviewRating?.ratingValue === undefined
resource.content?.reviewRating?.ratingValue >= rating ) {
results[resource.name] = resource;
}
if (
typeof rating === "number" &&
rating !== 0 &&
resource.content.reviewRating?.ratingValue &&
parseRating(resource.content.reviewRating.ratingValue) >= rating
) { ) {
results[resource.name] = resource; results[resource.name] = resource;
} }
} }
if (q.length && q !== "*") { if (q.length && q !== "*") {
q = decodeURIComponent(q);
const fuzzyResult = fuzzysort.go(q, resources, { const fuzzyResult = fuzzysort.go(q, resources, {
keys: [ keys: [
"name", "name",
@@ -91,6 +104,7 @@ export async function searchResource(
], ],
threshold: 0.3, threshold: 0.3,
}); });
console.log({ fuzzyResult });
for (const result of fuzzyResult) { for (const result of fuzzyResult) {
results[result.obj.name] = result.obj; results[result.obj.name] = result.obj;
} }

View File

@@ -1,25 +1,37 @@
import { resources } from "@lib/resources.ts"; export function formatDate(date?: string | Date): string {
if (!date) return "";
export function formatDate(date: Date): string { if (typeof date === "string") {
try {
const d = new Date(date);
return formatDate(d);
} catch (_e) {
return "";
}
}
const options = { year: "numeric", month: "long", day: "numeric" } as const; const options = { year: "numeric", month: "long", day: "numeric" } as const;
return new Intl.DateTimeFormat("en-US", options).format(date); return new Intl.DateTimeFormat("en-US", options).format(date);
} }
export function safeFileName(inputString: string): string { export function safeFileName(input: string): string {
let fileName = inputString.toLowerCase(); return input
fileName = fileName.replace(/ /g, "_"); .normalize("NFKD")
fileName = fileName.replace(/[^\w.-]/g, ""); .replace(/[\u0300-\u036f]/g, "")
fileName = fileName.replaceAll(":", ""); .replace(/[\s-]+/g, "_")
return fileName; .replace(/-+/g, "-")
.replace(/[^A-Za-z0-9_]+/g, "")
.replace(/_+/g, "_")
// Trim underscores/dots from ends and prevent leading dots
.replace(/^[_\.]+|[_\.]+$/g, "").replace(/^\.+/, "")
.toLowerCase();
} }
export function toUrlSafeString(input: string): string { export function toUrlSafeString(input: string): string {
return input return input
.trim() // Remove leading and trailing whitespace .normalize("NFKD")
.toLowerCase() // Convert to lowercase .replace(/[\u0300-\u036f]/g, "")
.replace(/[^a-z0-9\s-]/g, "") // Remove non-alphanumeric characters except spaces and hyphens .replace(/[^A-Za-z0-9 _-]+/g, "")
.replace(/\s+/g, "-") // Replace spaces with hyphens .replace(/\s+/g, " ")
.replace(/-+/g, "-"); // Remove consecutive hyphens .trim();
} }
export function extractHashTags(inputString: string) { export function extractHashTags(inputString: string) {

View File

@@ -48,7 +48,8 @@ export async function endTask(chatId: string): Promise<string | null> {
finalNote += "**[Voice message could not be transcribed]**\n\n"; finalNote += "**[Voice message could not be transcribed]**\n\n";
} }
} else if (entry.type === "photo") { } else if (entry.type === "photo") {
const photoUrl = `${task.noteName.replace(/\.md$/, "") const photoUrl = `${
task.noteName.replace(/\.md$/, "")
}/photo-${photoIndex++}.jpg`; }/photo-${photoIndex++}.jpg`;
finalNote += `**Photo**:\n ${photoUrl}\n\n`; finalNote += `**Photo**:\n ${photoUrl}\n\n`;

View File

@@ -1,4 +1,4 @@
import * as thumbhash from "https://esm.sh/thumbhash@0.1.1"; import * as thumbhash from "thumbhash";
export function generateThumbhash(buffer: Uint8Array, w: number, h: number) { export function generateThumbhash(buffer: Uint8Array, w: number, h: number) {
const hash = thumbhash.rgbaToThumbHash(w, h, buffer); const hash = thumbhash.rgbaToThumbHash(w, h, buffer);

View File

@@ -5,7 +5,7 @@ import {
MovieResultsResponse, MovieResultsResponse,
ShowResponse, ShowResponse,
TvResultsResponse, TvResultsResponse,
} from "https://esm.sh/moviedb-promise@3.4.1"; } from "moviedb-promise";
import { createCache } from "@lib/cache.ts"; import { createCache } from "@lib/cache.ts";
const moviedb = new MovieDb(Deno.env.get("TMDB_API_KEY") || ""); const moviedb = new MovieDb(Deno.env.get("TMDB_API_KEY") || "");

View File

@@ -1,4 +1,4 @@
import { GenericResource, GenericResourceSchema } from "./marka/schema.ts"; import { GenericResource } from "./marka/schema.ts";
export interface TMDBMovie { export interface TMDBMovie {
adult: boolean; adult: boolean;
@@ -39,7 +39,7 @@ export interface GiteaOauthUser {
preferred_username: string; preferred_username: string;
email: string; email: string;
picture: string; picture: string;
groups: any; groups: unknown;
} }
export type SearchResult = { export type SearchResult = {

29
lib/unsplash.ts Normal file
View File

@@ -0,0 +1,29 @@
import { UNSPLASH_API_KEY } from "./env.ts";
const API_URL = "https://api.unsplash.com";
export async function getImageBySearchTerm(
searchTerm: string,
): Promise<string | undefined> {
if (!UNSPLASH_API_KEY) {
throw new Error("UNSPLASH_API_KEY is not set");
}
const url = new URL("/search/photos", API_URL);
url.searchParams.append("query", searchTerm);
url.searchParams.append("per_page", "1");
url.searchParams.append("orientation", "landscape");
const response = await fetch(url.toString(), {
headers: {
Authorization: `Client-ID ${UNSPLASH_API_KEY}`,
},
});
if (!response.ok) {
throw new Error(`Unsplash API request failed: ${response.statusText}`);
}
const data = await response.json();
return data.results[0]?.urls?.regular;
}

View File

@@ -1,6 +1,8 @@
import { JSDOM } from "jsdom"; import { JSDOM } from "jsdom";
import { fetchHtmlWithPlaywright } from "./playwright.ts"; import { fetchHtmlWithPlaywright } from "./playwright.ts";
import { createStreamResponse } from "./helpers.ts"; import { createStreamResponse } from "./helpers.ts";
import { Defuddle } from "defuddle/node";
import TurndownService from "turndown";
/** /**
* Mutates the given JSDOM instance: rewrites all relevant URL-bearing attributes * Mutates the given JSDOM instance: rewrites all relevant URL-bearing attributes
@@ -11,12 +13,14 @@ export function absolutizeDomUrls(dom: JSDOM, domain: string): void {
const base = toBase(domain); const base = toBase(domain);
const rewrite = (selector: string, attr: string) => { const rewrite = (selector: string, attr: string) => {
document.querySelectorAll<HTMLElement>(selector).forEach((el) => { document.querySelectorAll<HTMLElement>(selector).forEach(
(el: HTMLElement) => {
const v = el.getAttribute(attr); const v = el.getAttribute(attr);
if (!v) return; if (!v) return;
const abs = toAbsolute(v, base); const abs = toAbsolute(v, base);
if (abs !== v) el.setAttribute(attr, abs); if (abs !== v) el.setAttribute(attr, abs);
}); },
);
}; };
// Common URL attributes // Common URL attributes
@@ -41,35 +45,35 @@ export function absolutizeDomUrls(dom: JSDOM, domain: string): void {
rewrite("form[action]", "action"); rewrite("form[action]", "action");
rewrite("video[poster]", "poster"); rewrite("video[poster]", "poster");
// srcset (img, source)
document document
.querySelectorAll<HTMLElement>("img[srcset], source[srcset]") .querySelectorAll("img[srcset], source[srcset]")
.forEach((el) => { .forEach((el: HTMLImageElement) => {
const v = el.getAttribute("srcset"); const v = el.getAttribute("srcset");
if (!v) return; if (!v) return;
const abs = absolutizeSrcset(v, base); const abs = absolutizeSrcset(v, base);
if (abs !== v) el.setAttribute("srcset", abs); if (abs !== v) el.setAttribute("srcset", abs);
}); });
// Inline CSS in style attributes: url(...) document.querySelectorAll("[style]").forEach(
document.querySelectorAll<HTMLElement>("[style]").forEach((el) => { (el: HTMLElement) => {
const v = el.getAttribute("style"); const v = el.getAttribute("style");
if (!v) return; if (!v) return;
const abs = absolutizeCssUrls(v, base); const abs = absolutizeCssUrls(v, base);
if (abs !== v) el.setAttribute("style", abs); if (abs !== v) el.setAttribute("style", abs);
}); },
);
// <style> blocks (inline CSS): url(...) document.querySelectorAll("style").forEach(
document.querySelectorAll<HTMLStyleElement>("style").forEach((styleEl) => { (styleEl: HTMLStyleElement) => {
const css = styleEl.textContent ?? ""; const css = styleEl.textContent ?? "";
const abs = absolutizeCssUrls(css, base); const abs = absolutizeCssUrls(css, base);
if (abs !== css) styleEl.textContent = abs; if (abs !== css) styleEl.textContent = abs;
}); },
);
// <meta http-equiv="refresh" content="5; url=/path">
document document
.querySelectorAll<HTMLMetaElement>('meta[http-equiv="refresh" i][content]') .querySelectorAll('meta[http-equiv="refresh" i][content]')
.forEach((meta) => { .forEach((meta: HTMLMetaElement) => {
const content = meta.getAttribute("content") || ""; const content = meta.getAttribute("content") || "";
const abs = absolutizeMetaRefresh(content, base); const abs = absolutizeMetaRefresh(content, base);
if (abs !== content) meta.setAttribute("content", abs); if (abs !== content) meta.setAttribute("content", abs);
@@ -162,6 +166,8 @@ function absolutizeMetaRefresh(content: string, base: string): string {
return `${delay}; url=${abs}`; return `${delay}; url=${abs}`;
} }
const turndownService = new TurndownService();
export async function webScrape( export async function webScrape(
url: string, url: string,
streamResponse: ReturnType<typeof createStreamResponse>, streamResponse: ReturnType<typeof createStreamResponse>,
@@ -170,5 +176,12 @@ export async function webScrape(
const html = await fetchHtmlWithPlaywright(url, streamResponse); const html = await fetchHtmlWithPlaywright(url, streamResponse);
const dom = new JSDOM(html); const dom = new JSDOM(html);
absolutizeDomUrls(dom, u.origin); absolutizeDomUrls(dom, u.origin);
return dom;
const result = await Defuddle(dom, url);
return {
...result,
dom: dom.window.document,
markdown: turndownService.turndown(result.content),
};
} }

View File

@@ -74,6 +74,11 @@ export interface PageInfo {
resultsPerPage: number; resultsPerPage: number;
} }
export async function getYoutubeVideoCover(id: string): Promise<ArrayBuffer> {
const res = await fetch(`https://i.ytimg.com/vi/${id}/maxresdefault.jpg`);
return res.arrayBuffer();
}
export async function getYoutubeVideoDetails( export async function getYoutubeVideoDetails(
id: string, id: string,
): Promise<Item> { ): Promise<Item> {
@@ -81,6 +86,5 @@ export async function getYoutubeVideoDetails(
`${BASE_URL}videos?part=snippet%2CcontentDetails%2Cstatistics&id=${id}&key=${YOUTUBE_API_KEY}`, `${BASE_URL}videos?part=snippet%2CcontentDetails%2Cstatistics&id=${id}&key=${YOUTUBE_API_KEY}`,
); );
const json = await response.json(); const json = await response.json();
return json?.items[0]; return json?.items[0];
} }

View File

@@ -7,7 +7,7 @@ export default function Error404() {
<Head> <Head>
<title>404 - Page not found</title> <title>404 - Page not found</title>
</Head> </Head>
<MainLayout> <MainLayout url="">
<div class="px-8 text-white mt-10"> <div class="px-8 text-white mt-10">
<div class="max-w-screen-md mx-auto flex flex-col items-center justify-center"> <div class="max-w-screen-md mx-auto flex flex-col items-center justify-center">
<h1 class="text-4xl font-bold">404 - Page not found</h1> <h1 class="text-4xl font-bold">404 - Page not found</h1>

View File

@@ -1,11 +1,9 @@
// deno-lint-ignore-file react-no-danger
import { PageProps } from "$fresh/server.ts"; import { PageProps } from "$fresh/server.ts";
import { Partial } from "$fresh/runtime.ts"; import { Partial } from "$fresh/runtime.ts";
import { useEffect } from "preact/hooks";
export default function App({ Component }: PageProps) { export default function App({ Component }: PageProps) {
const globalCss = Deno const globalCss = Deno.readTextFileSync("./static/global.css");
.readTextFileSync("./static/global.css")
.replaceAll("\n", "");
return ( return (
<html> <html>
@@ -21,7 +19,19 @@ export default function App({ Component }: PageProps) {
<meta name="msapplication-TileColor" content="#da532c" /> <meta name="msapplication-TileColor" content="#da532c" />
<meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#141218" /> <meta name="theme-color" content="#141218" />
<style>{globalCss}</style> <link
rel="preload"
href="/fonts/work-sans-v18-latin-regular.woff2"
as="font"
type="font/woff2"
/>
<link
rel="preload"
href="/fonts/work-sans-v18-latin-700.woff2"
as="font"
type="font/woff2"
/>
<style dangerouslySetInnerHTML={{ __html: globalCss }} />
<title>Memorium</title> <title>Memorium</title>
</head> </head>
<body f-client-nav> <body f-client-nav>

View File

@@ -2,6 +2,7 @@ import { PageProps } from "$fresh/server.ts";
import { resources } from "@lib/resources.ts"; import { resources } from "@lib/resources.ts";
import { Link } from "@islands/Link.tsx"; import { Link } from "@islands/Link.tsx";
import { Emoji } from "@components/Emoji.tsx"; import { Emoji } from "@components/Emoji.tsx";
import KMenuButton from "@islands/KMenuButton.tsx";
export default function MyLayout({ Component }: PageProps) { export default function MyLayout({ Component }: PageProps) {
return ( return (
@@ -23,12 +24,10 @@ export default function MyLayout({ Component }: PageProps) {
})} })}
</nav> </nav>
</aside> </aside>
<main <main class="py-5">
class="py-5"
style={{ fontFamily: "Work Sans" }}
>
<Component /> <Component />
</main> </main>
<KMenuButton />
</div> </div>
); );
} }

View File

@@ -20,7 +20,7 @@ export default function Greet(
<MainLayout <MainLayout
url={props.url} url={props.url}
title="Recipes" title="Recipes"
context={{ type: "recipe" }} context={{ type: "recipes" }}
> >
<code> <code>
<pre class="text-white"> <pre class="text-white">

View File

@@ -16,7 +16,6 @@ export const handler: Handlers = {
if (!("session" in ctx.state)) { if (!("session" in ctx.state)) {
throw new AccessDeniedError(); throw new AccessDeniedError();
} }
console.log({ logs });
return ctx.render({ return ctx.render({
logs: logs.map((l) => { logs: logs.map((l) => {
return { return {
@@ -30,7 +29,7 @@ export const handler: Handlers = {
function LogLine( function LogLine(
{ log }: { { log }: {
log: Log; log: Log & { html?: string };
}, },
) { ) {
return ( return (
@@ -53,7 +52,8 @@ function LogLine(
</div> </div>
<div <div
class="text-white" class="text-white"
dangerouslySetInnerHTML={{ __html: log.html }} // deno-lint-ignore react-no-danger
dangerouslySetInnerHTML={{ __html: log.html ?? "" }}
/> />
</div> </div>
); );

View File

@@ -3,19 +3,78 @@ import { Defuddle } from "defuddle/node";
import { AccessDeniedError, BadRequestError } from "@lib/errors.ts"; import { AccessDeniedError, BadRequestError } from "@lib/errors.ts";
import { createStreamResponse, isValidUrl } from "@lib/helpers.ts"; import { createStreamResponse, isValidUrl } from "@lib/helpers.ts";
import * as openai from "@lib/openai.ts"; import * as openai from "@lib/openai.ts";
import * as unsplash from "@lib/unsplash.ts";
import { getYoutubeVideoDetails } from "@lib/youtube.ts"; import { getYoutubeVideoDetails } from "@lib/youtube.ts";
import { import {
extractYoutubeId, extractYoutubeId,
formatDate,
isYoutubeLink, isYoutubeLink,
safeFileName,
toUrlSafeString, toUrlSafeString,
} from "@lib/string.ts"; } from "@lib/string.ts";
import { createLogger } from "@lib/log/index.ts"; import { createLogger } from "@lib/log/index.ts";
import { createResource } from "@lib/marka/index.ts"; import { createResource } from "@lib/marka/index.ts";
import { webScrape } from "@lib/webScraper.ts"; import { webScrape } from "@lib/webScraper.ts";
import { ArticleResource } from "@lib/marka/schema.ts"; import { ArticleResource } from "@lib/marka/schema.ts";
import { fileExtension } from "https://deno.land/x/file_extension@v2.1.0/mod.ts";
const log = createLogger("api/article"); const log = createLogger("api/article");
async function getUnsplashCoverImage(
content: string,
streamResponse: ReturnType<typeof createStreamResponse>,
): Promise<string | undefined> {
try {
streamResponse.info("creating unsplash search term");
const searchTerm = await openai.createUnsplashSearchTerm(content);
if (!searchTerm) return;
streamResponse.info(`searching for ${searchTerm}`);
const unsplashUrl = await unsplash.getImageBySearchTerm(searchTerm);
return unsplashUrl;
} catch (e) {
log.error("Failed to get unsplash cover image", e);
return undefined;
}
}
function ext(str: string) {
try {
const u = new URL(str);
if (u.searchParams.has("fm")) {
return u.searchParams.get("fm")!;
}
return fileExtension(u.pathname);
} catch (_e) {
return fileExtension(str);
}
}
async function fetchAndStoreCover(
imageUrl: string | undefined,
title: string,
streamResponse?: ReturnType<typeof createStreamResponse>,
): Promise<string | undefined> {
if (!imageUrl) return;
const imagePath = `articles/images/${safeFileName(title)}_cover.${
ext(imageUrl)
}`;
try {
streamResponse?.info("downloading image");
const res = await fetch(imageUrl);
streamResponse?.info("saving image");
if (!res.ok) {
console.log(`Failed to download remote image: ${imageUrl}`, res.status);
return;
}
const buffer = await res.arrayBuffer();
await createResource(imagePath, buffer);
return `resources/${imagePath}`;
} catch (err) {
console.log(`Failed to save image: ${imageUrl}`, err);
return;
}
}
async function processCreateArticle( async function processCreateArticle(
{ fetchUrl, streamResponse }: { { fetchUrl, streamResponse }: {
fetchUrl: string; fetchUrl: string;
@@ -24,36 +83,48 @@ async function processCreateArticle(
) { ) {
log.info("create article from url", { url: fetchUrl }); log.info("create article from url", { url: fetchUrl });
streamResponse.enqueue("downloading article"); streamResponse.info("downloading article");
const doc = await webScrape(fetchUrl, streamResponse); const result = await webScrape(fetchUrl, streamResponse);
const result = await Defuddle(doc, fetchUrl, { log.debug("downloaded and parse parsed", result);
markdown: true,
});
log.debug("downloaded and parse parsed", { streamResponse.info("parsed article, creating tags with openai");
url: fetchUrl,
content: result.content,
});
streamResponse.enqueue("parsed article, creating tags with openai"); const aiMeta = await openai.extractArticleMetadata(result.markdown);
const aiMeta = await openai.extractArticleMetadata(result.content); streamResponse.info("postprocessing article");
streamResponse.enqueue("postprocessing article");
const title = result?.title || aiMeta?.headline || ""; const title = result?.title || aiMeta?.headline || "";
const id = toUrlSafeString(title);
const newArticle: Article = { let coverImagePath: string | undefined = undefined;
if (result?.image?.length) {
log.debug("using local image for cover image", { image: result.image });
coverImagePath = await fetchAndStoreCover(
result.image,
title,
streamResponse,
);
} else {
const urlPath = await getUnsplashCoverImage(
result.markdown,
streamResponse,
);
coverImagePath = await fetchAndStoreCover(urlPath, title, streamResponse);
log.debug("using unsplash for cover image", { image: coverImagePath });
}
const url = toUrlSafeString(title);
const newArticle: ArticleResource["content"] = {
_type: "Article", _type: "Article",
headline: title, headline: title,
articleBody: result.content, articleBody: result.markdown,
url: fetchUrl, url: fetchUrl,
datePublished: result?.published || aiMeta?.datePublished || datePublished: formatDate(
new Date().toISOString(), result?.published || aiMeta?.datePublished || undefined,
image: result?.image, ),
image: coverImagePath,
author: { author: {
_type: "Person", _type: "Person",
name: (result.schemaOrgData?.author?.name || aiMeta?.author || "") name: (result.schemaOrgData?.author?.name || aiMeta?.author || "")
@@ -64,11 +135,16 @@ async function processCreateArticle(
}, },
} as const; } as const;
streamResponse.enqueue("writing to disk"); streamResponse.info("writing to disk");
await createResource(`articles/${id}.md`, newArticle); log.debug("writing to disk", {
...newArticle,
articleBody: newArticle.articleBody?.slice(0, 200),
});
streamResponse.enqueue("id: " + id); await createResource(`articles/${url}.md`, newArticle);
streamResponse.send({ type: "finished", url });
} }
async function processCreateYoutubeVideo( async function processCreateYoutubeVideo(
@@ -81,36 +157,48 @@ async function processCreateYoutubeVideo(
url: fetchUrl, url: fetchUrl,
}); });
streamResponse.enqueue("getting video infos from youtube api"); streamResponse.info("getting video infos from youtube api");
const youtubeId = extractYoutubeId(fetchUrl); const youtubeId = extractYoutubeId(fetchUrl);
const video = await getYoutubeVideoDetails(youtubeId); const video = await getYoutubeVideoDetails(youtubeId);
streamResponse.enqueue("shortening title with openai"); streamResponse.info("shortening title with openai");
const newId = await openai.shortenTitle(video.snippet.title); const videoTitle = await openai.shortenTitle(video.snippet.title) ||
video.snippet.title;
const id = newId || youtubeId; const thumbnail = video?.snippet?.thumbnails?.maxres;
const coverImagePath = await fetchAndStoreCover(
thumbnail.url,
videoTitle || video.snippet.title,
streamResponse,
);
const newArticle: ArticleResource["content"] = { const newArticle: ArticleResource["content"] = {
_type: "Article", _type: "Article",
headline: video.snippet.title, headline: video.snippet.title,
articleBody: video.snippet.description, articleBody: video.snippet.description,
image: coverImagePath,
url: fetchUrl, url: fetchUrl,
datePublished: new Date(video.snippet.publishedAt).toISOString(), datePublished: formatDate(video.snippet.publishedAt),
author: { author: {
_type: "Person", _type: "Person",
name: video.snippet.channelTitle, name: video.snippet.channelTitle,
}, },
}; };
streamResponse.enqueue("creating article"); streamResponse.info("creating article");
await createResource(`articles/${id}.md`, newArticle); const filename = toUrlSafeString(videoTitle);
streamResponse.enqueue("finished"); await createResource(
`articles/${filename}.md`,
newArticle,
);
streamResponse.enqueue("id: " + id); streamResponse.info("finished");
streamResponse.send({ type: "finished", url: filename });
} }
export const handler: Handlers = { export const handler: Handlers = {

View File

@@ -0,0 +1,191 @@
import { FreshContext, Handlers } from "$fresh/server.ts";
import { fileExtension } from "https://deno.land/x/file_extension@v2.1.0/mod.ts";
import { formatDate, safeFileName } from "@lib/string.ts";
import { createStreamResponse } from "@lib/helpers.ts";
import {
AccessDeniedError,
BadRequestError,
NotFoundError,
} from "@lib/errors.ts";
import { createResource, fetchResource } from "@lib/marka/index.ts";
import { ArticleResource } from "@lib/marka/schema.ts";
import { webScrape } from "@lib/webScraper.ts";
import * as openai from "@lib/openai.ts";
import * as unsplash from "@lib/unsplash.ts";
import { createLogger } from "@lib/log/index.ts";
function ext(str: string) {
try {
const u = new URL(str);
if (u.searchParams.has("fm")) {
return u.searchParams.get("fm")!;
}
return fileExtension(u.pathname);
} catch (_e) {
return fileExtension(str);
}
}
const log = createLogger("api/article/enhance");
async function getUnsplashCoverImage(
content: string,
streamResponse: ReturnType<typeof createStreamResponse>,
): Promise<string | undefined> {
try {
streamResponse.info("creating unsplash search term");
const searchTerm = await openai.createUnsplashSearchTerm(content);
if (!searchTerm) return;
streamResponse.info(`searching for ${searchTerm}`);
const unsplashUrl = await unsplash.getImageBySearchTerm(searchTerm);
return unsplashUrl;
} catch (e) {
log.error("Failed to get unsplash cover image", e);
return undefined;
}
}
async function fetchAndStoreCover(
imageUrl: string | undefined,
title: string,
streamResponse: ReturnType<typeof createStreamResponse>,
): Promise<string | undefined> {
if (!imageUrl) return;
const imagePath = `articles/images/${safeFileName(title)}_cover.${
ext(imageUrl)
}`;
try {
streamResponse.info("downloading cover");
const res = await fetch(imageUrl);
if (!res.ok) {
log.error(`Failed to download remote image: ${imageUrl}`, {
status: res.status,
});
return;
}
const buffer = await res.arrayBuffer();
streamResponse.info("saving cover");
await createResource(imagePath, buffer);
return `resources/${imagePath}`;
} catch (err) {
log.error(`Failed to save image: ${imageUrl}`, err);
return;
}
}
async function processEnhanceArticle(
name: string,
streamResponse: ReturnType<typeof createStreamResponse>,
) {
const article = await fetchResource<ArticleResource>(
`articles/${name}`,
);
if (!article) {
throw new NotFoundError();
}
const fetchUrl = article.content?.url;
if (!fetchUrl) {
throw new BadRequestError("Article has no URL to enhance from.");
}
log.info("enhancing article from url", { url: fetchUrl });
streamResponse.info("scraping url");
const result = await webScrape(fetchUrl, streamResponse);
streamResponse.info("parsing content");
log.debug("downloaded and parsed", result);
streamResponse.info("extracting metadata with openai");
const aiMeta = await openai.extractArticleMetadata(result.markdown);
const title = result?.title || aiMeta?.headline ||
article.content?.headline || "";
article.content ??= {
_type: "Article",
headline: title,
url: fetchUrl,
};
article.content.articleBody = result.markdown;
article.content.datePublished ??= formatDate(
result?.published || aiMeta?.datePublished || undefined,
);
if (!article.content.author?.name || article.content.author.name === "") {
article.content.author = {
_type: "Person",
name: (result.schemaOrgData?.author?.name || aiMeta?.author || "")
.replace(
"@",
"twitter:",
),
};
}
if (!article.content.image) {
let coverImagePath: string | undefined = undefined;
if (result?.image?.length) {
log.debug("using local image for cover image", { image: result.image });
coverImagePath = await fetchAndStoreCover(
result.image,
title,
streamResponse,
);
} else {
const urlPath = await getUnsplashCoverImage(
result.content,
streamResponse,
);
coverImagePath = await fetchAndStoreCover(urlPath, title, streamResponse);
log.debug("using unsplash for cover image", { image: coverImagePath });
}
if (coverImagePath) {
article.content.image = coverImagePath;
}
}
log.debug("writing to disk", {
name: name,
article: {
...article,
content: {
...article.content,
articleBody: article.content.articleBody?.slice(0, 200),
},
},
});
streamResponse.info("writing to disk");
await createResource(`articles/${name}`, article.content);
streamResponse.send({ type: "finished", url: name.replace(/$\.md/, "") });
}
const POST = (
_req: Request,
ctx: FreshContext,
): Response => {
const session = ctx.state.session;
if (!session) {
throw new AccessDeniedError();
}
const streamResponse = createStreamResponse();
processEnhanceArticle(ctx.params.name, streamResponse)
.catch((err) => {
log.error(err);
streamResponse.error(err.message);
})
.finally(() => {
streamResponse.cancel();
});
return streamResponse.response;
};
export const handler: Handlers = {
POST,
};

View File

@@ -60,9 +60,10 @@ function parseParams(reqUrl: URL): ImageParams | string {
} }
} }
// Helper function to generate ETag // Helper function to generate ETag
async function generateETag(content: ArrayBuffer): Promise<string> { async function generateETag(content: Uint8Array<ArrayBuffer>): Promise<string> {
const hashBuffer = await crypto.subtle.digest("SHA-256", content); const hashBuffer = await crypto.subtle.digest("SHA-256", content);
return `"${Array.from(new Uint8Array(hashBuffer)) return `"${
Array.from(new Uint8Array(hashBuffer))
.map((b) => b.toString(16).padStart(2, "0")) .map((b) => b.toString(16).padStart(2, "0"))
.join("") .join("")
}"`; }"`;

View File

@@ -2,7 +2,7 @@ import { Handlers } from "$fresh/server.ts";
import { json } from "@lib/helpers.ts"; import { json } from "@lib/helpers.ts";
export const handler: Handlers = { export const handler: Handlers = {
async GET() { GET() {
return json([]); return json([]);
}, },
}; };

View File

@@ -2,8 +2,13 @@ import { Handlers } from "$fresh/server.ts";
import { json } from "@lib/helpers.ts"; import { json } from "@lib/helpers.ts";
import * as tmdb from "@lib/tmdb.ts"; import * as tmdb from "@lib/tmdb.ts";
import { fileExtension } from "https://deno.land/x/file_extension@v2.1.0/mod.ts"; import { fileExtension } from "https://deno.land/x/file_extension@v2.1.0/mod.ts";
import { isString, safeFileName } from "@lib/string.ts"; import {
import { AccessDeniedError } from "@lib/errors.ts"; formatDate,
isString,
safeFileName,
toUrlSafeString,
} from "@lib/string.ts";
import { AccessDeniedError, BadRequestError } from "@lib/errors.ts";
import { createResource, fetchResource } from "@lib/marka/index.ts"; import { createResource, fetchResource } from "@lib/marka/index.ts";
import { ReviewResource } from "@lib/marka/schema.ts"; import { ReviewResource } from "@lib/marka/schema.ts";
@@ -14,26 +19,22 @@ export const handler: Handlers = {
}, },
async POST(_, ctx) { async POST(_, ctx) {
const session = ctx.state.session; const session = ctx.state.session;
if (!session) { if (!session) throw new AccessDeniedError();
throw new AccessDeniedError();
}
const tmdbId = parseInt(ctx.params.name); const tmdbId = parseInt(ctx.params.name);
if (Number.isNaN(tmdbId)) throw new BadRequestError();
const movieDetails = await tmdb.getMovie(tmdbId); const [movieDetails, movieCredits] = await Promise.all([
const movieCredits = await tmdb.getMovieCredits(tmdbId); tmdb.getMovie(tmdbId),
tmdb.getMovieCredits(tmdbId),
]);
const releaseDate = movieDetails.release_date; const name = movieDetails.title ||
const posterPath = movieDetails.poster_path; movieDetails.original_title ||
const director = movieCredits?.crew?.filter?.((person) =>
person.job === "Director"
)[0];
movieDetails.overview;
let finalPath = "";
const name = movieDetails.title || movieDetails.original_title ||
ctx.params.name; ctx.params.name;
const posterPath = movieDetails.poster_path;
let finalPath = "";
if (posterPath) { if (posterPath) {
const poster = await tmdb.getMoviePoster(posterPath); const poster = await tmdb.getMoviePoster(posterPath);
const extension = fileExtension(posterPath); const extension = fileExtension(posterPath);
@@ -41,33 +42,32 @@ export const handler: Handlers = {
await createResource(finalPath, poster); await createResource(finalPath, poster);
} }
const tags: string[] = []; const keywords = movieDetails.genres
if (movieDetails.genres) { ?.map((g) => g.name?.toLowerCase())
tags.push( .filter(isString) || [];
...movieDetails.genres.map((g) => g.name?.toLowerCase()).filter(
isString,
),
);
}
const movie: ReviewResource["content"] = { const movie: ReviewResource["content"] = {
_type: "Review", _type: "Review",
image: `resources/${finalPath}`, image: `resources/${finalPath}`,
datePublished: releaseDate, datePublished: formatDate(movieDetails.release_date),
tmdbId, tmdbId,
author: { author: {
_type: "Person", _type: "Person",
name: director?.name, name: movieCredits.crew?.filter?.((person) =>
person.job === "Director"
)[0]?.name,
}, },
itemReviewed: { itemReviewed: {
name: name, name,
}, },
reviewBody: "", reviewBody: "",
keywords: tags, keywords,
}; };
await createResource(`movies/${safeFileName(name)}.md`, movie); const fileName = toUrlSafeString(name);
return json(movie); await createResource(`movies/${fileName}.md`, movie);
return json({ name: fileName });
}, },
}; };

View File

@@ -1,7 +1,12 @@
import { FreshContext, Handlers } from "$fresh/server.ts"; import { FreshContext, Handlers } from "$fresh/server.ts";
import { fileExtension } from "https://deno.land/x/file_extension@v2.1.0/mod.ts"; import { fileExtension } from "https://deno.land/x/file_extension@v2.1.0/mod.ts";
import * as tmdb from "@lib/tmdb.ts"; import * as tmdb from "@lib/tmdb.ts";
import { isString, safeFileName } from "@lib/string.ts"; import {
formatDate,
isString,
safeFileName,
toUrlSafeString,
} from "@lib/string.ts";
import { json } from "@lib/helpers.ts"; import { json } from "@lib/helpers.ts";
import { import {
AccessDeniedError, AccessDeniedError,
@@ -10,6 +15,7 @@ import {
} from "@lib/errors.ts"; } from "@lib/errors.ts";
import { createRecommendationResource } from "@lib/recommendation.ts"; import { createRecommendationResource } from "@lib/recommendation.ts";
import { createResource, fetchResource } from "@lib/marka/index.ts"; import { createResource, fetchResource } from "@lib/marka/index.ts";
import { ReviewResource } from "@lib/marka/schema.ts";
const POST = async ( const POST = async (
req: Request, req: Request,
@@ -20,7 +26,9 @@ const POST = async (
throw new AccessDeniedError(); throw new AccessDeniedError();
} }
const movie = await fetchResource(`movies/${ctx.params.name}`); const movie = await fetchResource<ReviewResource>(
`movies/${ctx.params.name}`,
);
if (!movie) { if (!movie) {
throw new NotFoundError(); throw new NotFoundError();
} }
@@ -33,27 +41,29 @@ const POST = async (
} }
const movieDetails = await tmdb.getMovie(tmdbId); const movieDetails = await tmdb.getMovie(tmdbId);
const movieCredits = !movie.meta?.author && const movieCredits = !movie.content?.author &&
await tmdb.getMovieCredits(tmdbId); await tmdb.getMovieCredits(tmdbId);
const releaseDate = movieDetails.release_date; const director = movieCredits &&
if (releaseDate && !movie.meta?.date) { movieCredits?.crew?.filter?.((person) => person.job === "Director")[0];
movie.meta = movie.meta || {};
movie.meta.date = new Date(releaseDate);
}
const director = movieCredits?.crew?.filter?.((person) => movie.content ??= {
person.job === "Director" _type: "Review",
)[0]; };
if (director && !movie.meta?.author) {
movie.meta = movie.meta || {}; movie.content.datePublished ??= formatDate(movieDetails.release_date);
movie.meta.author = director.name;
if (director && !movie.content?.author) {
movie.content.author = {
_type: "Person",
name: director.name,
};
} }
if (movieDetails.genres) { if (movieDetails.genres) {
movie.tags = [ movie.content.keywords = [
...new Set([ ...new Set([
...(movie.tags?.map((g) => g.toLowerCase()) || []), ...(movie.content.keywords?.map((g) => g.toLowerCase()) || []),
...movieDetails.genres.map((g) => ...movieDetails.genres.map((g) =>
g.name?.toLowerCase().replaceAll(" ", "-") g.name?.toLowerCase().replaceAll(" ", "-")
), ),
@@ -61,22 +71,19 @@ const POST = async (
]; ];
} }
if (!movie.id) { movie.content.tmdbId ??= tmdbId;
movie.id = tmdbId;
}
let finalPath = ""; let finalPath = "";
const posterPath = movieDetails.poster_path; const posterPath = movieDetails.poster_path;
if (posterPath && !movie.meta?.image) { if (posterPath && !movie.content.image) {
const poster = await tmdb.getMoviePoster(posterPath); const poster = await tmdb.getMoviePoster(posterPath);
const extension = fileExtension(posterPath); const extension = fileExtension(posterPath);
finalPath = `movies/images/${safeFileName(name)}_cover.${extension}`; finalPath = `movies/images/${safeFileName(name)}_cover.${extension}`;
await createResource(finalPath, poster); await createResource(finalPath, poster);
movie.meta = movie.meta || {}; movie.content.image = finalPath;
movie.meta.image = finalPath;
} }
await createResource(`movies/${safeFileName(movie.id)}.md`, movie); await createResource(`movies/${toUrlSafeString(movie.name)}.md`, movie);
createRecommendationResource(movie, movieDetails.overview); createRecommendationResource(movie, movieDetails.overview);

View File

@@ -1,7 +1,7 @@
import { Handlers } from "$fresh/server.ts"; import { Handlers } from "$fresh/server.ts";
import { json } from "@lib/helpers.ts"; import { json } from "@lib/helpers.ts";
import { AccessDeniedError } from "@lib/errors.ts"; import { AccessDeniedError, BadRequestError } from "@lib/errors.ts";
import { searchResource } from "@lib/search.ts"; import { parseResourceUrl, searchResource } from "@lib/search.ts";
export const handler: Handlers = { export const handler: Handlers = {
async GET(req, ctx) { async GET(req, ctx) {
@@ -10,18 +10,13 @@ export const handler: Handlers = {
throw new AccessDeniedError(); throw new AccessDeniedError();
} }
const url = new URL(req.url); const s = parseResourceUrl(req.url);
if (!s) {
throw new BadRequestError();
}
const types = url.searchParams.get("types")?.split(","); console.log(s);
const tags = url.searchParams?.get("tags")?.split(","); const resources = await searchResource(s);
const authors = url.searchParams?.get("authors")?.split(",");
const resources = await searchResource({
q: url.searchParams.get("q") || "",
types,
tags,
authors,
});
return json(resources); return json(resources);
}, },

View File

@@ -5,12 +5,11 @@ import * as openai from "@lib/openai.ts";
import { createLogger } from "@lib/log/index.ts"; import { createLogger } from "@lib/log/index.ts";
import recipeSchema from "@lib/recipeSchema.ts"; import recipeSchema from "@lib/recipeSchema.ts";
import { fileExtension } from "https://deno.land/x/file_extension@v2.1.0/mod.ts"; import { fileExtension } from "https://deno.land/x/file_extension@v2.1.0/mod.ts";
import { safeFileName } from "@lib/string.ts"; import { safeFileName, toUrlSafeString } from "@lib/string.ts";
import { parseJsonLdToRecipeSchema } from "./parseJsonLd.ts"; import { parseJsonLdToRecipeSchema } from "./parseJsonLd.ts";
import z from "zod"; import z from "zod";
import { createResource } from "@lib/marka/index.ts"; import { createResource } from "@lib/marka/index.ts";
import { webScrape } from "@lib/webScraper.ts"; import { webScrape } from "@lib/webScraper.ts";
import { Defuddle } from "defuddle/node";
import { RecipeResource } from "@lib/marka/schema.ts"; import { RecipeResource } from "@lib/marka/schema.ts";
const log = createLogger("api/article"); const log = createLogger("api/article");
@@ -23,18 +22,14 @@ async function processCreateRecipeFromUrl(
) { ) {
log.info("create article from url", { url: fetchUrl }); log.info("create article from url", { url: fetchUrl });
streamResponse.enqueue("downloading article"); streamResponse.info("downloading article");
const doc = await webScrape(fetchUrl, streamResponse); const result = await webScrape(fetchUrl, streamResponse);
const result = await Defuddle(doc, fetchUrl, { streamResponse.info("download success");
markdown: true,
});
streamResponse.enqueue("download success");
const jsonLds = Array.from( const jsonLds = Array.from(
doc?.querySelectorAll( result.dom?.querySelectorAll(
"script[type='application/ld+json']", "script[type='application/ld+json']",
), ),
) as unknown as HTMLScriptElement[]; ) as unknown as HTMLScriptElement[];
@@ -42,19 +37,26 @@ async function processCreateRecipeFromUrl(
let recipe: z.infer<typeof recipeSchema> | undefined = undefined; let recipe: z.infer<typeof recipeSchema> | undefined = undefined;
if (jsonLds.length > 0) { if (jsonLds.length > 0) {
for (const jsonLd of jsonLds) { for (const jsonLd of jsonLds) {
recipe = parseJsonLdToRecipeSchema(jsonLd.textContent || ""); if (jsonLd.textContent) {
recipe = parseJsonLdToRecipeSchema(jsonLd.textContent);
if (recipe) break; if (recipe) break;
} }
} }
if (!recipe) {
recipe = await openai.extractRecipe(result.content);
} }
const id = safeFileName(recipe?.name || ""); if (!recipe) {
const res = await openai.extractRecipe(result.markdown);
if (!res || res === "none") {
streamResponse.error(`failed to extract recipe: ${res}`);
return;
}
recipe = res;
}
const id = toUrlSafeString(recipe?.name || "");
if (!recipe) { if (!recipe) {
streamResponse.enqueue("failed to parse recipe"); streamResponse.error("failed to parse recipe");
streamResponse.cancel(); streamResponse.cancel();
return; return;
} }
@@ -69,27 +71,26 @@ async function processCreateRecipeFromUrl(
}; };
if (newRecipe?.image && newRecipe.image.length > 5) { if (newRecipe?.image && newRecipe.image.length > 5) {
const extension = fileExtension(new URL(newRecipe.image).pathname); const extension = fileExtension(newRecipe.image);
const finalPath = `resources/recipes/images/${safeFileName(id) const finalPath = `recipes/images/${safeFileName(id)}_cover.${extension}`;
}_cover.${extension}`; streamResponse.info("downloading image");
streamResponse.enqueue("downloading image");
try { try {
streamResponse.enqueue("downloading image"); streamResponse.info("downloading image");
const res = await fetch(newRecipe.image); const res = await fetch(newRecipe.image);
streamResponse.enqueue("saving image"); streamResponse.info("saving image");
const buffer = await res.arrayBuffer(); const buffer = await res.arrayBuffer();
await createResource(finalPath, buffer); await createResource(finalPath, buffer);
newRecipe.image = finalPath; newRecipe.image = `resources/${finalPath}`;
} catch (err) { } catch (err) {
console.log("Failed to save image", err); console.log("Failed to save image", err);
} }
} }
streamResponse.enqueue("finished processing, creating file"); streamResponse.info("finished processing, creating file");
await createResource(`recipes/${id}.md`, newRecipe); await createResource(`recipes/${id}.md`, newRecipe);
streamResponse.enqueue("id: " + id); streamResponse.send({ type: "finished", url: id });
} }
export const handler: Handlers = { export const handler: Handlers = {
@@ -111,7 +112,7 @@ export const handler: Handlers = {
processCreateRecipeFromUrl({ fetchUrl, streamResponse }).then((article) => { processCreateRecipeFromUrl({ fetchUrl, streamResponse }).then((article) => {
log.debug("created article from link", { article }); log.debug("created article from link", { article });
}).catch((err) => { }).catch((err) => {
streamResponse.enqueue(`error creating recipe: ${err}`); streamResponse.error(`creating recipe: ${err}`);
log.error(err); log.error(err);
}).finally(() => { }).finally(() => {
streamResponse.cancel(); streamResponse.cancel();

View File

@@ -1,5 +1,4 @@
import recipeSchema from "@lib/recipeSchema.ts"; import recipeSchema, { Recipe } from "@lib/recipeSchema.ts";
import { parseIngredients } from "@lib/parseIngredient.ts";
export function parseJsonLdToRecipeSchema(jsonLdContent: string) { export function parseJsonLdToRecipeSchema(jsonLdContent: string) {
try { try {
@@ -19,57 +18,53 @@ export function parseJsonLdToRecipeSchema(jsonLdContent: string) {
return; return;
} }
// Map and parse ingredients into the new schema const recipeInstructions = Array.isArray(data.recipeInstructions)
const ingredients = parseIngredients( ? data.recipeInstructions.map((instr: unknown) => {
data?.recipeIngredient?.join("\n") || "", if (!instr) return "";
);
const instructions = Array.isArray(data.recipeInstructions)
? data.recipeInstructions.map((instr) => {
if (typeof instr === "string") return instr; if (typeof instr === "string") return instr;
if (typeof instr === "object" && instr.text) return instr.text; if (typeof instr === "object" && "text" in instr && instr.text) {
return instr.text;
}
return ""; return "";
}).filter((instr) => instr.trim() !== "") }).filter((instr: string) => instr.trim() !== "")
: []; : [];
// Parse servings // Parse servings
const servings = parseServings(data.recipeYield); const recipeYield = parseServings(data.recipeYield);
// Parse times // Parse times
const prepTime = parseDuration(data.prepTime);
const cookTime = parseDuration(data.cookTime);
const totalTime = parseDuration(data.totalTime); const totalTime = parseDuration(data.totalTime);
// Extract tags // Extract tags
const tags = data.keywords const keywords = data.keywords
? Array.isArray(data.keywords) ? Array.isArray(data.keywords)
? data.keywords ? data.keywords
: data.keywords.split(",").map((tag: string) => tag.trim()) : data.keywords.split(",").map((tag: string) => tag.trim())
: []; : [];
// Build the recipe object // Build the recipe object
const recipe = { const recipe: Recipe = {
_type: "Recipe", _type: "Recipe",
title: data.name || "Unnamed Recipe", name: data.name || "Unnamed Recipe",
image: pickImage(image || data.image || ""), image: pickImage(image || data.image || ""),
author: Array.isArray(data.author) author: {
? data.author.map((a: any) => a.name).join(", ") "_type": "Person",
name: Array.isArray(data.author)
? data.author.map((a: { name: string }) => a.name).join(", ")
: data.author?.name || "", : data.author?.name || "",
},
description: data.description || "", description: data.description || "",
ingredients, recipeIngredient: data.recipeIngredient,
instructions, recipeInstructions,
servings, recipeYield,
prepTime,
cookTime,
totalTime, totalTime,
tags, keywords,
notes: data.notes || [],
}; };
// Validate against the schema // Validate against the schema
return recipeSchema.parse(recipe); return recipeSchema.parse(recipe);
} catch (error) { } catch (error) {
console.error("Invalid JSON-LD content or parsing error:", error); console.log("Invalid JSON-LD content or parsing error:", error);
return undefined; return undefined;
} }
} }
@@ -81,7 +76,7 @@ function pickImage(images: string | string[]): string {
return images; return images;
} }
function parseServings(servingsData: any): number { function parseServings(servingsData: unknown): number {
if (typeof servingsData === "string") { if (typeof servingsData === "string") {
const match = servingsData.match(/\d+/); const match = servingsData.match(/\d+/);
return match ? parseInt(match[0], 10) : 1; return match ? parseInt(match[0], 10) : 1;

View File

@@ -21,7 +21,7 @@ async function processUpdateRecommendations(
return true; return true;
}) as ReviewResource[]; }) as ReviewResource[];
streamResponse.enqueue("Fetched all movies"); streamResponse.info("fetched all movies");
let done = 0; let done = 0;
const total = movies.length; const total = movies.length;
@@ -41,15 +41,16 @@ async function processUpdateRecommendations(
console.log(err); console.log(err);
} }
done++; done++;
streamResponse.enqueue( streamResponse.info(
`${Math.floor((done / total) * 100)}% [${done + 1 `${Math.floor((done / total) * 100)}% [${
done + 1
}/${total}] ${movie.name}`, }/${total}] ${movie.name}`,
); );
})).catch((err) => { })).catch((err) => {
console.log(err); console.log(err);
}); });
streamResponse.enqueue("100% Finished"); streamResponse.info("100% Finished");
} }
export const handler: Handlers = { export const handler: Handlers = {

View File

@@ -11,7 +11,6 @@ export const handler: Handlers = {
} }
const recommendations = await getSimilarMovies(ctx.params.id); const recommendations = await getSimilarMovies(ctx.params.id);
console.log({ recommendations });
return json(recommendations); return json(recommendations);
}, },

View File

@@ -2,10 +2,19 @@ import { Handlers } from "$fresh/server.ts";
import { json } from "@lib/helpers.ts"; import { json } from "@lib/helpers.ts";
import * as tmdb from "@lib/tmdb.ts"; import * as tmdb from "@lib/tmdb.ts";
import { fileExtension } from "https://deno.land/x/file_extension@v2.1.0/mod.ts"; import { fileExtension } from "https://deno.land/x/file_extension@v2.1.0/mod.ts";
import { isString, safeFileName } from "@lib/string.ts"; import { formatDate, isString, safeFileName } from "@lib/string.ts";
import { AccessDeniedError } from "@lib/errors.ts"; import { AccessDeniedError, BadRequestError } from "@lib/errors.ts";
import { createResource, fetchResource } from "@lib/marka/index.ts"; import { createResource, fetchResource } from "@lib/marka/index.ts";
import { ReviewResource } from "@lib/marka/schema.ts"; import { ReviewResource } from "@lib/marka/schema.ts";
import { toUrlSafeString } from "@lib/string.ts";
function pickDirector(
credits: Awaited<ReturnType<typeof tmdb.getSeriesCredits>>,
createdBy?: { name?: string }[],
): string | undefined {
const crewDirector = credits?.crew?.find?.((p) => p.job === "Director");
return crewDirector?.name ?? createdBy?.[0]?.name;
}
export const handler: Handlers = { export const handler: Handlers = {
async GET(_, ctx) { async GET(_, ctx) {
@@ -19,54 +28,54 @@ export const handler: Handlers = {
} }
const tmdbId = parseInt(ctx.params.name); const tmdbId = parseInt(ctx.params.name);
if (Number.isNaN(tmdbId)) throw new BadRequestError();
const seriesDetails = await tmdb.getSeries(tmdbId); const [seriesDetails, seriesCredits] = await Promise.all([
const seriesCredits = await tmdb.getSeriesCredits(tmdbId); tmdb.getSeries(tmdbId),
tmdb.getSeriesCredits(tmdbId),
]);
const releaseDate = seriesDetails.first_air_date; const name = seriesDetails.name ||
const posterPath = seriesDetails.poster_path; seriesDetails.original_name ||
const director = ctx.params.name;
seriesCredits?.crew?.filter?.((person) => person.job === "Director")[0] ||
seriesDetails.created_by?.[0];
let finalPath = ""; let finalPath = "";
const name = seriesDetails.name || seriesDetails.original_name || const posterPath = seriesDetails.poster_path;
ctx.params.name;
if (posterPath) { if (posterPath) {
const poster = await tmdb.getMoviePoster(posterPath); const poster = await tmdb.getMoviePoster(posterPath);
const extension = fileExtension(posterPath); const extension = fileExtension(posterPath);
const imagePath = `series/images/${
finalPath = `series/images/${safeFileName(name)}_cover.${extension}`; safeFileName(name)
await createResource(finalPath, poster); }_cover.${extension}`;
await createResource(imagePath, poster);
finalPath = imagePath;
} }
const tags: string[] = []; const keywords = seriesDetails.genres
if (seriesDetails.genres) { ?.map((g) => g.name?.toLowerCase())
tags.push( .filter(isString) ??
...seriesDetails.genres.map((g) => g.name?.toLowerCase()).filter( [];
isString,
),
);
}
const series: ReviewResource["content"] = { const series: ReviewResource["content"] = {
_type: "Review", _type: "Review",
image: `resources/${finalPath}`, image: `resources/${finalPath}`,
datePublished: releaseDate, datePublished: formatDate(seriesDetails.first_air_date),
tmdbId, tmdbId,
author: { author: {
_type: "Person", _type: "Person",
name: director?.name, name: pickDirector(seriesCredits, seriesDetails?.created_by),
}, },
itemReviewed: { itemReviewed: {
name: name, name: name,
}, },
reviewBody: "", reviewBody: "",
keywords: tags, keywords: keywords,
}; };
await createResource(`series/${safeFileName(name)}.md`, series); const fileName = toUrlSafeString(name);
return json(series); await createResource(`series/${fileName}.md`, series);
return json({ name: fileName });
}, },
}; };

View File

@@ -1,5 +1,4 @@
import { FreshContext, Handlers } from "$fresh/server.ts"; import { FreshContext, Handlers } from "$fresh/server.ts";
import { FreshContext, Handlers } from "$fresh/server.ts";
import { fileExtension } from "https://deno.land/x/file_extension@v2.1.0/mod.ts"; import { fileExtension } from "https://deno.land/x/file_extension@v2.1.0/mod.ts";
import * as tmdb from "@lib/tmdb.ts"; import * as tmdb from "@lib/tmdb.ts";
import { safeFileName } from "@lib/string.ts"; import { safeFileName } from "@lib/string.ts";
@@ -37,42 +36,43 @@ const POST = async (
} }
const seriesDetails = await tmdb.getSeries(tmdbId); const seriesDetails = await tmdb.getSeries(tmdbId);
const seriesCredits = !series.meta?.author && const seriesCredits = !series?.content?.author &&
await tmdb.getSeriesCredits(tmdbId); await tmdb.getSeriesCredits(tmdbId);
const releaseDate = seriesDetails.first_air_date; const releaseDate = seriesDetails.first_air_date;
if (releaseDate && series.meta?.date) { if (releaseDate && series.content?.datePublished) {
series.meta.date = new Date(releaseDate); series.content.datePublished = new Date(releaseDate).toISOString();
} }
const posterPath = seriesDetails.poster_path; const posterPath = seriesDetails.poster_path;
const director = seriesCredits && const director = seriesCredits &&
seriesCredits.crew?.filter?.((person) => person.job === "Director")[0] || seriesCredits.crew?.filter?.((person) => person.job === "Director")[0] ||
seriesDetails?.created_by?.[0]; seriesDetails?.created_by?.[0];
if (director && director.name && !series.meta?.author) { if (director && director.name && !series.content?.author) {
series.author = series.author || {}; series.content.author = series.content.author || {
series.author["_type"] = "Person"; _type: "Person",
series.author.name = director.name; name: director.name,
};
} }
if (seriesDetails.genres) { if (seriesDetails.genres) {
series.keywords = [ series.content.keywords = [
...new Set([ ...new Set([
...(series.tags?.map((t) => t.toLowerCase()) || []), ...(series.content.keywords?.map((t) => t.toLowerCase()) || []),
...seriesDetails.genres.map((g) => g.name?.toLowerCase()), ...seriesDetails.genres.map((g) => g.name?.toLowerCase()),
].filter(isString)), ].filter(isString)),
]; ];
} }
let finalPath = ""; let finalPath = "";
if (posterPath && !series.meta?.image) { if (posterPath && !series.content?.image) {
const poster = await tmdb.getMoviePoster(posterPath); const poster = await tmdb.getMoviePoster(posterPath);
const extension = fileExtension(posterPath); const extension = fileExtension(posterPath);
finalPath = `series/images/${safeFileName(name)}_cover.${extension}`; finalPath = `series/images/${safeFileName(name)}_cover.${extension}`;
await createResource(finalPath, poster); await createResource(finalPath, poster);
series.image = finalPath; series.content.image = finalPath;
} }
await createResource(`series/${safeFileName(series.id)}.md`, series); await createResource(`series/${safeFileName(series.name)}.md`, series);
return json(series); return json(series);
}; };

View File

@@ -19,9 +19,9 @@ const GET = async (
throw new BadRequestError(); throw new BadRequestError();
} }
const type = u.searchParams.get("type") || "movie"; const type = u.searchParams.get("type") || "movies";
const res = type === "movie" const res = type === "movies"
? await searchMovie(query) ? await searchMovie(query)
: await searchTVShow(query); : await searchTVShow(query);

View File

@@ -11,28 +11,38 @@ import { Star } from "@components/Stars.tsx";
import { MetaTags } from "@components/MetaTags.tsx"; import { MetaTags } from "@components/MetaTags.tsx";
import { fetchResource } from "@lib/marka/index.ts"; import { fetchResource } from "@lib/marka/index.ts";
import { ArticleResource } from "@lib/marka/schema.ts"; import { ArticleResource } from "@lib/marka/schema.ts";
import { parseRating } from "@lib/helpers.ts";
export const handler: Handlers<{ article: ArticleResource; session: unknown }> = { export const handler: Handlers<{ article: ArticleResource; session: unknown }> =
{
async GET(_, ctx) { async GET(_, ctx) {
const article = await fetchResource(`articles/${ctx.params.name}.md`); const article = await fetchResource<ArticleResource>(
`articles/${ctx.params.name}.md`,
);
if (!article) { if (!article) {
return ctx.renderNotFound(); return ctx.renderNotFound();
} }
return ctx.render({ article, session: ctx.state.session }); return ctx.render({ article, session: ctx.state.session });
}, },
}; };
export default function Greet( export default function Greet(
props: PageProps<{ article: Article; session: Record<string, string> }>, props: PageProps<
{ article: ArticleResource; session: Record<string, string> }
>,
) { ) {
const { article, session } = props.data; const { article, session } = props.data;
const { author = "", date = "", articleBody = "" } = article?.content || {}; const { author, datePublished, reviewRating, articleBody = "" } =
article?.content || {};
const content = renderMarkdown( const content = renderMarkdown(
removeImage(articleBody, article.image?.url), removeImage(articleBody, article.image?.url),
); );
const rating = reviewRating?.ratingValue &&
parseRating(reviewRating.ratingValue);
return ( return (
<MainLayout <MainLayout
url={props.url} url={props.url}
@@ -40,7 +50,7 @@ export default function Greet(
context={article} context={article}
> >
<RedirectSearchHandler /> <RedirectSearchHandler />
<KMenu type="main" context={{ type: "article" }} /> <KMenu type="main" context={article} />
<MetaTags resource={article} /> <MetaTags resource={article} />
<PageHero <PageHero
@@ -61,36 +71,35 @@ export default function Greet(
</PageHero.Title> </PageHero.Title>
<PageHero.Subline <PageHero.Subline
entries={[ entries={[
author && { author?.name && {
title: author, title: author.name,
href: `/?q=${encodeURIComponent(author)}`, href: `/?q=${encodeURIComponent(author?.name)}`,
}, },
date.toString(), datePublished?.toString(),
]} ]}
> >
{article.content.rating && <Star rating={article.content.rating} />} {rating && <Star rating={rating} />}
</PageHero.Subline> </PageHero.Subline>
</PageHero.Footer> </PageHero.Footer>
</PageHero> </PageHero>
{article.content?.tags?.length > 0 && ( {article.content?.keywords?.length && (
<> <>
<br /> <br />
<HashTags tags={article.content.tags} /> <HashTags tags={article.content.keywords} />
</> </>
)} )}
<div class="px-8 text-white mt-10"> <div class="px-8 text-white mt-10">
{isYoutubeLink(article.content.url) && ( {(article.content.url && isYoutubeLink(article.content.url)) && (
<YoutubePlayer link={article.content.url} /> <YoutubePlayer link={article.content.url} />
)} )}
<pre <pre
class="whitespace-break-spaces markdown-body" class="whitespace-break-spaces markdown-body"
data-color-mode="dark" data-color-mode="dark"
data-dark-theme="dark" data-dark-theme="dark"
// deno-lint-ignore react-no-danger
dangerouslySetInnerHTML={{ __html: content || "" }} dangerouslySetInnerHTML={{ __html: content || "" }}
> />
{content || ""}
</pre>
</div> </div>
</MainLayout> </MainLayout>
); );

View File

@@ -1,12 +1,11 @@
import { Handlers, PageProps } from "$fresh/server.ts"; import { Handlers, PageProps } from "$fresh/server.ts";
import { MainLayout } from "@components/layouts/main.tsx"; import { MainLayout } from "@components/layouts/main.tsx";
import { type ArticleResource } from "@lib/marka/schema.ts"; import { type ArticleResource, GenericResource } from "@lib/marka/schema.ts";
import { KMenu } from "@islands/KMenu.tsx"; import { KMenu } from "@islands/KMenu.tsx";
import { Grid } from "@components/Grid.tsx"; import { Grid } from "@components/Grid.tsx";
import { IconArrowLeft } from "@components/icons.tsx"; import { IconArrowLeft } from "@components/icons.tsx";
import { RedirectSearchHandler } from "@islands/Search.tsx"; import { RedirectSearchHandler } from "@islands/Search.tsx";
import { parseResourceUrl, searchResource } from "@lib/search.ts"; import { parseResourceUrl, searchResource } from "@lib/search.ts";
import { GenericResource } from "@lib/types.ts";
import { ResourceCard } from "@components/Card.tsx"; import { ResourceCard } from "@components/Card.tsx";
import { Link } from "@islands/Link.tsx"; import { Link } from "@islands/Link.tsx";
import { listResources } from "@lib/marka/index.ts"; import { listResources } from "@lib/marka/index.ts";
@@ -15,10 +14,10 @@ export const handler: Handlers<
{ articles: ArticleResource[] | null; searchResults?: GenericResource[] } { articles: ArticleResource[] | null; searchResults?: GenericResource[] }
> = { > = {
async GET(req, ctx) { async GET(req, ctx) {
const articles = await listResources("articles"); const articles = await listResources<ArticleResource>("articles");
const searchParams = parseResourceUrl(req.url); const searchParams = parseResourceUrl(req.url);
const searchResults = searchParams && const searchResults = searchParams &&
await searchResource({ ...searchParams, types: ["article"] }); await searchResource({ ...searchParams, types: ["articles"] });
return ctx.render({ articles, searchResults }); return ctx.render({ articles, searchResults });
}, },
}; };
@@ -33,7 +32,7 @@ export default function Greet(
<MainLayout <MainLayout
url={props.url} url={props.url}
title="Articles" title="Articles"
context={{ type: "article" }} context={{ type: "articles" }}
searchResults={searchResults} searchResults={searchResults}
> >
<header class="flex gap-4 items-center mb-5 md:hidden"> <header class="flex gap-4 items-center mb-5 md:hidden">
@@ -48,10 +47,11 @@ export default function Greet(
<h3 class="text-2xl text-white font-light">📝 Articles</h3> <h3 class="text-2xl text-white font-light">📝 Articles</h3>
</header> </header>
<RedirectSearchHandler /> <RedirectSearchHandler />
<KMenu type="main" context={{ type: "article" }} /> <KMenu type="main" context={{ type: "articles" }} />
<Grid> <Grid>
{articles?.map((doc) => ( {articles?.map((doc, i) => (
<ResourceCard <ResourceCard
key={doc.name || i}
sublink="articles" sublink="articles"
res={doc} res={doc}
/> />

View File

@@ -12,17 +12,16 @@ export default function Home(props: PageProps) {
<RedirectSearchHandler /> <RedirectSearchHandler />
<KMenu type="main" context={false} /> <KMenu type="main" context={false} />
<MainLayout url={props.url}> <MainLayout url={props.url}>
<h1 class="text-4xl mb-4 mt-3 text-white flex gap-2">
<img src="/favicon.png" class="w-8 h-8 inline" />
Resources
</h1>
<div class="flex flex-wrap items-center gap-4"> <div class="flex flex-wrap items-center gap-4">
{Object.values(resources).filter((v) => v.link !== "/").map((m) => { {Object.values(resources).filter((v) => v.link !== "/").map((m) => {
return ( return (
<Card <Card
splotch
key={m.link}
title={`${m.name}`} title={`${m.name}`}
backgroundSize={80} backgroundSize={80}
image={`${m.emoji.endsWith(".png") image={`${
m.emoji.endsWith(".png")
? `/emojis/${encodeURIComponent(m.emoji)}` ? `/emojis/${encodeURIComponent(m.emoji)}`
: "/placeholder.svg" : "/placeholder.svg"
}`} }`}

View File

@@ -1,6 +1,6 @@
import { PageProps, RouteContext } from "$fresh/server.ts"; import { PageProps, RouteContext } from "$fresh/server.ts";
import { MainLayout } from "@components/layouts/main.tsx"; import { MainLayout } from "@components/layouts/main.tsx";
import { ReviewResource, ReviewSchema } from "@lib/marka/schema.ts"; import { ReviewResource } from "@lib/marka/schema.ts";
import { removeImage, renderMarkdown } from "@lib/markdown.ts"; import { removeImage, renderMarkdown } from "@lib/markdown.ts";
import { KMenu } from "@islands/KMenu.tsx"; import { KMenu } from "@islands/KMenu.tsx";
import { RedirectSearchHandler } from "@islands/Search.tsx"; import { RedirectSearchHandler } from "@islands/Search.tsx";
@@ -24,12 +24,16 @@ export default async function Greet(
return ctx.renderNotFound(); return ctx.renderNotFound();
} }
const { author = "", datePublished = "" } = movie.content; const { author, datePublished, reviewBody = "", reviewRating } =
movie.content;
const content = renderMarkdown( const content = renderMarkdown(
removeImage(movie.content.reviewBody || "", movie.content.image), removeImage(reviewBody, movie.content.image),
); );
const rating = reviewRating?.ratingValue &&
parseRating(reviewRating.ratingValue);
return ( return (
<MainLayout url={props.url} title={`Movie > ${movie.name}`} context={movie}> <MainLayout url={props.url} title={`Movie > ${movie.name}`} context={movie}>
<RedirectSearchHandler /> <RedirectSearchHandler />
@@ -53,37 +57,33 @@ export default async function Greet(
</PageHero.Title> </PageHero.Title>
<PageHero.Subline <PageHero.Subline
entries={[ entries={[
author && { author?.name &&
title: author?.name, {
title: author.name,
href: `/?q=${encodeURIComponent(author?.name)}`, href: `/?q=${encodeURIComponent(author?.name)}`,
}, },
date.toString(), datePublished?.toString(),
]} ]}
> >
{movie.content.reviewRating && ( {rating && <Star rating={rating} />}
<Star
rating={parseRating(movie.content?.reviewRating?.ratingValue)}
/>
)}
</PageHero.Subline> </PageHero.Subline>
</PageHero.Footer> </PageHero.Footer>
</PageHero> </PageHero>
{false && ( {movie.name && (
<Recommendations <Recommendations
id={movie.id} id={movie.name}
type="movie" type="movies"
/> />
)} )}
<div class="px-8 text-white mt-10"> <div class="px-8 text-white mt-10">
{movie?.content?.reviewBody?.length > 80 {reviewBody?.length > 80 && (
? <h2 class="text-4xl font-bold mb-4">Review</h2> <h2 class="text-4xl font-bold mb-4">Review</h2>
: <></>} )}
<pre <pre
class="whitespace-break-spaces" class="whitespace-break-spaces"
// deno-lint-ignore react-no-danger
dangerouslySetInnerHTML={{ __html: content || "" }} dangerouslySetInnerHTML={{ __html: content || "" }}
> />
{content}
</pre>
</div> </div>
</MainLayout> </MainLayout>
); );

View File

@@ -1,40 +1,44 @@
import { MainLayout } from "@components/layouts/main.tsx"; import { MainLayout } from "@components/layouts/main.tsx";
import { ReviewResource } from "@lib/marka/schema.ts"; import { GenericResource, ReviewResource } from "@lib/marka/schema.ts";
import { ResourceCard } from "@components/Card.tsx"; import { ResourceCard } from "@components/Card.tsx";
import { Grid } from "@components/Grid.tsx"; import { Grid } from "@components/Grid.tsx";
import { IconArrowLeft } from "@components/icons.tsx"; import { IconArrowLeft } from "@components/icons.tsx";
import { KMenu } from "@islands/KMenu.tsx"; import { KMenu } from "@islands/KMenu.tsx";
import { RedirectSearchHandler } from "@islands/Search.tsx"; import { RedirectSearchHandler } from "@islands/Search.tsx";
import { GenericResource } from "@lib/types.ts";
import { PageProps } from "$fresh/server.ts"; import { PageProps } from "$fresh/server.ts";
import { listResources } from "@lib/marka/index.ts"; import { listResources } from "@lib/marka/index.ts";
import { parseResourceUrl, searchResource } from "@lib/search.ts"; import { parseResourceUrl, searchResource } from "@lib/search.ts";
import { parseRating } from "@lib/helpers.ts";
function sortOptional(a: number | string = 0, b: number | string = 0) {
return (parseRating(a) > parseRating(b)) ? 1 : -1;
}
export default async function MovieIndex( export default async function MovieIndex(
props: PageProps< props: PageProps<
{ movies: ReviewResource[] | null; searchResults: GenericResource[] } { movies: ReviewResource[] | null; searchResults: GenericResource[] }
>, >,
) { ) {
const allMovies = await listResources("movies"); const allMovies = await listResources<ReviewResource>("movies");
const searchParams = parseResourceUrl(props.url); const searchParams = parseResourceUrl(props.url);
const searchResults = searchParams && const searchResults = searchParams &&
await searchResource({ ...searchParams, types: ["movie"] }); await searchResource({ ...searchParams, types: ["movies"] });
const movies = allMovies.sort((a, b) => const movies = allMovies.sort((a, b) =>
a?.content?.reviewRating?.ratingValue > sortOptional(
b?.content?.reviewRating?.ratingValue a.content.reviewRating?.ratingValue,
? -1 b.content.reviewRating?.ratingValue,
: 1 )
); );
return ( return (
<MainLayout <MainLayout
url={props.url} url={props.url}
title="Movies" title="Movies"
context={{ type: "movie" }} context={{ type: "movies" }}
searchResults={searchResults} searchResults={searchResults}
> >
<RedirectSearchHandler /> <RedirectSearchHandler />
<KMenu type="main" context={{ type: "movie" }} /> <KMenu type="main" context={{ type: "movies" }} />
<header class="flex gap-4 items-center mb-5 md:hidden"> <header class="flex gap-4 items-center mb-5 md:hidden">
<a <a
class="px-4 ml-4 py-2 bg-gray-300 text-gray-800 rounded-lg flex items-center gap-1" class="px-4 ml-4 py-2 bg-gray-300 text-gray-800 rounded-lg flex items-center gap-1"
@@ -48,7 +52,7 @@ export default async function MovieIndex(
</header> </header>
<Grid> <Grid>
{movies?.map((doc, i) => { {movies?.map((doc, i) => {
return <ResourceCard key={i} res={doc} />; return <ResourceCard key={doc.name || i} res={doc} />;
})} })}
</Grid> </Grid>
</MainLayout> </MainLayout>

View File

@@ -3,7 +3,6 @@ import { IngredientsList } from "@islands/IngredientsList.tsx";
import { MainLayout } from "@components/layouts/main.tsx"; import { MainLayout } from "@components/layouts/main.tsx";
import Counter from "@islands/Counter.tsx"; import Counter from "@islands/Counter.tsx";
import { Signal, useSignal } from "@preact/signals"; import { Signal, useSignal } from "@preact/signals";
import { Recipe } from "@lib/recipeSchema.ts";
import { RedirectSearchHandler } from "@islands/Search.tsx"; import { RedirectSearchHandler } from "@islands/Search.tsx";
import { KMenu } from "@islands/KMenu.tsx"; import { KMenu } from "@islands/KMenu.tsx";
import PageHero from "@components/PageHero.tsx"; import PageHero from "@components/PageHero.tsx";
@@ -12,11 +11,18 @@ import { renderMarkdown } from "@lib/markdown.ts";
import { isValidRecipe } from "@lib/recipeSchema.ts"; import { isValidRecipe } from "@lib/recipeSchema.ts";
import { MetaTags } from "@components/MetaTags.tsx"; import { MetaTags } from "@components/MetaTags.tsx";
import { fetchResource } from "@lib/marka/index.ts"; import { fetchResource } from "@lib/marka/index.ts";
import { RecipeResource } from "@lib/marka/schema.ts";
import { parseIngredients } from "@lib/parseIngredient.ts";
import { parseRating } from "@lib/helpers.ts";
export const handler: Handlers<{ recipe: Recipe; session: unknown } | null> = { export const handler: Handlers<
{ recipe: RecipeResource; session: unknown } | null
> = {
async GET(_, ctx) { async GET(_, ctx) {
try { try {
const recipe = await fetchResource(`recipes/${ctx.params.name}.md`); const recipe = await fetchResource<RecipeResource>(
`recipes/${ctx.params.name}.md`,
);
if (!recipe) { if (!recipe) {
return ctx.renderNotFound(); return ctx.renderNotFound();
} }
@@ -31,7 +37,11 @@ function ValidRecipe({
recipe, recipe,
amount, amount,
portion, portion,
}: { recipe: Recipe; amount: Signal<number>; portion: number }) { }: { recipe: RecipeResource; amount: Signal<number>; portion: number }) {
const ingredients = parseIngredients(
recipe.content.recipeIngredient?.join("\n") || "",
);
return ( return (
<> <>
<div class="flex items-center gap-8"> <div class="flex items-center gap-8">
@@ -39,7 +49,7 @@ function ValidRecipe({
{portion && <Counter count={amount} />} {portion && <Counter count={amount} />}
</div> </div>
<IngredientsList <IngredientsList
ingredients={recipe.content.recipeIngredient} ingredients={ingredients}
amount={amount} amount={amount}
portion={portion} portion={portion}
/> />
@@ -47,10 +57,12 @@ function ValidRecipe({
<div class="pl-2"> <div class="pl-2">
<ol class="list-decimal grid gap-4"> <ol class="list-decimal grid gap-4">
{recipe.content.recipeInstructions && {recipe.content.recipeInstructions &&
(recipe.content.recipeInstructions.filter((inst) => !!inst?.length) (recipe.content.recipeInstructions
.filter((inst) => !!inst?.length)
.map((instruction) => { .map((instruction) => {
return ( return (
<li <li
// deno-lint-ignore react-no-danger
dangerouslySetInnerHTML={{ dangerouslySetInnerHTML={{
__html: renderMarkdown(instruction), __html: renderMarkdown(instruction),
}} }}
@@ -64,17 +76,20 @@ function ValidRecipe({
} }
export default function Page( export default function Page(
props: PageProps<{ recipe: Recipe; session: Record<string, string> }>, props: PageProps<{ recipe: RecipeResource; session: Record<string, string> }>,
) { ) {
const { recipe, session } = props.data; const { recipe, session } = props.data;
const portion = recipe.recipeYield; const portion = recipe.content.recipeYield;
const amount = useSignal(portion || 1); const amount = useSignal(portion || 1);
const subline = [ const subline = [
recipe?.content?.prepTime && `Duration ${recipe?.content?.prepTime}`, recipe?.content?.totalTime && `Duration ${recipe?.content?.totalTime}`,
].filter(Boolean) as string[]; ].filter(Boolean) as string[];
const rating = recipe.content.reviewRating?.ratingValue &&
parseRating(recipe.content.reviewRating.ratingValue);
return ( return (
<MainLayout <MainLayout
url={props.url} url={props.url}
@@ -98,13 +113,13 @@ export default function Page(
)} )}
</PageHero.Header> </PageHero.Header>
<PageHero.Footer> <PageHero.Footer>
<PageHero.Title link={recipe.content?.link}> <PageHero.Title link={recipe.content?.url}>
{recipe.content.name} {recipe.content.name}
</PageHero.Title> </PageHero.Title>
<PageHero.Subline <PageHero.Subline
entries={subline} entries={subline}
> >
{recipe.meta?.rating && <Star rating={recipe.meta?.rating} />} {rating && <Star rating={rating} />}
</PageHero.Subline> </PageHero.Subline>
</PageHero.Footer> </PageHero.Footer>
</PageHero> </PageHero>

View File

@@ -1,30 +1,29 @@
import { Handlers, PageProps } from "$fresh/server.ts"; import { Handlers, PageProps } from "$fresh/server.ts";
import { MainLayout } from "@components/layouts/main.tsx"; import { MainLayout } from "@components/layouts/main.tsx";
import { Recipe } from "@lib/recipeSchema.ts";
import { Grid } from "@components/Grid.tsx"; import { Grid } from "@components/Grid.tsx";
import { IconArrowLeft } from "@components/icons.tsx"; import { IconArrowLeft } from "@components/icons.tsx";
import { KMenu } from "@islands/KMenu.tsx"; import { KMenu } from "@islands/KMenu.tsx";
import { RedirectSearchHandler } from "@islands/Search.tsx"; import { RedirectSearchHandler } from "@islands/Search.tsx";
import { GenericResource } from "@lib/types.ts";
import { ResourceCard } from "@components/Card.tsx"; import { ResourceCard } from "@components/Card.tsx";
import { fetchResource, listResources } from "@lib/marka/index.ts"; import { listResources } from "@lib/marka/index.ts";
import { parseResourceUrl, searchResource } from "@lib/search.ts"; import { parseResourceUrl, searchResource } from "@lib/search.ts";
import { GenericResource, RecipeResource } from "@lib/marka/schema.ts";
export const handler: Handlers< export const handler: Handlers<
{ recipes: Recipe[] | null; searchResults?: GenericResource[] } { recipes: RecipeResource[] | null; searchResults?: GenericResource[] }
> = { > = {
async GET(req, ctx) { async GET(req, ctx) {
const recipes = await listResources("recipes"); const recipes = await listResources<RecipeResource>("recipes");
const searchParams = parseResourceUrl(req.url); const searchParams = parseResourceUrl(req.url);
const searchResults = searchParams && const searchResults = searchParams &&
await searchResource({ ...searchParams, types: ["recipe"] }); await searchResource({ ...searchParams, types: ["recipes"] });
return ctx.render({ recipes, searchResults }); return ctx.render({ recipes, searchResults });
}, },
}; };
export default function Greet( export default function Greet(
props: PageProps< props: PageProps<
{ recipes: Recipe[] | null; searchResults: GenericResource[] } { recipes: RecipeResource[] | null; searchResults: GenericResource[] }
>, >,
) { ) {
const { recipes, searchResults } = props.data; const { recipes, searchResults } = props.data;
@@ -33,10 +32,10 @@ export default function Greet(
url={props.url} url={props.url}
title="Recipes" title="Recipes"
searchResults={searchResults} searchResults={searchResults}
context={{ type: "recipe" }} context={{ type: "recipes" }}
> >
<RedirectSearchHandler /> <RedirectSearchHandler />
<KMenu type="main" context={{ type: "recipe" }} /> <KMenu type="main" context={{ type: "recipes" }} />
<header class="flex gap-4 items-center mb-2 lg:mb-5 md:hidden"> <header class="flex gap-4 items-center mb-2 lg:mb-5 md:hidden">
<a <a
class="px-4 lg:ml-4 py-2 bg-gray-300 text-gray-800 rounded-lg flex items-center gap-1" class="px-4 lg:ml-4 py-2 bg-gray-300 text-gray-800 rounded-lg flex items-center gap-1"

View File

@@ -9,11 +9,13 @@ import { Star } from "@components/Stars.tsx";
import { MetaTags } from "@components/MetaTags.tsx"; import { MetaTags } from "@components/MetaTags.tsx";
import { parseRating } from "@lib/helpers.ts"; import { parseRating } from "@lib/helpers.ts";
import { fetchResource } from "@lib/marka/index.ts"; import { fetchResource } from "@lib/marka/index.ts";
import { ReviewResource } from "@lib/marka/schema.ts"; import { getNameOfResource, ReviewResource } from "@lib/marka/schema.ts";
export const handler: Handlers<{ serie: ReviewResource; session: unknown }> = { export const handler: Handlers<{ serie: ReviewResource; session: unknown }> = {
async GET(_, ctx) { async GET(_, ctx) {
const serie = await fetchResource(`series/${ctx.params.name}.md`); const serie = await fetchResource<ReviewResource>(
`series/${ctx.params.name}.md`,
);
if (!serie) { if (!serie) {
return ctx.renderNotFound(); return ctx.renderNotFound();
@@ -23,20 +25,23 @@ export const handler: Handlers<{ serie: ReviewResource; session: unknown }> = {
}; };
export default function Greet( export default function Greet(
props: PageProps<{ serie: Series; session: Record<string, string> }>, props: PageProps<{ serie: ReviewResource; session: Record<string, string> }>,
) { ) {
const { serie, session } = props.data; const { serie, session } = props.data;
const { author = "", date = "" } = serie?.content || {}; const { author, datePublished, reviewBody = "" } = serie?.content || {};
const content = renderMarkdown( const content = renderMarkdown(
removeImage(serie.description || "", serie.content?.image), removeImage(reviewBody, serie.image?.url),
); );
const rating = serie.content.reviewRating?.ratingValue &&
parseRating(serie.content.reviewRating.ratingValue);
return ( return (
<MainLayout <MainLayout
url={props.url} url={props.url}
title={`Serie > ${serie.content?.name}`} title={`Serie > ${serie.content?.itemReviewed?.name}`}
context={serie} context={serie}
> >
<RedirectSearchHandler /> <RedirectSearchHandler />
@@ -56,40 +61,36 @@ export default function Greet(
)} )}
</PageHero.Header> </PageHero.Header>
<PageHero.Footer> <PageHero.Footer>
<PageHero.Title>{serie.name}</PageHero.Title> <PageHero.Title>{serie.content.itemReviewed?.name}</PageHero.Title>
<PageHero.Subline <PageHero.Subline
entries={[ entries={[
author && { author?.name &&
title: author, {
href: `/?q=${encodeURIComponent(author)}`, title: author.name,
href: `/?q=${encodeURIComponent(author?.name)}`,
}, },
date.toString(), datePublished?.toString(),
]} ]}
> >
{serie.content?.reviewRating && ( {rating && <Star rating={rating} />}
<Star
rating={parseRating(serie.content?.reviewRating?.ratingValue)}
/>
)}
</PageHero.Subline> </PageHero.Subline>
</PageHero.Footer> </PageHero.Footer>
</PageHero> </PageHero>
{serie.content?.tags?.length > 0 && ( {serie.content?.keywords?.length && (
<> <>
<br /> <br />
<HashTags tags={serie.content?.tags} /> <HashTags tags={serie.content?.keywords} />
</> </>
)} )}
<div class="px-8 text-white mt-10"> <div class="px-8 text-white mt-10">
{serie?.content?.reviewBody?.length > 80 {serie?.content?.reviewBody?.length && (
? <h2 class="text-4xl font-bold mb-4">Review</h2> <h2 class="text-4xl font-bold mb-4">Review</h2>
: <></>} )}
<pre <pre
class="whitespace-break-spaces" class="whitespace-break-spaces"
// deno-lint-ignore react-no-danger
dangerouslySetInnerHTML={{ __html: content || "" }} dangerouslySetInnerHTML={{ __html: content || "" }}
> />
{content}
</pre>
</div> </div>
</MainLayout> </MainLayout>
); );

View File

@@ -13,7 +13,7 @@ export const handler: Handlers<
{ series: ReviewResource[] | null; searchResults?: GenericResource[] } { series: ReviewResource[] | null; searchResults?: GenericResource[] }
> = { > = {
async GET(req, ctx) { async GET(req, ctx) {
const series = await listResources("series"); const series = await listResources<ReviewResource>("series");
const searchParams = parseResourceUrl(req.url); const searchParams = parseResourceUrl(req.url);
const searchResults = searchParams && const searchResults = searchParams &&
await searchResource({ ...searchParams, types: ["series"] }); await searchResource({ ...searchParams, types: ["series"] });
@@ -50,7 +50,9 @@ export default function Greet(
</header> </header>
<Grid> <Grid>
{series?.map((doc, i) => { {series?.map((doc, i) => {
return <ResourceCard key={i} sublink="series" res={doc} />; return (
<ResourceCard key={doc.name || i} sublink="series" res={doc} />
);
})} })}
</Grid> </Grid>
</MainLayout> </MainLayout>

View File

@@ -2,7 +2,7 @@
<browserconfig> <browserconfig>
<msapplication> <msapplication>
<tile> <tile>
<square150x150logo src="/mstile-150x150.png"/> <square150x150logo src="/mstile-150x150.png" />
<TileColor>#da532c</TileColor> <TileColor>#da532c</TileColor>
</tile> </tile>
</msapplication> </msapplication>

Binary file not shown.

After

Width:  |  Height:  |  Size: 162 KiB

BIN
static/emojis/home_icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 190 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 174 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 144 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 185 KiB

File diff suppressed because one or more lines are too long

View File

@@ -1,6 +1,19 @@
<svg width="40" height="40" fill="none" xmlns="http://www.w3.org/2000/svg"> <svg width="40" height="40" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M34.092 8.845C38.929 20.652 34.092 27 30 30.5c1 3.5-2.986 4.222-4.5 2.5-4.457 1.537-13.512 1.487-20-5C2 24.5 4.73 16.714 14 11.5c8-4.5 16-7 20.092-2.655Z" fill="#FFDB1E"/> <path
<path d="M14 11.5c6.848-4.497 15.025-6.38 18.368-3.47C37.5 12.5 21.5 22.612 15.5 25c-6.5 2.587-3 8.5-6.5 8.5-3 0-2.5-4-5.183-7.75C2.232 23.535 6.16 16.648 14 11.5Z" fill="#fff" stroke="#FFDB1E"/> d="M34.092 8.845C38.929 20.652 34.092 27 30 30.5c1 3.5-2.986 4.222-4.5 2.5-4.457 1.537-13.512 1.487-20-5C2 24.5 4.73 16.714 14 11.5c8-4.5 16-7 20.092-2.655Z"
<path d="M28.535 8.772c4.645 1.25-.365 5.695-4.303 8.536-3.732 2.692-6.606 4.21-7.923 4.83-.366.173-1.617-2.252-1.617-1 0 .417-.7 2.238-.934 2.326-1.365.512-4.223 1.29-5.835 1.29-3.491 0-1.923-4.754 3.014-9.122.892-.789 1.478-.645 2.283-.645-.537-.773-.534-.917.403-1.546C17.79 10.64 23 8.77 25.212 8.42c.366.014.82.35.82.629.41-.14 2.095-.388 2.503-.278Z" fill="#FFE600"/> fill="#FFDB1E"
<path d="M14.297 16.49c.985-.747 1.644-1.01 2.099-2.526.566.121.841-.08 1.29-.701.324.466 1.657.608 2.453.701-.715.451-1.057.852-1.452 2.106-1.464-.611-3.167-.302-4.39.42Z" fill="#fff"/> />
<path
d="M14 11.5c6.848-4.497 15.025-6.38 18.368-3.47C37.5 12.5 21.5 22.612 15.5 25c-6.5 2.587-3 8.5-6.5 8.5-3 0-2.5-4-5.183-7.75C2.232 23.535 6.16 16.648 14 11.5Z"
fill="#fff"
stroke="#FFDB1E"
/>
<path
d="M28.535 8.772c4.645 1.25-.365 5.695-4.303 8.536-3.732 2.692-6.606 4.21-7.923 4.83-.366.173-1.617-2.252-1.617-1 0 .417-.7 2.238-.934 2.326-1.365.512-4.223 1.29-5.835 1.29-3.491 0-1.923-4.754 3.014-9.122.892-.789 1.478-.645 2.283-.645-.537-.773-.534-.917.403-1.546C17.79 10.64 23 8.77 25.212 8.42c.366.014.82.35.82.629.41-.14 2.095-.388 2.503-.278Z"
fill="#FFE600"
/>
<path
d="M14.297 16.49c.985-.747 1.644-1.01 2.099-2.526.566.121.841-.08 1.29-.701.324.466 1.657.608 2.453.701-.715.451-1.057.852-1.452 2.106-1.464-.611-3.167-.302-4.39.42Z"
fill="#fff"
/>
</svg> </svg>

Before

Width:  |  Height:  |  Size: 1.0 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

BIN
static/noise.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

View File

@@ -1,7 +1,35 @@
<svg width="387" height="387" viewBox="0 0 387 387" fill="none" xmlns="http://www.w3.org/2000/svg"> <svg
<rect width="387" height="387" fill="#2B2930"/> width="387"
<circle cx="212.471" cy="154.8" r="15.5559" stroke="#39363F" stroke-width="3.03529"/> height="387"
<path d="M110.978 249.463L155.261 205.18C161.188 199.253 170.797 199.253 176.724 205.18L221.007 249.463" stroke="#39363F" stroke-width="3.03529"/> viewBox="0 0 387 387"
<path d="M192.362 220.628L207.81 205.18C213.737 199.253 223.346 199.253 229.273 205.18L273.556 249.463" stroke="#39363F" stroke-width="3.03529"/> fill="none"
<rect x="86.1265" y="86.1265" width="214.747" height="214.747" rx="13.6588" stroke="#39363F" stroke-width="3.03529"/> xmlns="http://www.w3.org/2000/svg"
>
<rect width="387" height="387" fill="#2B2930" />
<circle
cx="212.471"
cy="154.8"
r="15.5559"
stroke="#39363F"
stroke-width="3.03529"
/>
<path
d="M110.978 249.463L155.261 205.18C161.188 199.253 170.797 199.253 176.724 205.18L221.007 249.463"
stroke="#39363F"
stroke-width="3.03529"
/>
<path
d="M192.362 220.628L207.81 205.18C213.737 199.253 223.346 199.253 229.273 205.18L273.556 249.463"
stroke="#39363F"
stroke-width="3.03529"
/>
<rect
x="86.1265"
y="86.1265"
width="214.747"
height="214.747"
rx="13.6588"
stroke="#39363F"
stroke-width="3.03529"
/>
</svg> </svg>

Before

Width:  |  Height:  |  Size: 652 B

After

Width:  |  Height:  |  Size: 758 B

View File

@@ -7,10 +7,10 @@ code[class*="language-"],
pre[class*="language-"] { pre[class*="language-"] {
color: white; color: white;
background: none; background: none;
font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace; font-family: Consolas, Monaco, "Andale Mono", "Ubuntu Mono", monospace;
font-size: 1em; font-size: 1em;
text-align: left; text-align: left;
text-shadow: 0 -.1em .2em black; text-shadow: 0 -0.1em 0.2em black;
white-space: pre; white-space: pre;
word-spacing: normal; word-spacing: normal;
word-break: normal; word-break: normal;
@@ -34,10 +34,10 @@ pre[class*="language-"],
/* Code blocks */ /* Code blocks */
pre[class*="language-"] { pre[class*="language-"] {
border-radius: .5em; border-radius: 0.5em;
border: .3em solid hsl(0, 0%, 33%); /* #282A2B */ border: 0.3em solid hsl(0, 0%, 33%); /* #282A2B */
box-shadow: 1px 1px .5em black inset; box-shadow: 1px 1px 0.5em black inset;
margin: .5em 0; margin: 0.5em 0;
overflow: auto; overflow: auto;
padding: 1em; padding: 1em;
} }
@@ -53,24 +53,28 @@ pre[class*="language-"]::selection {
} }
/* Text Selection colour */ /* Text Selection colour */
pre[class*="language-"]::-moz-selection, pre[class*="language-"] ::-moz-selection, pre[class*="language-"]::-moz-selection,
code[class*="language-"]::-moz-selection, code[class*="language-"] ::-moz-selection { pre[class*="language-"] ::-moz-selection,
code[class*="language-"]::-moz-selection,
code[class*="language-"] ::-moz-selection {
text-shadow: none; text-shadow: none;
background: hsla(0, 0%, 93%, 0.15); /* #EDEDED */ background: hsla(0, 0%, 93%, 0.15); /* #EDEDED */
} }
pre[class*="language-"]::selection, pre[class*="language-"] ::selection, pre[class*="language-"]::selection,
code[class*="language-"]::selection, code[class*="language-"] ::selection { pre[class*="language-"] ::selection,
code[class*="language-"]::selection,
code[class*="language-"] ::selection {
text-shadow: none; text-shadow: none;
background: hsla(0, 0%, 93%, 0.15); /* #EDEDED */ background: hsla(0, 0%, 93%, 0.15); /* #EDEDED */
} }
/* Inline code */ /* Inline code */
:not(pre) > code[class*="language-"] { :not(pre) > code[class*="language-"] {
border-radius: .3em; border-radius: 0.3em;
border: .13em solid hsl(0, 0%, 33%); /* #545454 */ border: 0.13em solid hsl(0, 0%, 33%); /* #545454 */
box-shadow: 1px 1px .3em -.1em black inset; box-shadow: 1px 1px 0.3em -0.1em black inset;
padding: .15em .2em .05em; padding: 0.15em 0.2em 0.05em;
white-space: normal; white-space: normal;
} }
@@ -82,11 +86,11 @@ code[class*="language-"]::selection, code[class*="language-"] ::selection {
} }
.token.punctuation { .token.punctuation {
opacity: .7; opacity: 0.7;
} }
.token.namespace { .token.namespace {
opacity: .7; opacity: 0.7;
} }
.token.tag, .token.tag,
@@ -155,7 +159,11 @@ code[class*="language-"]::selection, code[class*="language-"] ::selection {
.line-highlight.line-highlight { .line-highlight.line-highlight {
background: hsla(0, 0%, 33%, 0.25); /* #545454 */ background: hsla(0, 0%, 33%, 0.25); /* #545454 */
background: linear-gradient(to right, hsla(0, 0%, 33%, .1) 70%, hsla(0, 0%, 33%, 0)); /* #545454 */ background: linear-gradient(
to right,
hsla(0, 0%, 33%, 0.1) 70%,
hsla(0, 0%, 33%, 0)
); /* #545454 */
border-bottom: 1px dashed hsl(0, 0%, 33%); /* #545454 */ border-bottom: 1px dashed hsl(0, 0%, 33%); /* #545454 */
border-top: 1px dashed hsl(0, 0%, 33%); /* #545454 */ border-top: 1px dashed hsl(0, 0%, 33%); /* #545454 */
margin-top: 0.75em; /* Same as .prisms padding-top */ margin-top: 0.75em; /* Same as .prisms padding-top */

View File

@@ -1,15 +1,25 @@
<?xml version="1.0" standalone="no"?> <?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN" <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"> "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg version="1.0" xmlns="http://www.w3.org/2000/svg" <svg
width="256.000000pt" height="256.000000pt" viewBox="0 0 256.000000 256.000000" version="1.0"
preserveAspectRatio="xMidYMid meet"> xmlns="http://www.w3.org/2000/svg"
<metadata> width="256.000000pt"
height="256.000000pt"
viewBox="0 0 256.000000 256.000000"
preserveAspectRatio="xMidYMid meet"
>
<metadata
>
Created by potrace 1.14, written by Peter Selinger 2001-2017 Created by potrace 1.14, written by Peter Selinger 2001-2017
</metadata> </metadata>
<g transform="translate(0.000000,256.000000) scale(0.100000,-0.100000)" <g
fill="#000000" stroke="none"> transform="translate(0.000000,256.000000) scale(0.100000,-0.100000)"
<path d="M1404 2075 c-16 -7 -75 -60 -131 -116 -55 -57 -130 -133 -166 -167 fill="#000000"
stroke="none"
>
<path
d="M1404 2075 c-16 -7 -75 -60 -131 -116 -55 -57 -130 -133 -166 -167
l-64 -64 0 122 c0 91 -3 124 -13 130 -19 12 -192 12 -210 0 -11 -7 -15 -34 l-64 -64 0 122 c0 91 -3 124 -13 130 -19 12 -192 12 -210 0 -11 -7 -15 -34
-16 -117 -1 -59 -3 -110 -3 -113 -1 -3 -20 -11 -43 -17 -67 -18 -126 -76 -164 -16 -117 -1 -59 -3 -110 -3 -113 -1 -3 -20 -11 -43 -17 -67 -18 -126 -76 -164
-160 -33 -72 -34 -73 -108 -108 -41 -19 -87 -50 -102 -68 -64 -76 -82 -169 -160 -33 -72 -34 -73 -108 -108 -41 -19 -87 -50 -102 -68 -64 -76 -82 -169
@@ -19,6 +29,7 @@ l-64 -64 0 122 c0 91 -3 124 -13 130 -19 12 -192 12 -210 0 -11 -7 -15 -34
-59 15 -9 -2 -12 10 -9 45 6 72 -17 145 -62 196 -13 16 -16 60 -16 318 0 164 -59 15 -9 -2 -12 10 -9 45 6 72 -17 145 -62 196 -13 16 -16 60 -16 318 0 164
3 299 6 299 3 0 18 -13 34 -30 39 -41 75 -52 113 -33 36 18 53 57 40 93 -5 14 3 299 6 299 3 0 18 -13 34 -30 39 -41 75 -52 113 -33 36 18 53 57 40 93 -5 14
-82 96 -171 183 -89 87 -282 277 -429 423 -146 145 -270 264 -275 265 -4 0 -82 96 -171 183 -89 87 -282 277 -429 423 -146 145 -270 264 -275 265 -4 0
-15 2 -24 4 -8 1 -29 -3 -45 -10z"/> -15 2 -24 4 -8 1 -29 -3 -45 -10z"
</g> />
</g>
</svg> </svg>

Before

Width:  |  Height:  |  Size: 1.3 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

BIN
static/splotch_1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 394 KiB

BIN
static/splotch_2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 407 KiB

BIN
static/splotch_3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 390 KiB

View File

@@ -229,8 +229,9 @@ function rgbaToDataURL(w, h, rgba) {
function updateThumbhashImages() { function updateThumbhashImages() {
document.querySelectorAll("[data-thumb]").forEach((entry) => { document.querySelectorAll("[data-thumb]").forEach((entry) => {
const hash = entry.getAttribute("data-thumb"); const hash = entry.getAttribute("data-thumb");
if (entry.getAttribute("data-thumb-decoded")) return;
if (!hash) return; if (!hash) return;
entry.setAttribute("data-thumb-decoded", true);
const decodedString = atob(hash); const decodedString = atob(hash);