Skip to content

Commit

Permalink
Merge pull request #53 from quasibit/feat/jsonld-without-script
Browse files Browse the repository at this point in the history
feat: add jsonLd shortocde that outputs without script tag
  • Loading branch information
nunof07 authored Feb 25, 2023
2 parents 7a7c0c4 + 74a1dbc commit e845d15
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 18 deletions.
4 changes: 4 additions & 0 deletions .eleventy.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";

const script = require("./src/script");
const jsonLd = require("./src/jsonLd");

/**
* Add a shortcode to generate JSON-LD structured data.
Expand All @@ -10,4 +11,7 @@ module.exports = (eleventyConfig) => {
eleventyConfig.addShortcode("jsonLdScript", (meta, type, tags) =>
script({ meta, type, tags })
);
eleventyConfig.addShortcode("jsonLd", (meta, type, tags) =>
jsonLd({ meta, type, tags })
);
};
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ module.exports = function(eleventyConfig) {

## Introduction

The plugin adds a shortcode to generate the JSON-LD script (including the `<script>` tag).
The plugin adds two shortcodes to generate the JSON-LD script:
- `jsonLdScript` (includes the `<script>` tag)
- `jsonLd` (just JSON-LD without the `<script>` tag)

The shortcode supports the following schema types:
The shortcodes support the following schema types:

- [WebSite](https://schema.org/WebSite).
- [BlogPosting](https://schema.org/BlogPosting).
Expand All @@ -64,6 +66,12 @@ Call the shortcode where you want the script to be displayed:
{% jsonLdScript meta, type, tags %}
```

And if you don't want the `<script>` tag, then use this instead:

```njk
{% jsonLd meta, type, tags %}
```

### Validation

You can validate the structured data using one of the following tools:
Expand Down
1 change: 1 addition & 0 deletions demo/_includes/jsonLd.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% jsonLd meta, type, tags %}
22 changes: 22 additions & 0 deletions demo/jsonLd.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
layout: jsonLd.njk
permalink: jsonLd.json
type: page
meta:
site:
name: Site title
description: Site description
url: https://example.com
logo:
src: https://example.com/images/logo.png
width: 1200
height: 630
language: en-US
url: https://example.com/page
title: Page title
description: Page description
image:
src: https://example.com/images/page.png
---

Page content...
67 changes: 67 additions & 0 deletions src/jsonLd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"use strict";

const main = require("./main");

/**
* Structured data script.
*
* @param {Object} param0 Parameters
* @param {Object} param0.meta Meta data.
* @param {Object} param0.meta.site Site properties.
* @param {String} param0.meta.site.name Site name.
* @param {String} param0.meta.site.description Site description.
* @param {String} param0.meta.site.url Site home URL.
* @param {Object} param0.meta.site.logo Site logo image properties.
* @param {String} param0.meta.site.logo.src Image URI.
* @param {String} param0.meta.site.logo.width Image width.
* @param {String} param0.meta.site.logo.height Image height.
* @param {String} param0.meta.url Page URL.
* @param {String} param0.meta.title Title.
* @param {String} param0.meta.description Description.
* @param {String} param0.meta.language Language code (e.g. "en-US" or "en").
* @param {Object} param0.meta.image Meta image properties.
* @param {String} param0.meta.image.src Image URI.
* @param {Object|String} param0.meta.author Author properties.
* @param {String} param0.meta.author.name Author name.
* @param {Date} param0.meta.published Published date.
* @param {Date} param0.meta.modified Modified date.
* @param {String} param0.meta.section Article section.
* @param {String} param0.type Type of content ("page" or "post").
* @param {String[]} param0.tags (Optional) Tags.
* @param {Object} param0.meta.offers Product offers.
* @param {Object} param0.meta.rating Product rating.
* @param {String} param0.meta.gtin A Global Trade Item Number (GTIN).
* @param {String} param0.meta.gtin12 The GTIN-12 code of the product.
* @param {String} param0.meta.gtin13 The GTIN-13 code of the product
* @param {String} param0.meta.gtin14 The GTIN-14 code of the product.
* @param {String} param0.meta.gtin8 The GTIN-8 code of the product.
* @param {String} param0.meta.sku The Stock Keeping Unit.
* @param {String} param0.meta.mpn The Manufacturer Part Number of the product.
* @param {String} param0.meta.countryOfOrigin The country of origin.
* @param {String} param0.meta.color The color of the product.
* @param {String} param0.meta.brand The brand(s) associated with a product.
* @param {String} param0.meta.manufacturer The manufacturer of the product.
* @param {String} param0.meta.material A material that something is made from.
* @param {String} param0.meta.productID The product identifier, such as ISBN.
* @param {String} param0.meta.productionDate The date of production.
* @param {String} param0.meta.category A category for the item.
* @param {String} param0.meta.identifier A identifier for the item.
* @returns {String}
*/
module.exports = ({ meta, type, tags = [] }) => {
if (!meta) {
return "";
}

const json = main({ meta, type, tags });
const spaces = 2;

return JSON.stringify(
json,
(key, value) =>
Array.isArray(value)
? value.filter((element) => element !== null && element !== undefined)
: value,
spaces
);
};
20 changes: 4 additions & 16 deletions src/script.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

const main = require("./main");
const jsonLd = require("./jsonLd");

/**
* Structured data script.
Expand Down Expand Up @@ -48,22 +48,10 @@ const main = require("./main");
* @param {String} param0.meta.identifier A identifier for the item.
* @returns {String}
*/
module.exports = ({ meta, type, tags = [] }) => {
if (!meta) {
module.exports = (data) => {
if (!data.meta) {
return "";
}

const json = main({ meta, type, tags });
const spaces = 2;

return `<script type="application/ld+json">
${JSON.stringify(
json,
(key, value) =>
Array.isArray(value)
? value.filter((element) => element !== null && element !== undefined)
: value,
spaces
)}
</script>`;
return `<script type="application/ld+json">${jsonLd(data)}</script>`;
};

0 comments on commit e845d15

Please sign in to comment.