unist-util-source

unist utility to get the source of a value

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
unist-util-source
605.0.010 months ago8 years agoMinified + gzip package size for unist-util-source in KB

Readme

unist-util-source
!Buildbuild-badgebuild !Coveragecoverage-badgecoverage !Downloadsdownloads-badgedownloads !Sizesize-badgesize !Sponsorssponsors-badgecollective !Backersbackers-badgecollective !Chatchat-badgechat
unist utility to get the source code of a node or position.

Contents

*   [`source(file[, value])`](#sourcefile-value)

What is this?

This is a tiny utility that lets you get the source code of a node or position.

When should I use this?

This is super tiny utility useful when you want to display the source code of something in a file.

Install

This package is ESM onlyesm. In Node.js (version 16+), install with npm:
npm install unist-util-source

In Deno with esm.shesmsh:
import {source} from 'https://esm.sh/unist-util-source@5'

In browsers with esm.shesmsh:
<script type="module">
  import {source} from 'https://esm.sh/unist-util-source@5?bundle'
</script>

Use

Say our document example.md contains:
> + **[Hello](./example)**
>   world.

…and our module example.js looks as follows:
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:
**[Hello](./example)**

API

This package exports the identifier sourcesource. There is no default export.

source(file[, value])

Get the source of a node or at a position.
Parameters
— file in which `value` exists
— value to get
Returns
Source of value in doc, if available (string or undefined).

Types

This package is fully typed with TypeScript. It exports no additional types.

Compatibility

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.

Contribute

See contributing.mdcontributing in syntax-tree/.githubhealth for ways to get started. See support.mdsupport for ways to get help.
This project has a code of conductcoc. By interacting with this repository, organisation, or community you agree to abide by its terms.

License

MITlicense © Titus Wormerauthor