open-index/open-npm huggingface dataset:
npm package: @segmsh/core
Description: Core types and utilities for the Segm.sh.
Author: Segm.sh
Latest version: 0.2.0

# @segmsh/core

Core types and utilities for the Segm.sh.

## Installation

```bash
npm install @segmsh/core
```

## Usage

### Implementing a Processor

Implement the `Processor` interface to create a custom processor.

```typescript
import { Processor, Document, id, root, segment } from "@segmsh/core";

interface MyParseOptions {
  preserveWhitespace?: boolean;
}

export class MyProcessor implements Processor<MyParseOptions> {
  parse(res: string, options?: MyParseOptions): Document {
    const segId = id({ text: "Hello World" });
    return {
      segments: [{ id: segId, text: "Hello World" }],
      tree: root([segment(segId)]),
    };
  }

  stringify(doc: Document): string {
    return doc.segments.map(s => s.text).join("\n");
  }
}
```

### ID Generation

Use `id()` to generate deterministic MD5-based segment IDs. Accepts a string key (for key-value formats) or segment content (for structured formats).

```typescript
import { id } from "@segmsh/core";

// Key-value formats (JSON, YAML)
const segId = id("button.submit");

// Structured formats (HTML, Markdown)
const segId = id({ text: "Hello World" });
const segId = id({ text: "Hello {b1}World{/b1}", tags: { b1: { class: "bold" } } });
const segId = id({ text: "Hello World", metadata: { section: "header" } });
```

### Tree Helpers

```typescript
import { root, element, segment, text } from "@segmsh/core";

const tree = root([
  element("p", segment("seg-1"), text("Hello")),
]);
```

## Development

```bash
npm install
npm run build
```

## License

[Apache 2.0](LICENSE)
npm package: @segmsh/json
Description: JSON processor for the [segm.sh](https://segm.sh). Parses JSON files into a document (AST + segments) and stringifies them back, preserving structure while allowing content extraction.
Author: Segm.sh
Latest version: 0.1.0

# @segmsh/json

JSON processor for the [segm.sh](https://segm.sh). Parses JSON files into a document (AST + segments) and stringifies them back, preserving structure while allowing content extraction.

## Installation

```bash
npm install @segmsh/json
```

## Usage

```typescript
import JsonProcessor from "@segmsh/json";

const processor = new JsonProcessor();

// Parse into a Document (AST + segments)
const doc = processor.parse('{"hello": "world"}');

// Modify doc.segments as needed ...

// Stringify back to JSON
const output = processor.stringify(doc);
```

## How it works

JSON is flattened into dotted keys, each value becomes a segment, and the structure is stored as a hast tree:

| JSON path | Flattened key |
|---|---|
| `menu.title` | `menu.title` |
| `items[0].name` | `items..0.name` |
| `items[0][1]` | `items..0..1` |

`..` distinguishes array indices from object keys. The original structure is fully restored on stringify.

## Development

```bash
npm run build
npm test
```

## License

[Apache-2.0](LICENSE)
npm package: @segmsh/md
Description: Markdown processor for [segm.sh](https://segm.sh). This package parses Markdown files into a document (AST + segments) and stringifies them back, preserving structure while allowing content extraction.
Author: Segm.sh
Latest version: 0.1.0

# @segmsh/md

Markdown processor for [segm.sh](https://segm.sh). This package parses Markdown files into a document (AST + segments) and stringifies them back, preserving structure while allowing content extraction.

## Installation

```bash
npm install @segmsh/md
```

## Usage

```typescript
import MdProcessor from "@segmsh/md";

const processor = new MdProcessor();

const mdContent = "# Hello world";
const document = processor.parse(mdContent);

// ... modify document segments ...

// Stringify back to Markdown
const newMdContent = processor.stringify(document);
```

## Features

- **Structure Preservation**: Maintains the original structure of the Markdown document.
- **Round-trip**: Ensures that parsing and then stringifying results in the original Markdown structure, preserving as much formatting as possible.

## Development

### Build

```bash
npm run build
```

### Test

```bash
npm test
```

## License

[Apache-2.0](LICENSE)
npm package: @segmsh/yaml
Description: YAML processor for the [segm.sh](https://segm.sh). Parses YAML files into a document (AST + segments) and stringifies them back, preserving structure while allowing content extraction.
Author: Segm.sh
Latest version: 0.1.0

# @segmsh/yaml

YAML processor for the [segm.sh](https://segm.sh). Parses YAML files into a document (AST + segments) and stringifies them back, preserving structure while allowing content extraction.

## Installation

```bash
npm install @segmsh/yaml
```

## Usage

```typescript
import YamlProcessor from "@segmsh/yaml";

const processor = new YamlProcessor();

// Parse into a Document (AST + segments)
const doc = processor.parse(`
site:
  title: "Hello"
  links:
    - "Docs"
    - "API"
`);

// Modify doc.segments as needed ...

// Stringify back to YAML
const output = processor.stringify(doc);
```

## How it works

YAML scalars become segments, and their positions are tracked in a hast tree using dotted keys:

| YAML shape | Segment key |
|---|---|
| `site.title` | `site.title` |
| `links[0]` | `links..0` |
| `items[1].name` | `items..1.name` |

`..` distinguishes array indices from object keys. On parse, text nodes are replaced by segment refs in the tree, and on stringify those refs are resolved back from `doc.segments`.

## Development

```bash
npm run build
npm test
```

## License

[Apache-2.0](LICENSE)