Skip to content
This repository has been archived by the owner on Dec 5, 2021. It is now read-only.

Commit

Permalink
fix: use XO Style
Browse files Browse the repository at this point in the history
  • Loading branch information
TiagoDanin committed Jun 2, 2019
1 parent 4747c6d commit 6bbacc5
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 88 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Nuxt SEO

[![Node](https://img.shields.io/node/v/nuxt-seo.svg?style=flat-square)](https://npmjs.org/package/nuxt-seo)[![Version](https://img.shields.io/npm/v/nuxt-seo.svg?style=flat-square)](https://npmjs.org/package/nuxt-seo)[![Downloads](https://img.shields.io/npm/dt/nuxt-seo.svg?style=flat-square)](https://npmjs.org/package/nuxt-seo)[![Travis](https://img.shields.io/travis/TiagoDanin/Nuxt-SEO.svg?branch=master&style=flat-square)](https://travis-ci.org/TiagoDanin/Nuxt-SEO)
[![Travis](https://img.shields.io/travis/TiagoDanin/Nuxt-SEO.svg?branch=master&style=flat-square)](https://travis-ci.org/TiagoDanin/Nuxt-SEO) [![Downloads](https://img.shields.io/npm/dt/nuxt-seo.svg?style=flat-square)](https://npmjs.org/package/nuxt-seo) [![Node](https://img.shields.io/node/v/nuxt-seo.svg?style=flat-square)](https://npmjs.org/package/nuxt-seo) [![Version](https://img.shields.io/npm/v/nuxt-seo.svg?style=flat-square)](https://npmjs.org/package/nuxt-seo) [![XO code style](https://img.shields.io/badge/code%20style-XO-red.svg?style=flat-square)](https://github.com/xojs/xo)

SEO / HTML Meta Tags Module for Nuxt.js

Expand Down Expand Up @@ -129,6 +129,7 @@ yarn test
- [got](https://ghub.io/got): Simplified HTTP requests
- [nuxt](https://ghub.io/nuxt): A minimalistic framework for server-rendered Vue.js applications (inspired by Next.js)
- [vue](https://ghub.io/vue): Reactive, component-oriented view layer for modern web interfaces.
- [xo](https://ghub.io/xo): JavaScript happiness style linter ❤️

## Contributors

Expand Down
56 changes: 34 additions & 22 deletions lib/module.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const path = require('path')
const debug = require('debug')

const log = debug('nuxt:seo:log')
const deb = debug('nuxt:seo:debug')

Expand Down Expand Up @@ -65,7 +66,7 @@ const allMetas = {
author: {fullId: 'book:author', content: true, multi: true},
isbn: {fullId: 'book:isbn', content: true},
releaseDate: {fullId: 'book:release_date', content: true},
tag: {fullId: 'book:author', content: true, multi: true},
tag: {fullId: 'book:author', content: true, multi: true}
},
price: {
id: 'price',
Expand Down Expand Up @@ -107,7 +108,7 @@ const createTitle = (options = {}) => {
const createMeta = (options = {}, inputMeta = [], template = {}) => {
const outputMeta = []

const getMetaKey = (input) => {
const getMetaKey = input => {
const keys = Object.keys(input)
if (input.hid) {
return input.hid
Expand All @@ -118,16 +119,17 @@ const createMeta = (options = {}, inputMeta = [], template = {}) => {
} else if (input.name) {
return input.name
}

return keys[0] || ''
}

const findAndRemove = (id) => {
inputMeta = inputMeta.filter((meta) => {
return getMetaKey(meta) != id
const findAndRemove = id => {
inputMeta = inputMeta.filter(meta => {
return getMetaKey(meta) !== id
})
}

const parserValue = (value) => {
const parserValue = value => {
if (Array.isArray(value)) {
value = value.join(',')
}
Expand All @@ -136,16 +138,17 @@ const createMeta = (options = {}, inputMeta = [], template = {}) => {
if (!value || value.length <= 0) {
return false
}

return value
}

const generate = (metas, opts, id = '', index=false) => {
const generate = (metas, opts, id = '', index = false) => {
metas = {...metas}
Object.keys(metas).map((k) => {
Object.keys(metas).map(k => {
const meta = {...metas[k]}
if (meta.fullId) {
meta.id = meta.fullId
} else if (!meta.id) {
} else if (!meta.id) { // eslint-disable-line no-negated-condition
meta.id = id + k
} else {
meta.id = id + meta.id
Expand All @@ -155,7 +158,7 @@ const createMeta = (options = {}, inputMeta = [], template = {}) => {
if (!opts[k]) {
return
}
} else if (typeof opts == 'string' && k == 'id') {
} else if (typeof opts === 'string' && k === 'id') {
meta.value = opts
meta.id = id
meta.content = id
Expand All @@ -172,28 +175,31 @@ const createMeta = (options = {}, inputMeta = [], template = {}) => {
let id = ''
if (meta.fullId) {
id = meta.fullId
} else if (typeof value == 'string') {
} else if (typeof value === 'string') {
id = meta.id
} else {
id = `${meta.id}:`
}

meta.multi = false
return generate(meta, value, id, index)
})
} else if (typeof meta.value == 'object' && !Array.isArray(meta.value)) {
return generate(meta, meta.value, meta.id+':')
} else if (typeof meta.value === 'object' && !Array.isArray(meta.value)) {
return generate(meta, meta.value, meta.id + ':')
} else if (meta.content) {
findAndRemove(meta.id)

meta.value = parserValue(meta.value)
if (!meta.value) return
if (!meta.value) {
return
}

if (meta.ids) {
meta.ids.map((id) => {
meta.ids.map(id => {
meta.id = id
outputMeta.push({
hid: meta.id,
key: typeof index == 'number' ? `${meta.id}:0${index}` : meta.id,
key: typeof index === 'number' ? `${meta.id}:0${index}` : meta.id,
property: meta.id,
name: meta.id,
content: meta.value
Expand All @@ -202,20 +208,21 @@ const createMeta = (options = {}, inputMeta = [], template = {}) => {
} else {
outputMeta.push({
hid: meta.id,
key: typeof index == 'number' ? `${meta.id}:0${index}` : meta.id,
key: typeof index === 'number' ? `${meta.id}:0${index}` : meta.id,
property: meta.id,
name: meta.id,
content: meta.value
})
}

} else if (meta.id) {
if (meta.ids) {
meta.ids.map((id) => {
meta.ids.map(id => {
findAndRemove(id)

meta.value = parserValue(meta.value)
if (!meta.value) return
if (!meta.value) {
return
}

outputMeta.push({
[id]: meta.value
Expand All @@ -225,7 +232,9 @@ const createMeta = (options = {}, inputMeta = [], template = {}) => {
findAndRemove(meta.id)

meta.value = parserValue(meta.value)
if (!meta.value) return
if (!meta.value) {
return
}

outputMeta.push({
[meta.id]: meta.value
Expand All @@ -234,6 +243,7 @@ const createMeta = (options = {}, inputMeta = [], template = {}) => {
}
})
}

generate(template, options)
return [...inputMeta, ...outputMeta]
}
Expand All @@ -242,6 +252,7 @@ module.exports = function (moduleOptions) {
if (!moduleOptions.meta) {
moduleOptions.meta = {}
}

this.nuxt.hook('build:before', () => {
const template = {
...allMetas,
Expand All @@ -265,7 +276,7 @@ module.exports = function (moduleOptions) {

const pluginOptions = {
moduleOptions: options,
template: template,
template,
func: {
createMeta,
createTitle
Expand All @@ -283,6 +294,7 @@ module.exports = function (moduleOptions) {
}

module.exports.meta = require('../package.json')

module.exports.defaults = defaults
module.exports.createTitle = createTitle
module.exports.createMeta = createMeta
Expand Down
21 changes: 18 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"version": "1.2.0",
"description": "SEO / HTML Meta Tags Module for Nuxt.js",
"author": {
"name": "Tiago Danin",
"email": "[email protected]",
"name": "Tiago Danin",
"url": "https://TiagoDanin.github.io"
},
"license": "MIT",
Expand All @@ -26,7 +26,7 @@
],
"scripts": {
"start": "nuxt example",
"test": "ava"
"test": "xo && ava"
},
"engines": {
"node": ">= 8"
Expand Down Expand Up @@ -57,7 +57,22 @@
"ava": "^2.0.0",
"got": "^9.6.0",
"nuxt": "^2.8.0",
"vue": "^2.6.10"
"vue": "^2.6.10",
"xo": "^0.24.0"
},
"xo": {
"ignores": [
"lib/plugin.js"
],
"rules": {
"quote-props": "warn",
"eqeqeq": "warn",
"array-callback-return": "warn",
"require-atomic-updates": "warn",
"no-else-return": "off",
"ava/no-ignored-test-files": "off"
},
"semicolon": false
},
"documentation": "./docs.md",
"ava": {
Expand Down
11 changes: 6 additions & 5 deletions scripts/genDoc.js → scripts/gen-doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ const removeList = [
'fullId'
]
const getKeys = (input, id = '') => {
return Object.keys(input).map((key) => {
if (typeof input[key] == 'object') {
getKeys(input[key], id+key+'.')
return Object.keys(input).map(key => {
if (typeof input[key] === 'object') {
getKeys(input[key], id + key + '.')
}

if (!removeList.includes(key)) {
keys.push(id+key)
keys.push(id + key)
}
})
}

getKeys(nuxtSeo.template)
console.log('`'+keys.join('`, `')+'`')
console.log('`' + keys.join('`, `') + '`')
51 changes: 26 additions & 25 deletions test/nuxt.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
const test = require('ava')
const { Nuxt, Builder } = require('nuxt')
const config = require('../example/nuxt.config')
const {Nuxt, Builder} = require('nuxt')
const got = require('got')
const config = require('../example/nuxt.config')

const url = path => `http://localhost:4000${path}`
const get = path => got(url(path)).then(res => res.body)
const get = async path => {
const {body} = await got(url(path))
return body
}

let nuxt = null

test.before('Init Nuxt.js', async t => {
test.before('Init Nuxt.js', async () => {
config.dev = false
config.mode = 'universal'
nuxt = new Nuxt(config)
Expand All @@ -17,34 +20,32 @@ test.before('Init Nuxt.js', async t => {
})

test('Route / and render HTML', async t => {
let context = {}
const html = await get('/')

t.true(html.includes(`<title data-n-head="true">Home Page</title>`))
t.true(html.includes(`<meta data-n-head="true" charset="utf-8"`))
t.true(html.includes(`<meta data-n-head="true" lang="en">`))
t.true(html.includes(`<meta data-n-head="true" language="English">`))
t.true(html.includes(`<meta data-n-head="true" data-hid="name" key="name" property="name" name="name" content="App name">`))
t.true(html.includes(`<meta data-n-head="true" data-hid="title" key="title" property="title" name="title" content="Home Page">`))
t.true(html.includes(`<meta data-n-head="true" data-hid="description" key="description" property="description" name="description" content="Hello World Page">`))
t.true(html.includes('<title data-n-head="true">Home Page</title>'))
t.true(html.includes('<meta data-n-head="true" charset="utf-8"'))
t.true(html.includes('<meta data-n-head="true" lang="en">'))
t.true(html.includes('<meta data-n-head="true" language="English">'))
t.true(html.includes('<meta data-n-head="true" data-hid="name" key="name" property="name" name="name" content="App name">'))
t.true(html.includes('<meta data-n-head="true" data-hid="title" key="title" property="title" name="title" content="Home Page">'))
t.true(html.includes('<meta data-n-head="true" data-hid="description" key="description" property="description" name="description" content="Hello World Page">'))
})

test('Route /post and render HTML', async t => {
let context = {}
const html = await get('/post')
t.true(html.includes(`<title data-n-head="true">Hello</title>`))
t.true(html.includes(`<meta data-n-head="true" charset="utf-8">`))
t.true(html.includes(`<meta data-n-head="true" lang="en">`))
t.true(html.includes(`<meta data-n-head="true" language="English">`))
t.true(html.includes(`<meta data-n-head="true" data-hid="name" key="name" property="name" name="name" content="App name">`))
t.true(html.includes(`<meta data-n-head="true" data-hid="title" key="title" property="title" name="title" content="Hello">`))
t.true(html.includes(`<meta data-n-head="true" data-hid="description" key="description" property="description" name="description" content="Hello World page in blog">`))
t.true(html.includes(`<meta data-n-head="true" data-hid="og:site_name" key="og:site_name" property="og:site_name" name="og:site_name" content="App name">`))
t.true(html.includes(`<meta data-n-head="true" data-hid="og:title" key="og:title" property="og:title" name="og:title" content="openGraph title">`))
t.true(html.includes(`<meta data-n-head="true" data-hid="og:description" key="og:description" property="og:description" name="og:description" content="Hello World page in blog">`))
t.true(html.includes(`<meta data-n-head="true" data-hid="og:locale" key="og:locale" property="og:locale" name="og:locale" content="en">`))
t.true(html.includes('<title data-n-head="true">Hello</title>'))
t.true(html.includes('<meta data-n-head="true" charset="utf-8">'))
t.true(html.includes('<meta data-n-head="true" lang="en">'))
t.true(html.includes('<meta data-n-head="true" language="English">'))
t.true(html.includes('<meta data-n-head="true" data-hid="name" key="name" property="name" name="name" content="App name">'))
t.true(html.includes('<meta data-n-head="true" data-hid="title" key="title" property="title" name="title" content="Hello">'))
t.true(html.includes('<meta data-n-head="true" data-hid="description" key="description" property="description" name="description" content="Hello World page in blog">'))
t.true(html.includes('<meta data-n-head="true" data-hid="og:site_name" key="og:site_name" property="og:site_name" name="og:site_name" content="App name">'))
t.true(html.includes('<meta data-n-head="true" data-hid="og:title" key="og:title" property="og:title" name="og:title" content="openGraph title">'))
t.true(html.includes('<meta data-n-head="true" data-hid="og:description" key="og:description" property="og:description" name="og:description" content="Hello World page in blog">'))
t.true(html.includes('<meta data-n-head="true" data-hid="og:locale" key="og:locale" property="og:locale" name="og:locale" content="en">'))
})

test.after('Closing server', t => {
test.after('Closing server', () => {
nuxt.close()
})
Loading

0 comments on commit 6bbacc5

Please sign in to comment.