nuxt-og-image
Version:
Enlightened OG Image generation for Nuxt.
47 lines (46 loc) • 1.86 kB
JavaScript
import { useRuntimeConfig } from "#imports";
import { joinURL, withQuery } from "ufo";
import { toValue } from "vue";
import { getExtension } from "./pure.js";
export * from "./pure.js";
export function generateMeta(url, resolvedOptions) {
const meta = [
{ property: "og:image", content: url },
{ property: "og:image:type", content: () => `image/${getExtension(toValue(url)) || resolvedOptions.extension}` },
{ name: "twitter:card", content: "summary_large_image" },
// we don't need this but avoids issue when using useSeoMeta({ twitterImage })
{ name: "twitter:image", content: url },
{ name: "twitter:image:src", content: url }
];
if (resolvedOptions.width) {
meta.push({ property: "og:image:width", content: resolvedOptions.width });
meta.push({ name: "twitter:image:width", content: resolvedOptions.width });
}
if (resolvedOptions.height) {
meta.push({ property: "og:image:height", content: resolvedOptions.height });
meta.push({ name: "twitter:image:height", content: resolvedOptions.height });
}
if (resolvedOptions.alt) {
meta.push({ property: "og:image:alt", content: resolvedOptions.alt });
meta.push({ name: "twitter:image:alt", content: resolvedOptions.alt });
}
return meta;
}
export function getOgImagePath(pagePath, _options) {
const baseURL = useRuntimeConfig().app.baseURL;
const extension = _options?.extension || useOgImageRuntimeConfig().defaults.extension;
const path = joinURL("/", baseURL, `__og-image__/${import.meta.prerender ? "static" : "image"}`, pagePath, `og.${extension}`);
if (Object.keys(_options?._query || {}).length) {
return withQuery(path, _options._query);
}
return path;
}
export function useOgImageRuntimeConfig() {
const c = useRuntimeConfig();
return {
...c["nuxt-og-image"],
app: {
baseURL: c.app.baseURL
}
};
}