2.6.0 • Published 2 years ago

remark-custom-blocks v2.6.0

Weekly downloads
4,532
License
MIT
Repository
github
Last release
2 years ago

remark-custom-blocks Build Status Coverage Status

This plugin parses custom Markdown syntax to create new custom blocks. It adds new nodes types to the mdast produced by remark:

  • {yourType}CustomBlock

If you are using rehype, the stringified HTML result will be divs with configurable CSS classes.

It is up to you to have CSS rules producing the desired result for these classes.

The goal is to let you create blocks or panels somewhat similar to these.

Each custom block can specify CSS classes and whether users are allowed or required to add a custom title to the block.

Only inline Markdown will be parsed in titles.

AST nodes (see mdast specification)

By default, the plugin will produce the following two nodes, where whatnot is the name of you block:

interface whatnotCustomBlock <: Parent {
  type: "whatnotCustomBlock";
  data: {
    hName: "div" or "details";
    hProperties: {
      className: [string];
    }
  }
}
interface whatnotCustomBlockBody <: Parent {
  type: "whatnotCustomBlockBody";
  data: {
    hName: "div";
    hProperties: {
      className: [string];
    }
  }
}

If your block has a heading, the following node will also be produced:

interface whatnotCustomBlockHeading <: Parent {
  type: "whatnotCustomBlockHeading";
  data: {
    hName: "div" or "summary";
    hProperties: {
      className: [string];
    }
  }
}

Installation

npm:

npm install remark-custom-blocks

Usage, Configuration, Syntax

Configuration:

The configuration object follows this pattern:

trigger: {
  classes: String, space-separated classes, optional, default: ''
  title: String, 'optional' | 'required', optional, default: custom titles not allowed
  containerElement: String, optional, default: 'div'
  titleElement: String, optional, default: 'div'
  contentsElement: String, optional, default: 'div'
  details: Boolean, optional, default: false
}

containerElement, titleElement, contentsElement allow you to customize the html elements that will be generated by remark-rehype stringifier. The generated HTML structure will be

<containerElement>
    <titleElement>
        block title
    </titleElement>
    <contentsElement>
        block content
    </contentsElement>
</containerElement>

you can see details: true as a shortcut to

{
    containerElement: 'details',
    titleElement: 'summary',
    contentsElement: 'div',
}

Those specific parameters are here to help you build semantic HTML structure.

Dependencies:

const unified = require('unified')
const remarkParse = require('remark-parse')
const stringify = require('rehype-stringify')
const remark2rehype = require('remark-rehype')

const remarkCustomBlocks = require('remark-custom-blocks')

Usage:

unified()
  .use(remarkParse)
  .use(remarkCustomBlocks, {
    foo: {
      classes: 'a-class another-class'
    },
    bar: {
      classes: 'something',
      title: 'optional'
    },
    qux: {
      classes: 'qux-block',
      title: 'required'
    },
    spoiler: {
      classes: 'spoiler-block',
      title: 'optional',
      details: true
    },
  })
  .use(remark2rehype)
  .use(stringify)

The sample configuration provided above would have the following effect:

  1. Allows you to use the following Markdown syntax to create blocks:

    [[foo]]
    | content
    
    [[bar]]
    | content
    
    [[bar | my **title**]]
    | content
    
    [[qux | my title]]
    | content
    
    [[spoiler | my title]]
    | content
    • Block foo cannot have a title, [[foo | title]] will not result in a block.
    • Block bar can have a title but does not need to.
    • Block qux requires a title, [[qux]] will not result in a block.
  2. This Remark plugin would create mdast nodes for these two blocks, these nodes would be of type:

    • fooCustomBlock, content will be in fooCustomBlockBody
    • barCustomBlock, content in barCustomBlockBody, optional title in barCustomBlockHeading
    • quxCustomBlock, content in quxCustomBlockBody, required title in quxCustomBlockHeading
  3. If you're using rehype, you will end up with these 4 divs and 1 details:

    <div class="custom-block a-class another-class">
      <div class="custom-block-body"><p>content</p></div>
    </div>
    
    <div class="custom-block something">
      <div class="custom-block-body"><p>content</p></div>
    </div>
    
    <div class="custom-block something">
      <div class="custom-block-heading">my <strong>title</strong></div>
      <div class="custom-block-body"><p>content</p></div>
    </div>
    
    <div class="custom-block qux-block">
      <div class="custom-block-heading">my title</div>
      <div class="custom-block-body"><p>content</p></div>
    </div>
    
     <details class="custom-block spoiler-block">
      <summary class="custom-block-heading">my title</summary>
      <div class="custom-block-body"><p>content</p></div>
    </details>

License

MIT © Zeste de Savoir

2.6.0

2 years ago

2.5.1

3 years ago

2.5.0

4 years ago

2.4.6

4 years ago

2.4.5

4 years ago

2.4.2

5 years ago

2.4.1

5 years ago

2.4.0

5 years ago

2.3.3

5 years ago

2.3.2

5 years ago

2.3.1

5 years ago

2.3.0

6 years ago

2.2.3

6 years ago

2.2.2

6 years ago

2.2.1

6 years ago

2.2.0

6 years ago

2.1.2

6 years ago

2.1.1

6 years ago

2.1.0

6 years ago

2.0.0

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

0.0.19

7 years ago

0.0.18

7 years ago

0.0.17

7 years ago

0.0.16

7 years ago

0.0.15

7 years ago

0.0.14

7 years ago

0.0.13

7 years ago

0.0.12

7 years ago

0.0.11

7 years ago

0.0.10

7 years ago

0.0.9

7 years ago

0.0.8

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago