# html-seo-lint

> SEO checker for HTML files

Latest version **0.2.1** (published 2019-04-22) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install html-seo-lint
pnpm add html-seo-lint
yarn add html-seo-lint
bun add html-seo-lint
```

Provides the command `html-seo-lint`.

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.2.1 |
| Published | 2019-04-22 |
| First published | 2019-04-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | ^8 \|\| ^9 \|\| ^10 \|\| ^11 |
| Dependencies | 11 |
| Unpacked size | 25.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | YEAM Wee Kian |
| Maintainers | ywk93 |
| Keywords | SEO, HTML, lint |

## Links

- npm: https://www.npmjs.com/package/html-seo-lint
- Repository: https://github.com/weekian/html-seo-lint
- Homepage: https://github.com/weekian/html-seo-lint#readme
- Issues: https://github.com/weekian/html-seo-lint/issues
- npm.io page: https://npm.io/package/html-seo-lint

## Dependencies (11)

- [ajv](https://npm.io/package/ajv.md) ^6.10.0
- [chalk](https://npm.io/package/chalk.md) ^2.4.2
- [debug](https://npm.io/package/debug.md) ^4.1.1
- [lodash](https://npm.io/package/lodash.md) ^4.17.11
- [js-yaml](https://npm.io/package/js-yaml.md) ^3.13.1
- [bluebird](https://npm.io/package/bluebird.md) ^3.5.4
- [domutils](https://npm.io/package/domutils.md) ^1.7.0
- [is-stream](https://npm.io/package/is-stream.md) ^1.1.0
- [domhandler](https://npm.io/package/domhandler.md) ^2.4.2
- [htmlparser2](https://npm.io/package/htmlparser2.md) ^3.10.1
- [ajv-keywords](https://npm.io/package/ajv-keywords.md) ^3.4.0

## Alternatives

- [eslint-plugin-sonarjs](https://npm.io/package/eslint-plugin-sonarjs.md) — 2.9M weekly downloads
- [eslint-config-expo](https://npm.io/package/eslint-config-expo.md) — 1.5M weekly downloads
- [@matter/protocol](https://npm.io/package/@matter/protocol.md) — 63.5K weekly downloads
- [@eventcatalog/linter](https://npm.io/package/@eventcatalog/linter.md) — 24.8K weekly downloads
- [@pandacss/eslint-plugin](https://npm.io/package/@pandacss/eslint-plugin.md) — 18.7K weekly downloads

## Recent versions

- 0.2.1 (latest) — 2019-04-22
- 0.2.0 — 2019-04-22
- 0.1.5 — 2019-04-21
- 0.1.4 — 2019-04-21
- 0.1.3 — 2019-04-19
- 0.1.2 — 2019-04-17
- 0.1.1 — 2019-04-17
- 0.1.0 — 2019-04-17

## README

# html-seo-lint

[![npm](https://img.shields.io/npm/v/html-seo-lint.svg)](https://www.npmjs.com/package/html-seo-lint)
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
[![Known Vulnerabilities](https://snyk.io/test/github/weekian/html-seo-lint/badge.svg?targetFile=package.json)](https://snyk.io/test/github/weekian/html-seo-lint?targetFile=package.json)
[![Build Status](https://travis-ci.org/weekian/html-seo-lint.svg?branch=master)](https://travis-ci.org/weekian/html-seo-lint)
[![Coverage Status](https://coveralls.io/repos/github/weekian/html-seo-lint/badge.svg?branch=master)](https://coveralls.io/github/weekian/html-seo-lint?branch=master)

A fully pluggable tool for identifying and reporting on SEO defects in HTML files

## Installation

```bash
npm install html-seo-lint
```

## Example

### 1. CLI


```bash
html-seo-lint sample.html
```


Contents of .htmlseolintrc.json

```javascript
[
  {
    "tag": "title",
    "type": "match"
  },
  {
    "tag": "h1",
    "type": "notMoreThan",
    "count": 1
  },
  {
    "tag": "img",
    "type": "missing",
    "where": [{ "key": "alt" }]
  }
]
```
To view the possible options, run

```bash
html-seo-lint --help
```

### 2. Require
Here is an example of reading from a file and printing the results of 3 rules to console via code

```javascript
const htmlSeoLint = require('html-seo-lint');

const rules = [
  {
    tag: 'title',
    type: 'match',
    ancestor: 'head'
  },
  {
    tag: 'img',
    type: 'missing',
    where: [
      {
        key: 'alt'
      }
    ]
  },
  {
    tag: 'h1',
    type: 'notMoreThan',
    count: 1
  }
];

htmlSeoLint.lint('sample.html', rules).then(() => {
  console.log('Done');
}).catch(err => {
  console.log('Something went wrong');
})
```

Console

```
This HTML does not have <title> tags within <head> ancestor tag
This HTML has 1 <img> tags that do not have alt=... attribute(s)
This HTML has 2 <h1> tags, more than the specified limit of 1
```
## Writing Rules

There are 3 types of rules available for usage

### 1. Match

A match rule checks if the given HTML contains an element that is of the correct tag (and **optionally** ancestor tag and attributes).

An error is displayed if the given HTML does not contain at least 1 element that fits the rule.

Below is the format of a single match rule

```javascript
{
  tag: '<tag to look for>', // required
  ancestor: '<ancestor tag to look for ^ tag in its children>', // optional
  type: 'match', // required
  where: [{ // optional
    key: '<attribute key>', // required
    value: '<attribute value' // optional
  },{
    key: '<attribute key>'
  }]
}
```

Below is a sample match rule

```javascript
{
  tag: 'title',
  type: 'match',
  ancestor: 'head'
}
```
In the example above, this match rule is looking for a title tag inside a head tag and displays an error if it cannot finds

### 2. Missing

A missing rule checks if the given html does not have any element that fits the given tag (and optionally if provided ancestor tag and attributes)

An error is displayed if there an element found in the HTML that fits the requirements

Below is the format of a missing rule

```javascript
{
  tag: '<tag to look for>', // required
  ancestor: '<ancestor tag to look for ^ tag in its children>', // optional
  type: 'missing', // required
  where: [{ // optional
    key: '<attribute key>', // required
    value: '<attribute value' // optional
  },{
    key: '<attribute key>'
  }]
}
```

Below is an example of a missing rule

```javascript
{
  tag: 'img',
  type: 'missing',
  where: [
    {
      key: 'alt'
    }
  ]
}
```

In the example above, this rule looks for any img tag in the HTML that does not have an alt attribute key and displays an error if it does find at least one

### 3. notMoreThan

A notMoreThan rule checks if the given HTML does not contain more than the specified number of elements that fit the given tag (and optionally ancestor tag and attributes)

An error is displayed if the number of elements that match exceeds the given limit

Below is the format for a notMoreThan rule

```javascript
{
  tag: '<tag to look for>', // required
  ancestor: '<ancestor tag to look for ^ tag in its children>', // optional
  type: 'notMoreThan', // required
  count: '<integer>' // required, >= 1
  where: [{ // optional
    key: '<attribute key>', // required
    value: '<attribute value' // optional
  },{
    key: '<attribute key>'
  }]
}
```

Below is an example of a notMoreThan rule

```javascript
{
  tag: 'h1',
  type: 'notMoreThan',
  count: 1
}
```

In the example above, this rule is looking for more than 1 h1 tag in given HTML and displays an error if there are more than 1 h1 tag found

## API

This module exposes a single method **lint(...)**

### ```lint(source, rules[, output, options])```

* ```source```  : String fileName or Readable stream
* ```rules```   : Array of rules, refer to writing rules section above for more details on how to write rules
* ```output```  : [optional] String fileName or Writable stream. If absent, prints to console (error)
* ```options``` : [optional] object with debug property { debug: true } to print debug statements to console

Returns a promise

## Test

### Running test

```bash
npm test
```

### Running test with coverage

```bash
npm run test:coverage
```

### Running test with debugging

```bash
npm run test:debug
```

## Running test while watching for file changes

```bash
npm run test:watch
```

Refer to package.json for other test-related scripts like inspecting during test running

## Good-to-have

1. Ancestor tag taking in a multiple ancestor tags (children/ancestor) and filtering by class/attributes
2. Improve error clarity, especially for rules-based validation
3. More tests for failure scenarios (e.g. passing a writable stream that has already closed)

---
_Source: https://npm.io/package/html-seo-lint · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
