{"size": 2375, "ext": "js", "max_stars_repo_path": "src/steps/rewrite-icons.js", "max_stars_repo_name": "adobe/helix-html-pipeline", "max_stars_repo_stars_event_min_datetime": "2022-03-04T05:05:12.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-04T05:05:12.000Z", "max_issues_repo_path": "src/steps/rewrite-icons.js", "max_issues_repo_name": "adobe/helix-html-pipeline", "max_issues_repo_issues_event_min_datetime": "2022-03-03T14:42:35.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-26T17:53:38.000Z", "max_forks_repo_path": "src/steps/rewrite-icons.js", "max_forks_repo_name": "adobe/helix-html-pipeline", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 27, "max_forks_count": null, "avg_line_length": 30.0632911392, "max_line_length": 89, "alphanum_fraction": 0.6366315789}
/*
 * Copyright 2021 Adobe. All rights reserved.
 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy
 * of the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under
 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
 * OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
/* eslint-disable no-param-reassign */
import { h } from 'hastscript';
import { CONTINUE, visit } from 'unist-util-visit';

const REGEXP_ICON = /:(#?[a-z_-]+[a-z\d]*):/gi;

/**
 * Create a <span> icon element:
 *
 * `<span class="icon icon-smile"></span>`
 *
 * @param {string} value the identifier of the icon
 */
function createIcon(value) {
  let name = encodeURIComponent(value);

  // icon starts with #
  if (name.startsWith('%23')) {
    // todo: still support sprite sheets?
    name = name.substring(3);
  }

  // create normal span
  return h('span', { className: ['icon', `icon-${name}`] });
}

/**
 * Rewrite :icons:
 *
 * @type PipelineStep
 * @param content
 */
export default function rewrite({ content }) {
  const { hast } = content;
  visit(hast, (node, idx, parent) => {
    if (node.type !== 'text') {
      return CONTINUE;
    }
    const text = node.value;
    let lastIdx = 0;
    for (const match of text.matchAll(REGEXP_ICON)) {
      const [matched, icon] = match;
      const before = text.substring(lastIdx, match.index);
      if (before) {
        // textNode.parentNode.insertBefore(document.createTextNode(before), textNode);
        parent.children.splice(idx, 0, { type: 'text', value: before });
        idx += 1;
      }
      // textNode.parentNode.insertBefore(createIcon(document, icon), textNode);
      parent.children.splice(idx, 0, createIcon(icon));
      idx += 1;
      lastIdx = match.index + matched.length;
    }

    if (lastIdx && lastIdx <= text.length) {
      // there is still some text left
      const after = text.substring(lastIdx);
      if (after) {
        node.value = after;
      } else {
        parent.children.splice(idx, 1);
        idx -= 1;
      }
    }
    return idx + 1;
  });
}
