Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More β†’
Socket
Sign inDemoInstall
Socket

remark-lint-code-block-style

Package Overview
Dependencies
Maintainers
2
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

remark-lint-code-block-style - npm Package Compare versions

Comparing version 2.0.1 to 3.1.2

index.d.ts

207

index.js
/**
* @author Titus Wormer
* @copyright 2015 Titus Wormer
* @license MIT
* @module code-block-style
* @fileoverview
* Warn when code blocks do not adhere to a given style.
* ## When should I use this?
*
* Options: `'consistent'`, `'fenced'`, or `'indented'`, default: `'consistent'`.
* You can use this package to check that code blocks are consistent.
*
* `'consistent'` detects the first used code block style and warns when
* subsequent code blocks uses different styles.
* ## API
*
* ## Fix
* The following options (default: `'consistent'`) are accepted:
*
* [`remark-stringify`](https://github.com/remarkjs/remark/tree/HEAD/packages/remark-stringify)
* formats code blocks using a fence if they have a language flag and
* indentation if not.
* Pass
* [`fences: true`](https://github.com/remarkjs/remark/tree/HEAD/packages/remark-stringify#optionsfences)
* to always use fences for code blocks.
* * `'fenced'`
* β€” prefer fenced code blocks:
* ````markdown
* ```js
* code()
* ```
* ````
* * `'indented'`
* β€” prefer indented code blocks:
* ```markdown
* code()
* ```
* * `'consistent'`
* β€” detect the first used style and warn when further code blocks differ
*
* 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.
* ## Recommendation
*
* @example {"setting": "indented", "name": "ok.md"}
* Indentation in markdown is complex, especially because lists and indented
* code can interfere in unexpected ways.
* Fenced code has more features than indented code: importantly, specifying a
* programming language.
* Since CommonMark took the idea of fenced code from GFM, fenced code became
* widely supported.
* Due to this, it’s recommended to configure this rule with `'fenced'`.
*
* alpha();
* ## Fix
*
* [`remark-stringify`](https://github.com/remarkjs/remark/tree/main/packages/remark-stringify)
* formats code blocks as fenced code when they have a language flag and as
* indented code otherwise.
* Pass
* [`fences: true`](https://github.com/remarkjs/remark/tree/main/packages/remark-stringify#optionsfences)
* to always use fenced code.
*
* @module code-block-style
* @summary
* remark-lint rule to warn when code blocks violate a given style.
* @author Titus Wormer
* @copyright 2015 Titus Wormer
* @license MIT
*
* @example
* {"config": "indented", "name": "ok.md"}
*
* alpha()
*
* Paragraph.
*
* bravo();
* bravo()
*
* @example {"setting": "indented", "name": "not-ok.md", "label": "input"}
* @example
* {"config": "indented", "name": "not-ok.md", "label": "input"}
*
* ```
* alpha();
* alpha()
* ```

@@ -43,6 +70,7 @@ *

* ```
* bravo();
* bravo()
* ```
*
* @example {"setting": "indented", "name": "not-ok.md", "label": "output"}
* @example
* {"config": "indented", "name": "not-ok.md", "label": "output"}
*

@@ -52,6 +80,7 @@ * 1:1-3:4: Code blocks should be indented

*
* @example {"setting": "fenced", "name": "ok.md"}
* @example
* {"config": "fenced", "name": "ok.md"}
*
* ```
* alpha();
* alpha()
* ```

@@ -62,21 +91,24 @@ *

* ```
* bravo();
* bravo()
* ```
*
* @example {"setting": "fenced", "name": "not-ok-fenced.md", "label": "input"}
* @example
* {"config": "fenced", "name": "not-ok-fenced.md", "label": "input"}
*
* alpha();
* alpha()
*
* Paragraph.
*
* bravo();
* bravo()
*
* @example {"setting": "fenced", "name": "not-ok-fenced.md", "label": "output"}
* @example
* {"config": "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()
*

@@ -86,10 +118,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
* {"config": "πŸ’©", "name": "not-ok-incorrect.md", "label": "output", "positionless": true}
*

@@ -99,56 +133,61 @@ * 1:1: Incorrect code block style `πŸ’©`: use either `'consistent'`, `'fenced'`, or `'indented'`

'use strict'
/**
* @typedef {import('mdast').Root} Root
*/
var rule = require('unified-lint-rule')
var visit = require('unist-util-visit')
var position = require('unist-util-position')
var generated = require('unist-util-generated')
/**
* @typedef {'fenced' | 'indented'} Style
* Styles.
* @typedef {'consistent' | Style} Options
* Options.
*/
module.exports = rule('remark-lint:code-block-style', codeBlockStyle)
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'
var start = position.start
var end = position.end
const remarkLintCodeBlockStyle = lintRule(
{
origin: 'remark-lint:code-block-style',
url: 'https://github.com/remarkjs/remark-lint/tree/main/packages/remark-lint-code-block-style#readme'
},
/** @type {import('unified-lint-rule').Rule<Root, Options>} */
(tree, file, option = 'consistent') => {
const value = String(file)
var styles = {null: true, fenced: true, indented: true}
if (
option !== 'consistent' &&
option !== 'fenced' &&
option !== 'indented'
) {
file.fail(
'Incorrect code block style `' +
option +
"`: use either `'consistent'`, `'fenced'`, or `'indented'`"
)
}
function codeBlockStyle(tree, file, option) {
var contents = String(file)
var preferred =
typeof option === 'string' && option !== 'consistent' ? option : null
visit(tree, 'code', (node) => {
if (generated(node)) {
return
}
if (styles[preferred] !== true) {
file.fail(
'Incorrect code block style `' +
preferred +
"`: use either `'consistent'`, `'fenced'`, or `'indented'`"
)
}
const initial = pointStart(node).offset
const final = pointEnd(node).offset
visit(tree, 'code', visitor)
const current =
node.lang || /^\s*([~`])\1{2,}/.test(value.slice(initial, final))
? 'fenced'
: 'indented'
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.1.2",
"description": "remark-lint rule to warn when code blocks do not adhere to a given style",

@@ -14,3 +14,7 @@ "license": "MIT",

],
"repository": "https://github.com/remarkjs/remark-lint/tree/main/packages/remark-lint-code-block-style",
"repository": {
"type": "git",
"url": "https://github.com/remarkjs/remark-lint",
"directory": "packages/remark-lint-code-block-style"
},
"bugs": "https://github.com/remarkjs/remark-lint/issues",

@@ -25,12 +29,26 @@ "funding": {

],
"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": {},
"xo": false,
"typeCoverage": {
"atLeast": 100,
"detail": true,
"strict": true,
"ignoreCatch": true
}
}

@@ -13,21 +13,30 @@ <!--This file is generated-->

Warn when code blocks do not adhere to a given style.
[`remark-lint`][mono] rule to warn when code blocks violate a given style.
Options: `'consistent'`, `'fenced'`, or `'indented'`, default: `'consistent'`.
## Contents
`'consistent'` detects the first used code block style and warns when
subsequent code blocks uses different styles.
* [What is this?](#what-is-this)
* [When should I use this?](#when-should-i-use-this)
* [Presets](#presets)
* [Install](#install)
* [Use](#use)
* [API](#api)
* [`unified().use(remarkLintCodeBlockStyle[, config])`](#unifieduseremarklintcodeblockstyle-config)
* [Recommendation](#recommendation)
* [Fix](#fix)
* [Examples](#examples)
* [Compatibility](#compatibility)
* [Contribute](#contribute)
* [License](#license)
## Fix
## What is this?
[`remark-stringify`](https://github.com/remarkjs/remark/tree/HEAD/packages/remark-stringify)
formats code blocks using a fence if they have a language flag and
indentation if not.
Pass
[`fences: true`](https://github.com/remarkjs/remark/tree/HEAD/packages/remark-stringify#optionsfences)
to always use fences for code blocks.
This package is a [unified][] ([remark][]) plugin, specifically a `remark-lint`
rule.
Lint rules check markdown code style.
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.
## When should I use this?
You can use this package to check that code blocks are consistent.
## Presets

@@ -42,4 +51,117 @@

## Example
## Install
This package is [ESM only][esm].
In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]:
```sh
npm install remark-lint-code-block-style
```
In Deno with [`esm.sh`][esmsh]:
```js
import remarkLintCodeBlockStyle from 'https://esm.sh/remark-lint-code-block-style@3'
```
In browsers with [`esm.sh`][esmsh]:
```html
<script type="module">
import remarkLintCodeBlockStyle from 'https://esm.sh/remark-lint-code-block-style@3?bundle'
</script>
```
## Use
On the API:
```js
import {read} from 'to-vfile'
import {reporter} from 'vfile-reporter'
import {remark} from 'remark'
import remarkLint from 'remark-lint'
import remarkLintCodeBlockStyle from 'remark-lint-code-block-style'
main()
async function main() {
const file = await remark()
.use(remarkLint)
.use(remarkLintCodeBlockStyle)
.process(await read('example.md'))
console.error(reporter(file))
}
```
On the CLI:
```sh
remark --use remark-lint --use remark-lint-code-block-style example.md
```
On the CLI in a config file (here a `package.json`):
```diff
…
"remarkConfig": {
"plugins": [
…
"remark-lint",
+ "remark-lint-code-block-style",
…
]
}
…
```
## API
This package exports no identifiers.
The default export is `remarkLintCodeBlockStyle`.
### `unified().use(remarkLintCodeBlockStyle[, config])`
This rule supports standard configuration that all remark lint rules accept
(such as `false` to turn it off or `[1, options]` to configure it).
The following options (default: `'consistent'`) are accepted:
* `'fenced'`
β€” prefer fenced code blocks:
````markdown
```js
code()
```
````
* `'indented'`
β€” prefer indented code blocks:
```markdown
code()
```
* `'consistent'`
β€” detect the first used style and warn when further code blocks differ
## Recommendation
Indentation in markdown is complex, especially because lists and indented
code can interfere in unexpected ways.
Fenced code has more features than indented code: importantly, specifying a
programming language.
Since CommonMark took the idea of fenced code from GFM, fenced code became
widely supported.
Due to this, it’s recommended to configure this rule with `'fenced'`.
## Fix
[`remark-stringify`](https://github.com/remarkjs/remark/tree/main/packages/remark-stringify)
formats code blocks as fenced code when they have a language flag and as
indented code otherwise.
Pass
[`fences: true`](https://github.com/remarkjs/remark/tree/main/packages/remark-stringify#optionsfences)
to always use fenced code.
## Examples
##### `ok.md`

@@ -52,7 +174,7 @@

```markdown
alpha();
alpha()
Paragraph.
bravo();
bravo()
```

@@ -72,3 +194,3 @@

```
alpha();
alpha()
```

@@ -79,3 +201,3 @@

```
bravo();
bravo()
```

@@ -99,3 +221,3 @@ ````

```
alpha();
alpha()
```

@@ -106,3 +228,3 @@

```
bravo();
bravo()
```

@@ -122,7 +244,7 @@ ````

```markdown
alpha();
alpha()
Paragraph.
bravo();
bravo()
```

@@ -133,4 +255,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
```

@@ -143,3 +265,3 @@

````markdown
alpha();
alpha()

@@ -149,3 +271,3 @@ Paragraph.

```
bravo();
bravo()
```

@@ -170,47 +292,9 @@ ````

## Install
## Compatibility
[npm][]:
Projects maintained by the unified collective are compatible with all maintained
versions of Node.js.
As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
Our projects sometimes work with older versions, but this is not guaranteed.
```sh
npm install remark-lint-code-block-style
```
## Use
You probably want to use it on the CLI through a config file:
```diff
…
"remarkConfig": {
"plugins": [
…
"lint",
+ "lint-code-block-style",
…
]
}
…
```
Or use it on the CLI directly
```sh
remark -u lint -u lint-code-block-style readme.md
```
Or use this on the API:
```diff
var remark = require('remark')
var report = require('vfile-reporter')
remark()
.use(require('remark-lint'))
+ .use(require('remark-lint-code-block-style'))
.process('_Emphasis_ and **importance**', function (err, file) {
console.error(report(err || file))
})
```
## Contribute

@@ -230,5 +314,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 +337,16 @@ [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
[unified]: https://github.com/unifiedjs/unified
[remark]: https://github.com/remarkjs/remark
[mono]: https://github.com/remarkjs/remark-lint
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
[esmsh]: https://esm.sh
[npm]: https://docs.npmjs.com/cli/install

@@ -262,7 +356,7 @@

[contributing]: https://github.com/remarkjs/.github/blob/HEAD/contributing.md
[contributing]: https://github.com/remarkjs/.github/blob/main/contributing.md
[support]: https://github.com/remarkjs/.github/blob/HEAD/support.md
[support]: https://github.com/remarkjs/.github/blob/main/support.md
[coc]: https://github.com/remarkjs/.github/blob/HEAD/code-of-conduct.md
[coc]: https://github.com/remarkjs/.github/blob/main/code-of-conduct.md

@@ -269,0 +363,0 @@ [license]: https://github.com/remarkjs/remark-lint/blob/main/license

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚑️ by Socket Inc