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

Commit

Permalink
feat: add twitter cards
Browse files Browse the repository at this point in the history
  • Loading branch information
TiagoDanin committed Apr 21, 2019
1 parent b2b1877 commit 3cb6ad9
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 19 deletions.
36 changes: 23 additions & 13 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const defaults = {
language: 'English',
templateTitle: '%title%',
title: 'Title',
name: ''
name: false,
description: false
}

const allMetas = {
Expand Down Expand Up @@ -40,21 +41,31 @@ const allMetas = {
url: {fullId: 'og:image', content: true},
width: {content: true},
height: {content: true},
alt: {content: true},
alt: {content: true}
},
price: {
id: 'price',
currency: {content: true},
amount: content: true}
}
},
facebook: {
id: 'fb',
appId: {id: 'app_id', content: true},
pageId: {id: 'page_id', content: true}
},
twitter: {
id: 'twitter',
title: {default: 'title', content: true},
description: {default: 'description', content: true},
card: {content: true},
type: {id: 'card', content: true}
site: {content: true},
creator: {content: true}
}
/*twitter: {
cardType: false,check(meta.id)
site: false,
handle: false,
creator: false
}*/
}
allMetas.og = allMetas.openGraph
allMetas.fb = allMetas.facebook

const createTitle = (options = {}) => {
return options.templateTitle.replace(/%title%/g, options.title)
Expand Down Expand Up @@ -82,7 +93,7 @@ const createMeta = (options = {}, inputMeta = [], template = {}) => {
}
const generate = (metas, opts, id = '') => {
Object.keys(metas).map((k) => {
const meta = metas[k]
const meta = {...metas[k]}
if (meta.fullId) {
meta.id = meta.fullId
} else if (!meta.id) {
Expand All @@ -93,14 +104,14 @@ const createMeta = (options = {}, inputMeta = [], template = {}) => {

if (opts[k]) {
meta.value = opts[k]
} else if (meta.default && opts[meta.default]) {
meta.value = opts[meta.default]
} else if (meta.default && options[meta.default]) {
meta.value = options[meta.default]
} else {
return
}

if (typeof meta.value == 'object') {
return generate(meta, opts[k], meta.id+':')
return generate(meta, meta.value, meta.id+':')
} else if (meta.content) {
findAndRemove(meta.id)

Expand All @@ -112,7 +123,6 @@ const createMeta = (options = {}, inputMeta = [], template = {}) => {
if (!meta.value || meta.value.length <= 0) {
return
}

outputMeta.push({
hid: meta.id,
key: meta.id,
Expand Down
3 changes: 3 additions & 0 deletions lib/plugin.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
const debug = require('debug')
const deb = debug('nuxt:seo:debug')



const nuxtSeo = <%= serialize(options.func) %>
const moduleOptions = <%= serialize(options.moduleOptions) %>
const template = <%= serialize(options.template) %>

export default function (ctx, inject) {

function create (options = {}) {
options = {
...moduleOptions,
Expand Down
30 changes: 30 additions & 0 deletions test/fixtures/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,36 @@ module.exports = [{
property: 'url',
name: 'url',
content: 'https://tiagodanin.github.io'
}, {
hid: 'og:site_name',
key: 'og:site_name',
property: 'og:site_name',
name: 'og:site_name',
content: 'TiagoDanin WebSite'
}, {
hid: 'og:title',
key: 'og:title',
property: 'og:title',
name: 'og:title',
content: 'Home Page'
}, {
hid: 'og:description',
key: 'og:description',
property: 'og:description',
name: 'og:description',
content: 'About the Tiago Danin'
}, {
hid: 'og:locale',
key: 'og:locale',
property: 'og:locale',
name: 'og:locale',
content: 'en'
}, {
hid: 'og:url',
key: 'og:url',
property: 'og:url',
name: 'og:url',
content: 'https://tiagodanin.github.io'
}, {
hid: 'og:type',
key: 'og:type',
Expand Down
26 changes: 20 additions & 6 deletions test/nuxt.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,29 @@ test.before('Init Nuxt.js', async t => {
test('Route / and render HTML', async t => {
let context = {}
const { html } = await nuxt.renderRoute('/', context)
t.true(html.includes(`<h1>Hello World</h1>`))
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" name="description" content="Hello World Page" property="description" data-hid="description">`))
t.true(html.includes(`<meta data-n-head="true" name="og:title" content="Home Page" property="og:title" data-hid="og:title">`))
t.true(html.includes(`<meta data-n-head="true" name="og:site_name" content="App name" property="og:site_name" data-hid="og:site_name">`))
t.true(html.includes(`<meta data-n-head="true" name="og:description" content="Hello World Page" property="og:description" data-hid="og:description">`))
t.true(html.includes(`<meta data-n-head="true" name="twitter:title" content="Home Page" property="twitter:title" data-hid="twitter:title">`))
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 nuxt.renderRoute('/post', context)
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 => {
Expand Down

0 comments on commit 3cb6ad9

Please sign in to comment.