feat: accept URL from form or searchParam

This commit is contained in:
Ayo 2023-05-11 16:19:57 +02:00
parent 050793635a
commit e2c0e77c4b
6 changed files with 836 additions and 1104 deletions

5
astro.config.mjs Normal file
View file

@ -0,0 +1,5 @@
import { defineConfig } from "astro/config";
export default defineConfig({
output: "server",
});

1878
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -1,5 +1,13 @@
{
"scripts": {
"start": "astro dev"
},
"devDependencies": {
"astro": "^2.4.4"
},
"dependencies": {
"@astro-reactive/form": "^0.8.1",
"@astro-reactive/validator": "^0.3.4",
"@extractus/article-extractor": "^7.2.15"
}
}

1
src/env.d.ts vendored Normal file
View file

@ -0,0 +1 @@
/// <reference types="astro/client" />

32
src/pages/index.astro Normal file
View file

@ -0,0 +1,32 @@
---
import Form, { FormGroup } from "@astro-reactive/form";
import { Validators } from "@astro-reactive/validator";
import { extract } from "@extractus/article-extractor";
import getParams from "../utils/get-params";
const params = getParams(Astro.url);
const url = params?.url;
const article = extract(url);
const form = new FormGroup([
{
type: "text",
name: "url",
value: url || "",
placeholder: "Put the URL here",
validators: [Validators.required, Validators.minLength(11)],
},
]);
---
<Form
formGroups={form}
showValidationHints
submitControl={{
type: "submit",
name: "submit",
value: "Get cozy!",
}}
/>
{article ? <p>{JSON.stringify(article)}</p> : "<hr />"}

16
src/utils/get-params.ts Normal file
View file

@ -0,0 +1,16 @@
export default function getParams(
url: URL
): { [key: string]: string } | undefined {
if (url.search === "") return;
return Object.fromEntries(
url.search
.replace("?", "")
.split("&")
.map((paramstr) => paramstr.split("="))
.map(([key, value]) => [
key,
decodeURIComponent(value?.replace(/\+/g, "%20")),
])
);
}