# twine_to_json

> Converts a Snowman-formatted Twine story file into a more easily parseable JSON format

Latest version **0.0.5** (published 2016-05-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install twine_to_json
pnpm add twine_to_json
yarn add twine_to_json
bun add twine_to_json
```

Provides the command `twinetojson`.

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.5 |
| Published | 2016-05-05 |
| First published | 2016-04-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=5.10.0 |
| Dependencies | 4 |
| Known vulnerabilities | 0 (+3 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 7 |
| Author | Jon Stout |
| Maintainers | tinwatchman |
| Keywords | twine, json, snowman |

## Links

- npm: https://www.npmjs.com/package/twine_to_json
- Repository: https://github.com/tinwatchman/twinetojson
- Homepage: https://github.com/tinwatchman/twinetojson#readme
- Issues: https://github.com/tinwatchman/twinetojson/issues
- npm.io page: https://npm.io/package/twine_to_json

## Dependencies (4)

- [marked](https://npm.io/package/marked.md) ^0.3.5
- [cheerio](https://npm.io/package/cheerio.md) ^0.20.0
- [commander](https://npm.io/package/commander.md) ^2.9.0
- [underscore](https://npm.io/package/underscore.md) ^1.8.3

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 0.0.5 (latest) — 2016-05-05
- 0.0.4 — 2016-05-05
- 0.0.3 — 2016-05-05
- 0.0.2 — 2016-04-29
- 0.0.1 — 2016-04-29

## README

TWINE TO JSON
=============

> A basic utility library / script to convert a [Snowman](https://bitbucket.org/klembot/snowman-2)-formatted 
> [Twine](https://twinery.org/) story file into a more easily parseable JSON 
> format. Useable as both a Node library and as a command line tool.

**NOTE: This library is still currently in development! Use at your own risk!**

## Install

``` bash
npm install twine_to_json --save
```

## Usage

To use from the command line, see:

``` bash
twinetojson --help
```

To use as a library with default values:

``` js
var twineToJSON = require("twine_to_json");
twineToJSON({
   in: "path_to_story_file",
   out: "path_to_write_JSON_file",
   ignoreComments: false,
   renderMarkdown: true,
   writeToFile: true,
   prettyPrint: false,
   ignorePassages: [],
   transformPassages: [],
   customTags: [],
   linkFormat: null,
   callback: function(err, story) { /* optional */ }
}).then(function(story) {
    // ...
}).catch(function(err) {
    // ...
});
```

## twineToJSON(options)

**Returns:** `Promise`

### options

Type: `Object`

Hash of options. See below for details.

## Options

### in

Type: `String`

Path to the Twine story file. Required.

### out

Type: `String`

Path to output the JSON file to. Defaults to the current working directory.

### ignoreComments

Type: `Boolean`

Whether or not to remove block comments (i.e. `/* */`). Defaults to false.

### renderMarkdown

Type: `Boolean`

Whether or not to render Markdown in story passages. Defaults to true.

### writeToFile

Type: `Boolean`

Whether or not to write the JSON file to disk. Defaults to true.

### prettyPrint

Type: `Boolean`

Whether or not to write the JSON file in a human-readable format. Defaults to 
false.

### ignorePassages

Type: `Array` of Numbers

An array of passage ids (pids) to exclude from processing. Allows certain 
passages to be skipped over. These passages will not be parsed and will not 
appear in the final JSON output.

#### Usage

```js
twineToJSON({
   ignorePassages: [ 1, 2, 3 ] // ids to ignore
   // ...
});
```

### transformPassages

Type: `Function` or `Array` of Functions

A function or array of functions to apply to a passage. The function(s) will be
provided with two arguments: `passage` (the `Passage` object) and `story` (the
`Story` object). Note that any transform functions will be run on the passage
**before** any other processing or parsing is done!

#### Usage

```js
twineToJSON({
    transformPassages: function(passage, story) {
        // do something to the passage text here...
    },
    // or:
    transformPassages: [
        (passage, story) => {
            // do something to passage here
        },
        (passage, story) => {
            // do something to passage here
        },
        (passage, story) => {
            // etc.
        }
    ]
});
```

### customTags

Type: `Array` of Objects

Allows the replacement of custom HTML tags within the passage text. Each object
specifies the custom tag to replace. It should/can have the following 
properties:

- **name**: (`String`) the tag's name [required]
- **swap**: (`String`) an HTML string to swap the tag with. The tag's children 
    will be appended to the elements in the string.

#### Usage

```js
twineToJSON({
    // ...
    customTags: [
        {
            name: "options",
            swap: '<div class="options"></div>'
            // this will take anything within an <options> tag, wrap it in a 
            // <div> with class 'options', and replace the original tag.
        }
    ] 
});
```

### linkFormat

Type: `Function` or `String`

Specifies how to format the links in the passage text. Can be either an 
[Underscore template string](http://underscorejs.org/#template) or a function. 
Functions will be given an object with three properties:

* `id`: String representing a generated unique identifier for the link.
* `label`: String representing the link's intended display text.
* `passageId`: the `pid` of the passage the link goes to.

#### Usage

```js
twineToJSON({
    // ...
    linkFormat: function(link) {
      return '<a data-passage="' + link.passageId + '">' + link.label + '</a>';
    },
    // or as Underscore template string
    linkFormat: '<a id="<%=id%>" data-passage="<%=passageId%>"><%=label%></a>'
});

### callback

Type: `Function`

Standard callback function for those who don't like Promises. Optional.

## Tests

``` bash
npm test
```

## License

Created by [Jon Stout](http://www.jonstout.net). Licensed under [the MIT license](http://opensource.org/licenses/MIT).

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