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

100% Statements 15/15
95.24% Branches 20/21
100% Functions 3/3
100% Lines 12/12

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 271x     1x 1x 1x         1x 60x 60x 60x 60x   60x       34x       34x    
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 linkMatch = autoLink ? matchLink(text)?.normalizedUrl : undefined;
    const telMatch = autoTel ? matchTel(text) : undefined;
    const mailtoMatch = autoMailto ? matchMailTo(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;
}