Skip to content

Commit

Permalink
feat: Insert newlines at the beginning and end of the stringified jso…
Browse files Browse the repository at this point in the history
…n when a space option is not 0
  • Loading branch information
ymmooot committed May 13, 2019
1 parent 5c00b64 commit 337f5f9
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ import Vue from 'vue';
import NuxtJsonld from 'nuxt-jsonld';

Vue.use(NuxtJsonld);

// you can set the indentation
Vue.use(NuxtJsonld, {
space: 4, // default: 2
});
```

## Usage
Expand Down
4 changes: 3 additions & 1 deletion example/plugins/jsonld.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import Vue from 'vue';
import NuxtJsonld from 'nuxt-jsonld';

Vue.use(NuxtJsonld);
Vue.use(NuxtJsonld, {
space: 4, // default: 2
});
9 changes: 6 additions & 3 deletions src/createMixin.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = options => {
module.exports = (options = {}) => {
const mergedOptions = {
space: 2,
...(options || {}),
...options,
};

return {
Expand All @@ -10,13 +10,16 @@ module.exports = options => {
return {};
}

const stringifiedJson = JSON.stringify(this.$options.jsonld.call(this), null, mergedOptions.space);
const innerHTML = mergedOptions.space === 0 ? stringifiedJson : `\n${stringifiedJson}\n`;

const hid = `nuxt-jsonld-${this._uid}`;
return {
script: [
{
hid,
type: 'application/ld+json',
innerHTML: JSON.stringify(this.$options.jsonld.call(this), null, mergedOptions.space),
innerHTML,
},
],
__dangerouslyDisableSanitizersByTagID: {
Expand Down
12 changes: 8 additions & 4 deletions test/createMixin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ describe('with jsonld', () => {
script: [
{
hid: mockHid,
innerHTML: `{
innerHTML: `
{
"@context": "http://schema.org",
"@type": "BreadcrumbList",
"itemListElement": [
Expand All @@ -82,7 +83,8 @@ describe('with jsonld', () => {
}
}
]
}`,
}
`,
type: 'application/ld+json',
},
],
Expand All @@ -101,7 +103,8 @@ describe('with jsonld', () => {
script: [
{
hid: mockHid,
innerHTML: `{
innerHTML: `
{
"@context": "http://schema.org",
"@type": "BreadcrumbList",
"itemListElement": [
Expand All @@ -120,7 +123,8 @@ describe('with jsonld', () => {
}
}
]
}`,
}
`,
type: 'application/ld+json',
},
],
Expand Down

0 comments on commit 337f5f9

Please sign in to comment.