diff --git a/packages/formatters/package.json b/packages/formatters/package.json index dcc1f64c6..b7e277073 100644 --- a/packages/formatters/package.json +++ b/packages/formatters/package.json @@ -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", diff --git a/packages/formatters/src/markdown.ts b/packages/formatters/src/markdown.ts index 8a7b969ec..cea57f6ed 100644 --- a/packages/formatters/src/markdown.ts +++ b/packages/formatters/src/markdown.ts @@ -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); @@ -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; +} diff --git a/yarn.lock b/yarn.lock index 6c7d12ac9..9dd175889 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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 @@ -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"