项目作者: doha99

项目描述 :
Lightweight module for checking SEO HTML defect elements.
高级语言: JavaScript
项目地址: git://github.com/doha99/seo-html-defect-checker.git
创建时间: 2018-03-12T07:28:18Z
项目社区:https://github.com/doha99/seo-html-defect-checker

开源协议:MIT License

下载


SEO HTML defect checker

Lightweight module for checking SEO HTML defect elements.

Built for

Status

Build Status

Installation

This is node.js libarary. Install nodejs first, then:

npm install seo-html-defect-checker

or

yarn add seo-html-defect-checker

How to use?

There are 7 defined rules that you can use to validate HTML.

  1. Detect if there are any <img /> tags without alt attribute
  2. Detect if there are any <a ></a> tags without rel attribute
  3. In tag
  • Detect if there is any header that doesn’t have <title> tag
  • Detect if there is any header that doesn’t have <meta name=“descriptions” … /> tag
  • Detect if there is any header that doesn’t have <meta name=“keywords” … /> tag
  1. Detect if there are more than 15 <strong> tag in HTML (15 is a value should be configurable by user)
  2. Detect if a HTML have more than one <H1> tag.

Rules object as follows:

  1. Rules = {
  2. definedRules : {
  3. aTagWithoutRel,
  4. imgTagWithoutAlt,
  5. dontHaveTitle,
  6. dontHaveMetaDescription,
  7. dontHaveMetaKeywords,
  8. moreThan15StrongTag,
  9. moreThan1H1Tag,
  10. }
  11. }

For example:

  1. const {Checker, Rules} = require('seo-html-defect-checker')
  2. const c = new Checker(htmlText)
  3. c.check([Rules.definedRules.aTagWithoutRel], (results) => {
  4. // manipulate results
  5. })

click here to test it

There is a defined rule list that include 7 rules as follows example:

  1. const {Checker, Rules} = require('seo-html-defect-checker')
  2. const htmlText = loadHtmlFunction() // function return string
  3. const c = new Checker(htmlText)
  4. c.check(Rules.definedRules.defaultRuleList, (results) => { // results is array
  5. // manipulate results
  6. })

And you can customize the rules list such as

  1. const {Checker, Rules} = require('seo-html-defect-checker')
  2. const htmlText = loadHtmlFunction() // function return string
  3. const c = new Checker(htmlText)
  4. const myRules = [
  5. Rules.definedRules.aTagWithoutRel,
  6. Rules.definedRules.dontHaveMetaDescription
  7. ]
  8. c.check(myRules, results => {
  9. console.log(results)
  10. })

click here to test it

Beside that you can customize the rule throw 4 objects:

  1. Rules = {
  2. //....
  3. MissAttributeRule, // check tags that don't have some specific attributes
  4. MissTagRule, // check HTML without tags which have some specific attributes (or only just tags)
  5. MoreTagThanRule, // check there is more than a specific number of tags which occur in HTML
  6. CustomRule, // custom whatever rule you want
  7. }

Examples:

  1. const {MissAttributeRule, MissTag, MoreTagThan} = Rules
  2. const r1 = new MissAttributeRule('img', {
  3. alt: null,
  4. })
  5. const r2 = new MissTag('title', {}, 'head >')
  6. const r3 = new MoreTagThan('strong', {}, null, 5)
  7. const rules = [r1, r2, r3]
  8. const c = new Checker(htmlText)
  9. c.check(rules, results => {
  10. // manipulate results
  11. })
  1. const {CustomRule} = Rules
  2. const r4 = new CustomRule('meta', {
  3. 'keywords*': 'shopping'
  4. }, 'head >', (dom, selector) => {
  5. var num = dom.count(selector)
  6. if (num == 0) {
  7. return 'miss shopping keyword'
  8. }
  9. return null
  10. })
  11. const c = new Checker(htmlText)
  12. c.check([r4], results => {
  13. // manipulate results
  14. })

And you can customize your selector query

  1. const r5 = new CustomRule('p', {}, null, (dom, selector) => {
  2. var num = dom.count('p.highlight:not(.warning)')
  3. if (num == 0) {
  4. return `there are ${num} p tags with hightlight class and without warning class`
  5. }
  6. return null
  7. })
  8. const c = new Checker(htmlText)
  9. c.check([r4], results => {
  10. // manipulate results
  11. })

API


Checker

- constructor(input, output)
input is text or stream reader output is file path or stream writer or console
- check(rules, cb)
rules is an array list, contains rule object cb is callback function with first parameter is results (array) of check



Rules.CustomRule

- constructor(tag, attributes, prefix, cb)
- check(domQuery)


Rules.MissAttributeRule

- constructor(tag, attributes, prefix)
- check(domQuery)


Rules.MissTag

- constructor(tag, attributes, prefix)
- check(domQuery)


Rules.MoreTagThan

- constructor(tag, attributes, prefix, specificNumber)
- check(domQuery)

Features

  • Detect HTML element defect by the defined rules
  • Can add customize rule flexibility
  • Support i18n

Contribute

Let people know how they can contribute into your project.

License

A short snippet describing the license (MIT, Apache etc)

MIT © DoHa