@gmb/bitmark-parser-generator

A bitmark parser and generator using Peggy.js.
Features:
- Convert bitmark to JSON, and vice-versa.
- Programmatically create or modify bitmark.
- Works in NodeJS or the browser.
- Validate and prettify bitmark.
- Fast, with a small browser footprint, less than 60kB.
List of supported bits
Installation
Package manager
Using yarn:
$ yarn add @gmb/bitmark-parser-generator
Using npm:
$ npm install @gmb/bitmark-parser-generator
CDN
Using jsDelivr CDN (ES6 UMD module):
<script src="https://cdn.jsdelivr.net/npm/@gmb/bitmark-parser-generator@latest/dist/browser/bitmark-parser-generator.min.js"></script>
Using unpkg CDN (ES6 UMD module):
<script src="https://unpkg.com/@gmb/bitmark-parser-generator@latest/dist/browser/bitmark-parser-generator.min.js"></script>
Replace latest with a specific version in either of the URLs above to use a specific version.
Basic Usage
Import
import { BitmarkParserGenerator } from 'bitmark-parser-generator';
const { BitmarkParserGenerator } = require('bitmark-parser-generator');
const { BitmarkParserGenerator } = window.bitmarkParserGenerator;
Conversion
const bpg = new BitmarkParserGenerator();
const json = await bpg.convert("[.article] Hello World");
const bitmark = await bpg.convert('[{"bitmark": "[.article] Hello World","bit": { "type": "article", "format": "bitmark--", "body": "Hello World" }}]');
await bpg.convert("./input.bit", { output: "./output.json" });
await bpg.convert("./input.json", { output: "./output.bit" });
Convertion Options
await bpg.convert("./input.json", {
bitmarkParserType: 'peggy',
outputFormat: 'ast',
outputFile: "./output.ast.json",
fileOptions: {
append: true,
encoding: 'utf8',
},
jsonOptions: {
prettify: 2,
stringify: false,
includeExtraProperties: false,
debugGenerationInline: false,
},
bitmarkOptions: {
explicitTextFormat: false,
cardSetVersion: 1,
debugGenerationInline: false,
}
});
Programmatic Bitmark Creation
import { BitmarkParserGenerator, Builder, Ast, BitType, TextFormat } from 'bitmark-parser-generator';
const bpg = new BitmarkParserGenerator();
const builder = new Builder();
const ast = builder.bitmark({
bits: [
builder.bit({
bitType: BitType.article,
textFormat: TextFormat.bitmarkMinusMinus,
body: builder.body({
bodyParts: [
builder.bodyText({
text: "Hello World!"
})
]
}),
})
]
});
bpg.convert(ast, { output: "./output.bit" });
bpg.convert(ast, { output: "./output.json", outputFormat: 'json' });
Upgrade
The bitmark-parser-generator can 'upgrade' bitmark markup or JSON. This will upgrade the markup / JSON to the latest
version and remove any invalid code or markup.
THIS FEATURE SHOULD BE USED WITH CAUTION. IT WILL POTENTIALLY DELETE DATA.
If the input contains data that is invalid or unrecognised, it will be removed from the output. Therefore it is
recommended to use this feature with source code control or another system for data backup.
const bpg = new BitmarkParserGenerator();
const json = await bpg.convert("[.article] Hello World [$I will be removed as I am invalid]");
const bitmark = await bpg.convert('[{"bitmark": "[.article] Hello World","bit": { "type": "article", "format": "bitmark--", "body": "Hello World", unknownProperty: "Will be removed" }}]');
await bpg.convert("./input.bit", { output: "./output.bit" });
await bpg.convert("./input.json", { output: "./output.json" });
Version
const version = bpg.version();
Advanced Usage
More advanced usage is possible, such as implementing custom streaming output writers.
See the API Documentation for more information.
License
This open source software is licenced under the ISC licence.