feat: simple url validation

This commit is contained in:
Ayo 2023-05-11 16:50:17 +02:00
parent e9fc737ce3
commit 695b9d337c
5 changed files with 17 additions and 10 deletions

View file

@ -8,7 +8,10 @@ export interface Props {
} }
--- ---
<h2>{article.title}</h2> {
<p>by <em>{article.author}</em></p> article ? (
<h2>{article.title}</h2>
<span set:html={article.content} /> <p>by <em>{article.author}</em></p>
<span set:html={article.content} />
) : ''
}

View file

@ -1,13 +1,13 @@
--- ---
import Form, { FormGroup } from "@astro-reactive/form"; import Form, { FormGroup } from "@astro-reactive/form";
import { Validators } from "@astro-reactive/validator"; import { Validators } from "@astro-reactive/validator";
import getParams from "../utils/get-params";
import { extract } from "@extractus/article-extractor"; import { extract } from "@extractus/article-extractor";
import Article from "../components/article.astro"; import Article from "../components/article.astro";
import { getParams, isURL } from "../utils";
const params = getParams(Astro.url); const params = getParams(Astro.url);
const url = params?.url; const url: string = params?.url;
const article = await extract(url); const article = isURL(url) && (await extract(url));
const form = new FormGroup([ const form = new FormGroup([
{ {

View file

@ -1,6 +1,4 @@
export default function getParams( export function getParams(url: URL): { [key: string]: string } | undefined {
url: URL
): { [key: string]: string } | undefined {
if (url.search === "") return; if (url.search === "") return;
return Object.fromEntries( return Object.fromEntries(

2
src/utils/index.ts Normal file
View file

@ -0,0 +1,2 @@
export * from "./get-params";
export * from "./is-url";

4
src/utils/is-url.ts Normal file
View file

@ -0,0 +1,4 @@
export function isURL(str: string): boolean {
// TODO: improve pls
return str.includes("http://") || str.includes("https://");
}