-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
json-ld.js
172 lines (151 loc) · 6.14 KB
/
json-ld.js
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
169
170
171
172
/**
* @license Copyright 2019 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
'use strict';
const Audit = require('../audit.js');
const validateJsonLD = require('../../lib/sd-validation/sd-validation.js');
const i18n = require('../../lib/i18n/i18n.js');
const UIStrings = {
/** Title of a Lighthouse audit that provides detail on whether JSON-LD structured data snippets are valid. This descriptive title is shown when no invalid JSON-LD snippets were found. */
title: 'JSON-LD structured data syntax is valid',
/** Title of a Lighthouse audit that provides detail on whether JSON-LD structured data snippets are valid. This descriptive title is shown when JSON-LD snippets with invalid content were found. */
failureTitle: 'JSON-LD structured data syntax is invalid',
/** Description of a Lighthouse audit that tells the user whether JSON-LD snippets on the page are invalid. This is displayed after a user expands the section to see more. No character length limits. */
/* eslint-disable-next-line max-len */
description: 'Structured data contains rich metadata about a web page. The data is used in search results and social sharing. Invalid metadata will affect how the page appears in these contexts. This audit currently validates a subset of JSON-LD rules. See also the [Structured Data Testing Tool](https://search.google.com/structured-data/testing-tool/) to validate other types of structured data.',
/** Explanatory message stating how many JSON-LD structured data snippets are invalid */
displayValue: `{invalidSnippetCount, plural,
=1 {# invalid snippet}
other {# invalid snippets}
}`,
};
const str_ = i18n.createMessageInstanceIdFn(__filename, UIStrings);
class StructuredDataAutomatic extends Audit {
/**
* @return {LH.Audit.Meta}
*/
static get meta() {
return {
id: 'json-ld',
title: str_(UIStrings.title),
failureTitle: str_(UIStrings.failureTitle),
description: str_(UIStrings.description),
requiredArtifacts: ['ScriptElements'],
};
}
/**
* @param {LH.Artifacts} artifacts
* @return {Promise<LH.Audit.Product>}
*/
static async audit(artifacts) {
const jsonLDElements = artifacts.ScriptElements.filter(
script => script.type === 'application/ld+json' && !!script.content);
if (jsonLDElements.length === 0) {
return {
notApplicable: true,
score: 1,
};
}
const validatedSnippets = await Promise.all(jsonLDElements.map(async (element) => {
// We don't want to show empty lines around the snippet
const content = /** @type string */ (element.content).trim();
return {
devtoolsNodePath: element.devtoolsNodePath,
content,
errors: await validateJsonLD(content),
};
}));
// Show invalid snippets at the top
validatedSnippets.sort((a, b) => {
return b.errors.length - a.errors.length;
});
const renderedSnippets = validatedSnippets.map(
snippetWithErrors => renderValidatedSnippet(snippetWithErrors)
);
const details = Audit.makeListDetails(renderedSnippets);
const invalidSnippets = validatedSnippets.filter(vs => vs.errors.length > 0);
const displayValue = str_(UIStrings.displayValue, {
invalidSnippetCount: invalidSnippets.length,
});
return {
score: invalidSnippets.length === 0 ? 1 : 0,
details,
displayValue,
};
}
}
/**
* @param {{content: string, devtoolsNodePath: string, errors: LH.StructuredData.ValidationError[]}} validatedSnippet
*/
function renderValidatedSnippet(validatedSnippet) {
const {content, devtoolsNodePath, errors} = validatedSnippet;
/** @type {LH.StructuredData.JsonLDDocument} */
let parsedContent = {};
let topLevelType = '';
let topLevelName = '';
try {
parsedContent = JSON.parse(content);
} catch (err) {}
if (parsedContent['@type']) {
topLevelType = parsedContent['@type'];
}
if (typeof parsedContent.name === 'string') {
topLevelName = parsedContent.name.toString();
}
let title = '';
if (topLevelName && topLevelType) {
title = `${topLevelType}: ${topLevelName}`;
} else if (topLevelType) {
title = `@type ${topLevelType}`;
} else {
title = 'Invalid JSON-LD element';
}
// No 18n here, because it's tricky to do because of the if statement above. The
// entity type and error messages are in English anyway.
title += ` (${errors.length} Error${errors.length !== 1 ? 's' : ''})`;
/** @type LH.Audit.Details.NodeValue */
const node = {
type: 'node',
path: devtoolsNodePath,
snippet: `<script type="application/ld+json">`,
};
const {lineMessages, generalMessages} = getErrorMessages(errors);
return Audit.makeSnippetDetails({
content: parsedContent ? JSON.stringify(parsedContent, null, 2) : content,
title,
lineMessages,
generalMessages,
node,
});
}
/**
* @param {Array<LH.StructuredData.ValidationError>} errors
*/
function getErrorMessages(errors) {
/** @type {LH.Audit.Details.SnippetValue['lineMessages']} */
const lineMessages = [];
/** @type {LH.Audit.Details.SnippetValue['generalMessages']} */
const generalMessages = [];
errors.forEach(({
message, lineNumber, validTypes, validator,
}) => {
if (validTypes && validator === 'schema-org') {
const typeStrings = validTypes.map(type => {
return `[${type.name}](${type.uri})`;
});
message = `Invalid ${typeStrings.join('/')}: ${message}`;
}
if (lineNumber) {
lineMessages.push({lineNumber, message});
} else {
generalMessages.push({
message,
});
}
});
return {lineMessages, generalMessages};
}
module.exports = StructuredDataAutomatic;
module.exports.UIStrings = UIStrings;