JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 10
  • Score
    100M100P100Q56686F
  • License APACHE-2.0

A metadata parser for code-fences in markdown

Package Exports

  • @microflash/fenceparser
  • @microflash/fenceparser/src/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@microflash/fenceparser) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

fenceparser

npm regression license

A metadata parser for code-fences in markdown

Contents

What's this?

Many markdown processors can parse the language token associated with a code-fence. fenceparser is meant for parsing other metadata besides language token. It supports

  • line highlight ranges, (e.g., {1} {3, 7} {9-11, 88} {90, 101..167}) and
  • key-value pairs (e.g., caption='Hello, World')

Install

This package is ESM only.

In Node.js (version 12.20+, 14.14+, or 16.0+), install with npm:

npm install @microflash/fenceparser

Use

Say, you have the following code-fence

```js {1} {3, 7} {9-11, 88} {90, 101..112} text-color='--text-default' syntax_theme="nord" css=`{ *: { display: none }}`

remark will provide the meta and lang for the above code-fence.

{
  "lang": "js",
  "meta": "{1} {3, 7} {9-11, 88} {90, 101..112} text-color='--text-default' syntax_theme=\"nord\" css=`{ *: { display: none }}`"
}

Use the fenceparser to parse the meta as follows.

import parse from '@microflash/fenceparser'

console.log(parse("{1} {3, 7} {9-11, 88} {90, 101..112} text-color='--text-default' syntax_theme=\"nord\" css=`{ *: { display: none }}`"))

Running the above example yields.

{
  'text-color': '--text-default',
  syntax_theme: 'nord',
  css: '{ *: { display: none }}',
  highlight: [
      1,   3,   7,   9,  10,  11,
     88,  90, 101, 102, 103, 104,
    105, 106, 107, 108, 109, 110,
    111, 112
  ]
}

API

The default export is parse.

Syntax

  • For key-value pairs, the key and value must be separated by =. The value must be wrapped within single-quotes ', double-quotes " or backticks ```.
  • To initialize the highlight object, provide comma separated numbers or ranges wrapped within the curly braces.
  • A range must be separated by a hyphen - or double-dots ...
  • Multiple ranges will be merged together in a single array.

Check the fixtures for examples on the syntax.

Development

Any changes in the lexer or parser should have companion tests.

Run the tests with the following command.

npm test

License

Apache License, Version 2.0