@getmorebrain/bitmark-parser-generator

NOTE: THIS PROJECT IS NOT YET PRODUCTION READY.
A bitmark parser and generator using Peggy.js.
List of supported bits
Use this package to:
- parse bitmark markup in NodeJS or the browser.
- programmatically create or modify bitmark.
- validate and prettify bitmark.
Installation
Package manager
Using yarn:
$ yarn add @getmorebrain/bitmark-parser-generator
Using npm:
$ npm install @getmorebrain/bitmark-parser-generator
CDN (TODO - not yet implemented)
Using jsDelivr CDN (ES5 UMD module):
<script src="https://cdn.jsdelivr.net/npm/@getmorebrain/bitmark-parser-generator@<version>/dist/browser/bitmark-parser-generator.min.js"></script>
Using unpkg CDN:
<script src="https://unpkg.com/@getmorebrain/bitmark-parser-generator@<version>/dist/bitmark-parser-generator.min.js"></script>
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.