💾 Archived View for iich.space › src › util › comments.ts captured on 2022-03-01 at 16:00:09.

View Raw

More Information

⬅️ Previous capture (2021-12-03)

-=-=-=-=-=-=-

const linkPattern =
  '[-a-zA-Z0-9.+]*://[-a-zA-Z0-9@:%._\\+~#=/?,&]*[-a-zA-Z0-9/]';

const SPACE = '​';

export const getLinks = (comment: string): Array<string> => {
  const regex = new RegExp(linkPattern, 'g');
  const links: Array<string> = [];
  let match: RegExpExecArray | null;

  while ((match = regex.exec(comment)) !== null) {
    links.push(match[0]);
  }

  return links;
};

export const sanitize = (comment: string): string => {
  const lines = comment.split('\n');
  const parts = [];

  let pre = false;
  let wasEmpty = false;

  for (const line of lines) {
    if (line.startsWith('```')) {
      pre = !pre;
    }

    if (pre === false) {
      const cleaned = line
        .replace(/ +/g, ' ')
        .replace(/(^|\n)([#*=>])/g, `$1${SPACE}$2`);

      if (cleaned === '') {
        if (wasEmpty === false) {
          parts.push('');
        }

        wasEmpty = true;
      } else {
        parts.push(cleaned);
      }
    } else {
      parts.push(line);
    }
  }

  if (parts[parts.length - 1] === '') {
    parts.pop();
  }

  if (pre === true) {
    parts.push('```');
  }

  return parts.join('\n');
};

export const prepareFingerprint = (raw: string): string =>
  raw.replaceAll(':', '');