Skip to content

Commit

Permalink
chore(deps): use custom markdown table generator
Browse files Browse the repository at this point in the history
markdown-table-ts is not compatible with node12
  • Loading branch information
jb.muscat committed Jul 31, 2024
1 parent 66b9503 commit c756ad5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 20 deletions.
1 change: 0 additions & 1 deletion packages/formatters/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"cliui": "7.0.4",
"lodash": "^4.17.21",
"markdown-escape": "^2.0.0",
"markdown-table-ts": "^1.0.3",
"node-sarif-builder": "^2.0.3",
"strip-ansi": "6.0",
"text-table": "^0.2.0",
Expand Down
49 changes: 38 additions & 11 deletions packages/formatters/src/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ import { printPath, PrintStyle } from '@stoplight/spectral-runtime';
import { Formatter, FormatterContext } from './types';
import { groupBySource } from './utils';
import { DiagnosticSeverity } from '@stoplight/types';
import { getMarkdownTable } from 'markdown-table-ts';
import markdownEscape from 'markdown-escape';
import { getRuleDocumentationUrl } from './utils/getDocumentationUrl';

export const markdown: Formatter = (results, { failSeverity }, ctx?: FormatterContext) => {
const groupedResults = groupBySource(results);

const body: string[][] = [];
const lines: string[][] = [];
for (const [source, validationResults] of Object.entries(groupedResults)) {
validationResults.sort((a, b) => a.range.start.line - b.range.start.line);

Expand All @@ -28,17 +27,45 @@ export const markdown: Formatter = (results, { failSeverity }, ctx?: FormatterCo
const start = `${result.range.start.line}:${result.range.start.character}`;
const end = `${result.range.end.line}:${result.range.end.character}`;
const escapedSource = markdownEscape(source);
body.push([codeWithOptionalLink, escapedPath, escapedMessage, severityString, start, end, escapedSource]);
lines.push([codeWithOptionalLink, escapedPath, escapedMessage, severityString, start, end, escapedSource]);
}
}
}

const table = getMarkdownTable({
table: {
head: ['Code', 'Path', 'Message', 'Severity', 'Start', 'End', 'Source'],
body: body,
},
});

return table;
const headers = ['Code', 'Path', 'Message', 'Severity', 'Start', 'End', 'Source'];
return createMdTable(headers, lines);
};

function createMdTable(headers: string[], lines: string[][]): string {
//find lenght of each column
const columnLengths = headers.map((_, i) => Math.max(...lines.map(line => line[i].length), headers[i].length));

let string = '';
//create markdown table header
string += '|';
for (const header of headers) {
string += ` ${header}`;
string += ' '.repeat(columnLengths[headers.indexOf(header)] - header.length);
string += ' |';
}

//create markdown table rows delimiter
string += '\n|';
for (const _ of headers) {
string += ' ';
string += '-'.repeat(columnLengths[headers.indexOf(_)]);
string += ' |';
}

//create markdown table rows
for (const line of lines) {
string += '\n|';
for (const cell of line) {
string += ` ${cell}`;
string += ' '.repeat(columnLengths[line.indexOf(cell)] - cell.length);
string += ' |';
}
}

return string;
}
8 changes: 0 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2736,7 +2736,6 @@ __metadata:
eol: 0.9.1
lodash: ^4.17.21
markdown-escape: ^2.0.0
markdown-table-ts: ^1.0.3
node-html-parser: ^4.1.5
node-sarif-builder: ^2.0.3
strip-ansi: 6.0
Expand Down Expand Up @@ -9343,13 +9342,6 @@ __metadata:
languageName: node
linkType: hard

"markdown-table-ts@npm:^1.0.3":
version: 1.0.3
resolution: "markdown-table-ts@npm:1.0.3"
checksum: af684c664f14d628cec0b554d3accc12309be02d25c7fc455bfd962e04484d2d2781c4d58dd1bfe07f5e895962b137ddbdae43e05f95ed3190f31f4035174840
languageName: node
linkType: hard

"marked-terminal@npm:^5.0.0":
version: 5.2.0
resolution: "marked-terminal@npm:5.2.0"
Expand Down

0 comments on commit c756ad5

Please sign in to comment.