Function sortMarksByOccurences

  • Figures out the optimal order of marks, in order to minimize the amount of nesting/repeated elements in environments such as HTML. For instance, a naive implementation might render something like:

    <strong>This block contains </strong>
    <strong><a href="https://some.url/">a link</a></strong>
    <strong> and some bolded text</strong>

    ...whereas an optimal order would be:

    <strong>
    This block contains <a href="https://some.url/">a link</a> and some bolded text
    </strong>

    This is particularly necessary for cases like links, where you don't want multiple individual links for different segments of the link text, even if parts of it are bolded/italicized.

    This function is meant to be used like: block.children.map(sortMarksByOccurences), and is used internally in buildMarksTree().

    The marks are sorted in the following order:

    1. Marks that are shared amongst the most adjacent siblings
    2. Non-default marks (links, custom metadata)
    3. Decorators (bold, emphasis, code etc), in a predefined, preferred order

    Parameters

    • span: PortableTextSpan | TypedObject

      The current span to sort

    • index: number

      The index of the current span within the block

    • blockChildren: (PortableTextSpan | TypedObject)[]

      All children of the block being sorted

    Returns string[]

    Array of decorators and annotations, sorted by "most adjacent siblings"