From cc90cba5471039707540b7885c6de767575eadf6 Mon Sep 17 00:00:00 2001 From: Chris Waring Date: Wed, 7 Apr 2021 18:48:03 +0100 Subject: [PATCH 1/2] feat vuepress-plugin-og --- src/.vuepress/config.js | 1 + .../plugins/vuepress-plugin-og-image/index.js | 6 ++++++ .../pageMeta-mixin.js | 20 +++++++++++++++++++ 3 files changed, 27 insertions(+) create mode 100644 src/.vuepress/plugins/vuepress-plugin-og-image/index.js create mode 100644 src/.vuepress/plugins/vuepress-plugin-og-image/pageMeta-mixin.js diff --git a/src/.vuepress/config.js b/src/.vuepress/config.js index 3fdb8baa..43fff623 100644 --- a/src/.vuepress/config.js +++ b/src/.vuepress/config.js @@ -239,6 +239,7 @@ module.exports = { $page.lastUpdated && new Date($page.lastUpdated).toISOString(), }, ], + [require('./plugins/vuepress-plugin-og-image')], ['vuepress-plugin-robots', { host: CANONICAL_BASE }], [ '@vuepress/html-redirect', diff --git a/src/.vuepress/plugins/vuepress-plugin-og-image/index.js b/src/.vuepress/plugins/vuepress-plugin-og-image/index.js new file mode 100644 index 00000000..aa5c96eb --- /dev/null +++ b/src/.vuepress/plugins/vuepress-plugin-og-image/index.js @@ -0,0 +1,6 @@ +const path = require('path') + +module.exports = { + name: 'vuepress-plugin-og-image', + clientRootMixin: path.resolve(__dirname, 'pageMeta-mixin.js'), +} diff --git a/src/.vuepress/plugins/vuepress-plugin-og-image/pageMeta-mixin.js b/src/.vuepress/plugins/vuepress-plugin-og-image/pageMeta-mixin.js new file mode 100644 index 00000000..c62dd1d9 --- /dev/null +++ b/src/.vuepress/plugins/vuepress-plugin-og-image/pageMeta-mixin.js @@ -0,0 +1,20 @@ +export default { + created() { + if (typeof this.$ssrContext !== 'undefined') { + const meta = this.$frontmatter.meta + const og = meta && meta.find((m) => m.property === 'og:image') + if (meta && og) { + try { + const imgPath = require('@source/assets/' + og.content) + const { pageMeta } = this.$ssrContext + this.$ssrContext.pageMeta = pageMeta.replace( + new RegExp(og.content, 'g'), + imgPath + ) + } catch (e) { + throw new Error(`could not load og:image ${og.content}`) + } + } + } + }, +} From ec138b2ee956976458a33b2148aa5e669395a2e1 Mon Sep 17 00:00:00 2001 From: Chris Waring Date: Wed, 7 Apr 2021 18:53:46 +0100 Subject: [PATCH 2/2] normalize paths --- .../plugins/vuepress-plugin-og-image/pageMeta-mixin.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/.vuepress/plugins/vuepress-plugin-og-image/pageMeta-mixin.js b/src/.vuepress/plugins/vuepress-plugin-og-image/pageMeta-mixin.js index c62dd1d9..94998cef 100644 --- a/src/.vuepress/plugins/vuepress-plugin-og-image/pageMeta-mixin.js +++ b/src/.vuepress/plugins/vuepress-plugin-og-image/pageMeta-mixin.js @@ -1,3 +1,5 @@ +const { normalize } = require('path') + export default { created() { if (typeof this.$ssrContext !== 'undefined') { @@ -5,7 +7,8 @@ export default { const og = meta && meta.find((m) => m.property === 'og:image') if (meta && og) { try { - const imgPath = require('@source/assets/' + og.content) + const assetPath = normalize(og.content).replace(/^\/|\/$/g, '') + const imgPath = require('@source/assets/' + assetPath) const { pageMeta } = this.$ssrContext this.$ssrContext.pageMeta = pageMeta.replace( new RegExp(og.content, 'g'),