remark-lint-ordered-list-marker-value
Advanced tools
Comparing version 1.0.1 to 1.0.2
111
index.js
@@ -17,2 +17,13 @@ /** | ||
* | ||
* ## Fix | ||
* | ||
* [`remark-stringify`](https://github.com/remarkjs/remark/tree/master/packages/remark-stringify) | ||
* retains the number of the first list-item bullet, and by default | ||
* increments the other items. Pass | ||
* [`incrementListMarker: false`](https://github.com/remarkjs/remark/tree/master/packages/remark-stringify#optionsincrementlistmarker) | ||
* to not increment further list-items. | ||
* | ||
* See [Using remark to fix your markdown](https://github.com/remarkjs/remark-lint#using-remark-to-fix-your-markdown) | ||
* on how to automatically fix warnings for this rule. | ||
* | ||
* @example {"name": "valid.md"} | ||
@@ -82,2 +93,11 @@ * | ||
* | ||
* @example {"name": "also-invalid.md", "setting": "one", "label": "input"} | ||
* | ||
* 2. Foo | ||
* 1. Bar | ||
* | ||
* @example {"name": "also-invalid.md", "setting": "one", "label": "output"} | ||
* | ||
* 1:1-1:8: Marker should be `1`, was `2` | ||
* | ||
* @example {"name": "invalid.md", "setting": "ordered", "label": "input"} | ||
@@ -97,50 +117,47 @@ * | ||
'use strict'; | ||
'use strict' | ||
var rule = require('unified-lint-rule'); | ||
var visit = require('unist-util-visit'); | ||
var position = require('unist-util-position'); | ||
var generated = require('unist-util-generated'); | ||
var rule = require('unified-lint-rule') | ||
var visit = require('unist-util-visit') | ||
var position = require('unist-util-position') | ||
var generated = require('unist-util-generated') | ||
module.exports = rule('remark-lint:ordered-list-marker-value', orderedListMarkerValue); | ||
module.exports = rule( | ||
'remark-lint:ordered-list-marker-value', | ||
orderedListMarkerValue | ||
) | ||
var start = position.start; | ||
var start = position.start | ||
var STYLES = { | ||
ordered: true, | ||
single: true, | ||
one: true | ||
}; | ||
var styles = {ordered: true, single: true, one: true} | ||
function orderedListMarkerValue(ast, file, preferred) { | ||
var contents = file.toString(); | ||
function orderedListMarkerValue(tree, file, pref) { | ||
var contents = String(file) | ||
preferred = typeof preferred === 'string' ? preferred : 'ordered'; | ||
pref = typeof pref === 'string' ? pref : 'ordered' | ||
if (STYLES[preferred] !== true) { | ||
file.fail('Invalid ordered list-item marker value `' + preferred + '`: use either `\'ordered\'` or `\'one\'`'); | ||
if (styles[pref] !== true) { | ||
file.fail( | ||
'Invalid ordered list-item marker value `' + | ||
pref + | ||
"`: use either `'ordered'` or `'one'`" | ||
) | ||
} | ||
visit(ast, 'list', visitor); | ||
visit(tree, 'list', visitor) | ||
function visitor(node) { | ||
var items = node.children; | ||
var shouldBe = (preferred === 'one' ? 1 : node.start) || 1; | ||
var children = node.children | ||
var shouldBe = (pref === 'one' ? 1 : node.start) || 1 | ||
var length = node.ordered ? children.length : 0 | ||
var index = -1 | ||
var child | ||
var marker | ||
/* Ignore unordered lists. */ | ||
if (!node.ordered) { | ||
return; | ||
} | ||
while (++index < length) { | ||
child = children[index] | ||
items.forEach(each); | ||
function each(item, index) { | ||
var head = item.children[0]; | ||
var initial = start(item).offset; | ||
var final = start(head).offset; | ||
var marker; | ||
/* Ignore first list item. */ | ||
if (index === 0) { | ||
return; | ||
/* Ignore generated nodes, first items. */ | ||
if (generated(child) || (index === 0 && pref !== 'one')) { | ||
continue | ||
} | ||
@@ -150,18 +167,18 @@ | ||
* `ordered` mode. */ | ||
if (preferred === 'ordered') { | ||
shouldBe++; | ||
if (pref === 'ordered') { | ||
shouldBe++ | ||
} | ||
/* Ignore generated nodes. */ | ||
if (generated(item)) { | ||
return; | ||
} | ||
marker = Number( | ||
contents | ||
.slice(start(child).offset, start(child.children[0]).offset) | ||
.replace(/[\s.)]/g, '') | ||
.replace(/\[[x ]?]\s*$/i, '') | ||
) | ||
marker = contents.slice(initial, final).replace(/[\s.)]/g, ''); | ||
/* Support checkboxes. */ | ||
marker = Number(marker.replace(/\[[x ]?]\s*$/i, '')); | ||
if (marker !== shouldBe) { | ||
file.message('Marker should be `' + shouldBe + '`, was `' + marker + '`', item); | ||
file.message( | ||
'Marker should be `' + shouldBe + '`, was `' + marker + '`', | ||
child | ||
) | ||
} | ||
@@ -168,0 +185,0 @@ } |
{ | ||
"name": "remark-lint-ordered-list-marker-value", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "remark-lint rule to warn when the marker value of ordered lists violates a given style", | ||
@@ -14,4 +14,4 @@ "license": "MIT", | ||
], | ||
"repository": "https://github.com/wooorm/remark-lint/tree/master/packages/remark-lint-ordered-list-marker-value", | ||
"bugs": "https://github.com/wooorm/remark-lint/issues", | ||
"repository": "https://github.com/remarkjs/remark-lint/tree/master/packages/remark-lint-ordered-list-marker-value", | ||
"bugs": "https://github.com/remarkjs/remark-lint/issues", | ||
"author": "Titus Wormer <tituswormer@gmail.com> (http://wooorm.com)", | ||
@@ -18,0 +18,0 @@ "contributors": [ |
@@ -15,2 +15,13 @@ <!--This file is generated--> | ||
## Fix | ||
[`remark-stringify`](https://github.com/remarkjs/remark/tree/master/packages/remark-stringify) | ||
retains the number of the first list-item bullet, and by default | ||
increments the other items. Pass | ||
[`incrementListMarker: false`](https://github.com/remarkjs/remark/tree/master/packages/remark-stringify#optionsincrementlistmarker) | ||
to not increment further list-items. | ||
See [Using remark to fix your markdown](https://github.com/remarkjs/remark-lint#using-remark-to-fix-your-markdown) | ||
on how to automatically fix warnings for this rule. | ||
## Presets | ||
@@ -22,3 +33,3 @@ | ||
| ------ | ------- | | ||
| [`remark-preset-lint-markdown-style-guide`](https://github.com/wooorm/remark-lint/tree/master/packages/remark-preset-lint-markdown-style-guide) | | | ||
| [`remark-preset-lint-markdown-style-guide`](https://github.com/remarkjs/remark-lint/tree/master/packages/remark-preset-lint-markdown-style-guide) | `'one'` | | ||
@@ -93,2 +104,19 @@ ## Example | ||
##### `also-invalid.md` | ||
When configured with `'one'`. | ||
###### In | ||
```markdown | ||
2. Foo | ||
1. Bar | ||
``` | ||
###### Out | ||
```text | ||
1:1-1:8: Marker should be `1`, was `2` | ||
``` | ||
##### `valid.md` | ||
@@ -210,2 +238,2 @@ | ||
[MIT](https://github.com/wooorm/remark-lint/blob/master/LICENSE) © [Titus Wormer](http://wooorm.com) | ||
[MIT](https://github.com/remarkjs/remark-lint/blob/master/LICENSE) © [Titus Wormer](http://wooorm.com) |
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
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
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
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
8883
168
236