diff --git a/lib/models/note.js b/lib/models/note.js index 3a8ccb677..141402ac0 100644 --- a/lib/models/note.js +++ b/lib/models/note.js @@ -409,9 +409,18 @@ module.exports = function (sequelize, DataTypes) { if (meta.GA && (typeof meta.GA === 'string' || typeof meta.GA === 'number')) { _meta.GA = meta.GA } if (meta.disqus && (typeof meta.disqus === 'string' || typeof meta.disqus === 'number')) { _meta.disqus = meta.disqus } if (meta.slideOptions && (typeof meta.slideOptions === 'object')) { _meta.slideOptions = meta.slideOptions } + if (meta.opengraph && (typeof meta.opengraph === 'object')) { _meta.opengraph = meta.opengraph } } return _meta } + Note.parseOpengraph = function (meta, title) { + var _ogdata = {} + if (meta.opengraph) { _ogdata = meta.opengraph } + if (!(_ogdata.title && (typeof _ogdata.title === 'string' || typeof _ogdata.title === 'number'))) { _ogdata.title = title } + if (!(_ogdata.description && (typeof _ogdata.description === 'string' || typeof _ogdata.description === 'number'))) { _ogdata.description = meta.description || '' } + if (!(_ogdata.type && (typeof _ogdata.type === 'string'))) { _ogdata.type = 'website' } + return _ogdata + } Note.updateAuthorshipByOperation = function (operation, userId, authorships) { var index = 0 var timestamp = Date.now() diff --git a/lib/response.js b/lib/response.js index 81d732a53..7535d2660 100644 --- a/lib/response.js +++ b/lib/response.js @@ -98,12 +98,14 @@ function responseCodiMD (res, note) { var meta = models.Note.parseMeta(extracted.meta) var title = models.Note.decodeTitle(note.title) title = models.Note.generateWebTitle(meta.title || title) + var opengraph = models.Note.parseOpengraph(meta, title) res.set({ 'Cache-Control': 'private', // only cache by client 'X-Robots-Tag': 'noindex, nofollow' // prevent crawling }) res.render('codimd.ejs', { - title: title + title: title, + opengraph: opengraph }) } @@ -217,6 +219,7 @@ function showPublishNote (req, res, next) { var updatetime = note.lastchangeAt var title = models.Note.decodeTitle(note.title) title = models.Note.generateWebTitle(meta.title || title) + var ogdata = models.Note.parseOpengraph(meta, title) var data = { title: title, description: meta.description || (markdown ? models.Note.generateDescription(markdown) : null), @@ -232,7 +235,8 @@ function showPublishNote (req, res, next) { GA: meta.GA, disqus: meta.disqus, cspNonce: res.locals.nonce, - dnt: req.headers.dnt + dnt: req.headers.dnt, + opengraph: ogdata } return renderPublish(data, res) }).catch(function (err) { diff --git a/public/views/codimd/head.ejs b/public/views/codimd/head.ejs index e3edde66c..e2f0375c2 100644 --- a/public/views/codimd/head.ejs +++ b/public/views/codimd/head.ejs @@ -4,6 +4,10 @@ +<% for (var og in opengraph) { %> +<% if (opengraph.hasOwnProperty(og) && opengraph[og].trim() !== '') { %> + +<% }} %> <%= title %> diff --git a/public/views/pretty.ejs b/public/views/pretty.ejs index 1970ab2f0..5f18ea8b3 100644 --- a/public/views/pretty.ejs +++ b/public/views/pretty.ejs @@ -14,6 +14,10 @@ <% if(typeof description !== 'undefined' && description) { %> <% } %> + <% for (var og in opengraph) { %> + <% if (opengraph.hasOwnProperty(og) && opengraph[og].trim() !== '') { %> + + <% }} %> <%= title %>