remark-lint rule to warn when too many spaces are used to create a hard break
npx @tessl/cli install tessl/npm-remark-lint-hard-break-spaces@4.1.0remark-lint-hard-break-spaces is a specialized remark-lint rule that validates the spacing used in Markdown hard line breaks. It warns when more than two spaces are used at the end of a line to create a hard break, or when spaces are used instead of the recommended backslash escape sequence.
npm install remark-lint-hard-break-spacesimport remarkLintHardBreakSpaces from "remark-lint-hard-break-spaces";For CommonJS:
const remarkLintHardBreakSpaces = require("remark-lint-hard-break-spaces");import remarkLint from "remark-lint";
import remarkLintHardBreakSpaces from "remark-lint-hard-break-spaces";
import remarkParse from "remark-parse";
import remarkStringify from "remark-stringify";
import { unified } from "unified";
import { read } from "to-vfile";
import { reporter } from "vfile-reporter";
const file = await read("example.md");
await unified()
.use(remarkParse)
.use(remarkLint)
.use(remarkLintHardBreakSpaces)
.use(remarkStringify)
.process(file);
console.error(reporter(file));The main plugin function that creates a remark-lint rule to validate hard break spacing.
/**
* remark-lint rule to warn when spaces are used incorrectly for hard breaks
* @param {Options | null | undefined} options - Configuration options
* @returns {Transformer} Transform function for unified pipeline
*/
function remarkLintHardBreakSpaces(options);Usage with unified:
import { unified } from "unified";
import remarkParse from "remark-parse";
import remarkLint from "remark-lint";
import remarkLintHardBreakSpaces from "remark-lint-hard-break-spaces";
const processor = unified()
.use(remarkParse)
.use(remarkLint)
.use(remarkLintHardBreakSpaces, { allowSpaces: false });Usage with configuration:
// Allow trailing spaces (default behavior)
.use(remarkLintHardBreakSpaces)
.use(remarkLintHardBreakSpaces, { allowSpaces: true })
// Enforce escape-style hard breaks only
.use(remarkLintHardBreakSpaces, { allowSpaces: false })Common integration patterns:
// In package.json remarkConfig
{
"remarkConfig": {
"plugins": [
"remark-lint",
"remark-lint-hard-break-spaces",
// or with options
["remark-lint-hard-break-spaces", { "allowSpaces": false }]
]
}
}
// With remark CLI
remark --use remark-lint --use remark-lint-hard-break-spaces ./**
* Configuration options for the hard break spaces rule
*/
interface Options {
/**
* Allow trailing space hard breaks at all
* @default true
* When false: use escape hard breaks (backslash) instead
* When true: allow 2-space hard breaks, warn on more than 2 spaces
*/
allowSpaces?: boolean | null | undefined;
}The plugin generates specific error messages based on configuration:
allowSpaces: true (default): "Unexpected \3` spaces for hard break, expected `2` spaces"`allowSpaces: false: "Unexpected \2` spaces for hard break, expected escape"`Configuration validation errors:
// Invalid option type
"Unexpected value `🌍` for `options`, expected object"
// Invalid allowSpaces type
"Unexpected value `🌍` for `options.allowSpaces`, expected `boolean`"Less than two spaces do not create hard breaks and more than two spaces have no effect. Due to this, it's recommended to turn this rule on.
With CommonMark, it is now possible to use a backslash (\) at the end of a line to create a hard break. It is now recommended to pass allowSpaces: false to enforce this cleaner approach.
Valid hard break examples:
<!-- With allowSpaces: true (default) -->
**Mercury** is the first planet from the Sun
and the smallest in the Solar System.
<!-- With allowSpaces: false -->
**Venus** is the second planet from\
the Sun.Invalid examples that trigger warnings:
<!-- Too many spaces (3+ spaces) -->
**Mercury** is the first planet from the Sun
and the smallest in the Solar System.
<!-- Warning: "Unexpected `3` spaces for hard break, expected `2` spaces" -->
<!-- Spaces when allowSpaces: false -->
**Mercury** is the first planet from the Sun
and the smallest in the Solar System.
<!-- Warning: "Unexpected `2` spaces for hard break, expected escape" -->