Skip to content

Commit

Permalink
removed setProjectLogo method and it's function improved and moved in…
Browse files Browse the repository at this point in the history
…side setProjectDetails
  • Loading branch information
muratgozel committed Jul 17, 2024
1 parent 92b17a2 commit 35ec11c
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 57 deletions.
61 changes: 45 additions & 16 deletions dist/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,6 @@ class Metapatcher {
this.setProjectUrl(params.url);
if (params.robots)
this.setRobots(params.robots);
if (params.logo && params.url)
this.setProjectLogo(params.logo, params.url);
if (params.themeColor)
this.setThemeColor(params.themeColor);
if (params.twitterSite)
Expand All @@ -186,6 +184,50 @@ class Metapatcher {
this.setSafariPinnedTab(params.safariPinnedTab);
if (params.icons)
this.setIcons(params.icons);
if (this.features.includes("structuredData")) {
const id = this.idPrefix + "-project-org";
const json = {
"@context": "https://schema.org",
"@type": "Organization"
};
if (params.logo)
json["logo"] = params.logo;
if (params.url)
json["url"] = params.url;
if (params.name)
json["name"] = params.name;
if (params.email)
json["email"] = params.email;
if (params.phone)
json["telephone"] = params.phone;
if (params.description)
json["description"] = params.description;
if (params.image)
json["image"] = params.image;
if (params.legalName)
json["legalName"] = params.legalName;
if (params.address) {
json["address"] = {
"@type": "PostalAddress"
};
if (params.address.country)
json["address"]["addressCountry"] = params.address.country;
if (params.address.region)
json["address"]["addressRegion"] = params.address.region;
if (params.address.city)
json["address"]["addressLocality"] = params.address.city;
if (params.address.postalCode)
json["address"]["postalCode"] = params.address.postalCode;
if (params.address.street)
json["address"]["streetAddress"] = params.address.street;
}
const _data = JSON.stringify(json);
this.removeOne("script", { id });
if (this.isDomAvailable)
this.setJsonLdDom(id, _data);
else
this.setJsonLdMemory(id, _data);
}
return this;
}
setProjectName(name) {
Expand All @@ -211,20 +253,6 @@ class Metapatcher {
}
return this;
}
setProjectLogo(logo, url) {
if (this.features.includes("structuredData")) {
const id = this.idPrefix + "-project-logo";
const json = {
"@type": "Organization",
logo,
url
};
const _data = JSON.stringify(json);
this.removeOne("script", { id });
return this.isDomAvailable ? this.setJsonLdDom(id, _data) : this.setJsonLdMemory(id, _data);
}
return "";
}
setThemeColor(colorHexCode) {
const id = this.idPrefix + "-theme-color";
this.removeOne("meta", { id });
Expand Down Expand Up @@ -327,6 +355,7 @@ class Metapatcher {
return "";
const id = this.idPrefix + "-breadcrumb";
const json = {
"@context": "https://schema.org",
"@type": "BreadcrumbList",
"itemListElement": data.map(({ title, url }, ind) => ({
"@type": "ListItem",
Expand Down
2 changes: 1 addition & 1 deletion dist/index.cjs.map

Large diffs are not rendered by default.

13 changes: 12 additions & 1 deletion dist/index.d.cts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ declare class Metapatcher {
setProjectDetails(params: MetapatcherProjectParams): this;
setProjectName(name: string): this;
setProjectUrl(url: string): this;
setProjectLogo(logo: string, url: string): HTMLScriptElement | string;
setThemeColor(colorHexCode: string): this;
setTwitterSite(username: string): this;
addDnsPrefetch(param: string | MetapatcherDnsPrefetchAttrs): this;
Expand Down Expand Up @@ -190,6 +189,18 @@ interface MetapatcherProjectParams {
twitterSite?: string;
safariPinnedTab?: MetapatcherSafariPinnedTabAttrs;
icons?: string[];
email?: string;
phone?: string;
description?: string;
image?: string;
legalName?: string;
address?: {
country: string;
region?: string;
city?: string;
postalCode?: string;
street?: string;
};
}
interface MetapatcherMsApplicationConfigAttrs {
readonly name?: 'msapplication-config';
Expand Down
13 changes: 12 additions & 1 deletion dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ declare class Metapatcher {
setProjectDetails(params: MetapatcherProjectParams): this;
setProjectName(name: string): this;
setProjectUrl(url: string): this;
setProjectLogo(logo: string, url: string): HTMLScriptElement | string;
setThemeColor(colorHexCode: string): this;
setTwitterSite(username: string): this;
addDnsPrefetch(param: string | MetapatcherDnsPrefetchAttrs): this;
Expand Down Expand Up @@ -190,6 +189,18 @@ interface MetapatcherProjectParams {
twitterSite?: string;
safariPinnedTab?: MetapatcherSafariPinnedTabAttrs;
icons?: string[];
email?: string;
phone?: string;
description?: string;
image?: string;
legalName?: string;
address?: {
country: string;
region?: string;
city?: string;
postalCode?: string;
street?: string;
};
}
interface MetapatcherMsApplicationConfigAttrs {
readonly name?: 'msapplication-config';
Expand Down
61 changes: 45 additions & 16 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "metapatcher",
"version": "v4.3.3",
"version": "v4.4.0",
"description": "HTML document head management library with convenient api. Manage social media tags, icons, device specific tags and event structured data with html meta tags and JSONLD documents.",
"type": "module",
"main": "./dist/index.js",
Expand Down
2 changes: 1 addition & 1 deletion src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ test('set breadcrumb', () => {
const b = [{ title: 'abc', url: '/abc' }, { title: 'def', url: '/abc/def' }]
metapatcher.setBreadcrumb(b)
const data = document.head.outerHTML
expect(data).toEqual(expect.stringContaining('<script id="metapatcher-breadcrumb" type="application/ld+json">{"@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"abc","item":"/abc"},{"@type":"ListItem","position":2,"name":"def","item":"/abc/def"}]}</script>'))
expect(data).toEqual(expect.stringContaining('<script id="metapatcher-breadcrumb" type="application/ld+json">{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"abc","item":"/abc"},{"@type":"ListItem","position":2,"name":"def","item":"/abc/def"}]}</script>'))
})

test('set safari pinned tab', () => {
Expand Down
63 changes: 44 additions & 19 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,42 @@ export class Metapatcher {
if (params.name) this.setProjectName(params.name)
if (params.url) this.setProjectUrl(params.url)
if (params.robots) this.setRobots(params.robots)
if (params.logo && params.url) this.setProjectLogo(params.logo, params.url)
if (params.themeColor) this.setThemeColor(params.themeColor)
if (params.twitterSite) this.setTwitterSite(params.twitterSite)
if (params.safariPinnedTab) this.setSafariPinnedTab(params.safariPinnedTab)
if (params.icons) this.setIcons(params.icons)

if (this.features.includes('structuredData')) {
const id = this.idPrefix + '-project-org'
const json: Record<string, string | Record<string, string>> = {
'@context': 'https://schema.org',
'@type': 'Organization'
}
if (params.logo) json['logo'] = params.logo
if (params.url) json['url'] = params.url
if (params.name) json['name'] = params.name
if (params.email) json['email'] = params.email
if (params.phone) json['telephone'] = params.phone
if (params.description) json['description'] = params.description
if (params.image) json['image'] = params.image
if (params.legalName) json['legalName'] = params.legalName
if (params.address) {
json['address'] = {
'@type': 'PostalAddress'
}
if (params.address.country) json['address']['addressCountry'] = params.address.country
if (params.address.region) json['address']['addressRegion'] = params.address.region
if (params.address.city) json['address']['addressLocality'] = params.address.city
if (params.address.postalCode) json['address']['postalCode'] = params.address.postalCode
if (params.address.street) json['address']['streetAddress'] = params.address.street
}
const _data = JSON.stringify(json)

this.removeOne('script', { id: id })
if (this.isDomAvailable) this.setJsonLdDom(id, _data)
else this.setJsonLdMemory(id, _data)
}

return this
}

Expand Down Expand Up @@ -234,24 +264,6 @@ export class Metapatcher {
return this
}

setProjectLogo (logo: string, url: string): HTMLScriptElement | string {
if (this.features.includes('structuredData')) {
const id = this.idPrefix + '-project-logo'
const json = {
'@type': 'Organization',
logo: logo,
url: url
}
const _data = JSON.stringify(json)

this.removeOne('script', { id: id })

return this.isDomAvailable ? this.setJsonLdDom(id, _data) : this.setJsonLdMemory(id, _data)
}

return ''
}

setThemeColor (colorHexCode: string): this {
const id = this.idPrefix + '-theme-color'
this.removeOne('meta', { id: id })
Expand Down Expand Up @@ -383,6 +395,7 @@ export class Metapatcher {

const id = this.idPrefix + '-breadcrumb'
const json = {
'@context': 'https://schema.org',
'@type': 'BreadcrumbList',
'itemListElement': data.map(({title, url}, ind) => ({
'@type': 'ListItem',
Expand Down Expand Up @@ -822,6 +835,18 @@ export interface MetapatcherProjectParams {
twitterSite?: string
safariPinnedTab?: MetapatcherSafariPinnedTabAttrs
icons?: string[]
email?: string
phone?: string
description?: string
image?: string
legalName?: string
address?: {
country: string
region?: string
city?: string
postalCode?: string
street?: string
}
}

export interface MetapatcherMsApplicationConfigAttrs {
Expand Down

0 comments on commit 35ec11c

Please sign in to comment.