|
| 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