All files / app/util editor.ts

55% Statements 11/20
25% Branches 5/20
50% Functions 2/4
55.55% Lines 10/18

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 5980x     80x 80x 80x     5x                 5x 5x 1x 1x           1x                                                                
import * as he from 'he';
import { difference, uniq } from 'lodash-es';
import { marked } from 'marked';
import { getMailbox } from '../mods/mailbox';
import { wikiUriFormat } from '../mods/org/wiki';
import { QUALIFIED_USER_REGEX, TAG_REGEX } from './format';
 
export function getMailboxes(markdown: string, origin = '') {
  return extractPattern(markdown, /[_+]user\/[a-z0-9]+([./][a-z0-9]+)*(@[a-z0-9]+(\.[a-z0-9]+)*)?/g, undefined, QUALIFIED_USER_REGEX)
    .map(u => getMailbox(u, origin));
}
 
export function getTags(markdown: string) {
  return extractPattern(markdown, /#[_+]?[a-z0-9]+([./][a-z0-9]+)*/g, /#([a-z0-9]+([./][a-z0-9]+)*)/, TAG_REGEX);
}
 
export function extractPattern(markdown: string, pattern: RegExp, extractor?: RegExp, validator?: RegExp) {
  const result: string[] = [];
  if (!markdown) return result;
  const matches = markdown.match(pattern);
  for (const s of matches || []) {
    const url = (extractor ? s.match(extractor)?.[1] : s)?.trim();
    if (url && (!validator || validator.test(url))) {
      result.push(url);
    }
  }
  return result;
}
 
export function getIfNew<T>(list: T[], old?: T[]): T[] {
  old ??= [];
  const diff = difference(uniq(list), uniq(old));
  if (!diff.length) return [];
  return list;
}
 
export function getLinks(markdown: string, withText?: RegExp) {
  const result: string[] = [];
  marked.walkTokens(marked.lexer(markdown), t => {
    if (t.type === 'link') {
      if (!t.href) return;
      if (withText && !withText.test(t.text)) return;
      if (t.href.startsWith('mailto:')) t.href = he.decode(t.href);
      result.push(t.href);
      // @ts-ignore
    }
    if (withText) return;
    const customType = t as any;
    if (customType.text) {
      if (customType.type === 'wiki' || customType.type === 'wiki-embed') {
        result.push(wikiUriFormat(customType.text));
      } else if (customType.type === 'embed') {
        result.push(customType.text);
      }
    }
  });
  return result;
}