mirror of
https://github.com/Sevichecc/Urara-Blog.git
synced 2025-05-02 16:29:30 +08:00
62 lines
1.3 KiB
Text
62 lines
1.3 KiB
Text
/**
|
||
* @typedef {import('unist').Point} Point
|
||
* @typedef {import('unist').Node} Node
|
||
* @typedef {import('unist').Position} Position
|
||
* @typedef {object & {type: string, position?: Position|undefined}} NodeLike
|
||
*/
|
||
|
||
/**
|
||
* Stringify one point, a position (start and end points), or a node’s
|
||
* positional information.
|
||
*
|
||
* @param {Node|NodeLike|Position|Point|null} [value]
|
||
* @returns {string}
|
||
*/
|
||
export function stringifyPosition(value) {
|
||
// Nothing.
|
||
if (!value || typeof value !== 'object') {
|
||
return ''
|
||
}
|
||
|
||
// Node.
|
||
if ('position' in value || 'type' in value) {
|
||
return position(value.position)
|
||
}
|
||
|
||
// Position.
|
||
if ('start' in value || 'end' in value) {
|
||
return position(value)
|
||
}
|
||
|
||
// Point.
|
||
if ('line' in value || 'column' in value) {
|
||
return point(value)
|
||
}
|
||
|
||
// ?
|
||
return ''
|
||
}
|
||
|
||
/**
|
||
* @param {Point|undefined} point
|
||
* @returns {string}
|
||
*/
|
||
function point(point) {
|
||
return index(point && point.line) + ':' + index(point && point.column)
|
||
}
|
||
|
||
/**
|
||
* @param {Position|undefined} pos
|
||
* @returns {string}
|
||
*/
|
||
function position(pos) {
|
||
return point(pos && pos.start) + '-' + point(pos && pos.end)
|
||
}
|
||
|
||
/**
|
||
* @param {number|undefined} value
|
||
* @returns {number}
|
||
*/
|
||
function index(value) {
|
||
return value && typeof value === 'number' ? value : 1
|
||
}
|