« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/content/dependencies/generateSocialEmbed.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/content/dependencies/generateSocialEmbed.js')
-rw-r--r--src/content/dependencies/generateSocialEmbed.js65
1 files changed, 65 insertions, 0 deletions
diff --git a/src/content/dependencies/generateSocialEmbed.js b/src/content/dependencies/generateSocialEmbed.js
new file mode 100644
index 0000000..0144c7f
--- /dev/null
+++ b/src/content/dependencies/generateSocialEmbed.js
@@ -0,0 +1,65 @@
+export default {
+  extraDependencies: ['html', 'language', 'wikiData'],
+
+  sprawl({wikiInfo}) {
+    return {
+      canonicalBase: wikiInfo.canonicalBase,
+      shortWikiName: wikiInfo.nameShort,
+    };
+  },
+
+  data(sprawl) {
+    return {
+      canonicalBase: sprawl.canonicalBase,
+      shortWikiName: sprawl.shortWikiName,
+    };
+  },
+
+  slots: {
+    mode: {validate: v => v.is('html', 'json')},
+
+    title: {type: 'string'},
+    description: {type: 'string'},
+
+    headingContent: {type: 'string'},
+    headingLink: {type: 'string'},
+    imagePath: {type: 'string'},
+  },
+
+  generate(data, slots, {html, language}) {
+    switch (slots.mode) {
+      case 'html':
+        return html.tags([
+          slots.title &&
+            html.tag('meta', {property: 'og:title', content: slots.title}),
+
+          slots.description &&
+            html.tag('meta', {
+              property: 'og:description',
+              content: slots.description,
+            }),
+
+          slots.imagePath &&
+            html.tag('meta', {property: 'og:image', content: slots.imagePath}),
+        ]);
+
+      case 'json':
+        return JSON.stringify({
+          author_name:
+            (slots.headingContent
+              ? language.$('misc.socialEmbed.heading', {
+                  wikiName: data.shortWikiName,
+                  heading: slots.headingContent,
+                })
+              : undefined),
+
+          author_url:
+            (slots.headingLink && data.canonicalBase
+              ? data.canonicalBase.replace(/\/$/, '') +
+                '/' +
+                slots.headingLink.replace(/^\//, '')
+              : undefined),
+        });
+    }
+  },
+};