Skip to content

Attributes Extension

Vladimir Schneider edited this page Jul 11, 2019 · 4 revisions

flexmark-java extension for attribute list processing

Overview

Converts attributes {...} syntax into attributes AST nodes and adds an attribute provider to set attributes for immediately preceding sibling element during HTML rendering.

Syntax

The attributes is a space separated list of attribute syntax of one of the following:

  • name=value
  • name='value'
  • name="value"
  • #id
  • .class

Attribute which starts with # such as #id-string is equivalent to id="id-string" and .class-name are equivalent to class="class-name"

NOTE: Handling of multiple value assignment for attributes depends on its name:

  • class values are accumulated as a space ( ) separated list.
  • style values are accumulated as a semicolon (;) separated list.
  • all others override any previous values of the same name.

Parsing Details

  • Backward compatible with ASSIGN_TEXT_ATTRIBUTES option is false

    Applied to the preceding element, if that element is plain text then attributes are applied to the parent element of the text.

    If the attributes element is the first element of a paragraph then the attributes will be applied to the paragraph's parent element. Effectively putting attributes element first in a paragraph allows you to set the attributes of list items, block quotes or other paragraph containers.

  • Default Extended Behavior with ASSIGN_TEXT_ATTRIBUTES option is true

    If an attribute element is spaced from the previous plain text element some text {attributes} then attributes are for the parent of the text. If they are attached to the text element some text{attributes} then they are assigned to the immediately preceding plain text span. Within an inline element same rules apply: **bold text {attr}** are for the strong emphasis span, while **bold text{attr}** will wrap the text between the strong emphasis tags, in a span with given attributes.

    If a plain text span is interrupted by an HTML comment then it is considered as two separate plain text spans. ie. some <!---->text{attr} will result in some <!----><span attr>text</span> rendering.

Defined in AttributeExtension from artifact flexmark-ext-attributes

  • ASSIGN_TEXT_ATTRIBUTES, default true. When false attribute assignment rules to nodes are changed not to allow text elements to get attributes.

  • FENCED_CODE_INFO_ATTRIBUTES, default true. When false attribute assignment at end of fenced code info string will be ignored.

    ℹ️ Only attributes elements at the end of the info string line will be parsed.

    For example:

    ```info {#not-id} not {title="Title" caption="Cap"} {caption="Caption"}
    
    ```
    

    The {#not-id} is not parsed as attributes because it is followed non-attributes text and will result in the following HTML:

    <pre title="Title" caption="Caption"><code class="language-info">
    </code></pre>

    And AST:

    Document[0, 81]
      FencedCodeBlock[0, 76] open:[0, 3, "```"] info:[3, 22, "info {#not-id} not "] attributes:[22, 71, "{title=\"Title\" caption=\"Cap\"} {caption=\"Caption\"}"] content:[72, 73] lines[1] close:[73, 76, "```"]
        AttributesNode[22, 51] textOpen:[22, 23, "{"] text:[23, 50, "title=\"Title\" caption=\"Cap\""] textClose:[50, 51, "}"]
          AttributeNode[23, 36] name:[23, 28, "title"] sep:[28, 29, "="] valueOpen:[29, 30, "\""] value:[30, 35, "Title"] valueClose:[35, 36, "\""]
          AttributeNode[37, 50] name:[37, 44, "caption"] sep:[44, 45, "="] valueOpen:[45, 46, "\""] value:[46, 49, "Cap"] valueClose:[49, 50, "\""]
        AttributesNode[52, 71] textOpen:[52, 53, "{"] text:[53, 70, "caption=\"Caption\""] textClose:[70, 71, "}"]
          AttributeNode[53, 70] name:[53, 60, "caption"] sep:[60, 61, "="] valueOpen:[61, 62, "\""] value:[62, 69, "Caption"] valueClose:[69, 70, "\""]
        Text[72, 73] chars:[72, 73, "\n"]
    
  • USE_EMPTY_IMPLICIT_AS_SPAN_DELIMITER default false. When true, will treat {.} or {#} as markers for start of closest matching attributes node to give greater control of where attributes are attached in text.

Use class AttributesExtension from artifact flexmark-ext-attributes.

Full spec: ext_attributes_ast_spec