Unist Utility to Stringify Position

4.0.0 · active · verified Sun Apr 19

unist-util-stringify-position is a utility package within the unified (unist) ecosystem designed to consistently serialize positional information from abstract syntax tree (AST) nodes, positions, or points into a human-readable string format. This is particularly useful for generating error messages, warnings, or logging location-specific information in parsers, compilers, and transformers. The current stable version is 4.0.0, which was released recently and is actively maintained by the syntax-tree collective. Its release cadence is tied to breaking changes and necessary updates within the broader unist ecosystem. Key differentiators include its adherence to the unist specification, full TypeScript support, and its focus on standardizing how location data is presented to end-users, ensuring consistency across various unified processors like `remark` or `rehype`.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to use `stringifyPosition` to convert a point, position range, or a unist node's position into a human-readable string format, returning an empty string for invalid inputs.

import { stringifyPosition } from 'unist-util-stringify-position';

console.log('Point:', stringifyPosition({line: 2, column: 3}));
// => '2:3'

console.log('Position:', stringifyPosition({start: {line: 2, column: 1}, end: {line: 3, column: 1}}));
// => '2:1-3:1'

console.log('Node:', stringifyPosition({
  type: 'text',
  value: '!',
  position: {
    start: {line: 5, column: 11},
    end: {line: 5, column: 12}
  }
}));
// => '5:11-5:12'

console.log('Invalid input:', stringifyPosition(null));
// => ''

view raw JSON →