Skip to content

Commit

Permalink
chore: refactor and fix type
Browse files Browse the repository at this point in the history
  • Loading branch information
ymmooot committed Sep 23, 2019
1 parent d66fdca commit 6d7e128
Show file tree
Hide file tree
Showing 8 changed files with 254 additions and 217 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = {
],
},
},
'import/core-modules': ['vue'],
'import/core-modules': ['vue', 'vue-meta'],
},
overrides: [
{
Expand Down
415 changes: 217 additions & 198 deletions examples/typescript/yarn.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@
"ts-jest": "^24.0.2",
"typescript": "^3.6.2",
"vue": "^2.6.10",
"vue-class-component": "^7.1.0"
"vue-class-component": "^7.1.0",
"vue-meta": "^2.2.2"
},
"config": {
"commitizen": {
Expand Down
4 changes: 2 additions & 2 deletions src/createMixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ export default (options: Options = {}): JsonldMixin => {
};

return {
beforeCreate (this: Vue) {
beforeCreate(this: Vue) {
if (this.$options && typeof this.$options.jsonld === 'function') {
this.$options.head = mergeHead.call(this, mergedOptions)
this.$options.head = mergeHead.call(this, mergedOptions);
}
},
};
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import VueT from 'vue';
import { MetaInfo } from 'vue-meta';
import createJsonldMixin from './createMixin';
import decorator from './decorator';

declare module 'vue/types/options' {
interface ComponentOptions<V extends VueT> {
jsonld?: () => object;
head?: object;
head?: MetaInfo | (() => MetaInfo);
}
}

Expand Down
31 changes: 18 additions & 13 deletions src/mergeHead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ interface Options {
space?: number | string;
}

const stringifyLD = (options: Options) => function () {
const jsonLd = this.$options.jsonld.call(this);
const stringifyLD = (options: Options): Function =>
function stringifyLDFn(): any {
const jsonLd = this.$options.jsonld.call(this);

if (jsonLd === null) {
return {};
}

if (jsonLd !== null) {
const stringifiedJson = JSON.stringify(jsonLd, null, options.space);
const innerHTML = options.space === 0 ? stringifiedJson : `\n${stringifiedJson}\n`;
const hid = `nuxt-jsonld-${XXH.h32(innerHTML, 0).toString(16)}`;
Expand All @@ -22,17 +26,18 @@ const stringifyLD = (options: Options) => function () {
],
__dangerouslyDisableSanitizersByTagID: {
[hid]: ['innerHTML'],
}
}
}
}
},
};
};

export default function (pluginOpts: Options) {
if (!this.$options) { return; }
export default function(pluginOpts: Options): Function {
if (!this.$options) {
return () => {};
}

this.$options.computed = this.$options.computed || {}
this.$options.computed = this.$options.computed || {};

this.$options.computed.$jsonld = stringifyLD(pluginOpts)
this.$options.computed.$jsonld = stringifyLD(pluginOpts);

if (this.$options.head) {
if (typeof this.$options.head === 'function') {
Expand All @@ -42,5 +47,5 @@ export default function (pluginOpts: Options) {
}
}

return () => Object.assign({}, this.$head, this.$jsonld)
};
return () => ({ ...this.$head, ...this.$jsonld });
}
1 change: 0 additions & 1 deletion test/createMixin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ describe('with head and jsonld', () => {
});
});


describe('hid', () => {
test('hid hash suffix is xxHash based innerHTML', () => {
const mixin = createJsonldMixin({ space: 0 });
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1827,6 +1827,11 @@ deep-is@~0.1.3:
resolved "https://registry.yarnpkg.com/deep-is/-/deep-is-0.1.3.tgz#b369d6fb5dbc13eecf524f91b070feedc357cf34"
integrity sha1-s2nW+128E+7PUk+RsHD+7cNXzzQ=

deepmerge@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/deepmerge/-/deepmerge-4.0.0.tgz#3e3110ca29205f120d7cb064960a39c3d2087c09"
integrity sha512-YZ1rOP5+kHor4hMAH+HRQnBQHg+wvS1un1hAOuIcxcBy0hzcUf6Jg2a1w65kpoOUnurOfZbERwjI1TfZxNjcww==

defaults@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/defaults/-/defaults-1.0.3.tgz#c656051e9817d9ff08ed881477f3fe4019f3ef7d"
Expand Down Expand Up @@ -7308,6 +7313,13 @@ vue-class-component@^7.1.0:
resolved "https://registry.yarnpkg.com/vue-class-component/-/vue-class-component-7.1.0.tgz#b33efcb10e17236d684f70b1e96f1946ec793e87"
integrity sha512-G9152NzUkz0i0xTfhk0Afc8vzdXxDR1pfN4dTwE72cskkgJtdXfrKBkMfGvDuxUh35U500g5Ve4xL8PEGdWeHg==

vue-meta@^2.2.2:
version "2.2.2"
resolved "https://registry.yarnpkg.com/vue-meta/-/vue-meta-2.2.2.tgz#ec6be76d75b7d6e4abb6d995d92245487081428b"
integrity sha512-aSzjfc5xV3szxPZMsSk6JeoHUqwrVHNPzpMciTCek56LDd2xKWCv+Ch4IGWL5EIxVyfxA+krsD7T11PTyLY4TQ==
dependencies:
deepmerge "^4.0.0"

vue@^2.6.10:
version "2.6.10"
resolved "https://registry.yarnpkg.com/vue/-/vue-2.6.10.tgz#a72b1a42a4d82a721ea438d1b6bf55e66195c637"
Expand Down

0 comments on commit 6d7e128

Please sign in to comment.