deno.land / x / lume@v2.1.4 / plugins / attributes.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import type Site from "../core/site.ts";
const escapeChars: Record<string, string> = { "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&#34;", "'": "&#39;",};
/** * A plugin to register the filters "attrs" and "class", * which allow to handle HTML attributes and class names easily */export default function () { return (site: Site) => { site.filter("attr", attributes); site.filter("class", className); };}
export function attributes(values: unknown, ...validNames: string[]): string { const attributes = new Map();
handleAttributes(attributes, values, validNames);
return joinAttributes(attributes);}
export function className(...names: unknown[]): string { const classes = new Set<string>();
names.forEach((name) => handleClass(classes, name));
return Array.from(classes).join(" ");}
function handleClass(classes: Set<string>, name: unknown): void { if (!name) { return; }
if (typeof name === "string") { classes.add(name); return; }
if (Array.isArray(name)) { return name.forEach((value) => handleClass(classes, value)); }
for (const [key, value] of Object.entries(name as Record<string, unknown>)) { if (value) { classes.add(key); } }}
function handleAttributes( attributes: Map<string, unknown>, name: unknown, validNames: string[],): void { if (!name) { return; }
if (typeof name === "string") { if (isValid(name, validNames)) { attributes.set(name, true); } return; }
if (Array.isArray(name)) { return name.forEach((value) => handleAttributes(attributes, value, validNames) ); }
for (const [key, value] of Object.entries(name as Record<string, unknown>)) { if (!isValid(key, validNames)) { continue; }
if (key === "class") { const classList = (attributes.get("class") || new Set()) as Set<string>; handleClass(classList, value); attributes.set("class", classList); continue; }
attributes.set(key, value); }}
function joinAttributes(attributes: Map<string, unknown>) { const values = [];
for (const [name, value] of attributes) { if (value === undefined || value === null || value === false) { continue; }
if (value === true) { values.push(name); continue; }
if (value instanceof Set) { if (value.size) { values.push(`${name}="${escape(Array.from(value).join(" "))}"`); } continue; }
values.push(`${name}="${escape(value as string)}"`); }
return values.join(" ");}
function escape(value: string) { return value.replace(/[&<>'"]/g, (match) => escapeChars[match]);}
function isValid(name: string, validNames: string[]) { return name && (!validNames.length || validNames.includes(name));}
/** Extends Helpers interface */declare global { namespace Lume { export interface Helpers { /** @see https://lume.land/plugins/attributes/ */ attr: (values: unknown, ...validNames: string[]) => string;
/** @see https://lume.land/plugins/attributes/ */ class: (...names: unknown[]) => string; } }}
lume

Version Info

Tagged at
5 months ago