feat: add initial add article command

This commit is contained in:
2023-08-01 21:35:21 +02:00
parent e51667bbac
commit ea11495d1a
17 changed files with 534 additions and 67 deletions

View File

@ -0,0 +1,10 @@
import { Handlers } from "$fresh/server.ts";
import { getArticle } from "@lib/resource/articles.ts";
import { json } from "@lib/helpers.ts";
export const handler: Handlers = {
async GET(_, ctx) {
const article = await getArticle(ctx.params.name);
return json(article);
},
};

View File

@ -0,0 +1,90 @@
import { Handlers } from "$fresh/server.ts";
import { Readability } from "https://cdn.skypack.dev/@mozilla/readability";
import { DOMParser } from "https://deno.land/x/deno_dom@v0.1.38/deno-dom-wasm.ts";
import { BadRequestError } from "@lib/errors.ts";
import { isValidUrl, json } from "@lib/helpers.ts";
import * as openai from "@lib/openai.ts";
import tds from "https://cdn.skypack.dev/turndown@7.1.1";
//import { gfm } from "https://cdn.skypack.dev/@guyplusplus/turndown-plugin-gfm@1.0.7";
import { createArticle } from "@lib/resource/articles.ts";
const service = new tds({
headingStyle: "atx",
codeBlockStyle: "fenced",
hr: "---",
bulletListMarker: "-",
});
const parser = new DOMParser();
//service.use(gfm);
export const handler: Handlers = {
async GET(req) {
const url = new URL(req.url);
const fetchUrl = url.searchParams.get("url");
if (!fetchUrl || !isValidUrl(fetchUrl)) {
throw new BadRequestError();
}
console.log("[api/article] create article from url", { url: fetchUrl });
const request = await fetch(fetchUrl);
const html = await request.text();
const document = parser.parseFromString(html, "text/html");
const title = document?.querySelector("title")?.innerText;
const metaAuthor =
document?.querySelector('meta[name="twitter:creator"]')?.getAttribute(
"content",
) ||
document?.querySelector('meta[name="author"]')?.getAttribute("content");
console.log({ metaAuthor });
const readable = new Readability(document);
const result = readable.parse();
console.log("[api/article] parsed ", {
url: fetchUrl,
content: result.textContent,
});
const cleanDocument = parser.parseFromString(
result.content,
"text/html",
);
const [tags, summary, shortTitle, author] = await Promise.all([
openai.createTags(result.textContent),
openai.summarize(result.textContent),
title && openai.shortenTitle(title),
metaAuthor || openai.extractAuthorName(result.textContent),
]);
const markdown = service.turndown(cleanDocument);
const id = shortTitle || title || "";
const newArticle = {
id,
name: title || "",
content: markdown,
tags: tags || [],
meta: {
author: author || "",
link: fetchUrl,
status: "not-finished",
date: new Date(),
},
} as const;
await createArticle(newArticle);
return json(newArticle);
},
};

View File

@ -0,0 +1,10 @@
import { Handlers } from "$fresh/server.ts";
import { getAllArticles } from "@lib/resource/articles.ts";
import { json } from "@lib/helpers.ts";
export const handler: Handlers = {
async GET() {
const movies = await getAllArticles();
return json(movies);
},
};

View File

@ -0,0 +1,51 @@
import { Handlers, PageProps } from "$fresh/server.ts";
import { MainLayout } from "@components/layouts/main.tsx";
import { Article, getArticle } from "@lib/resource/articles.ts";
import { RecipeHero } from "@components/RecipeHero.tsx";
import { KMenu } from "@islands/KMenu.tsx";
export const handler: Handlers<Article | null> = {
async GET(_, ctx) {
const movie = await getArticle(ctx.params.name);
return ctx.render(movie);
},
};
export default function Greet(props: PageProps<Article>) {
const article = props.data;
const { author = "", date = "" } = article.meta;
console.log({ tags: article.tags });
return (
<MainLayout url={props.url}>
<RecipeHero
data={article}
subline={[author, date.toString()]}
backlink="/articles"
/>
<KMenu type="main" context={article} />
{article.tags.length &&
(
<div class="flex gap-2 px-8">
{article.tags.map((t) => {
return (
<span class="bg-gray-700 text-white p-2 rounded-xl text-sm">
#{t}
</span>
);
})}
</div>
)}
<div class="px-8 text-white mt-10">
<pre
class="whitespace-break-spaces"
dangerouslySetInnerHTML={{ __html: article.content || "" }}
>
{article.content}
</pre>
</div>
</MainLayout>
);
}

38
routes/articles/index.tsx Normal file
View File

@ -0,0 +1,38 @@
import { Handlers, PageProps } from "$fresh/server.ts";
import { MainLayout } from "@components/layouts/main.tsx";
import IconArrowLeft from "https://deno.land/x/tabler_icons_tsx@0.0.3/tsx/arrow-left.tsx";
import { Article, getAllArticles } from "@lib/resource/articles.ts";
import { Card } from "@components/Card.tsx";
import { KMenu } from "@islands/KMenu.tsx";
export const handler: Handlers<Article[] | null> = {
async GET(_, ctx) {
const movies = await getAllArticles();
return ctx.render(movies);
},
};
export default function Greet(props: PageProps<Article[] | null>) {
return (
<MainLayout url={props.url}>
<header class="flex gap-4 items-center mb-5 md:hidden">
<a
class="px-4 ml-4 py-2 bg-gray-300 text-gray-800 rounded-lg flex items-center gap-1"
href="/"
>
<IconArrowLeft class="w-5 h-5" />
Back
</a>
<h3 class="text-2xl text-white font-light">📝 Articles</h3>
</header>
<KMenu type="main" context={false} />
<div class="flex flex-wrap items-center gap-4 px-4">
{props.data?.map((doc) => {
return <Card link={`/articles/${doc.id}`} title={doc.name} />;
})}
</div>
</MainLayout>
);
}