-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Matej Jellus
authored and
Matej Jellus
committed
Jun 21, 2023
1 parent
7dfb0cd
commit 9253baf
Showing
11 changed files
with
241 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { URL } from 'url'; | ||
import getObject from './getObject'; | ||
|
||
const getHeading = (result: any): string | string[] => { | ||
const titles = getObject(result.html, 'name', 'h1') | ||
.map((title: any) => { | ||
return title.children[0].data; | ||
}); | ||
|
||
return titles.length > 1 ? titles : titles.shift(); | ||
}; | ||
|
||
export default getHeading; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { URL } from 'url'; | ||
import getObject from './getObject'; | ||
|
||
const getTitle = (result: any): string | string[] => { | ||
const titles = getObject(result.html, 'name', 'title') | ||
.map((title: any) => { | ||
return title.children[0].data; | ||
}); | ||
|
||
return titles.length > 1 ? titles : titles.shift(); | ||
}; | ||
|
||
export default getTitle; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import Test, { TestParameters, Result } from '../Test'; | ||
import request from '../request'; | ||
import logger from '../logger'; | ||
import { getHeading, parseHtml } from '../functions'; | ||
|
||
class Heading extends Test { | ||
public name = 'Heading'; | ||
|
||
public async test({ url }: TestParameters): Promise<Result> { | ||
logger.info(`Starting ${this.constructor.name} test...`); | ||
|
||
const response = await request.get(url); | ||
const html = await parseHtml(response); | ||
const heading = getHeading(html); | ||
const subTests = this.checkHeading(heading); | ||
|
||
return { | ||
status: this.getStatus(subTests.map(test => test.status)), | ||
title: this.constructor.name, | ||
description: '', | ||
results: subTests, | ||
}; | ||
} | ||
|
||
private checkHeading(title: string | string[]): Array<Result> { | ||
const results = []; | ||
|
||
results.push({ | ||
status: typeof title !== undefined && title.length > 0 ? 'SUCCESS' : 'WARNING', | ||
title: 'H1 tag', | ||
}); | ||
|
||
results.push({ | ||
status: Array.isArray(title) ? 'ERROR' : 'SUCCESS', | ||
title: 'Duplicate H1 tag', | ||
description: `HTML should contain just one title tag.`, | ||
}); | ||
|
||
results.push({ | ||
status: title.length <= 60 ? 'SUCCESS' : 'WARNING', | ||
title: 'Title length', | ||
description: `Title length should be under 60 characters and it is ${title.length}.`, | ||
}); | ||
|
||
return results; | ||
} | ||
} | ||
|
||
export default Heading; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import Test, { TestParameters, Result } from '../Test'; | ||
import request from '../request'; | ||
import logger from '../logger'; | ||
|
||
class Robots extends Test { | ||
public name = 'Robots'; | ||
|
||
public async test({ url }: TestParameters): Promise<Result> { | ||
logger.info(`Starting ${this.constructor.name} test...`); | ||
|
||
const response = await request.get(`${url}/robots.txt`); | ||
|
||
return { | ||
status: Math.floor(response.statusCode / 100) === 2 ? 'SUCCESS' : 'WARNING', | ||
title: 'Robots.txt', | ||
description: '', | ||
}; | ||
} | ||
} | ||
|
||
export default Robots; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import Test, { TestParameters, Result } from '../Test'; | ||
import request from '../request'; | ||
import logger from '../logger'; | ||
import { parseXml } from '../functions'; | ||
|
||
class Sitemap extends Test { | ||
public name = 'Sitemap'; | ||
|
||
public async test({ url }: TestParameters): Promise<Result> { | ||
logger.info(`Starting ${this.constructor.name} test...`); | ||
|
||
const robotsResponse = await request.get(`${url}/robots.txt`); | ||
|
||
let sitemapUrl = `${url}/sitemap.xml`; | ||
|
||
if (Math.floor(robotsResponse.statusCode / 100) === 2) { | ||
const lines = robotsResponse.body.split(/\r?\n/); | ||
|
||
const sitemap = lines.find(line => line.startsWith('Sitemap')); | ||
|
||
if (typeof sitemap !== 'undefined') { | ||
sitemapUrl = sitemap.split(' ')[1]; | ||
} | ||
} | ||
|
||
const response = await request.get(sitemapUrl); | ||
const xml = await parseXml(response) as object; | ||
|
||
return { | ||
status: 'sitemapindex' in xml || 'urlset' in xml ? 'SUCCESS' : 'WARNING', | ||
title: this.constructor.name, | ||
description: '', | ||
}; | ||
} | ||
} | ||
|
||
export default Sitemap; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import Test, { TestParameters, Result } from '../Test'; | ||
import request from '../request'; | ||
import logger from '../logger'; | ||
import { getTitle, parseHtml } from '../functions'; | ||
|
||
class Title extends Test { | ||
public name = 'Title'; | ||
|
||
public async test({ url }: TestParameters): Promise<Result> { | ||
logger.info(`Starting ${this.constructor.name} test...`); | ||
|
||
const response = await request.get(url); | ||
const html = await parseHtml(response); | ||
const title = getTitle(html); | ||
const subTests = this.checkTitle(title); | ||
|
||
return { | ||
status: this.getStatus(subTests.map(test => test.status)), | ||
title: this.constructor.name, | ||
description: '', | ||
results: subTests, | ||
}; | ||
} | ||
|
||
private checkTitle(title: string | string[]): Array<Result> { | ||
const results = []; | ||
|
||
results.push({ | ||
status: typeof title !== undefined && title.length > 0 ? 'SUCCESS' : 'WARNING', | ||
title: 'Title tag', | ||
}); | ||
|
||
results.push({ | ||
status: Array.isArray(title) ? 'ERROR' : 'SUCCESS', | ||
title: 'Duplicate title tag', | ||
description: `HTML should contain just one title tag.`, | ||
}); | ||
|
||
results.push({ | ||
status: title.length <= 60 ? 'SUCCESS' : 'WARNING', | ||
title: 'Title length', | ||
description: `Title length should be under 60 characters and it is ${title.length}.`, | ||
}); | ||
|
||
return results; | ||
} | ||
} | ||
|
||
export default Title; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import Test, { TestParameters, Result } from '../Test'; | ||
import Title from './Title'; | ||
import Heading from './Heading'; | ||
import Sitemap from './Sitemap'; | ||
import Robots from './Robots'; | ||
|
||
export default class SEO extends Test { | ||
public name = 'SEO'; | ||
|
||
constructor() { | ||
super(); | ||
this.tests = [ | ||
new Title(), | ||
new Heading(), | ||
new Sitemap(), | ||
new Robots(), | ||
]; | ||
} | ||
|
||
public async test(params: TestParameters): Promise<Result> { | ||
const tests = this.getTests(); | ||
const results = []; | ||
|
||
for (const test of tests) { | ||
let result = null; | ||
|
||
try { | ||
result = await test.run(params); | ||
} catch (error) { | ||
result = { | ||
status: 'ERROR', | ||
title: test.name, | ||
description: 'Test failed or cannot be run!', | ||
} | ||
} | ||
|
||
results.push(result); | ||
} | ||
|
||
return { | ||
status: this.getStatus(results.map(result => result.status)), | ||
title: this.name, | ||
description: '', | ||
results, | ||
}; | ||
} | ||
} |