vite-plugin-meta-tags
Version:
A Vite plug-in to automatically inject meta tags into your index.html
55 lines (54 loc) • 1.68 kB
JavaScript
function title(value) {
return {
tag: 'title',
children: value,
};
}
function twitterDomain(value) {
return {
tag: 'meta',
attrs: {
property: 'twitter:domain',
content: new URL(value).hostname,
},
};
}
function twitterCardImg() {
return {
tag: 'meta',
attrs: {
name: 'twitter:card',
content: 'summary_large_image',
},
};
}
const GENERATORS = {
title: [title, { property: 'og:title' }, { name: 'twitter:title' }],
description: [{ name: 'description' }, { property: 'og:description' }, { name: 'twitter:description' }],
url: [{ property: 'og:url' }, { property: 'twitter:url' }, twitterDomain],
img: [{ property: 'og:image' }, twitterCardImg, { name: 'twitter:image' }],
color: [{ name: 'theme-color' }, { name: 'msapplication-TileColor' }],
google: [{ name: 'google-site-verification' }],
};
export function* tagsGenerator(meta, injectTo) {
for (const [key, $value] of Object.entries(meta)) {
if ($value == null) {
continue;
}
const value = $value.toString();
const type = key;
const generators = GENERATORS[type];
for (const generator of generators) {
if (typeof generator === 'function') {
yield Object.assign(Object.assign({}, generator(value)), { injectTo });
}
else {
yield {
tag: 'meta',
attrs: Object.assign(Object.assign({}, generator), { content: value }),
injectTo,
};
}
}
}
}