-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathformatting.ts
More file actions
173 lines (162 loc) · 4.41 KB
/
Copy pathformatting.ts
File metadata and controls
173 lines (162 loc) · 4.41 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import {
type HeadingLevel,
type InlineText,
MarkdownDocument,
md,
} from 'build-md';
import path from 'node:path';
import type {
AuditReport,
SourceFileLocation,
Table,
Tree,
} from '@code-pushup/models';
import { formatAsciiTree } from '../text-formats/ascii/tree.js';
import {
columnsToStringArray,
getColumnAlignments,
rowToStringArray,
} from '../text-formats/table.js';
import { AUDIT_DETAILS_HEADING_LEVEL } from './constants.js';
import {
getEnvironmentType,
getGitHubBaseUrl,
getGitLabBaseUrl,
} from './environment-type.js';
import type { MdReportOptions } from './types.js';
export function tableSection(
table: Table,
options?: {
level?: HeadingLevel;
},
): MarkdownDocument | null {
if (table.rows.length === 0) {
return null;
}
const { level = AUDIT_DETAILS_HEADING_LEVEL } = options ?? {};
const columns = columnsToStringArray(table);
const alignments = getColumnAlignments(table);
const rows = rowToStringArray(table);
return new MarkdownDocument().heading(level, table.title).table(
columns.map((heading, i) => {
const alignment = alignments[i];
if (alignment) {
return { heading, alignment };
}
return heading;
}),
rows,
);
}
export function treeSection(
tree: Tree,
options?: {
level?: HeadingLevel;
},
): MarkdownDocument {
const { level = AUDIT_DETAILS_HEADING_LEVEL } = options ?? {};
return new MarkdownDocument()
.heading(level, tree.title)
.code(formatAsciiTree(tree));
}
// @TODO extract `Pick<AuditReport, 'docsUrl' | 'description'>` to a reusable schema and type
export function metaDescription(
audit: Pick<AuditReport, 'docsUrl' | 'description'>,
): InlineText {
const docsUrl = audit.docsUrl;
const description = audit.description?.trim();
if (docsUrl) {
const docsLink = md.link(docsUrl, '📖 Docs');
if (!description) {
return docsLink;
}
const parsedDescription = description.endsWith('```')
? `${description}\n\n`
: `${description} `;
return md`${parsedDescription}${docsLink}`;
}
if (description && description.trim().length > 0) {
return description;
}
return '';
}
/**
* Link to local source for IDE
* @param source
* @param reportLocation
*
* @example
* linkToLocalSourceInIde({ file: 'src/index.ts' }, { outputDir: '.code-pushup' }) // [`src/index.ts`](../src/index.ts)
*/
export function linkToLocalSourceForIde(
source: SourceFileLocation,
options?: Pick<MdReportOptions, 'outputDir'>,
): InlineText {
const { file, position } = source;
const { outputDir } = options ?? {};
// NOT linkable
if (!outputDir) {
return md.code(file);
}
return md.link(formatFileLink(file, position, outputDir), md.code(file));
}
export function formatSourceLine(
position: SourceFileLocation['position'],
): string {
if (!position) {
return '';
}
const { startLine, endLine } = position;
return endLine && startLine !== endLine
? `${startLine}-${endLine}`
: `${startLine}`;
}
export function formatGitHubLink(
file: string,
position: SourceFileLocation['position'],
): string {
const baseUrl = getGitHubBaseUrl();
if (!position) {
return `${baseUrl}/${file}`;
}
const { startLine, endLine, startColumn, endColumn } = position;
const start = startColumn ? `L${startLine}C${startColumn}` : `L${startLine}`;
const end = endLine
? endColumn
? `L${endLine}C${endColumn}`
: `L${endLine}`
: '';
const lineRange = end && start !== end ? `${start}-${end}` : start;
return `${baseUrl}/${file}#${lineRange}`;
}
export function formatGitLabLink(
file: string,
position: SourceFileLocation['position'],
): string {
const baseUrl = getGitLabBaseUrl();
if (!position) {
return `${baseUrl}/${file}`;
}
const { startLine, endLine } = position;
const lineRange =
endLine && startLine !== endLine ? `${startLine}-${endLine}` : startLine;
return `${baseUrl}/${file}#L${lineRange}`;
}
export function formatFileLink(
file: string,
position: SourceFileLocation['position'],
outputDir: string,
): string {
const relativePath = path.posix.relative(outputDir, file);
const env = getEnvironmentType();
switch (env) {
case 'vscode':
return position ? `${relativePath}#L${position.startLine}` : relativePath;
case 'github':
return formatGitHubLink(file, position);
case 'gitlab':
return formatGitLabLink(file, position);
default:
return relativePath;
}
}