From 116bd7230c91e76e1d94e8878bef6487cbcc00ea Mon Sep 17 00:00:00 2001 From: Erik Michelson Date: Fri, 4 Oct 2019 11:04:58 +0200 Subject: Added og-metadata to index page - image and URL properties are only included if the server url is set, because opengraph protocol does not support relative links Signed-off-by: Erik Michelson --- public/views/index/head.ejs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/public/views/index/head.ejs b/public/views/index/head.ejs index d790d917..400e2e59 100644 --- a/public/views/index/head.ejs +++ b/public/views/index/head.ejs @@ -6,6 +6,15 @@ + + + +<% if (serverURL !== "") { %> + + + + +<% } %> CodiMD - <%= __('Collaborative markdown notes') %> -- cgit v1.2.3 From 2881f8211aaaa2def3f3cedabf635f03733ac82e Mon Sep 17 00:00:00 2001 From: Erik Michelson Date: Fri, 4 Oct 2019 19:49:45 +0200 Subject: Added customizable og-metadata to notes Signed-off-by: Erik Michelson --- lib/models/note.js | 9 +++++++++ lib/response.js | 8 ++++++-- public/views/codimd/head.ejs | 4 ++++ public/views/pretty.ejs | 4 ++++ 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/models/note.js b/lib/models/note.js index 3a8ccb67..141402ac 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 81d732a5..7535d266 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 e3edde66..e2f0375c 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 1970ab2f..5f18ea8b 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 %> -- cgit v1.2.3 From 8f61d840de0ff1a40fbb61ce9e688d9372f9117e Mon Sep 17 00:00:00 2001 From: Erik Michelson Date: Fri, 4 Oct 2019 19:58:02 +0200 Subject: Added documentation for opengraph metadata Signed-off-by: Erik Michelson --- public/docs/yaml-metadata.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/public/docs/yaml-metadata.md b/public/docs/yaml-metadata.md index 8e59c8f2..f09591bc 100644 --- a/public/docs/yaml-metadata.md +++ b/public/docs/yaml-metadata.md @@ -159,3 +159,20 @@ slideOptions: transition: fade theme: white ``` + +opengraph +--- +This option allows you to override the default generated opengraph metadata. +See the [OpenGraph protocol documentation](https://ogp.me) for more information. + +**Notice: always use two spaces as indention in YAML metadata!** + +> default: not set (uses auto-generated metadata) + +**Example** +```yml +opengraph: + title: Special title for OpenGraph protocol + image: https://dummyimage.com/600x600/000/fff + image:type: image/png +``` -- cgit v1.2.3 From f26f48793fdf5bb81f4bec5d7c50e5fe1ba978d6 Mon Sep 17 00:00:00 2001 From: Erik Michelson Date: Tue, 8 Oct 2019 21:11:01 +0200 Subject: Fixed bugs, added default image Signed-off-by: Erik Michelson --- public/views/codimd/head.ejs | 8 ++++++-- public/views/index/head.ejs | 4 +--- public/views/pretty.ejs | 6 +++++- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/public/views/codimd/head.ejs b/public/views/codimd/head.ejs index e2f0375c..934767f6 100644 --- a/public/views/codimd/head.ejs +++ b/public/views/codimd/head.ejs @@ -6,8 +6,12 @@ <% for (var og in opengraph) { %> <% if (opengraph.hasOwnProperty(og) && opengraph[og].trim() !== '') { %> - -<% }} %> + +<% }} if (!opengraph.hasOwnProperty('image')) { %> + + + +<% } %> <%= title %> diff --git a/public/views/index/head.ejs b/public/views/index/head.ejs index 400e2e59..0f1e584e 100644 --- a/public/views/index/head.ejs +++ b/public/views/index/head.ejs @@ -9,12 +9,10 @@ -<% if (serverURL !== "") { %> - + -<% } %> CodiMD - <%= __('Collaborative markdown notes') %> diff --git a/public/views/pretty.ejs b/public/views/pretty.ejs index 5f18ea8b..20bb44e3 100644 --- a/public/views/pretty.ejs +++ b/public/views/pretty.ejs @@ -17,7 +17,11 @@ <% for (var og in opengraph) { %> <% if (opengraph.hasOwnProperty(og) && opengraph[og].trim() !== '') { %> - <% }} %> + <% }} if (!opengraph.hasOwnProperty('image')) { %> + + + + <% } %> <%= title %> -- cgit v1.2.3