Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat) Add Markdown output format and corresponding test #629

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const cli = meow(
Defaults to 'false'.

--format, -f
Return the data in CSV or JSON format.
Return the data in CSV, JSON, or MD (Markdown) format.

--help
Show this command.
Expand Down Expand Up @@ -234,6 +234,24 @@ async function main() {
return;
}

if (format === Format.MD) {
result.links = filteredResults;
// Output the results in Markdown table format
console.log('# Link Check Results\n');
console.log('| URL | Status | State | Parent | Failure Details |');
console.log('|-----|--------|-------|--------|-----------------|');
for (const link of result.links) {
const failureDetails =
link.failureDetails && verbosity <= LogLevel.DEBUG
? JSON.stringify(link.failureDetails).replace(/\|/g, '\\|')
: '';
console.log(
`| ${link.url} | ${link.status} | ${link.state} | ${link.parent || ''} | ${failureDetails} |`,
);
}
return;
}

// Build a collection scanned links, collated by the parent link used in
// the scan. For example:
// {
Expand Down Expand Up @@ -366,9 +384,11 @@ function parseFormat(flags: Flags): Format {
}

flags.format = flags.format.toUpperCase();
const options = Object.values(Format);
const options = Object.values(Format).map((format) => format.toString());
if (!options.includes(flags.format)) {
throw new Error("Invalid flag: FORMAT must be 'TEXT', 'JSON', or 'CSV'.");
throw new Error(
"Invalid flag: FORMAT must be one of 'TEXT', 'JSON', 'CSV', or 'MD'.",
);
}

return Format[flags.format as keyof typeof Format];
Expand Down
1 change: 1 addition & 0 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export enum Format {
TEXT = 0,
JSON = 1,
CSV = 2,
MD = 3,
}

export class Logger {
Expand Down
21 changes: 21 additions & 0 deletions test/test.cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,27 @@ describe('cli', function () {
assert.ok(output.links);
});

it('should provide Markdown if asked nicely', async () => {
const response = await execa(node, [
linkinator,
'--format',
'md',
'test/fixtures/markdown/README.md',
]);
assert.match(
response.stdout,
/\| test\/fixtures\/markdown\/README.md \| 200 \| OK \|/,
);
assert.match(
response.stdout,
/\| URL \| Status \| State \| Parent \| Failure Details \|/,
);
assert.match(
response.stdout,
/\|-----|--------|-------|--------|-----------------|/,
);
});

it('should look for linkinator.config.json in the cwd', async () => {
const response = await execa(node, ['../../../build/src/cli.js', '.'], {
cwd: 'test/fixtures/defaultconfig',
Expand Down