remark-lint-first-heading-level
Advanced tools
Comparing version 2.0.1 to 3.0.0
107
index.js
@@ -11,11 +11,14 @@ /** | ||
* | ||
* @example {"name": "ok.md"} | ||
* @example | ||
* {"name": "ok.md"} | ||
* | ||
* # The default is to expect a level one heading | ||
* | ||
* @example {"name": "ok-html.md"} | ||
* @example | ||
* {"name": "ok-html.md"} | ||
* | ||
* <h1>An HTML heading is also seen by this rule.</h1> | ||
* | ||
* @example {"name": "ok-delayed.md"} | ||
* @example | ||
* {"name": "ok-delayed.md"} | ||
* | ||
@@ -28,3 +31,4 @@ * You can use markdown content before the heading. | ||
* | ||
* @example {"name": "not-ok.md", "label": "input"} | ||
* @example | ||
* {"name": "not-ok.md", "label": "input"} | ||
* | ||
@@ -35,7 +39,9 @@ * ## Bravo | ||
* | ||
* @example {"name": "not-ok.md", "label": "output"} | ||
* @example | ||
* {"name": "not-ok.md", "label": "output"} | ||
* | ||
* 1:1-1:9: First heading level should be `1` | ||
* | ||
* @example {"name": "not-ok-html.md", "label": "input"} | ||
* @example | ||
* {"name": "not-ok-html.md", "label": "input"} | ||
* | ||
@@ -46,7 +52,9 @@ * <h2>Charlie</h2> | ||
* | ||
* @example {"name": "not-ok-html.md", "label": "output"} | ||
* @example | ||
* {"name": "not-ok-html.md", "label": "output"} | ||
* | ||
* 1:1-1:17: First heading level should be `1` | ||
* | ||
* @example {"name": "ok.md", "setting": 2} | ||
* @example | ||
* {"name": "ok.md", "setting": 2} | ||
* | ||
@@ -57,3 +65,4 @@ * ## Delta | ||
* | ||
* @example {"name": "ok-html.md", "setting": 2} | ||
* @example | ||
* {"name": "ok-html.md", "setting": 2} | ||
* | ||
@@ -64,3 +73,4 @@ * <h2>Echo</h2> | ||
* | ||
* @example {"name": "not-ok.md", "setting": 2, "label": "input"} | ||
* @example | ||
* {"name": "not-ok.md", "setting": 2, "label": "input"} | ||
* | ||
@@ -71,7 +81,9 @@ * # Foxtrot | ||
* | ||
* @example {"name": "not-ok.md", "setting": 2, "label": "output"} | ||
* @example | ||
* {"name": "not-ok.md", "setting": 2, "label": "output"} | ||
* | ||
* 1:1-1:10: First heading level should be `2` | ||
* | ||
* @example {"name": "not-ok-html.md", "setting": 2, "label": "input"} | ||
* @example | ||
* {"name": "not-ok-html.md", "setting": 2, "label": "input"} | ||
* | ||
@@ -82,3 +94,4 @@ * <h1>Golf</h1> | ||
* | ||
* @example {"name": "not-ok-html.md", "setting": 2, "label": "output"} | ||
* @example | ||
* {"name": "not-ok-html.md", "setting": 2, "label": "output"} | ||
* | ||
@@ -88,44 +101,52 @@ * 1:1-1:14: First heading level should be `2` | ||
'use strict' | ||
/** | ||
* @typedef {import('mdast').Root} Root | ||
* @typedef {import('mdast').HTML} HTML | ||
* @typedef {import('mdast').Heading['depth']} Depth | ||
* @typedef {Depth} Options | ||
*/ | ||
var rule = require('unified-lint-rule') | ||
var visit = require('unist-util-visit') | ||
var generated = require('unist-util-generated') | ||
import {lintRule} from 'unified-lint-rule' | ||
import {visit, EXIT} from 'unist-util-visit' | ||
import {generated} from 'unist-util-generated' | ||
module.exports = rule('remark-lint:first-heading-level', firstHeadingLevel) | ||
const re = /<h([1-6])/ | ||
var re = /<h([1-6])/ | ||
const remarkLintFirstHeadingLevel = lintRule( | ||
'remark-lint:first-heading-level', | ||
/** @type {import('unified-lint-rule').Rule<Root, Options>} */ | ||
(tree, file, option = 1) => { | ||
visit(tree, (node) => { | ||
if (!generated(node)) { | ||
/** @type {Depth|undefined} */ | ||
let rank | ||
function firstHeadingLevel(tree, file, option) { | ||
var preferred = option && option !== true ? option : 1 | ||
if (node.type === 'heading') { | ||
rank = node.depth | ||
} else if (node.type === 'html') { | ||
rank = infer(node) | ||
} | ||
visit(tree, visitor) | ||
if (rank !== undefined) { | ||
if (rank !== option) { | ||
file.message('First heading level should be `' + option + '`', node) | ||
} | ||
function visitor(node) { | ||
var rank | ||
if (!generated(node)) { | ||
if (node.type === 'heading') { | ||
rank = node.depth | ||
} else if (node.type === 'html') { | ||
rank = infer(node) | ||
} | ||
if (rank !== undefined) { | ||
if (rank !== preferred) { | ||
file.message( | ||
'First heading level should be `' + preferred + '`', | ||
node | ||
) | ||
return EXIT | ||
} | ||
return visit.EXIT | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
) | ||
export default remarkLintFirstHeadingLevel | ||
/** | ||
* @param {HTML} node | ||
* @returns {Depth|undefined} | ||
*/ | ||
function infer(node) { | ||
var results = node.value.match(re) | ||
const results = node.value.match(re) | ||
// @ts-expect-error: can be castes fine. | ||
return results ? Number(results[1]) : undefined | ||
} |
{ | ||
"name": "remark-lint-first-heading-level", | ||
"version": "2.0.1", | ||
"version": "3.0.0", | ||
"description": "remark-lint rule to warn when the first heading has a level other than a specified value", | ||
@@ -26,11 +26,27 @@ "license": "MIT", | ||
], | ||
"sideEffects": false, | ||
"type": "module", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"files": [ | ||
"index.d.ts", | ||
"index.js" | ||
], | ||
"dependencies": { | ||
"unified-lint-rule": "^1.0.0", | ||
"unist-util-generated": "^1.1.0", | ||
"unist-util-visit": "^2.0.0" | ||
"@types/mdast": "^3.0.0", | ||
"unified": "^10.0.0", | ||
"unified-lint-rule": "^2.0.0", | ||
"unist-util-generated": "^2.0.0", | ||
"unist-util-visit": "^4.0.0" | ||
}, | ||
"xo": false | ||
"scripts": { | ||
"build": "rimraf \"*.d.ts\" && tsc && type-coverage" | ||
}, | ||
"xo": false, | ||
"typeCoverage": { | ||
"atLeast": 100, | ||
"detail": true, | ||
"strict": true, | ||
"ignoreCatch": true | ||
} | ||
} |
@@ -165,2 +165,5 @@ <!--This file is generated--> | ||
This package is [ESM only][esm]: | ||
Node 12+ is needed to use it and it must be `imported`ed instead of `required`d. | ||
[npm][]: | ||
@@ -172,2 +175,5 @@ | ||
This package exports no identifiers. | ||
The default export is `remarkLintFirstHeadingLevel`. | ||
## Use | ||
@@ -199,10 +205,13 @@ | ||
```diff | ||
var remark = require('remark') | ||
var report = require('vfile-reporter') | ||
import {remark} from 'remark' | ||
import {reporter} from 'vfile-reporter' | ||
import remarkLint from 'remark-lint' | ||
import remarkLintFirstHeadingLevel from 'remark-lint-first-heading-level' | ||
remark() | ||
.use(require('remark-lint')) | ||
+ .use(require('remark-lint-first-heading-level')) | ||
.process('_Emphasis_ and **importance**', function (err, file) { | ||
console.error(report(err || file)) | ||
.use(remarkLint) | ||
+ .use(remarkLintFirstHeadingLevel) | ||
.process('_Emphasis_ and **importance**') | ||
.then((file) => { | ||
console.error(reporter(file)) | ||
}) | ||
@@ -225,5 +234,5 @@ ``` | ||
[build-badge]: https://img.shields.io/travis/remarkjs/remark-lint/main.svg | ||
[build-badge]: https://github.com/remarkjs/remark-lint/workflows/main/badge.svg | ||
[build]: https://travis-ci.org/remarkjs/remark-lint | ||
[build]: https://github.com/remarkjs/remark-lint/actions | ||
@@ -248,6 +257,8 @@ [coverage-badge]: https://img.shields.io/codecov/c/github/remarkjs/remark-lint.svg | ||
[chat-badge]: https://img.shields.io/badge/chat-spectrum.svg | ||
[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg | ||
[chat]: https://spectrum.chat/unified/remark | ||
[chat]: https://github.com/remarkjs/remark/discussions | ||
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c | ||
[npm]: https://docs.npmjs.com/cli/install | ||
@@ -254,0 +265,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9605
4
161
272
Yes
5
+ Added@types/mdast@^3.0.0
+ Addedunified@^10.0.0
+ Added@types/mdast@3.0.15(transitive)
+ Addedbail@2.0.2(transitive)
+ Addedextend@3.0.2(transitive)
+ Addedis-buffer@2.0.5(transitive)
+ Addedis-plain-obj@4.1.0(transitive)
+ Addedtrough@2.2.0(transitive)
+ Addedunified@10.1.2(transitive)
+ Addedunified-lint-rule@2.1.2(transitive)
+ Addedunist-util-generated@2.0.1(transitive)
+ Addedunist-util-is@5.2.1(transitive)
+ Addedunist-util-stringify-position@3.0.3(transitive)
+ Addedunist-util-visit@4.1.2(transitive)
+ Addedunist-util-visit-parents@5.1.3(transitive)
+ Addedvfile@5.3.7(transitive)
+ Addedvfile-message@3.1.4(transitive)
- Removedco@3.1.0(transitive)
- Removedsliced@1.0.1(transitive)
- Removedunified-lint-rule@1.0.6(transitive)
- Removedunist-util-generated@1.1.6(transitive)
- Removedunist-util-is@4.1.0(transitive)
- Removedunist-util-visit@2.0.3(transitive)
- Removedunist-util-visit-parents@3.1.1(transitive)
- Removedwrapped@1.0.1(transitive)
Updatedunified-lint-rule@^2.0.0
Updatedunist-util-generated@^2.0.0
Updatedunist-util-visit@^4.0.0