remark-lint-code-block-style
Advanced tools
Comparing version 2.0.1 to 3.0.0
140
index.js
@@ -26,14 +26,16 @@ /** | ||
* | ||
* @example {"setting": "indented", "name": "ok.md"} | ||
* @example | ||
* {"setting": "indented", "name": "ok.md"} | ||
* | ||
* alpha(); | ||
* alpha() | ||
* | ||
* Paragraph. | ||
* | ||
* bravo(); | ||
* bravo() | ||
* | ||
* @example {"setting": "indented", "name": "not-ok.md", "label": "input"} | ||
* @example | ||
* {"setting": "indented", "name": "not-ok.md", "label": "input"} | ||
* | ||
* ``` | ||
* alpha(); | ||
* alpha() | ||
* ``` | ||
@@ -44,6 +46,7 @@ * | ||
* ``` | ||
* bravo(); | ||
* bravo() | ||
* ``` | ||
* | ||
* @example {"setting": "indented", "name": "not-ok.md", "label": "output"} | ||
* @example | ||
* {"setting": "indented", "name": "not-ok.md", "label": "output"} | ||
* | ||
@@ -53,6 +56,7 @@ * 1:1-3:4: Code blocks should be indented | ||
* | ||
* @example {"setting": "fenced", "name": "ok.md"} | ||
* @example | ||
* {"setting": "fenced", "name": "ok.md"} | ||
* | ||
* ``` | ||
* alpha(); | ||
* alpha() | ||
* ``` | ||
@@ -63,21 +67,24 @@ * | ||
* ``` | ||
* bravo(); | ||
* bravo() | ||
* ``` | ||
* | ||
* @example {"setting": "fenced", "name": "not-ok-fenced.md", "label": "input"} | ||
* @example | ||
* {"setting": "fenced", "name": "not-ok-fenced.md", "label": "input"} | ||
* | ||
* alpha(); | ||
* alpha() | ||
* | ||
* Paragraph. | ||
* | ||
* bravo(); | ||
* bravo() | ||
* | ||
* @example {"setting": "fenced", "name": "not-ok-fenced.md", "label": "output"} | ||
* @example | ||
* {"setting": "fenced", "name": "not-ok-fenced.md", "label": "output"} | ||
* | ||
* 1:1-1:13: Code blocks should be fenced | ||
* 5:1-5:13: Code blocks should be fenced | ||
* 1:1-1:12: Code blocks should be fenced | ||
* 5:1-5:12: Code blocks should be fenced | ||
* | ||
* @example {"name": "not-ok-consistent.md", "label": "input"} | ||
* @example | ||
* {"name": "not-ok-consistent.md", "label": "input"} | ||
* | ||
* alpha(); | ||
* alpha() | ||
* | ||
@@ -87,10 +94,12 @@ * Paragraph. | ||
* ``` | ||
* bravo(); | ||
* bravo() | ||
* ``` | ||
* | ||
* @example {"name": "not-ok-consistent.md", "label": "output"} | ||
* @example | ||
* {"name": "not-ok-consistent.md", "label": "output"} | ||
* | ||
* 5:1-7:4: Code blocks should be indented | ||
* | ||
* @example {"setting": "💩", "name": "not-ok-incorrect.md", "label": "output", "config": {"positionless": true}} | ||
* @example | ||
* {"setting": "💩", "name": "not-ok-incorrect.md", "label": "output", "positionless": true} | ||
* | ||
@@ -100,56 +109,53 @@ * 1:1: Incorrect code block style `💩`: use either `'consistent'`, `'fenced'`, or `'indented'` | ||
'use strict' | ||
/** | ||
* @typedef {import('mdast').Root} Root | ||
* @typedef {'fenced'|'indented'} Style | ||
* @typedef {'consistent'|Style} Options | ||
*/ | ||
var rule = require('unified-lint-rule') | ||
var visit = require('unist-util-visit') | ||
var position = require('unist-util-position') | ||
var generated = require('unist-util-generated') | ||
import {lintRule} from 'unified-lint-rule' | ||
import {visit} from 'unist-util-visit' | ||
import {pointStart, pointEnd} from 'unist-util-position' | ||
import {generated} from 'unist-util-generated' | ||
module.exports = rule('remark-lint:code-block-style', codeBlockStyle) | ||
const remarkLintCodeBlockStyle = lintRule( | ||
'remark-lint:code-block-style', | ||
/** @type {import('unified-lint-rule').Rule<Root, Options>} */ | ||
(tree, file, option = 'consistent') => { | ||
const value = String(file) | ||
var start = position.start | ||
var end = position.end | ||
if ( | ||
option !== 'consistent' && | ||
option !== 'fenced' && | ||
option !== 'indented' | ||
) { | ||
file.fail( | ||
'Incorrect code block style `' + | ||
option + | ||
"`: use either `'consistent'`, `'fenced'`, or `'indented'`" | ||
) | ||
} | ||
var styles = {null: true, fenced: true, indented: true} | ||
visit(tree, 'code', (node) => { | ||
if (generated(node)) { | ||
return | ||
} | ||
function codeBlockStyle(tree, file, option) { | ||
var contents = String(file) | ||
var preferred = | ||
typeof option === 'string' && option !== 'consistent' ? option : null | ||
const initial = pointStart(node).offset | ||
const final = pointEnd(node).offset | ||
if (styles[preferred] !== true) { | ||
file.fail( | ||
'Incorrect code block style `' + | ||
preferred + | ||
"`: use either `'consistent'`, `'fenced'`, or `'indented'`" | ||
) | ||
} | ||
const current = | ||
node.lang || /^\s*([~`])\1{2,}/.test(value.slice(initial, final)) | ||
? 'fenced' | ||
: 'indented' | ||
visit(tree, 'code', visitor) | ||
function visitor(node) { | ||
var initial | ||
var final | ||
var current | ||
if (generated(node)) { | ||
return null | ||
} | ||
initial = start(node).offset | ||
final = end(node).offset | ||
current = | ||
node.lang || /^\s*([~`])\1{2,}/.test(contents.slice(initial, final)) | ||
? 'fenced' | ||
: 'indented' | ||
if (preferred) { | ||
if (preferred !== current) { | ||
file.message('Code blocks should be ' + preferred, node) | ||
if (option === 'consistent') { | ||
option = current | ||
} else if (option !== current) { | ||
file.message('Code blocks should be ' + option, node) | ||
} | ||
} else { | ||
preferred = current | ||
} | ||
}) | ||
} | ||
} | ||
) | ||
export default remarkLintCodeBlockStyle |
{ | ||
"name": "remark-lint-code-block-style", | ||
"version": "2.0.1", | ||
"version": "3.0.0", | ||
"description": "remark-lint rule to warn when code blocks do not adhere to a given style", | ||
@@ -24,12 +24,28 @@ "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-position": "^3.0.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-position": "^4.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 | ||
} | ||
} |
@@ -50,7 +50,7 @@ <!--This file is generated--> | ||
```markdown | ||
alpha(); | ||
alpha() | ||
Paragraph. | ||
bravo(); | ||
bravo() | ||
``` | ||
@@ -70,3 +70,3 @@ | ||
``` | ||
alpha(); | ||
alpha() | ||
``` | ||
@@ -77,3 +77,3 @@ | ||
``` | ||
bravo(); | ||
bravo() | ||
``` | ||
@@ -97,3 +97,3 @@ ```` | ||
``` | ||
alpha(); | ||
alpha() | ||
``` | ||
@@ -104,3 +104,3 @@ | ||
``` | ||
bravo(); | ||
bravo() | ||
``` | ||
@@ -120,7 +120,7 @@ ```` | ||
```markdown | ||
alpha(); | ||
alpha() | ||
Paragraph. | ||
bravo(); | ||
bravo() | ||
``` | ||
@@ -131,4 +131,4 @@ | ||
```text | ||
1:1-1:13: Code blocks should be fenced | ||
5:1-5:13: Code blocks should be fenced | ||
1:1-1:12: Code blocks should be fenced | ||
5:1-5:12: Code blocks should be fenced | ||
``` | ||
@@ -141,3 +141,3 @@ | ||
````markdown | ||
alpha(); | ||
alpha() | ||
@@ -147,3 +147,3 @@ Paragraph. | ||
``` | ||
bravo(); | ||
bravo() | ||
``` | ||
@@ -170,2 +170,5 @@ ```` | ||
This package is [ESM only][esm]: | ||
Node 12+ is needed to use it and it must be `imported`ed instead of `required`d. | ||
[npm][]: | ||
@@ -177,2 +180,5 @@ | ||
This package exports no identifiers. | ||
The default export is `remarkLintCodeBlockStyle`. | ||
## Use | ||
@@ -204,10 +210,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 remarkLintCodeBlockStyle from 'remark-lint-code-block-style' | ||
remark() | ||
.use(require('remark-lint')) | ||
+ .use(require('remark-lint-code-block-style')) | ||
.process('_Emphasis_ and **importance**', function (err, file) { | ||
console.error(report(err || file)) | ||
.use(remarkLint) | ||
+ .use(remarkLintCodeBlockStyle) | ||
.process('_Emphasis_ and **importance**') | ||
.then((file) => { | ||
console.error(reporter(file)) | ||
}) | ||
@@ -230,5 +239,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 | ||
@@ -253,6 +262,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 | ||
@@ -259,0 +270,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
10931
4
169
268
Yes
6
+ 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-position@4.0.4(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-position@3.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-position@^4.0.0
Updatedunist-util-visit@^4.0.0