Nebulous Plasma Muffin

    unist-util-source
    TypeScript icon, indicating that this package has built-in type declarations

    4.0.2 • Public • Published

    unist-util-source

    Build Coverage Downloads Size Sponsors Backers Chat

    unist utility to get the source code of a node or position.

    Contents

    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 only. In Node.js (version 14.14+ and 16.0+), install with npm:

    npm install unist-util-source

    In Deno with esm.sh:

    import {source} from 'https://esm.sh/unist-util-source@4'

    In browsers with esm.sh:

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

    Use

    Say our document example.md contains:

    > + **[Hello](./example)**
    >   world.

    …and our module example.js looks as follows:

    import {read} from 'to-vfile'
    import {fromMarkdown} from 'mdast-util-from-markdown'
    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(strong, file))

    …now running node example.js yields:

    **[Hello](./example)**

    API

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

    source(value, file)

    Get the source of a node or at a position.

    Parameters
    • value (Node or Position) — value to get
    • file (VFile or string) — file in which value exists
    Returns

    Source of value in doc, if available (string or null).

    Types

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

    Compatibility

    Projects maintained by the unified collective are compatible with all maintained versions of Node.js. As of now, that is Node.js 14.14+ and 16.0+. Our projects sometimes work with older versions, but this is not guaranteed.

    Contribute

    See contributing.md in syntax-tree/.github for ways to get started. See support.md for ways to get help.

    This project has a code of conduct. By interacting with this repository, organisation, or community you agree to abide by its terms.

    License

    MIT © Titus Wormer

    Install

    npm i unist-util-source

    DownloadsWeekly Downloads

    2,695

    Version

    4.0.2

    License

    MIT

    Unpacked Size

    10.8 kB

    Total Files

    7

    Last publish

    Collaborators

    • wooorm
    • kmck