Skip to content

Commit 2e28146

Browse files
committed
Create markdown files for sharing results
1 parent 85e051a commit 2e28146

8 files changed

Lines changed: 827 additions & 1 deletion

File tree

extensions/ql-vscode/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1221,7 +1221,7 @@
12211221
}
12221222
},
12231223
"lint-staged": {
1224-
"./**/*.{json,css,scss,md}": [
1224+
"./**/*.{json,css,scss}": [
12251225
"prettier --write"
12261226
],
12271227
"./**/*.{ts,tsx}": [

extensions/ql-vscode/src/pure/location-link-utils.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,17 @@ export function createRemoteFileRef(
1313
return `${fileLink.fileLinkPrefix}/${fileLink.filePath}`;
1414
}
1515
}
16+
17+
/**
18+
* Creates a markdown link to a remote file.
19+
* If the "link text" is not provided, we use the file path.
20+
*/
21+
export function createMarkdownRemoteFileRef(
22+
fileLink: FileLink,
23+
startLine?: number,
24+
endLine?: number,
25+
linkText?: string,
26+
): string {
27+
const markdownLink = `[${linkText || fileLink.filePath}](${createRemoteFileRef(fileLink, startLine, endLine)})`;
28+
return markdownLink;
29+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { createMarkdownRemoteFileRef } from '../pure/location-link-utils';
2+
import { RemoteQuery } from './remote-query';
3+
import { AnalysisAlert, AnalysisResults } from './shared/analysis-result';
4+
5+
// Each array item is a line of the markdown file.
6+
export type MarkdownFile = string[];
7+
8+
export function generateMarkdown(query: RemoteQuery, analysesResults: AnalysisResults[]): MarkdownFile[] {
9+
const files: MarkdownFile[] = [];
10+
for (const analysisResult of analysesResults) {
11+
if (analysisResult.interpretedResults.length === 0) {
12+
continue;
13+
}
14+
const lines = [
15+
`### ${analysisResult.nwo}`,
16+
''
17+
];
18+
for (const interpretedResult of analysisResult.interpretedResults) {
19+
const individualResult = generateMarkdownForInterpretedResult(interpretedResult, query.language);
20+
lines.push(...individualResult);
21+
}
22+
files.push(lines);
23+
}
24+
return files;
25+
26+
}
27+
28+
function generateMarkdownForInterpretedResult(interpretedResult: AnalysisAlert, language: string): MarkdownFile {
29+
const lines: MarkdownFile = [];
30+
lines.push(createMarkdownRemoteFileRef(
31+
interpretedResult.fileLink,
32+
interpretedResult.highlightedRegion?.startLine,
33+
interpretedResult.highlightedRegion?.endLine
34+
), '');
35+
const codeSnippet = interpretedResult.codeSnippet?.text;
36+
if (codeSnippet) {
37+
lines.push(
38+
`\`\`\`${language}`,
39+
...codeSnippet.split('\n'),
40+
'```',
41+
''
42+
);
43+
}
44+
const alertMessage = buildAlertMessage(interpretedResult);
45+
lines.push(alertMessage);
46+
47+
// Padding between results
48+
lines.push(
49+
'',
50+
'----------------------------------------',
51+
'',
52+
);
53+
return lines;
54+
}
55+
56+
function buildAlertMessage(interpretedResult: AnalysisAlert): string {
57+
let alertMessage = '';
58+
for (const token of interpretedResult.message.tokens) {
59+
if (token.t === 'text') {
60+
alertMessage += token.text;
61+
} else if (token.t === 'location') {
62+
alertMessage += createMarkdownRemoteFileRef(
63+
token.location.fileLink,
64+
token.location.highlightedRegion?.startLine,
65+
token.location.highlightedRegion?.endLine,
66+
token.text,
67+
);
68+
}
69+
}
70+
return alertMessage;
71+
}

0 commit comments

Comments
 (0)