Skip to content
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

Merged
merged 19 commits into from Sep 23, 2018
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Parser.js
Expand Up @@ -20,6 +20,7 @@ class Parser {
this.registerExtension('vue', './assets/VueAsset');
this.registerExtension('json', './assets/JSONAsset');
this.registerExtension('json5', './assets/JSONAsset');
this.registerExtension('jsonld', './assets/JSONLDAsset');
this.registerExtension('yaml', './assets/YAMLAsset');
this.registerExtension('yml', './assets/YAMLAsset');
this.registerExtension('toml', './assets/TOMLAsset');
Expand Down
12 changes: 8 additions & 4 deletions src/assets/HTMLAsset.js
Expand Up @@ -49,7 +49,8 @@ const META = {
'msapplication-square310x310logo',
'msapplication-square70x70logo',
'msapplication-wide310x150logo',
'msapplication-TileImage'
'msapplication-TileImage',
'msapplication-config'
],
itemprop: [
'image',
Expand All @@ -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
Expand Down Expand Up @@ -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')
Copy link
Contributor Author

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 ?

) {
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') {
Copy link
Contributor Author

@bre7 bre7 Aug 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick fix. Proper solution: #1924

delete node.attrs.type;
}
}
Expand Down
61 changes: 61 additions & 0 deletions src/assets/JSONLDAsset.js
@@ -0,0 +1,61 @@
const urlJoin = require('../utils/urlJoin');
const isURL = require('../utils/is-url');
const Asset = require('../Asset');

// 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'
Copy link
Member

Choose a reason for hiding this comment

The 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.

];
Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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() {
for (let schemaKey in this.ast) {
// only check for single values, not nested data
// todo: check for nested data
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make a recursive function for this?

if (
SCHEMA_ATTRS.includes(schemaKey) &&
typeof this.ast[schemaKey] === 'string'
) {
// paths aren't allowed, values must be urls
let assetPath = this.addURLDependency(this.ast[schemaKey]);
if (!isURL(assetPath)) {
assetPath = urlJoin(this.options.publicURL, assetPath);
}
this.ast[schemaKey] = assetPath;
this.isAstDirty = true;

if (this.options.publicURL === '/') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so I take it JSON LD requires full URLs not paths? If so, then this is probably fine to show a warning.

Maybe this check should check whether the public URL starts with / instead of fully equaling it?

console.warn(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use the parcel logger for this

"Please specify publicURL using --public-url, otherwise schema asset links won't work"
);
}
}
}
}

generate() {
if (this.options.production) {
return JSON.stringify(this.ast);
} else {
return JSON.stringify(this.ast, null, 2);
}
}
}

module.exports = JSONLDAsset;
Binary file added test/integration/schema-jsonld/images/image.jpeg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/integration/schema-jsonld/images/logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions test/integration/schema-jsonld/index.html
@@ -0,0 +1,23 @@
<html>
<head>
<title>hi</title>
</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": "images/logo.png",
"image": "images/image.jpeg"
}
</script>

</body>
</html>
22 changes: 22 additions & 0 deletions test/schema-jsonld.js
@@ -0,0 +1,22 @@
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
});

await assertBundleTree(b, {
name: 'index.html',
assets: ['index.html'],
childBundles: [
{
type: 'jpeg'
},
{
type: 'png'
}
]
});
});
});