Registry / serialization / mdast-util-newline-to-break

mdast-util-newline-to-break

JSON →
library2.0.0jsnpmunverified

mdast-util-newline-to-break is a utility for the unified ecosystem that transforms soft line endings (simple newlines) in an mdast syntax tree into hard break (<br>) nodes. This is particularly useful for rendering user-authored content where line breaks should visually translate to actual line breaks, mimicking GitHub's behavior in comments or issues. The current stable version is 2.0.0. As part of the unified collective, it maintains compatibility with actively supported Node.js versions, with major releases typically coinciding with Node.js LTS updates. Unlike standard Markdown, which requires two trailing spaces or a backslash for a hard break, this utility provides a more direct conversion. It is often used internally by higher-level plugins like `remark-breaks`.

npm install mdast-util-newline-to-break
INSTALL
IMPORT
SIG · MDAST-UTIL-NEWLINE
M
mdast-util-newline-to-break
serializationjavascriptv2.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

newlineToBreak
import { newlineToBreak } from 'mdast-util-newline-to-break';
const newlineToBreak = require('mdast-util-newline-to-break');
This package is ESM-only since v1.0.0. CommonJS `require` is not supported.

Demonstrates how to parse a Markdown string, apply `newlineToBreak` to its mdast tree, and then convert it back to Markdown to see the effect of hard breaks.

import fs from 'node:fs/promises'; import { fromMarkdown } from 'mdast-util-from-markdown'; import { toMarkdown } from 'mdast-util-to-markdown'; import { newlineToBreak } from 'mdast-util-newline-to-break'; async function processMarkdown() { // Assume example.md contains: // This is a // paragraph. const doc = 'This is a\nparagraph.'; // Simulate reading a file const tree = fromMarkdown(doc); newlineToBreak(tree); // The output will show 'This is a\nparagraph.' as 'This is a\ // paragraph.' after conversion to markdown, representing a hard break. console.log(toMarkdown(tree)); } processMarkdown().catch(console.error);
Debug
Known issues
breakingVersion 2.0.0 requires Node.js 16 or higher. Older Node.js versions are no longer supported.
fix
Upgrade your Node.js environment to version 16 or newer. If you cannot upgrade, remain on `mdast-util-newline-to-break@^1`.
affects: >=2.0.0
breakingThe package transitioned to ESM-only starting from version 1.0.0. CommonJS `require()` is no longer supported.
fix
Refactor your imports to use ESM `import` statements. Ensure your project is configured for ESM (e.g., `"type": "module"` in `package.json`).
affects: >=1.0.0
gotchaThis utility directly modifies the provided mdast tree in place. If you need to preserve the original tree, you must clone it before passing it to `newlineToBreak`.
fix
Use a utility like `unist-util-deepcopy` or structured cloning (`structuredClone(tree)`) if available in your environment, to create a copy of the tree before modification.
affects: >=1.0.0
gotchaWhile this utility helps with rendering 'soft' newlines as 'hard' breaks, it deviates from the CommonMark specification, which requires two trailing spaces or a backslash for a hard break. Consider if this behavior is truly desired for your application or if standard Markdown semantics should be enforced.
fix
Document this behavior to users if they are authoring content, or consider using standard Markdown practices (trailing spaces or backslashes) instead for strict CommonMark compliance.
affects: *
Errors
Common errors & fixes
ERR_REQUIRE_ESM
Attempting to `require()` an ESM-only package in a CommonJS context.
fix
Change `const { newlineToBreak } = require('mdast-util-newline-to-break');` to `import { newlineToBreak } from 'mdast-util-newline-to-break';` and ensure your project uses ESM.
TypeError: newlineToBreak is not a function
Incorrectly importing `newlineToBreak` (e.g., trying a default import or a CommonJS import that fails silently in some setups).
fix
Ensure you are using a named import for `newlineToBreak`: `import { newlineToBreak } from 'mdast-util-newline-to-break';`
Upgrade
Version history
2.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
39 hits · last 30 days
node
38
Bingbot
1
Resources
mdast-util-newline-to-break — npm install mdast-util-newline-to-break · libregistry