# All files / src/html cardStackProcessor.ts

100% Statements 23/23
93.75% Branches 15/16
100% Functions 5/5
100% Lines 23/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    5710x

    38x

    19x

    10x

    11x
    1x

    10x
    19x

    10x
    1x

    9x
    9x

    9x
    9x
    38x
    14x
    13x

    13x

    9x
    8x

    8x
    8x

    9x

     import { encode } from 'html-entities';
    import { MbNode, NodeOrText } from '../utils/node';

    export type CardStackTagConfig = { name: string; color?: string };

    function isTag(child: NodeOrText): child is MbNode {
      return child.type === 'tag' && (child as MbNode).name === 'tag';
    }

    function isTags(child: NodeOrText): child is MbNode {
      return child.type === 'tag' && (child as MbNode).name === 'tags';
    }

    export function processCardStackAttributes(node: MbNode) {
      // Look for a <tags> child element
      if (!node.children) {
        return;
      }

      const tagsNodeIndex = node.children.findIndex(
        child => isTags(child),
      );

      if (tagsNodeIndex === -1) {
        return;
      }

      const tagsNode = node.children[tagsNodeIndex] as MbNode;
      const tagConfigs: Array<CardStackTagConfig> = [];

      // Parse each <tag> element
      if (tagsNode.children) {
        tagsNode.children.forEach((child) => {
          if (isTag(child)) {
            if (child.attribs?.name) {
              const config: CardStackTagConfig = {
                name: child.attribs.name,
                ...(child.attribs.color && { color: child.attribs.color }),
              };
              tagConfigs.push(config);
            }
          }
        });
      }

      // Add tag-configs as a prop if we found any tags
      if (tagConfigs.length > 0) {
        const jsonString = JSON.stringify(tagConfigs);
        // Replace double quotes with HTML entities to avoid SSR warnings
        const escapedJson = encode(jsonString);
        node.attribs['data-tag-configs'] = escapedJson;
      }

      // Remove the <tags> node from the DOM tree
      node.children.splice(tagsNodeIndex, 1);
    }