format report output
User story
As a reader of the CLI reports I want to be able to easily consume the generated information. Formatting is key here. Even if many IDEs support a markdown viewer or auto-format JSON files, it still helps to be able to read the reports in a plain editor.
The current output of the markdown format is not formatted.
Acceptance criteria
- [ ] implement helper form text formatting by wrapping prettier
- [ ] accept the
contentto format and a optional argument for theformat
- [ ] accept the
- [ ] format all markdown formats accordingly
- [ ] ensure formatting is given over snapshot tests
Implementation details
Prettier wrapper:
// packages/utils/src/lib/formatting.ts
import { format, Options } from 'prettier';
export function formatText(
content: string,
parser: Options['parser'] = 'typescript'
) {
return format(content, {
parser
}).trim();
}
Markdown helper:
// packages/utils/src/lib/report/md/table.ts
import { formatText } from '../formatting';
export function tableHtml(data: (string | number)[][]): string {
if (data.length === 0) {
throw new Error("Data can't be empty");
}
const tableContent = data.map((arr, index) => {
if (index === 0) {
const headerRow = arr.map(s => `<th>${s}</th>`).join('');
return `<tr>${headerRow}</tr>`;
}
const row = arr.map(s => `<td>${s}</td>`).join('');
return `<tr>${row}</tr>`;
});
return formatText(`<table>${tableContent.join('')}</table>`;
}
We should check if prettier is installed and run it if available. That way we don't have to list an extra dependency.
I believe this one was closed by the new md-build library. Cc @matejchalk
Indeed, after #755 was merged the tables have padding and alignment to make the source more readable. In general, the output should be prettified already, so there's no need to run Prettier afterwards.