UNPKG

@stefanobartoletti/nuxt-social-share

Version:
39 lines (38 loc) 1.72 kB
import { computed, ref, useRoute, useRuntimeConfig } from "#imports"; import { networksIndex } from "./networksIndex.js"; const defaultOptions = { network: "", url: void 0, title: void 0, user: void 0, hashtags: void 0, image: void 0 }; export function useSocialShare(options = defaultOptions) { const { network, url, title, user, hashtags, image } = options; const moduleOptions = useRuntimeConfig().public.socialShare; const selectedNetwork = ref({ ...networksIndex[network] }); const route = useRoute(); const pageUrl = computed(() => { if (url !== void 0) { return new URL(url).href; } if (moduleOptions.baseUrl !== "") { return new URL(route.fullPath, moduleOptions.baseUrl).href; } return ""; }); const fullShareUrl = computed(() => { const shareUrl = selectedNetwork.value.shareUrl; const argTitle = selectedNetwork.value.args?.title && title ? selectedNetwork.value.args?.title : ""; const argUser = selectedNetwork.value.args?.user && user ? selectedNetwork.value.args?.user : ""; const argHashtags = selectedNetwork.value.args?.hashtags && hashtags ? selectedNetwork.value.args?.hashtags : ""; const argImage = selectedNetwork.value.args?.image && image ? selectedNetwork.value.args?.image : ""; let fullUrl = shareUrl + argTitle + argUser + argHashtags + argImage; fullUrl = fullUrl.replace(/\[u\]/i, encodeURIComponent(pageUrl.value)).replace(/\[t\]/i, title || "").replace(/\[uid\]/i, user || "").replace(/\[h\]/i, hashtags || "").replace(/\[i\]/i, image || ""); return new URL(fullUrl).href; }); selectedNetwork.value.shareUrl = fullShareUrl.value; delete selectedNetwork.value.args; return selectedNetwork; }