-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added LD+JSON asset and test case #1936
Changes from 16 commits
eeddf2f
810602a
b614c5d
c578ffc
44da9dd
b6e4c27
ddd6c2b
301fcf9
d747754
ef1ef6f
999fe2d
c14f601
9ce4134
db13b8c
d56d2f2
1be851b
f416174
42a0b0f
9e56bd3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,7 +49,8 @@ const META = { | |
'msapplication-square310x310logo', | ||
'msapplication-square70x70logo', | ||
'msapplication-wide310x150logo', | ||
'msapplication-TileImage' | ||
'msapplication-TileImage', | ||
'msapplication-config' | ||
], | ||
itemprop: [ | ||
'image', | ||
|
@@ -64,7 +65,8 @@ const META = { | |
const SCRIPT_TYPES = { | ||
'application/javascript': 'js', | ||
'text/javascript': 'js', | ||
'application/json': false | ||
'application/json': false, | ||
'application/ld+json': 'jsonld' | ||
}; | ||
|
||
// Options to be passed to `addURLDependency` for certain tags + attributes | ||
|
@@ -247,13 +249,15 @@ class HTMLAsset extends Asset { | |
} else if (type === 'tag') { | ||
if ( | ||
(rendition.type === 'js' && node.tag === 'script') || | ||
(rendition.type === 'css' && node.tag === 'style') | ||
(rendition.type === 'css' && node.tag === 'style') || | ||
(rendition.type === 'jsonld' && node.tag === 'script') | ||
) { | ||
node.content = rendition.value; | ||
} | ||
|
||
// Delete "type" attribute, since CSS and JS are the defaults. | ||
if (node.attrs) { | ||
// Unless it's application/ld+json | ||
if (node.attrs && node.attrs.type !== 'application/ld+json') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Quick fix. Proper solution: #1924 |
||
delete node.attrs.type; | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
const urlJoin = require('../utils/urlJoin'); | ||
const isURL = require('../utils/is-url'); | ||
const Asset = require('../Asset'); | ||
const logger = require('../Logger'); | ||
|
||
// A list of all attributes in a schema that may produce a dependency | ||
// Based on https://schema.org/ImageObject | ||
// Section "Instances of ImageObject may appear as values for the following properties" | ||
const SCHEMA_ATTRS = [ | ||
'logo', | ||
'photo', | ||
'image', | ||
'thumbnail', | ||
'screenshot', | ||
'primaryImageOfPage' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could look through https://schema.org/URL for more URL instances. Maybe Video/MediaObject and a few others of those would be good to support too. |
||
]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good start. I guess we should go through the specs and find all the possible attributes that are URLs There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if they list every single property, but https://schema.org/ImageObject has a dependents list: "Instances of ImageObject may appear as values for the following properties" |
||
|
||
class JSONLDAsset extends Asset { | ||
constructor(name, options) { | ||
super(name, options); | ||
this.type = 'jsonld'; | ||
} | ||
|
||
parse(content) { | ||
return JSON.parse(content.trim()); | ||
} | ||
|
||
collectDependencies() { | ||
if (!this.options.publicURL.startsWith('http')) { | ||
logger.warn( | ||
"Please specify a publicURL using --public-url, otherwise schema asset links won't work" | ||
); | ||
return; | ||
} | ||
|
||
for (let schemaKey in this.ast) { | ||
if (SCHEMA_ATTRS.includes(schemaKey)) { | ||
this.collectFromKey(this.ast, schemaKey); | ||
this.isAstDirty = true; | ||
} | ||
} | ||
} | ||
|
||
// Auxiliary method for collectDependencies() to use for recursion | ||
collectFromKey(schema, schemaKey) { | ||
if (!schema.hasOwnProperty(schemaKey)) { | ||
return; | ||
} | ||
// values can be strings or objects | ||
// if it's not a string, it should have a url | ||
if (typeof schema[schemaKey] === 'string') { | ||
let assetPath = this.addURLDependency(schema[schemaKey]); | ||
if (!isURL(assetPath)) { | ||
// paths aren't allowed, values must be urls | ||
assetPath = urlJoin(this.options.publicURL, assetPath); | ||
} | ||
schema[schemaKey] = assetPath; | ||
} else { | ||
this.collectFromKey(schema[schemaKey], 'url'); | ||
} | ||
} | ||
|
||
generate() { | ||
if (this.options.production) { | ||
return JSON.stringify(this.ast); | ||
} else { | ||
return JSON.stringify(this.ast, null, 2); | ||
} | ||
} | ||
} | ||
|
||
module.exports = JSONLDAsset; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<html> | ||
<head> | ||
<title>hi</title> | ||
<link rel="stylesheet" href="other.css"> | ||
</head> | ||
<body> | ||
|
||
<p>text 123</p> | ||
|
||
<script type="application/ld+json"> | ||
{ | ||
"@context": "http://schema.org", | ||
"@type": "LocalBusiness", | ||
"description": "This is your business description.", | ||
"name": "Parcel's parcel", | ||
"telephone": "555-111-2345", | ||
"openingHours": "Mo,Tu,We,Th,Fr 09:00-17:00", | ||
"logo": { | ||
"@type": "ImageObject", | ||
"url": "images/logo.png", | ||
"width": 180, | ||
"height": 120 | ||
}, | ||
"image": "images/image.jpeg" | ||
} | ||
</script> | ||
|
||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.other { | ||
color: green; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const {bundle, assertBundleTree} = require('./utils'); | ||
|
||
describe('schema ld+json', function() { | ||
it('Should parse a LD+JSON schema and collect dependencies', async function() { | ||
let b = await bundle(__dirname + '/integration/schema-jsonld/index.html', { | ||
production: true, | ||
publicURL: 'https://place.holder/' | ||
}); | ||
|
||
await assertBundleTree(b, { | ||
name: 'index.html', | ||
assets: ['index.html'], | ||
childBundles: [ | ||
{ | ||
type: 'jpeg' | ||
}, | ||
{ | ||
type: 'png' | ||
}, | ||
{ | ||
type: 'css' | ||
} | ||
] | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just check
isAstDirty
?