|
1 |
| -/** |
2 |
| - * @typedef {import('unist').Node} Node |
3 |
| - * @typedef {import('unist').Position} Position |
4 |
| - * @typedef {import('vfile').VFile} VFile |
5 |
| - * @typedef {import('vfile').VFileValue} VFileValue |
6 |
| - */ |
7 |
| - |
8 |
| -import {location} from 'vfile-location' |
9 |
| - |
10 |
| -const search = /\r?\n|\r/g |
11 |
| - |
12 |
| -/** |
13 |
| - * Get the source of a node or at a position. |
14 |
| - * |
15 |
| - * @param {Node|Position} value |
16 |
| - * Value to get. |
17 |
| - * @param {VFile|VFileValue} file |
18 |
| - * File in which `value` exists. |
19 |
| - * @returns {string|null} |
20 |
| - * Source of `value` in `doc`, if available. |
21 |
| - */ |
22 |
| -export function source(value, file) { |
23 |
| - const doc = String(file) |
24 |
| - const loc = location(file) |
25 |
| - /** @type {import('unist').Position} */ |
26 |
| - // @ts-expect-error Looks like a node. |
27 |
| - const position = (value && value.position) || value || {} |
28 |
| - const endOffset = loc.toOffset(position.end) |
29 |
| - let startOffset = loc.toOffset(position.start) |
30 |
| - |
31 |
| - if (endOffset === -1 || startOffset === -1) { |
32 |
| - return null |
33 |
| - } |
34 |
| - |
35 |
| - /** @type {Array.<string>} */ |
36 |
| - const results = [] |
37 |
| - |
38 |
| - while (startOffset < endOffset) { |
39 |
| - search.lastIndex = startOffset |
40 |
| - const match = search.exec(doc) |
41 |
| - const end = match && match.index < endOffset ? match.index : endOffset |
42 |
| - results.push(doc.slice(startOffset, end)) |
43 |
| - startOffset = end |
44 |
| - |
45 |
| - if (match && match.index < endOffset) { |
46 |
| - startOffset += match[0].length |
47 |
| - results.push(match[0]) |
48 |
| - } |
49 |
| - } |
50 |
| - |
51 |
| - return results.join('') |
52 |
| -} |
| 1 | +export {source} from './lib/index.js' |
0 commit comments