Skip to content

Commit

Permalink
feature: add markdownlint transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
MeilCli committed May 10, 2021
1 parent 7ccf951 commit 9ff48ca
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Focuses on:
Current supporting lint file format:
- eslint(JSON)
- textlint(JSON)
- markdownlint
- checkstyle
- junit
- compatibility: eslint, textlint
Expand Down Expand Up @@ -63,6 +64,7 @@ jobs:
- Transformer
- [eslint](documents/transformer/eslint.md)
- [textlint](documents/transformer/textlint.md)
- [markdownlint](documents/transformer/markdownlint.md)
- [checkstyle](documents/transformer/checkstyle.md)
- [junit](documents/transformer/junit.md)
- Operator
Expand Down
41 changes: 41 additions & 0 deletions __test__/transformer/markdownlint.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { MarkdownLintTransformer } from "../../src/transformer/markdownlint";
import { LintResult } from "../../src/lint-result";
import * as fs from "fs";

test("transform", async () => {
const text = fs.readFileSync("data/markdownlint.txt", "utf-8");
const transformer = new MarkdownLintTransformer();
const result = transformer.parse(text);

expect(result.length).toBe(3);
expect(result[0]).toMatchObject({
path: "data/markdownlint/test.md",
rule: "MD022/blanks-around-headings/blanks-around-headers",
message: "Headings should be surrounded by blank lines",
startLine: 1,
endLine: undefined,
startColumn: undefined,
endColumn: undefined,
level: "warning",
} as LintResult);
expect(result[1]).toMatchObject({
path: "data/markdownlint/test.md",
rule: "MD041/first-line-heading/first-line-h1",
message: "First line in a file should be a top-level heading",
startLine: 1,
endLine: undefined,
startColumn: undefined,
endColumn: undefined,
level: "warning",
} as LintResult);
expect(result[2]).toMatchObject({
path: "data/markdownlint/test.md",
rule: "MD047/single-trailing-newline",
message: "Files should end with a single newline character",
startLine: 3,
endLine: undefined,
startColumn: undefined,
endColumn: undefined,
level: "warning",
} as LintResult);
});
3 changes: 3 additions & 0 deletions data/markdownlint.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data/markdownlint/test.md:1 MD022/blanks-around-headings/blanks-around-headers Headings should be surrounded by blank lines [Expected: 1; Actual: 0; Below] [Context: "## Summary"]
data/markdownlint/test.md:1 MD041/first-line-heading/first-line-h1 First line in a file should be a top-level heading [Context: "## Summary"]
data/markdownlint/test.md:3:13 MD047/single-trailing-newline Files should end with a single newline character
2 changes: 2 additions & 0 deletions data/markdownlint/command.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1. `npm install -g markdownlint-cli`
1. `markdownlint -o data/markdownlint.txt data/markdownlint/test.md `
3 changes: 3 additions & 0 deletions data/markdownlint/test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Summary
test message
test`message`
22 changes: 22 additions & 0 deletions documents/transformer/markdownlint.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Transformer of markdownlint
```yml
- uses: MeilCli/common-lint-reporter/transformer/markdownlint@v0
with:
report_files: |
markdownlint_report.txt
```
**must save markdownlint result to file**
## Option
### Input
- `report_files`:
- report file glob pattern
- required
- `report_files_follow_symbolic_links`:
- report file glob pattern option
- value: `true` or `false`
- `output_path`:
- output path
- required
- default: `common_lint.json`
55 changes: 55 additions & 0 deletions src/transformer/markdownlint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import * as core from "@actions/core";
import { getOption } from "./option";
import { LintResult } from "../lint-result";
import { Transformer } from "./transformer";

export class MarkdownLintTransformer extends Transformer {
parse(body: string): LintResult[] {
const lintResults: LintResult[] = [];
for (const line of body.split(/(\r\n)|\n|\r/g)) {
if (line == undefined || line.length == 0) {
continue;
}
const pathAndLineAndColumnEndIndex = line.indexOf(" ");
if (pathAndLineAndColumnEndIndex < 0) {
continue;
}
const pathAndLineAndColumn = line.slice(0, pathAndLineAndColumnEndIndex);
const locations = pathAndLineAndColumn.split(":");
const path = locations[0];
const startLine =
1 <= locations.length && Number.isInteger(parseInt(locations[1])) ? parseInt(locations[1]) : undefined;
const ruleEndIndex = line.indexOf(" ", pathAndLineAndColumnEndIndex + 1);
if (ruleEndIndex < 0) {
continue;
}
const rule = line.slice(pathAndLineAndColumnEndIndex + 1, ruleEndIndex);
const message = line.slice(ruleEndIndex + 1).replace(/^(.+?)\s?(\[.+\])*?$/, "$1");
lintResults.push({
path: path,
message: message,
rule: rule,
startLine: startLine,
startColumn: undefined,
endLine: undefined,
endColumn: undefined,
level: "warning",
});
}
return lintResults;
}
}

async function run() {
try {
const option = getOption();
const transformer = new MarkdownLintTransformer();
await transformer.transform(option);
} catch (error) {
core.setFailed(error.message);
}
}

if (process.env.NODE_ENV != "test") {
run();
}
19 changes: 19 additions & 0 deletions transformer/markdownlint/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: 'markdownlint transformer of common lint reporter'
description: 'transformer of markdownlint report file to common lint report'
author: 'MeilCli'
branding:
icon: layers
color: orange
inputs:
report_files:
description: 'report file glob pattern'
required: true
report_files_follow_symbolic_links:
description: 'report file glob pattern option'
output_path:
description: 'output path'
required: true
default: 'common_lint.json'
runs:
using: 'node12'
main: '../../dist/transformer-markdownlint.js'
1 change: 1 addition & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = {
"transformer-checkstyle": "./src/transformer/checkstyle.ts",
"transformer-eslint": "./src/transformer/eslint.ts",
"transformer-textlint": "./src/transformer/textlint.ts",
"transformer-markdownlint": "./src/transformer/markdownlint.ts",
"transformer-junit": "./src/transformer/junit.ts",
"operator-filter": "./src/operator/filter.ts",
"operator-filter-by-file-changed": "./src/operator/filter-by-file-changed.ts",
Expand Down

0 comments on commit 9ff48ca

Please sign in to comment.