@dukebot/astro-html-validator 1.1.3 → 1.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/utils/meta.mjs +181 -122
package/package.json
CHANGED
package/src/utils/meta.mjs
CHANGED
|
@@ -1,122 +1,181 @@
|
|
|
1
|
-
import {
|
|
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
|
-
|
|
1
|
+
import { parse } from 'parse5';
|
|
2
|
+
|
|
3
|
+
function parseHtmlDocument(html = '') {
|
|
4
|
+
if (!html) return null;
|
|
5
|
+
return parse(html, { sourceCodeLocationInfo: false });
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function getAttrValue(node, attrName) {
|
|
9
|
+
if (!Array.isArray(node?.attrs)) return '';
|
|
10
|
+
const attr = node.attrs.find((item) => item?.name?.toLowerCase() === attrName.toLowerCase());
|
|
11
|
+
return attr?.value?.trim() ?? '';
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function findFirstNode(root, predicate) {
|
|
15
|
+
if (!root) return null;
|
|
16
|
+
|
|
17
|
+
const queue = [root];
|
|
18
|
+
while (queue.length > 0) {
|
|
19
|
+
const node = queue.shift();
|
|
20
|
+
if (!node) continue;
|
|
21
|
+
if (predicate(node)) return node;
|
|
22
|
+
|
|
23
|
+
if (node.content) queue.push(node.content);
|
|
24
|
+
if (Array.isArray(node.childNodes) && node.childNodes.length > 0) {
|
|
25
|
+
queue.push(...node.childNodes);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function getNodeText(node) {
|
|
33
|
+
if (!node) return '';
|
|
34
|
+
|
|
35
|
+
let text = '';
|
|
36
|
+
const queue = [node];
|
|
37
|
+
while (queue.length > 0) {
|
|
38
|
+
const current = queue.shift();
|
|
39
|
+
if (!current) continue;
|
|
40
|
+
|
|
41
|
+
if (current.nodeName === '#text' && typeof current.value === 'string') {
|
|
42
|
+
text += current.value;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (current.content) queue.push(current.content);
|
|
46
|
+
if (Array.isArray(current.childNodes) && current.childNodes.length > 0) {
|
|
47
|
+
queue.push(...current.childNodes);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return text.replace(/\s+/g, ' ').trim();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function getMetaNode(document, name, isProperty = false) {
|
|
55
|
+
const attrName = isProperty ? 'property' : 'name';
|
|
56
|
+
const normalizedExpected = String(name).trim().toLowerCase();
|
|
57
|
+
|
|
58
|
+
return findFirstNode(document, (node) => {
|
|
59
|
+
if (node?.tagName !== 'meta') return false;
|
|
60
|
+
const key = getAttrValue(node, attrName).toLowerCase();
|
|
61
|
+
return key === normalizedExpected;
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function getStringLength(value) {
|
|
66
|
+
return Array.from(value.normalize('NFC')).length;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Core metadata tags expected on every page.
|
|
70
|
+
export const REQUIRED_META_CHECKS = [
|
|
71
|
+
{ label: 'meta title', check: getTitleContent },
|
|
72
|
+
{ label: 'meta description', check: (html) => hasMeta(html, 'description') },
|
|
73
|
+
{ label: 'canonical', check: hasCanonical },
|
|
74
|
+
{ label: 'meta robots', check: (html) => hasMeta(html, 'robots') },
|
|
75
|
+
{ label: 'og:title', check: (html) => hasMeta(html, 'og:title', true) },
|
|
76
|
+
{ label: 'og:description', check: (html) => hasMeta(html, 'og:description', true) },
|
|
77
|
+
{ label: 'og:url', check: (html) => hasMeta(html, 'og:url', true) },
|
|
78
|
+
{ label: 'og:type', check: (html) => hasMeta(html, 'og:type', true) },
|
|
79
|
+
];
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Returns whether a meta tag exists with the expected key and non-empty content.
|
|
83
|
+
*/
|
|
84
|
+
export function hasMeta(html, name, isProperty = false) {
|
|
85
|
+
return !!getMetaContent(html, name, isProperty);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Returns the `content` value for a meta tag by name/property.
|
|
90
|
+
*/
|
|
91
|
+
export function getMetaContent(html, name, isProperty = false) {
|
|
92
|
+
const document = parseHtmlDocument(html);
|
|
93
|
+
const node = getMetaNode(document, name, isProperty);
|
|
94
|
+
return getAttrValue(node, 'content');
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Checks that a canonical link tag exists with a non-empty href.
|
|
99
|
+
*/
|
|
100
|
+
export function hasCanonical(html) {
|
|
101
|
+
const document = parseHtmlDocument(html);
|
|
102
|
+
|
|
103
|
+
return !!findFirstNode(document, (node) => {
|
|
104
|
+
if (node?.tagName !== 'link') return false;
|
|
105
|
+
const rel = getAttrValue(node, 'rel').toLowerCase();
|
|
106
|
+
if (rel !== 'canonical') return false;
|
|
107
|
+
return !!getAttrValue(node, 'href');
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Extracts the document title text.
|
|
113
|
+
*/
|
|
114
|
+
export function getTitleContent(html) {
|
|
115
|
+
const document = parseHtmlDocument(html);
|
|
116
|
+
const titleNode = findFirstNode(document, (node) => node?.tagName === 'title');
|
|
117
|
+
return getNodeText(titleNode);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Validates optional length ranges for title/description fields.
|
|
122
|
+
*/
|
|
123
|
+
export function validateLengthRange({ value, min = 1, max = Infinity, fieldLabel }) {
|
|
124
|
+
if (!value) return;
|
|
125
|
+
|
|
126
|
+
const length = getStringLength(value);
|
|
127
|
+
if (length >= min && length <= max) return;
|
|
128
|
+
return `Recommended ${fieldLabel} length is ${min}-${max}. Current: ${length}.`;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Evaluates all mandatory metadata checks for a page.
|
|
133
|
+
*/
|
|
134
|
+
export function validateRequiredMeta(html) {
|
|
135
|
+
const warnings = [];
|
|
136
|
+
|
|
137
|
+
for (const item of REQUIRED_META_CHECKS) {
|
|
138
|
+
if (!item.check(html)) {
|
|
139
|
+
warnings.push(`Missing ${item.label}.`);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return warnings;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Runs required and optional SEO metadata checks for one HTML string.
|
|
148
|
+
*/
|
|
149
|
+
export function validateHtmlMeta(
|
|
150
|
+
html,
|
|
151
|
+
{
|
|
152
|
+
metaTitleMinLength,
|
|
153
|
+
metaTitleMaxLength,
|
|
154
|
+
metaDescriptionMinLength,
|
|
155
|
+
metaDescriptionMaxLength,
|
|
156
|
+
} = {}
|
|
157
|
+
) {
|
|
158
|
+
const warnings = [];
|
|
159
|
+
|
|
160
|
+
warnings.push(...validateRequiredMeta(html));
|
|
161
|
+
|
|
162
|
+
warnings.push(
|
|
163
|
+
validateLengthRange({
|
|
164
|
+
value: getTitleContent(html),
|
|
165
|
+
min: metaTitleMinLength,
|
|
166
|
+
max: metaTitleMaxLength,
|
|
167
|
+
fieldLabel: 'meta title',
|
|
168
|
+
})
|
|
169
|
+
);
|
|
170
|
+
|
|
171
|
+
warnings.push(
|
|
172
|
+
validateLengthRange({
|
|
173
|
+
value: getMetaContent(html, 'description'),
|
|
174
|
+
min: metaDescriptionMinLength,
|
|
175
|
+
max: metaDescriptionMaxLength,
|
|
176
|
+
fieldLabel: 'meta description',
|
|
177
|
+
})
|
|
178
|
+
);
|
|
179
|
+
|
|
180
|
+
return warnings.filter(Boolean);
|
|
181
|
+
}
|