# @sarahdayan/sitemaptree

> ```sh npm install @sarahdayan/sitemaptree # or yarn add @sarahdayan/sitemaptree ```

Latest version **0.1.3** (published 2020-12-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install @sarahdayan/sitemaptree
pnpm add @sarahdayan/sitemaptree
yarn add @sarahdayan/sitemaptree
bun add @sarahdayan/sitemaptree
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.3 |
| Published | 2020-12-22 |
| First published | 2020-12-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=10 |
| Dependencies | 0 |
| Unpacked size | 36.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Sarah Dayan |
| Maintainers | sarahdayan |

## Links

- npm: https://www.npmjs.com/package/@sarahdayan/sitemaptree
- npm.io page: https://npm.io/package/@sarahdayan/sitemaptree

## Recent versions

- 0.1.3 (latest) — 2020-12-22
- 0.1.2 — 2020-12-20
- 0.1.1 — 2020-12-20
- 0.1.0 — 2020-12-20

## README

<p align="center">
  <h1 align="center">SitemapTree</h1>
  <h4 align="center">A port of <a href="https://github.com/algolia/frontman/blob/master/lib/frontman/sitemap_tree.rb" target="_blank">Frontman's <code>SitemapTree</code></a> in TypeScript.</h4>

  <p align="center">
    <img src="https://img.shields.io/github/workflow/status/sarahdayan/sitemaptree/CI/master" alt="GitHub Workflow Status" />
    <img src="https://img.shields.io/node/v/@sarahdayan/sitemaptree" alt="Node.js supported versions" />
    <img src="https://img.shields.io/bundlephobia/minzip/@sarahdayan/sitemaptree" alt="npm bundle size" />
    <img src="https://img.shields.io/npm/l/@sarahdayan/sitemaptree" alt="License" />
  </p>
</p>

SitemapTree is a data structure to represent pages from a static site. It lets you retrieve any resource from the tree with constant time complexity.

## Install

```sh
npm install @sarahdayan/sitemaptree
# or
yarn add @sarahdayan/sitemaptree
```

## Usage

```ts
import { SitemapTree, Resource } from '@sarahdayan/sitemaptree'

const tree = new SitemapTree()

tree.add(new Resource('path/to/first.md'))
tree.add(new Resource('path/to/second.md'))

tree.children[0].children[0].children[0].resource.source // "path/to/first.md"

tree.fromUrl('path/to/second').children[0].resource.source // "path/to/second.md"
```

### Windows

Paths are normalized. If you're running this package on Windows, make sure to use `\\` instead of `/` in your paths, or `path.normalize` to ensure that your input paths match the ones in your `Resource` instances.

## API

### `SitemapTree`

Create a tree of resources.

#### `SitemapTree.resource`

> `Resource | null`

The attached resource.

#### `SitemapTree.parent`

> `SitemapTree | null`

The parent of the sitemap tree.

#### `SitemapTree.children`

> `SitemapTree[]`

The children of the sitemap tree.

#### `SitemapTree.url`

> `string | null`

The URL of the sitemap tree.

#### `SitemapTree.urlPart`

> `string | null`

The URL part of the sitemap tree.

#### `SitemapTree.siblings`

> `SitemapTree[]`

The siblings of the sitemap tree, including itself.

#### `SitemapTree.allUrls`

> `Record<string, SitemapTree>`

All URLs in the sitemap tree.

#### `SitemapTree.add(resource)`

> `(resource: Resource): void`

Add a resource to the sitemap tree.

#### `SitemapTree.fromUrl(url)`

> `(url: string): SitemapTree`

Retrieve a sub-tree in a sitemap tree from a URL.

#### `SitemapTree.fromResource(resource)`

> `(resource: Resource): SitemapTree`

Retrieve a sub-tree in a sitemap tree from a resource.

### `Resource`

Create a resource from a file path.

---
_Source: https://npm.io/package/@sarahdayan/sitemaptree · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
