remark-lint-fenced-code-flag
Advanced tools
Comparing version
139
index.js
@@ -18,101 +18,128 @@ /** | ||
* | ||
* @example {"name": "ok.md"} | ||
* @example | ||
* {"name": "ok.md"} | ||
* | ||
* ```alpha | ||
* bravo(); | ||
* bravo() | ||
* ``` | ||
* | ||
* @example {"name": "not-ok.md", "label": "input"} | ||
* @example | ||
* {"name": "not-ok.md", "label": "input"} | ||
* | ||
* ``` | ||
* alpha(); | ||
* alpha() | ||
* ``` | ||
* | ||
* @example {"name": "not-ok.md", "label": "output"} | ||
* @example | ||
* {"name": "not-ok.md", "label": "output"} | ||
* | ||
* 1:1-3:4: Missing code language flag | ||
* | ||
* @example {"name": "ok.md", "setting": {"allowEmpty": true}} | ||
* @example | ||
* {"name": "ok.md", "setting": {"allowEmpty": true}} | ||
* | ||
* ``` | ||
* alpha(); | ||
* alpha() | ||
* ``` | ||
* | ||
* @example {"name": "not-ok.md", "setting": {"allowEmpty": false}, "label": "input"} | ||
* @example | ||
* {"name": "not-ok.md", "setting": {"allowEmpty": false}, "label": "input"} | ||
* | ||
* ``` | ||
* alpha(); | ||
* alpha() | ||
* ``` | ||
* | ||
* @example {"name": "not-ok.md", "setting": {"allowEmpty": false}, "label": "output"} | ||
* @example | ||
* {"name": "not-ok.md", "setting": {"allowEmpty": false}, "label": "output"} | ||
* | ||
* 1:1-3:4: Missing code language flag | ||
* | ||
* @example {"name": "ok.md", "setting": ["alpha"]} | ||
* @example | ||
* {"name": "ok.md", "setting": ["alpha"]} | ||
* | ||
* ```alpha | ||
* bravo(); | ||
* bravo() | ||
* ``` | ||
* | ||
* @example {"name": "not-ok.md", "setting": ["charlie"], "label": "input"} | ||
* @example | ||
* {"name": "ok.md", "setting": {"flags":["alpha"]}} | ||
* | ||
* ```alpha | ||
* bravo(); | ||
* bravo() | ||
* ``` | ||
* | ||
* @example {"name": "not-ok.md", "setting": ["charlie"], "label": "output"} | ||
* @example | ||
* {"name": "not-ok.md", "setting": ["charlie"], "label": "input"} | ||
* | ||
* ```alpha | ||
* bravo() | ||
* ``` | ||
* | ||
* @example | ||
* {"name": "not-ok.md", "setting": ["charlie"], "label": "output"} | ||
* | ||
* 1:1-3:4: Incorrect code language flag | ||
*/ | ||
'use strict' | ||
/** | ||
* @typedef {import('mdast').Root} Root | ||
* | ||
* @typedef {string[]} Flags | ||
* | ||
* @typedef FlagMap | ||
* @property {Flags} [flags] | ||
* @property {boolean} [allowEmpty=false] | ||
* | ||
* @typedef {Flags|FlagMap} 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:fenced-code-flag', fencedCodeFlag) | ||
const fence = /^ {0,3}([~`])\1{2,}/ | ||
var start = position.start | ||
var end = position.end | ||
const remarkLintFencedCodeFlag = lintRule( | ||
'remark-lint:fenced-code-flag', | ||
/** @type {import('unified-lint-rule').Rule<Root, Options>} */ | ||
(tree, file, option) => { | ||
const value = String(file) | ||
let allowEmpty = false | ||
/** @type {string[]} */ | ||
let allowed = [] | ||
var fence = /^ {0,3}([~`])\1{2,}/ | ||
var reasonIncorrect = 'Incorrect code language flag' | ||
var reasonMissing = 'Missing code language flag' | ||
if (typeof option === 'object') { | ||
if (Array.isArray(option)) { | ||
allowed = option | ||
} else { | ||
allowEmpty = Boolean(option.allowEmpty) | ||
function fencedCodeFlag(tree, file, option) { | ||
var contents = String(file) | ||
var allowEmpty = false | ||
var allowed = [] | ||
var flags = option | ||
if (option.flags) { | ||
allowed = option.flags | ||
} | ||
} | ||
} | ||
if (typeof flags === 'object' && !('length' in flags)) { | ||
allowEmpty = Boolean(flags.allowEmpty) | ||
flags = flags.flags | ||
} | ||
visit(tree, 'code', (node) => { | ||
if (!generated(node)) { | ||
if (node.lang) { | ||
if (allowed.length > 0 && !allowed.includes(node.lang)) { | ||
file.message('Incorrect code language flag', node) | ||
} | ||
} else { | ||
const slice = value.slice( | ||
pointStart(node).offset, | ||
pointEnd(node).offset | ||
) | ||
if (typeof flags === 'object' && 'length' in flags) { | ||
allowed = String(flags).split(',') | ||
} | ||
visit(tree, 'code', visitor) | ||
function visitor(node) { | ||
var value | ||
if (!generated(node)) { | ||
if (node.lang) { | ||
if (allowed.length !== 0 && allowed.indexOf(node.lang) === -1) { | ||
file.message(reasonIncorrect, node) | ||
if (!allowEmpty && fence.test(slice)) { | ||
file.message('Missing code language flag', node) | ||
} | ||
} | ||
} else { | ||
value = contents.slice(start(node).offset, end(node).offset) | ||
if (!allowEmpty && fence.test(value)) { | ||
file.message(reasonMissing, node) | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
) | ||
export default remarkLintFencedCodeFlag |
{ | ||
"name": "remark-lint-fenced-code-flag", | ||
"version": "2.0.1", | ||
"version": "3.0.0", | ||
"description": "remark-lint rule to warn when fenced code blocks occur without language flag", | ||
@@ -26,12 +26,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 | ||
} | ||
} |
@@ -30,3 +30,3 @@ <!--This file is generated--> | ||
| - | - | | ||
| [`remark-preset-lint-markdown-style-guide`](https://github.com/remarkjs/remark-lint/tree/main/packages/remark-preset-lint-markdown-style-guide) | `{allowEmpty: false}` | | ||
| [`remark-preset-lint-markdown-style-guide`](https://github.com/remarkjs/remark-lint/tree/main/packages/remark-preset-lint-markdown-style-guide) | `{ allowEmpty: false }` | | ||
@@ -41,3 +41,3 @@ ## Example | ||
```alpha | ||
bravo(); | ||
bravo() | ||
``` | ||
@@ -56,3 +56,3 @@ ```` | ||
``` | ||
alpha(); | ||
alpha() | ||
``` | ||
@@ -75,3 +75,3 @@ ```` | ||
``` | ||
alpha(); | ||
alpha() | ||
``` | ||
@@ -92,3 +92,3 @@ ```` | ||
``` | ||
alpha(); | ||
alpha() | ||
``` | ||
@@ -111,3 +111,3 @@ ```` | ||
```alpha | ||
bravo(); | ||
bravo() | ||
``` | ||
@@ -120,2 +120,18 @@ ```` | ||
##### `ok.md` | ||
When configured with `{ flags: [ 'alpha' ] }`. | ||
###### In | ||
````markdown | ||
```alpha | ||
bravo() | ||
``` | ||
```` | ||
###### Out | ||
No messages. | ||
##### `not-ok.md` | ||
@@ -129,3 +145,3 @@ | ||
```alpha | ||
bravo(); | ||
bravo() | ||
``` | ||
@@ -142,2 +158,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][]: | ||
@@ -149,2 +168,5 @@ | ||
This package exports no identifiers. | ||
The default export is `remarkLintFencedCodeFlag`. | ||
## Use | ||
@@ -176,10 +198,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 remarkLintFencedCodeFlag from 'remark-lint-fenced-code-flag' | ||
remark() | ||
.use(require('remark-lint')) | ||
+ .use(require('remark-lint-fenced-code-flag')) | ||
.process('_Emphasis_ and **importance**', function (err, file) { | ||
console.error(report(err || file)) | ||
.use(remarkLint) | ||
+ .use(remarkLintFencedCodeFlag) | ||
.process('_Emphasis_ and **importance**') | ||
.then((file) => { | ||
console.error(reporter(file)) | ||
}) | ||
@@ -202,5 +227,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 | ||
@@ -225,6 +250,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 | ||
@@ -231,0 +258,0 @@ |
9746
24.74%4
33.33%161
53.33%257
11.74%Yes
NaN6
50%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
Updated
Updated
Updated
Updated