generated from kawarimidoll/deno-dev-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.ts
55 lines (50 loc) · 1.61 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { httpStatusText, tag as h } from "./deps.ts";
export function toTitle(str: string): string {
str = str.replace(/\/|-|_/g, " ").trim();
const [first, ...rest] = [...str.toLowerCase()];
return [first.toUpperCase(), ...rest].join("");
}
export function getH1(content: string) {
const h1 = content.match(/<h1[^>]*>(.*?)<\/h1>/)?.at(1) || "";
return h1.replace(/<[^>]*>/g, " ").replace(/\s+/g, " ").trim();
}
export function aTag(
attributes: Record<string, string | number | boolean>,
...children: string[]
) {
const { href } = attributes;
if (typeof href !== "string") {
throw new Error("href must be a string");
}
if (/^https?:\/\//.test(href)) {
attributes.target = "_blank";
attributes.rel = "noopener noreferrer";
// external-link icon by feather icons
// https://github.com/feathericons/feather#svg-sprite
children.push(
h(
"svg",
{
xmlns: "http://www.w3.org/2000/svg",
viewBox: "0 0 24 24",
class: "feather",
},
h("path", {
d: "M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6",
}),
h("polyline", { points: "15 3 21 3 21 9" }),
h("line", { x1: "10", y1: "14", x2: "21", y2: "3" }),
),
);
} else if (!href.startsWith("/") && !href.startsWith("#")) {
attributes.href = "/" + href;
}
return h("a", attributes, ...children);
}
export function genResponseArgs(
status: number,
init?: ResponseInit,
): [BodyInit, ResponseInit] {
const statusText = `${httpStatusText(status)}`;
return [`${status}: ${statusText}`, { ...(init || {}), status, statusText }];
}