Skip to content

Macros Extension

Sascha edited this page Oct 1, 2018 · 2 revisions

flexmark-java extension for defining and expanding simple macros in markdown

Overview

Macro Definitions are block elements which can contain any markdown element(s) but can be expanded in a block or inline context, allowing block elements to be used where only inline elements are permitted by the syntax.

Syntax

Macro definitions start with a line of >>>macroName where macro name consists of a-z, A-Z, 0-9, - or _ and is terminated by a line with <<<. All text between these lines is parsed as markdown blocks. Macro definition blocks can only be defined at the document level without any indentation and are not rendered in the output.

Macro reference is an inline element of the form <<<macroName>>> and is replaced by the macro definition rendered content, including any block elements. This implies that it is possible to include block elements where they are normally not possible using plain markdown.

When a macro definition contains a single paragraph node then it will be rendered as text contained by the paragraph without the <p></p> wrapper to allow plain text to be rendered as an inline text.

If a macro reference contains an undefined macroName then the node will be rendered as plain text consisting of <<<macroName>>>

Recursive macros are resolved by stopping macro expansion on the first macro reference which refers to a macro definition which is currently being expanded. The first recursive macro reference will result in no expansion.

Examples

Paragraph with a <<<simpleMacro>>>.

>>>simpleMacro
**bold** text
<<<

Is equivalent to:

Paragraph with a **bold** text.
Paragraph with a <<<blockMacro>>> inserted.

>>>blockMacro
1. item 1
1. item 2
<<<

Renders as:

<p>Paragraph with a 
<ol>
  <li>item 1</li>
  <li>item 2</li>
</ol>
inserted.</p>

To give:

Paragraph with a

  1. item 1
  2. item 2
inserted.

Adding complex content to tables is possible with macros.

|   Complex   |     Data     |
|-------------|--------------|
| <<<macro>>> | <<<macro2>>> |

>>>macro
1. Item 1
2. Item 2
3. Item 3

| Column 1 | Column 2 |
|----------|----------|
| a        | b        |
| c        | d        |

> Block Quote and more

<<<

>>>macro2
- Item 1
- Item 2
- Item 3
<<<

Renders as:

<table>
  <thead>
    <tr><th>   Complex   </th><th>     Data     </th></tr>
  </thead>
  <tbody>
    <tr><td> 
    <ol>
      <li>Item 1</li>
      <li>Item 2</li>
      <li>Item 3</li>
    </ol>
    <table>
      <thead>
        <tr><th> Column 1 </th><th> Column 2 </th></tr>
      </thead>
      <tbody>
        <tr><td> a        </td><td> b        </td></tr>
        <tr><td> c        </td><td> d        </td></tr>
      </tbody>
    </table>
    <blockquote>
      <p>Block Quote and more</p>
    </blockquote>
    </td><td> 
    <ul>
      <li>Item 1</li>
      <li>Item 2</li>
      <li>Item 3</li>
    </ul>
    </td></tr>
  </tbody>
</table>

To result in:

Complex Data
  1. Item 1
  2. Item 2
  3. Item 3
Column 1 Column 2
a b
c d

Block Quote and more

  • Item 1
  • Item 2
  • Item 3

Parsing Details

Use class MacrosExtension in artifact flexmark-ext-macros.

The following options are available:

Defined in MacrosExtension class:

Static Field Default Value Description
MACRO_DEFINITIONS new repository repository for document's macro definitions
MACRO_DEFINITIONS_KEEP KeepType.FIRST which duplicates to keep.
MACRO_DEFINITIONS_PLACEMENT ElementPlacement.AS_IS formatting option see: Markdown Formatter
MACRO_DEFINITIONS_SORT ElementPlacementSort.AS_IS formatting option see: Markdown Formatter
SOURCE_WRAP_MACRO_REFERENCES false when true will wrap macro reference generated code in div or span with source position information