Skip to content

Commit

Permalink
fixed #2
Browse files Browse the repository at this point in the history
  • Loading branch information
egg- committed Nov 20, 2015
1 parent 10ea1c8 commit 7209863
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
54 changes: 38 additions & 16 deletions lib/ogp-meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,13 @@ OpenGraph.prototype.site_name = function (site_name) {
}

OpenGraph.prototype.setObject = function (type, obj) {
if (typeof obj === 'string') {
this.meta['og:' + type] = obj
} else {
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
this.meta[['og', type, prop].join(':')] = obj[prop]
}
}

if (obj.url) {
this.meta['og:' + type] = obj.url
}
// multiple value
var key = 'og:' + type
if (typeof this.meta[key] === 'undefined') {
this.meta[key] = []
}

this.meta[key].unshift(obj)
}

/**
Expand All @@ -56,6 +50,11 @@ OpenGraph.prototype.setObject = function (type, obj) {
* @param {string} image.height
*/
OpenGraph.prototype.image = function (image) {
// for kakaotalk
if (image.url) {
this.setObject('image', image.url)
}

this.setObject('image', image)
}

Expand Down Expand Up @@ -90,13 +89,36 @@ OpenGraph.prototype.app = function (meta, type) {

OpenGraph.prototype.toHTML = function (delimiter) {
var html = []
for (var name in this.meta) {
if (this.meta.hasOwnProperty(name)) {
html.push('<meta property="' + name + '" content="' + entities.encode('' + this.meta[name]) + '">')

OpenGraph.each(this.meta, function (meta, name) {
if (typeof meta === 'string') {
html.push(OpenGraph.toMeta(name, meta))
} else {
OpenGraph.each(meta, function (obj) {
if (typeof obj === 'string') {
html.push(OpenGraph.toMeta(name, obj))
} else {
OpenGraph.each(obj, function (val, key) {
html.push(OpenGraph.toMeta(name + ':' + key, val))
})
}
})
}
}
})

return html.join(delimiter || '\n')
}

OpenGraph.toMeta = function (name, value) {
return '<meta property="' + name + '" content="' + entities.encode('' + value) + '">'
}

OpenGraph.each = function (obj, cb) {
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
cb(obj[key], key)
}
}
}

module.exports = OpenGraph
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ogp-meta",
"version": "0.1.6",
"version": "0.1.7",
"author": {
"name": "egg",
"email": "[email protected]"
Expand Down

0 comments on commit 7209863

Please sign in to comment.