Skip to content

Commit f7616cf

Browse files
committed
Refactor: Include filename when generating markdown
1 parent f55d982 commit f7616cf

2 files changed

Lines changed: 55 additions & 41 deletions

File tree

extensions/ql-vscode/src/remote-queries/remote-queries-markdown-generation.ts

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import { AnalysisAlert, AnalysisRawResults, AnalysisResults, CodeSnippet, FileLi
88

99
export type MarkdownLinkType = 'local' | 'gist';
1010

11-
// Each array item is a line of the markdown file.
12-
export type MarkdownFile = string[];
11+
export interface MarkdownFile {
12+
fileName: string;
13+
content: string[]; // Each array item is a line of the markdown file.
14+
}
1315

1416
/**
1517
* Generates markdown files with variant analysis results.
@@ -19,9 +21,9 @@ export function generateMarkdown(
1921
analysesResults: AnalysisResults[],
2022
linkType: MarkdownLinkType
2123
): MarkdownFile[] {
22-
const files: MarkdownFile[] = [];
24+
const resultsFiles: MarkdownFile[] = [];
2325
// Generate summary file with links to individual files
24-
const summaryLines: MarkdownFile = generateMarkdownSummary(query);
26+
const summaryFile: MarkdownFile = generateMarkdownSummary(query);
2527
for (const analysisResult of analysesResults) {
2628
const resultsCount = getAnalysisResultCount(analysisResult);
2729
if (resultsCount === 0) {
@@ -30,29 +32,33 @@ export function generateMarkdown(
3032

3133
// Append nwo and results count to the summary table
3234
const nwo = analysisResult.nwo;
33-
const link = createRelativeLink(nwo, linkType);
34-
summaryLines.push(`| ${nwo} | [${resultsCount} result(s)](${link}) |`);
35+
const fileName = createFileName(nwo);
36+
const link = createRelativeLink(fileName, linkType);
37+
summaryFile.content.push(`| ${nwo} | [${resultsCount} result(s)](${link}) |`);
3538

3639
// Generate individual markdown file for each repository
37-
const lines = [
40+
const resultsFileContent = [
3841
`### ${analysisResult.nwo}`,
3942
''
4043
];
4144
for (const interpretedResult of analysisResult.interpretedResults) {
4245
const individualResult = generateMarkdownForInterpretedResult(interpretedResult, query.language);
43-
lines.push(...individualResult);
46+
resultsFileContent.push(...individualResult);
4447
}
4548
if (analysisResult.rawResults) {
4649
const rawResultTable = generateMarkdownForRawResults(analysisResult.rawResults);
47-
lines.push(...rawResultTable);
50+
resultsFileContent.push(...rawResultTable);
4851
}
49-
files.push(lines);
52+
resultsFiles.push({
53+
fileName: fileName,
54+
content: resultsFileContent,
55+
});
5056
}
51-
return [summaryLines, ...files];
57+
return [summaryFile, ...resultsFiles];
5258
}
5359

5460
export function generateMarkdownSummary(query: RemoteQuery): MarkdownFile {
55-
const lines: MarkdownFile = [];
61+
const lines: string[] = [];
5662
// Title
5763
lines.push(
5864
`### Results for "${query.queryName}"`,
@@ -83,11 +89,14 @@ export function generateMarkdownSummary(query: RemoteQuery): MarkdownFile {
8389
'| --- | --- |',
8490
);
8591
// nwo and result count will be appended to this table
86-
return lines;
92+
return {
93+
fileName: 'summary.md',
94+
content: lines
95+
};
8796
}
8897

89-
function generateMarkdownForInterpretedResult(interpretedResult: AnalysisAlert, language: string): MarkdownFile {
90-
const lines: MarkdownFile = [];
98+
function generateMarkdownForInterpretedResult(interpretedResult: AnalysisAlert, language: string): string[] {
99+
const lines: string[] = [];
91100
lines.push(createMarkdownRemoteFileRef(
92101
interpretedResult.fileLink,
93102
interpretedResult.highlightedRegion?.startLine,
@@ -123,8 +132,8 @@ function generateMarkdownForCodeSnippet(
123132
codeSnippet: CodeSnippet,
124133
language: string,
125134
highlightedRegion?: HighlightedRegion
126-
): MarkdownFile {
127-
const lines: MarkdownFile = [];
135+
): string[] {
136+
const lines: string[] = [];
128137
const snippetStartLine = codeSnippet.startLine || 0;
129138
const codeLines = codeSnippet.text
130139
.split('\n')
@@ -183,11 +192,11 @@ function generateMarkdownForAlertMessage(
183192
function generateMarkdownForPathResults(
184193
interpretedResult: AnalysisAlert,
185194
language: string
186-
): MarkdownFile {
187-
const lines: MarkdownFile = [];
195+
): string[] {
196+
const lines: string[] = [];
188197
lines.push('#### Paths', '');
189198
for (const codeFlow of interpretedResult.codeFlows) {
190-
const pathLines: MarkdownFile = [];
199+
const pathLines: string[] = [];
191200
const stepCount = codeFlow.threadFlows.length;
192201
const title = `Path with ${stepCount} steps`;
193202
for (let i = 0; i < stepCount; i++) {
@@ -215,8 +224,8 @@ function generateMarkdownForPathResults(
215224

216225
function generateMarkdownForRawResults(
217226
analysisRawResults: AnalysisRawResults
218-
): MarkdownFile {
219-
const tableRows: MarkdownFile = [];
227+
): string[] {
228+
const tableRows: string[] = [];
220229
const columnCount = analysisRawResults.schema.columns.length;
221230
// Table headers are the column names if they exist, and empty otherwise
222231
const headers = analysisRawResults.schema.columns.map(
@@ -282,8 +291,8 @@ export function createMarkdownRemoteFileRef(
282291
*
283292
* </details>
284293
*/
285-
function buildExpandableMarkdownSection(title: string, contents: MarkdownFile): MarkdownFile {
286-
const expandableLines: MarkdownFile = [];
294+
function buildExpandableMarkdownSection(title: string, contents: string[]): string[] {
295+
const expandableLines: string[] = [];
287296
expandableLines.push(
288297
'<details>',
289298
`<summary>${title}</summary>`,
@@ -296,18 +305,23 @@ function buildExpandableMarkdownSection(title: string, contents: MarkdownFile):
296305
return expandableLines;
297306
}
298307

299-
function createRelativeLink(nwo: string, linkType: MarkdownLinkType): string {
300-
const [owner, repo] = nwo.split('/');
301-
308+
function createRelativeLink(fileName: string, linkType: MarkdownLinkType): string {
302309
switch (linkType) {
303310
case 'local':
304-
return `./${owner}-${repo}.md`;
311+
return `./${fileName}.md`;
305312

306313
case 'gist':
307-
// Creates anchor link to a file in the gist. This is of the form:
314+
// Creates an anchor link to a file in the gist. This is of the form:
308315
// '#file-<name>-<file-extension>'
309-
//
310-
// TODO: Make sure these names align with the actual file names once we upload them to a gist.
311-
return `#file-${owner}-${repo}-md`;
316+
return `#file-${fileName}-md`;
312317
}
313318
}
319+
320+
/**
321+
* Creates the name of the markdown file for a given repository nwo.
322+
* This name doesn't include the file extension.
323+
*/
324+
function createFileName(nwo: string) {
325+
const [owner, repo] = nwo.split('/');
326+
return `${owner}-${repo}`;
327+
}

extensions/ql-vscode/test/pure-tests/remote-queries/markdown-generation/markdown-generation.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ describe('markdown generation', async function() {
2727
const expectedTestOutput2 = await readTestOutputFile('data/interpreted-results/path-problem/results-repo2.md');
2828

2929
// Check that markdown output is correct, after making line endings consistent
30-
expect(markdownFile0.join('\n')).to.equal(expectedSummaryFile);
31-
expect(markdownFile1.join('\n')).to.equal(expectedTestOutput1);
32-
expect(markdownFile2.join('\n')).to.equal(expectedTestOutput2);
30+
expect(markdownFile0.content.join('\n')).to.equal(expectedSummaryFile);
31+
expect(markdownFile1.content.join('\n')).to.equal(expectedTestOutput1);
32+
expect(markdownFile2.content.join('\n')).to.equal(expectedTestOutput2);
3333
});
3434
});
3535

@@ -56,9 +56,9 @@ describe('markdown generation', async function() {
5656
const expectedTestOutput2 = await readTestOutputFile('data/interpreted-results/problem/results-repo2.md');
5757

5858
// Check that markdown output is correct, after making line endings consistent
59-
expect(markdownFile0.join('\n')).to.equal(expectedSummaryFile);
60-
expect(markdownFile1.join('\n')).to.equal(expectedTestOutput1);
61-
expect(markdownFile2.join('\n')).to.equal(expectedTestOutput2);
59+
expect(markdownFile0.content.join('\n')).to.equal(expectedSummaryFile);
60+
expect(markdownFile1.content.join('\n')).to.equal(expectedTestOutput1);
61+
expect(markdownFile2.content.join('\n')).to.equal(expectedTestOutput2);
6262
});
6363
});
6464

@@ -85,9 +85,9 @@ describe('markdown generation', async function() {
8585
const expectedTestOutput2 = await readTestOutputFile('data/raw-results/results-repo2.md');
8686

8787
// Check that markdown output is correct, after making line endings consistent
88-
expect(markdownFile0.join('\n')).to.equal(expectedSummaryFile);
89-
expect(markdownFile1.join('\n')).to.equal(expectedTestOutput1);
90-
expect(markdownFile2.join('\n')).to.equal(expectedTestOutput2);
88+
expect(markdownFile0.content.join('\n')).to.equal(expectedSummaryFile);
89+
expect(markdownFile1.content.join('\n')).to.equal(expectedTestOutput1);
90+
expect(markdownFile2.content.join('\n')).to.equal(expectedTestOutput2);
9191
});
9292
});
9393
});

0 commit comments

Comments
 (0)