-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathproperty.ts
More file actions
168 lines (145 loc) · 4.81 KB
/
property.ts
File metadata and controls
168 lines (145 loc) · 4.81 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { serialize } from "parse5";
import {
getAttributeIfTag,
getClassNames,
getAttributeValue,
} from "../helpers/attributes";
import {
ParsedProperty,
MicroformatProperty,
Html,
PropertyType,
ParsingOptions,
Element,
} from "../types";
import { isMicroformatRoot } from "../helpers/nodeMatchers";
import { parseMicroformat } from "./parse";
import { valueClassPattern } from "../helpers/valueClassPattern";
import { textContent, impliedTextContent } from "../helpers/textContent";
import { parseImage } from "../helpers/images";
import { isLocalLink, applyBaseUrl } from "../helpers/url";
import { convertV1PropertyClassNames } from "../backcompat";
import { isEnabled } from "../helpers/experimental";
const propertyRegexp = /^(p|u|e|dt)-/;
const getType = (className: string): PropertyType =>
(className.startsWith("p-") && "p") ||
(className.startsWith("u-") && "u") ||
(className.startsWith("e-") && "e") ||
"dt";
export const parseP = (node: Element, options: ParsingOptions): string =>
valueClassPattern(node, options) ??
getAttributeIfTag(node, ["abbr", "link"], "title") ??
getAttributeIfTag(node, ["input", "data"], "value") ??
getAttributeIfTag(node, ["img", "area"], "alt") ??
getAttributeIfTag(node, ["meta"], "content") ??
impliedTextContent(node, options);
export const parseU = (
node: Element,
options: ParsingOptions,
): MicroformatProperty => {
const url =
getAttributeIfTag(node, ["a", "area", "link"], "href") ??
parseImage(node, options) ??
getAttributeIfTag(node, ["audio", "source", "iframe", "video"], "src") ??
getAttributeIfTag(node, ["video"], "poster") ??
getAttributeIfTag(node, ["object"], "data") ??
valueClassPattern(node, options) ??
getAttributeIfTag(node, ["abbr"], "title") ??
getAttributeIfTag(node, ["data", "input"], "value") ??
getAttributeIfTag(node, ["meta"], "content") ??
textContent(node, options);
if (typeof url === "string" && isLocalLink(url)) {
return applyBaseUrl(url, options.baseUrl);
}
return typeof url === "string" ? url.trim() : url;
};
export const parseDt = (node: Element, options: ParsingOptions): string =>
valueClassPattern(node, { ...options, datetime: true }) ??
getAttributeIfTag(node, ["time", "ins", "del"], "datetime") ??
getAttributeIfTag(node, ["abbr"], "title") ??
getAttributeIfTag(node, ["data", "input"], "value") ??
getAttributeIfTag(node, ["meta"], "content") ??
textContent(node, options);
export const parseE = (node: Element, options: ParsingOptions): Html => {
const value = {
value: textContent(node, options),
html: serialize(node).trim(),
};
const lang =
isEnabled(options, "lang") &&
(getAttributeValue(node, "lang") || options.inherited.lang);
return lang ? { ...value, lang } : value;
};
const getPropertyClassNames = (
node: Element,
{ inherited }: ParsingOptions,
): string[] => {
if (inherited.roots.length) {
return convertV1PropertyClassNames(node, inherited.roots);
}
return getClassNames(node, /^(p|u|e|dt)-/);
};
const handleProperty = (
node: Element,
type: PropertyType,
options: ParsingOptions,
): MicroformatProperty => {
if (type === "p") {
return parseP(node, options);
}
if (type === "e") {
return parseE(node, options);
}
if (type === "u") {
return parseU(node, options);
}
return parseDt(node, options);
};
export const parseProperty = (
child: Element,
options: ParsingOptions,
): ParsedProperty[] =>
getPropertyClassNames(child, options)
.map((className): ParsedProperty | undefined => {
const type = getType(className);
const key = className.replace(propertyRegexp, "");
const value =
["u", "p", "e", "dt"].includes(type) && isMicroformatRoot(child)
? parseMicroformat(child, {
...options,
valueType: type,
valueKey: key,
})
: handleProperty(child, type, options);
return { type, key, value };
})
.filter((p): p is ParsedProperty => Boolean(p));
/**
* Some properties require knowledge of other properties to be parsed correctly
* Apply known post-initial-parse rules here:
* - dt-end should be dt-start aware
*/
export const postParseNode = (
prop: ParsedProperty,
_i: number,
all: ParsedProperty[],
): ParsedProperty => {
// Imply an end date if only time specified
if (
prop.type === "dt" &&
prop.key === "end" &&
typeof prop.value === "string" &&
!prop.value.match(/^[0-9]{4}-[0-9]{2}-[0-9]{2}/) &&
prop.value.match(/^[0-9]{2}:[0-9]{2}/)
) {
const value = all.find(
(p) =>
p.type === "dt" && p.key === "start" && typeof prop.value === "string",
)?.value as string;
if (value) {
const date = value.match(/^[0-9]{4}-[0-9]{2}-[0-9]{2}/);
return { ...prop, value: `${date} ${prop.value}` };
}
}
return prop;
};