forked from ionic-team/ionic-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
42 lines (34 loc) · 1.44 KB
/
utils.ts
File metadata and controls
42 lines (34 loc) · 1.44 KB
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
import { Colors } from '@ionic/cli-framework';
import { COLORS } from '@ionic/cli/lib/color';
import * as chalk from 'chalk';
import * as escapeStringRegexp from 'escape-string-regexp';
interface CodePair {
open: string;
close: string;
}
interface Color {
_styler: CodePair;
}
type ColorRegistry = { [K in keyof Colors]: Color };
export function links2md(str: string): string {
str = str.replace(/((http|https):\/\/(\w+:{0,1}\w*@)?([^\s\*\)`]+)(\/|\/([\w#!:.?+=&%@!\-\/]))?)/g, '[$1]($1)');
str = str.replace(/\[(\d+)\]/g, '\\\[$1\\\]');
return str;
}
export function ansi2md(str: string): string {
const yellow = chalk.yellow as any as Color;
const { input, strong } = COLORS as any as ColorRegistry;
str = convertAnsiToMd(str, [input._styler], { open: '`', close: '`' });
str = convertAnsiToMd(str, [yellow._styler], { open: '', close: '' });
str = convertAnsiToMd(str, [strong._styler], { open: '**', close: '**' });
return str;
}
export function convertHTMLEntities(str: string): string {
return str.replace(/(?<=^(?:[^\`]|\`[^\`]*\`)*)\<(\S+)\>/g, '<$1>');
}
function convertAnsiToMd(str: string, styles: readonly CodePair[], md: CodePair): string {
const start = styles.map(style => style.open).join('');
const end = [...styles].reverse().map(style => style.close).join('');
const re = new RegExp(escapeStringRegexp(start) + '(.*?)' + escapeStringRegexp(end) , 'g');
return str.replace(re, md.open + '$1' + md.close);
}