Skip to content

Instantly share code, notes, and snippets.

@jayrbolton
Last active April 21, 2022 16:01
Show Gist options
  • Save jayrbolton/5c52d13bed5e261a485762b90b290f88 to your computer and use it in GitHub Desktop.
Save jayrbolton/5c52d13bed5e261a485762b90b290f88 to your computer and use it in GitHub Desktop.

JSON generated by markdown

One downside of JSON is it is not very readable, especially when used as a configuration language.

To fix this, we can embed JSON data in a markdown file using a certain format that is readable and commentable. A script takes the markdown file and converts it into one or more JSON files.

Define the start of a JSON file in your markdown with a header that is a link to a local json file, such as:

## [my_config.json](my_config.json)

Bulleted lists underneath that section are the keys and values for your JSON file. Similar to YAML, a colon separates key/values, and you don't need quotes. Any text outside the bulleted list are comments.

## [my_config.json](my_config.json)

These are the keys and values for the above JSON file:

* key1: value1
* key2: value2

Sub-headers below the JSON file header denote the start of nested objects.

## [my_config.json](my_config.json)

These are the keys and values for this JSON file:

* key1: value1
* key2: value2

### my_subobject

Everything in this section is set under the `my_subobject` key:

* subkey1: value1
* subkey2: value2

The above JSON is equivalent to a more compact nested list:

## [my_config.json](my_config.json)

* key1: value1
* key2: value2
* my_subobject:
  * subkey1: value1
  * subkey2: value2

You can also reduce nesting with a list by using a JSON path:

## [my_config.json](my_config.json)

* key1: value1
* key2: value2
* my_subobject.subkey1: value1
* my_subobject.subkey2: value2

If you need a key with dots in it, simply quote each part: "parent.key"."child.key": value

NPM package definition

The following is an example of a readable NPM package configuration that generates a package.json.

scripts

Run all tests with npm run test

  • test: standard && tape test/index.js | tap-spec

dependencies

  • curry: 1.2.0

devDependencies

  • ramda: 0.24.1
  • standard: 10.0.3
  • tap-spec: 4.1.1
  • tape: 4.8.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment