deno.land / x / lume@v2.1.4 / core / utils / data_values.ts

data_values.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
import type { Data } from "../file.ts";
/** * Get the value of a page data * For example, if the value is "=title", it returns the value of the page data "title" * If the value is "$.title", it will return the value of the element with the selector ".title" */export function getDataValue(data: Partial<Data>, value?: unknown) { // Get the value from the page data if (typeof value === "string") { if (value.startsWith("=")) { const key = value.slice(1);
if (!key.includes(".")) { return data[key]; }
const keys = key.split("."); let val = data; for (const key of keys) { val = val?.[key]; } return val; }
if (value.startsWith("$")) { return queryCss(value, data.page?.document); } }
if (typeof value === "function") { return value(data); }
return value;}
function queryCss(value: string, document?: Document) { // https://regexr.com/7qnot const checkForAttrPattern = /^\$(.+)\s+(?:attr\(([\w\-]+)\))$/; const checkResult = value.match(checkForAttrPattern);
const hasAttr = checkResult?.[0]; if (hasAttr) { const [_, query, name] = checkResult; return document?.querySelector(query)?.getAttribute(name); }
const query = value.slice(1); return document?.querySelector(query)?.innerHTML;}
lume

Version Info

Tagged at
7 months ago