ShopBack Node.js Code Challenge
Sample Code
"use strict";
const fs = require("fs");
var shopback_seo_challenge = require("shopback_seo_challenge");
var seoParser = shopback_seo_challenge.seoParser;
var ruleManager = shopback_seo_challenge.ruleManager;
// Define SEO Rules:
seoParser.addSeoRule(new ruleManager.tagWithoutAttributeRule("img", "alt"));
seoParser.addSeoRule(new ruleManager.tagWithoutAttributeRule("a", "rel"));
seoParser.addSeoRule(new ruleManager.tagWithoutChildTagRule("head", "title"));
seoParser.addSeoRule(new ruleManager.tagWithoutChildTagAndAttributeRule("head", "meta", "name", "descriptions"));
seoParser.addSeoRule(new ruleManager.tagWithoutChildTagAndAttributeRule("head", "meta", "name", "keywords"));
seoParser.addSeoRule(new ruleManager.tagWithoutChildTagAndAttributeRule("head", "meta", "name", "robots"));
seoParser.addSeoRule(new ruleManager.tagExceedsMaxRula("strong", 15));
seoParser.addSeoRule(new ruleManager.tagExceedsMaxRula("H1", 1));
// 1. Set Input Format 2. Detect SEO defects 3. Set Output Format
(async function () {
// 1. Set Input Format: (1)loadFile() (2)loadStream()
//var html = seoParser.loadFile('./test_files/test.html');
//var html = seoParser.loadFile('./test_files/valid.html');
var html = seoParser.loadFile('./test_files/invalid.html');
//var html = await seoParser.loadStream(fs.createReadStream('./test_files/invalid.html'));
// 2. Get defect list by the defined SEO rules
var defects = seoParser.detectSeoDefect(html);
// 3. Set Output Format: (1)writeToConsole() (2)writeToFile() (3)writeToStream
seoParser.writeToConsole(defects);
//seoParser.writeToFile(defects, "./test_files/invalid.log");
//seoParser.writeToStream(defects, fs.createWriteStream("./test_files/invalidStream.log"));
})();
Sample Input
<!DOCTYPE html>
<head>
</head>
<body>
Test
<p>Hello World!</p>
<img src="" alt="alt1" />
<img src="" alt="alt2" />
<img src="" />
<a href="https://tw.buy.yahoo.com" rel="re11">Link1</a>
<a href="https://tw.buy.yahoo.com" rel='rel2'>Link2</a>
<a href="https://tw.buy.yahoo.com">Link3</a>
<strong>strong1</strong>
<strong>strong2</strong>
<strong>strong3</strong>
<strong>strong4</strong>
<strong>strong5</strong>
<strong>strong6</strong>
<strong>strong7</strong>
<strong>strong8</strong>
<strong>strong9</strong>
<strong>strong10</strong>
<strong>strong11</strong>
<strong>strong12</strong>
<strong>strong13</strong>
<strong>strong14</strong>
<strong>strong15</strong>
<strong>strong16</strong>
<h1>h1</h1>
<h1>h2</h1>
</body>
</html>
Sample Output
In this HTML, there are totally 8 SEO defects:
0. There are 1 <img> tag without alt attribute
1. There are 1 <a> tag without rel attribute
2. This HTML without <title> tag in <head> tag
3. This HTML without <meta name="descriptions"> tag in <head> tag
4. This HTML without <meta name="keywords"> tag in <head> tag
5. This HTML without <meta name="robots"> tag in <head> tag
6. This HTML have more than 15 <strong> tag
7. This HTML have more than 1 <H1> tag
Installation
npm i shopback_seo_challenge