All files / roosterjs-content-model-api/lib/modelApi/link getLinkUrl.ts

100% Statements 17/17
95.65% Branches 22/23
100% Functions 3/3
100% Lines 14/14

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 301x     1x 1x 1x         1x 103x 103x 103x 2x   101x 101x   101x       54x       103x    
import { matchLink } from './matchLink';
import type { AutoLinkOptions } from 'roosterjs-content-model-types';
 
const COMMON_REGEX = `[\s]*[a-zA-Z0-9+][\s]*`;
const TELEPHONE_REGEX = `(T|t)el:${COMMON_REGEX}`;
const MAILTO_REGEX = `(M|m)ailto:${COMMON_REGEX}`;
 
/**
 * @internal
 */
export function getLinkUrl(text: string, autoLinkOptions?: AutoLinkOptions): string | undefined {
    const { autoLink, autoMailto, autoTel } = autoLinkOptions ?? {};
    const mailtoMatch = matchMailTo(text);
    if (mailtoMatch && !autoMailto) {
        return undefined;
    }
    const linkMatch = autoLink ? matchLink(text)?.normalizedUrl : undefined;
    const telMatch = autoTel ? matchTel(text) : undefined;
 
    return linkMatch || telMatch || mailtoMatch;
}
 
function matchTel(text: string) {
    return text.match(TELEPHONE_REGEX) ? text.toLocaleLowerCase() : undefined;
}
 
function matchMailTo(text: string) {
    return text.match(MAILTO_REGEX) ? text.toLocaleLowerCase() : undefined;
}