websetup/ts/meta/index.ts

123 lines
4.0 KiB
TypeScript
Raw Normal View History

2020-03-06 15:12:23 +00:00
import * as plugins from '../websetup.plugins';
2019-03-27 13:38:24 +00:00
export interface IMetaObject {
title: string;
description: string;
2020-03-06 09:57:31 +00:00
canonicalDomain?: string;
2020-03-06 15:12:23 +00:00
ldCompany?: plugins.tsclass.business.ICompany;
ldProduct?: any;
2019-03-27 13:38:24 +00:00
}
export const setupMetaInformation = async (metaObjectArg: IMetaObject) => {
document.title = metaObjectArg.title;
addMetaTag('description', metaObjectArg.description);
addMetaTag('google', 'notranslate');
2020-03-07 16:08:21 +00:00
addMetaTag('revisit-after', '1 days');
2020-03-06 09:57:31 +00:00
metaObjectArg.canonicalDomain ? addLinkTag('canonical', metaObjectArg.canonicalDomain) : null;
2020-03-06 15:12:23 +00:00
if (metaObjectArg.ldCompany) {
2020-03-07 16:08:21 +00:00
addCompanyInfo(metaObjectArg.ldCompany);
2020-03-06 15:12:23 +00:00
}
2019-03-27 13:38:24 +00:00
};
2020-03-07 16:08:21 +00:00
const addMetaTag = async (metaNameArg: string, contentArg: string) => {
2019-03-27 13:38:24 +00:00
const metaElement = document.createElement('meta');
2020-03-07 16:08:21 +00:00
metaElement.name = metaNameArg;
2019-03-27 13:38:24 +00:00
metaElement.content = contentArg;
document.getElementsByTagName('head')[0].appendChild(metaElement);
};
2020-03-06 09:57:31 +00:00
2020-03-07 16:08:21 +00:00
const addLinkTag = async (relArg, hrefArg): Promise<Element> => {
2020-03-06 09:57:31 +00:00
const link = !!document.querySelector("link[rel='canonical']")
? document.querySelector("link[rel='canonical']")
: document.createElement('link');
link.setAttribute('rel', relArg);
link.setAttribute('href', hrefArg);
document.head.appendChild(link);
2020-03-07 16:08:21 +00:00
return link;
2020-03-06 09:57:31 +00:00
};
2020-03-06 15:12:23 +00:00
2020-03-07 16:08:21 +00:00
const addOpenGraphProperty = async (
propertyNameArg: string,
contentArg: string
): Promise<Element> => {
const openGraphElement = document.createElement('meta');
2020-03-08 10:54:12 +00:00
openGraphElement.setAttribute('property', propertyNameArg);
2020-03-07 16:08:21 +00:00
openGraphElement.content = contentArg;
document.getElementsByTagName('head')[0].appendChild(openGraphElement);
return openGraphElement;
};
const addCompanyInfo = async (
companyDataArg: plugins.tsclass.business.ICompany
): Promise<Element[]> => {
const returnElementArray: Element[] = [];
// lets care about linked data
2020-03-06 15:12:23 +00:00
const companyLd = {
2020-03-07 16:08:21 +00:00
'@context': 'https://schema.org',
'@type': 'Corporation',
name: companyDataArg.name,
alternateName: companyDataArg.name.replace(' GmbH', ''),
url: companyDataArg.contact.website,
logo: companyDataArg.contact.logoUrl,
contactPoint: {
'@type': 'ContactPoint',
telephone: companyDataArg.contact.phone,
contactType: 'customer service',
areaServed: 'DE',
2020-06-27 12:50:44 +00:00
availableLanguage: ['en', 'German'],
2020-03-06 15:12:23 +00:00
},
2020-06-27 12:50:44 +00:00
sameAs: [],
2020-03-06 15:12:23 +00:00
};
if (companyDataArg.contact.facebookUrl) {
companyLd.sameAs.push(companyDataArg.contact.facebookUrl);
}
if (companyDataArg.contact.twitterUrl) {
companyLd.sameAs.push(companyDataArg.contact.twitterUrl);
}
const jsonLdElement = document.createElement('script');
jsonLdElement.type = 'application/ld+json';
jsonLdElement.text = JSON.stringify(companyLd);
document.querySelector('head').appendChild(jsonLdElement);
2020-03-07 16:08:21 +00:00
returnElementArray.push(jsonLdElement);
// lets care about open graph
returnElementArray.push(await addOpenGraphProperty('og:type', 'business.business'));
returnElementArray.push(await addOpenGraphProperty('og:title', companyDataArg.name));
returnElementArray.push(await addOpenGraphProperty('og:url', companyDataArg.contact.website));
2020-03-07 16:23:12 +00:00
returnElementArray.push(await addOpenGraphProperty('og:image', companyDataArg.contact.logoUrl));
2020-03-07 16:08:21 +00:00
returnElementArray.push(
await addOpenGraphProperty(
'business:contact_data:street_address',
`${companyDataArg.contact.address.streetName} ${companyDataArg.contact.address.houseNumber}`
)
);
2020-03-15 19:11:14 +00:00
returnElementArray.push(
await addOpenGraphProperty(
'business:contact_data:locality',
companyDataArg.contact.address.postalCode
)
);
returnElementArray.push(
await addOpenGraphProperty('business:contact_data:region', companyDataArg.contact.address.city)
);
returnElementArray.push(
await addOpenGraphProperty(
'business:contact_data:postal_code',
companyDataArg.contact.address.postalCode
)
);
returnElementArray.push(
await addOpenGraphProperty(
'business:contact_data:country_name',
companyDataArg.contact.address.country
)
);
2020-03-07 16:08:21 +00:00
return returnElementArray;
2020-03-06 15:12:23 +00:00
};