# All files / src/html CustomListIconProcessor.ts

11.94% Statements 8/67
0% Branches 0/67
0% Functions 0/14
12.3% Lines 8/65

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
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    1838x

    8x
    8x

    8x

    8x

    8x

    8x

    8x

     import cheerio from 'cheerio';
    import { MbNode, NodeOrText } from '../utils/node';

    const { processIconString } = require('../lib/markdown-it/plugins/markdown-it-icons');
    const emojiDictionary = require('../lib/markdown-it/patches/markdown-it-emoji-fixed');

    interface EmojiData {
      [key: string]: string;
    }

    const emojiData = emojiDictionary as unknown as EmojiData;

    const ICON_ATTRIBUTES = ['icon', 'i-width', 'i-height', 'i-size', 'i-class'];

    interface IconAttributes {
      icon?: string;
      className?: string;
      size?: string;
      width?: string;
      height?: string;
    }

    type IconAttributeDetail = {
      isFirst: boolean;
      iconAttrs: IconAttributes | null;
    };

    function classifyIcon(icon: string) {
      const isEmoji = Object.prototype.hasOwnProperty.call(emojiData, icon);

      return {
        isEmoji,
        unicodeEmoji: isEmoji
          ? emojiData[icon]
          : undefined,
      };
    }

    function createIconSpan(iconAttrs: IconAttributes): cheerio.Cheerio {
      const {
        isEmoji,
        unicodeEmoji,
      } = classifyIcon(iconAttrs.icon!);

      let spanContent;
      if (isEmoji) {
        spanContent = `<span aria-hidden="true">${unicodeEmoji}</span>`;
      } else {
        spanContent = processIconString(iconAttrs.icon);
      }

      let spanNode;
      if (spanContent === null) {
        const img = cheerio(`<img src='${iconAttrs.icon}' alt='Icon'>`)
          .css({ width: iconAttrs.width, height: iconAttrs.height, display: 'inline-block' })
          .addClass(iconAttrs.className || '');
        img.append('\u200B');

        spanContent = cheerio('<span></span>').append(img).css({
          'padding-bottom': '0.3em',
          'padding-top': '0.3em',
        });
        spanNode = cheerio(spanContent).css({ 'font-size': 'unset', 'min-width': '16px' });
      } else {
        spanNode = cheerio(spanContent).css({ 'font-size': 'unset', 'min-width': '16px' });
        spanNode = spanNode.css({ 'font-size': iconAttrs.size }).addClass(iconAttrs.className || '');
      }
      // Add invisible character to avoid the element from being empty
      spanNode.append('\u200B');
      return spanNode.css({
        'line-height': 'unset',
        'margin-inline-end': '0.35em',
        'align-self': 'flex-start',
        'flex-shrink': '0',
      });
    }

    function updateNodeStyle(node: NodeOrText) {
      const nodeCheerio = cheerio(node);

      nodeCheerio.css({
        'list-style-type': 'none',
        'padding-inline-start': '0px',
      });
    }

    // If an item has a specified icon, that icon will be used for it and for subsequent
    // items at that level to prevent duplication of icons attribute declarations.
    const getIconAttributes = (node: MbNode, iconAttrsSoFar?: IconAttributes):
    IconAttributes | null => {
      Iif (iconAttrsSoFar?.icon === undefined && node.attribs.icon === undefined) {
        return null;
      }

      return {
        icon: node.attribs.icon !== undefined ? node.attribs.icon : iconAttrsSoFar?.icon,
        width: node.attribs['i-width'] !== undefined ? node.attribs['i-width'] : iconAttrsSoFar?.width,
        height: node.attribs['i-height'] !== undefined ? node.attribs['i-height'] : iconAttrsSoFar?.height,
        size: node.attribs['i-size'] !== undefined ? node.attribs['i-size'] : iconAttrsSoFar?.size,
        className: node.attribs['i-class'] !== undefined ? node.attribs['i-class'] : iconAttrsSoFar?.className,
      };
    };

    const deleteAttributes = (node: MbNode, attributes: string[]) => {
      attributes.forEach((attr) => {
        delete node.attribs[attr];
      });
    };

    function updateLi(node: MbNode, iconAttributes: IconAttributes) {
      Iif (
        iconAttributes.icon === undefined
      ) return;
      const curLiIcon = getIconAttributes(node, iconAttributes);

      deleteAttributes(node, ICON_ATTRIBUTES);

      // Create a new div and span
      const div = cheerio('<div></div>');
      const iconSpan = createIconSpan(curLiIcon!);

      div.append(cheerio(node.children).remove());

      // Append iconSpan and div to the child
      cheerio(node).append(iconSpan);
      cheerio(node).append(div);

      cheerio(node).css({
        display: 'flex',
      });
    }

    // This function ensures the first item at that level must also be customized.
    // If not, the list will be invalidated and default bullets will be used.
    // This is to prevent unintentional mixing of standard and customized lists.
    // See https://github.com/MarkBind/markbind/pull/2316#discussion_r1255364486 for more details.
    function handleLiNode(node: MbNode, iconAttrValue: IconAttributeDetail) {
      if (iconAttrValue.isFirst) {
        iconAttrValue.iconAttrs = getIconAttributes(node);
        iconAttrValue.isFirst = false;
      } else Iif (iconAttrValue.iconAttrs) {
        iconAttrValue.iconAttrs = getIconAttributes(node, iconAttrValue.iconAttrs);
      }
      updateLi(node, iconAttrValue.iconAttrs ?? {});
    }

    export function processUlNode(node: NodeOrText) {
      const nodeAsMbNode = node as MbNode;
      Iif (nodeAsMbNode.attribs.isIconListProcessed === 'true') {
        delete nodeAsMbNode.attribs.isIconListProcessed;
        return;
      }

      const iconAttrs: IconAttributeDetail[] = [];
      function dfs(currentNode: NodeOrText, level: number) {
        Iif (!iconAttrs[level]) {
          iconAttrs[level] = { isFirst: true, iconAttrs: null };
        }

        const ulNode = currentNode as MbNode;
        const liNodes = ulNode.children.filter(child => child.name === 'li');

        liNodes.forEach((liNode) => {
          const ulChildren = liNode.children?.filter(child => child.name === 'ul');
          handleLiNode(liNode as MbNode, iconAttrs[level]);

          ulChildren?.forEach((ulChildNode) => {
            // Traverse the children if any
            dfs(ulChildNode, level + 1);

            // Insert an `isIconListProcessed` flag attribute to the node.
            Iif (ulChildNode.attribs) {
              ulChildNode.attribs.isIconListProcessed = 'true';
            }
          });
        });
        Iif (iconAttrs[level].iconAttrs !== null) {
          updateNodeStyle(ulNode);
        }
      }
      dfs(node, 0);
    }