@gmb/bitmark-parser-generator

A bitmark parser and generator using Peggy.js.
Try out bitmark - visit the bitmark Playground
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 '@gmb/bitmark-parser-generator';
const { BitmarkParserGenerator } = require('@gmb/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++", "bitLevel": 1, "body": "Hello World" }}]');
await bpg.convert("./input.bitmark", { output: "./output.json" });
await bpg.convert("./input.json", { output: "./output.bitmark" });
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,
prettifyJson: 2,
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.bitmarkText,
body: builder.body({
bodyParts: [
builder.bodyText({
text: "Hello World!"
})
]
}),
})
]
});
bpg.convert(ast, { output: "./output.bitmark" });
bpg.convert(ast, { output: "./output.json", outputFormat: 'json' });
Breakscaping
A text can be breakscaped programmatically for inclusion in bitmark.
NOTE: It is recommended the bit builder documented above in Programmatic Bitmark Creation is used rather than
hand-coding bitmark creation, because it guarantees the bitmark will be valid, and all breakscaping will be correct.
When breakscaping a text for inclusion in bitmark, the breakscaping applied depends on where that text appears in the
bitmark. The following four text locations require different breakscaping:
| bitmark++ | body |
| bitmark++ | tag |
| text (not bitmark++) | body |
| text (not bitmark++) | tag |
Also, if a plain text divider ==== text ==== is used, then text after the divider must be breakscaped using
text:body even if the bit is bitmark++.
import { BitmarkParserGenerator, BodyTextFormat, TextLocation, InputType } from 'bitmark-parser-generator';
const bpg = new BitmarkParserGenerator();
const breakscaped = bpg.breakscapeText("This is the [!text] to be breakscaped", {
inputFormat: InputType.string,
textFormat: BodyTextFormat.bitmarkPlusPlus,
textLocation: TextLocation.body,
});
const breakscaped = bpg.breakscapeText("This is the [!text] to be breakscaped", {
inputFormat: InputType.string,
textFormat: BodyTextFormat.bitmarkPlusPlus,
textLocation: TextLocation.tag,
});
Info
Information about the supported bits can be retreived via the info API
import { BitmarkParserGenerator } from 'bitmark-parser-generator';
const bpg = new BitmarkParserGenerator();
const info = bpg.info();
console.log(JSON.stringify(info, null, 2));
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++",
"bitLevel": 1, "body": "Hello World", unknownProperty: "Will be removed" }}]');
await bpg.convert("./input.bitmark", { output: "./output.bitmark" });
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.