unist utility to get the source of a value
npm install unist-util-source[![Build][build-badge]][build]
[![Coverage][coverage-badge]][coverage]
[![Downloads][downloads-badge]][downloads]
[![Size][size-badge]][size]
[![Sponsors][sponsors-badge]][collective]
[![Backers][backers-badge]][collective]
[![Chat][chat-badge]][chat]
[unist][] utility to get the source code of a node or position.
* What is this?
* When should I use this?
* Install
* Use
* API
* [source(file[, value])](#sourcefile-value)
* Types
* Compatibility
* Contribute
* License
This is a tiny utility that lets you get the source code of a node or position.
This is super tiny utility useful when you want to display the source code
of something in a file.
This package is [ESM only][esm].
In Node.js (version 16+), install with [npm][]:
``sh`
npm install unist-util-source
In Deno with [esm.sh][esmsh]:
`js`
import {source} from 'https://esm.sh/unist-util-source@5'
In browsers with [esm.sh][esmsh]:
`html`
Say our document example.md contains:
`markdown`
> + Hello
> world.
…and our module example.js looks as follows:
`js
import {fromMarkdown} from 'mdast-util-from-markdown'
import {read} from 'to-vfile'
import {source} from 'unist-util-source'
const file = await read('example.md')
const tree = fromMarkdown(String(file))
const strong = tree.children[0].children[0].children[0].children[0].children[0]
console.log(source(file, strong))
`
…now running node example.js yields:
`markdown`
Hello
This package exports the identifier [source][source].
There is no default export.
Get the source of a node or at a position.
###### Parameters
* file ([VFile][vfile] or string)value
— file in which existsvalue
* ([Node][node], [Position][position], optional)
— value to get
###### Returns
Source of value in doc, if available (string or undefined).
This package is fully typed with [TypeScript][].
It exports no additional types.
Projects maintained by the unified collective are compatible with maintained
versions of Node.js.
When we cut a new major release, we drop support for unmaintained versions of
Node.
This means we try to keep the current release line, unist-util-source@^5,
compatible with Node.js 16.
See [contributing.md][contributing] in [syntax-tree/.github][health] forsupport.md`][support] for ways to get help.
ways to get started.
See [
This project has a [code of conduct][coc].
By interacting with this repository, organisation, or community you agree to
abide by its terms.
[MIT][license] © [Titus Wormer][author]
[build-badge]: https://github.com/syntax-tree/unist-util-source/workflows/main/badge.svg
[build]: https://github.com/syntax-tree/unist-util-source/actions
[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/unist-util-source.svg
[coverage]: https://codecov.io/github/syntax-tree/unist-util-source
[downloads-badge]: https://img.shields.io/npm/dm/unist-util-source.svg
[downloads]: https://www.npmjs.com/package/unist-util-source
[size-badge]: https://img.shields.io/badge/dynamic/json?label=minzipped%20size&query=$.size.compressedSize&url=https://deno.bundlejs.com/?q=unist-util-source
[size]: https://bundlejs.com/?q=unist-util-source
[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg
[backers-badge]: https://opencollective.com/unified/backers/badge.svg
[collective]: https://opencollective.com/unified
[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg
[chat]: https://github.com/syntax-tree/unist/discussions
[npm]: https://docs.npmjs.com/cli/install
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
[esmsh]: https://esm.sh
[typescript]: https://www.typescriptlang.org
[license]: license
[author]: https://wooorm.com
[health]: https://github.com/syntax-tree/.github
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md
[support]: https://github.com/syntax-tree/.github/blob/main/support.md
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
[unist]: https://github.com/syntax-tree/unist
[node]: https://github.com/syntax-tree/unist#node
[position]: https://github.com/syntax-tree/unist#position
[vfile]: https://github.com/vfile/vfile
[source]: #sourcefile-value