Skip to content
Merged
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
13 changes: 12 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,15 @@ jobs:
run: npm ci

- name: Run tests
run: npm test
run: npm test

- name: Check types
run: npm run typecheck

- name: Lint source
run: npm run lint

- name: Verify bundled Action
run: |
npm run build
git diff --exit-code -- dist/
44 changes: 34 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
uses: actions/checkout@v6.0.2

- name: Lint Markdown
uses: lint-md/github-action@v0.2.0
uses: lint-md/github-action@v0.3.0
```

这会使用默认规则检查当前目录下的所有 Markdown 文件。
Expand All @@ -46,7 +46,7 @@ jobs:

```yaml
- name: Lint Markdown
uses: lint-md/github-action@v0.2.0
uses: lint-md/github-action@v0.3.0
with:
files: './docs ./src ./README.md'
```
Expand All @@ -55,7 +55,7 @@ jobs:

```yaml
- name: Lint Markdown
uses: lint-md/github-action@v0.2.0
uses: lint-md/github-action@v0.3.0
with:
configFile: './config/.lintmdrc'
```
Expand All @@ -64,7 +64,7 @@ jobs:

```yaml
- name: Lint Markdown
uses: lint-md/github-action@v0.2.0
uses: lint-md/github-action@v0.3.0
with:
failOnWarnings: 'true'
```
Expand All @@ -82,7 +82,7 @@ jobs:
"space-around-alphabet": 1,
"space-around-number": 1,
"no-empty-code-lang": 2,
"no-trailing-punctuation": 2
"correct-title-trailing-punctuation": 2
}
}
```
Expand All @@ -96,7 +96,7 @@ module.exports = {
"space-around-alphabet": 1,
"space-around-number": 1,
"no-empty-code-lang": 2,
"no-trailing-punctuation": 2
"correct-title-trailing-punctuation": 2
}
}
```
Expand All @@ -110,9 +110,27 @@ module.exports = {

### 规则值含义

- `0` — 关闭规则
- `1` — 作为警告(不影响退出码,除非 `failOnWarnings: 'true'`)
- `2` — 作为错误(会使 Action 失败)
`rules` 对象的键是规则名。值是该规则的检查级别。

| 值 | 级别 | Action 行为 |
| --- | --- | --- |
| `0` | 关闭 | 不执行该规则,也不产生检查结果 |
| `1` | 警告 | 将问题标记为警告。Action 默认成功 |
| `2` | 错误 | 将问题标记为错误。Action 失败 |

当 `failOnWarnings: 'true'` 时,级别 `1` 也会使 Action 失败。
此设置不会把警告转换为错误。

例如,以下配置将空列表项视为错误,将中英文间距问题视为警告:

```json
{
"rules": {
"space-around-alphabet": 1,
"no-empty-list": 2
}
}
```

## 可用规则

Expand All @@ -135,6 +153,12 @@ module.exports = {
| `no-special-characters` | 禁止特殊字符 |
| `use-standard-ellipsis` | 使用标准省略号 |
| `correct-title-trailing-punctuation` | 标题结尾标点符号检查 |
| `require-trailing-spaces` | 软换行前需要两个空格 |
| `space-around-link` | 链接与正文之间需要空格 |
| `no-multiple-blank-lines` | 连续空白行最多保留一行 |

`require-trailing-spaces`、`space-around-link` 和 `no-multiple-blank-lines` 默认关闭。
请在配置文件中显式启用它们。

完整规则列表请参考 [lint-md 文档](https://github.com/lint-md/lint-md#rules-%E9%85%8D%E7%BD%AE)。

Expand All @@ -157,7 +181,7 @@ jobs:
uses: actions/checkout@v6.0.2

- name: Lint Markdown
uses: lint-md/github-action@v0.2.0
uses: lint-md/github-action@v0.3.0
with:
files: './docs ./README.md'
configFile: '.lintmdrc'
Expand Down
14 changes: 14 additions & 0 deletions __tests__/github-action.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import * as path from 'path'
import * as core from '@actions/core'

import { LintMdAction } from '../src/lint-md-action'

Expand All @@ -15,6 +16,10 @@ import { mockAction } from '../src/test-utils'

describe('lint-md GitHub action 测试', () => {

beforeEach(() => {
jest.clearAllMocks()
})

test('用户工作目录下没有任何配置文件', async () => {
process.env.GITHUB_WORKSPACE = path.resolve(process.cwd(), 'examples', 'no-config-file')
mockAction()
Expand Down Expand Up @@ -73,7 +78,12 @@ describe('lint-md GitHub action 测试', () => {
// lint
await lintMdAction.lint()
lintMdAction.showResult()
lintMdAction.showErrorOrPassInfo()
expect(lintMdAction.isPass()).toStrictEqual(true)
expect(core.warning).toHaveBeenCalledWith(expect.stringContaining('[space-around-alphabet]'))
expect(core.error).not.toHaveBeenCalled()
expect(core.setFailed).not.toHaveBeenCalled()
expect(core.info).toHaveBeenCalledWith('\nMarkdown lint passed! 🎉')
})

test('用户在 GitHub Action 将 failOnWarnings 值设为 true,即使只有 warning 本次 ci fail', async () => {
Expand All @@ -82,7 +92,11 @@ describe('lint-md GitHub action 测试', () => {
const lintMdAction = new LintMdAction()
// lint
await lintMdAction.lint()
lintMdAction.showErrorOrPassInfo()
expect(lintMdAction.isPass()).toStrictEqual(false)
expect(core.warning).toHaveBeenCalledWith(expect.stringContaining('[space-around-alphabet]'))
expect(core.error).not.toHaveBeenCalled()
expect(core.setFailed).toHaveBeenCalledWith('\nThere are lint issues in your files 😭...')
})

test('用户自定义的配置文件为 JavaScript 模块', async () => {
Expand Down
16 changes: 16 additions & 0 deletions __tests__/linter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,20 @@ describe('lint 继承对象测试集合', () => {
'warning': 0
})
})

test('支持 core 2.3.0 新增规则', () => {
const result = lintMarkdown(
'第一行\n第二行含有[链接](https://example.com)文字\n\n\n结尾',
{
'require-trailing-spaces': 2,
'space-around-link': 2,
'no-multiple-blank-lines': 2
},
false
)

expect(new Set(result.lintResult.map(item => item.name))).toStrictEqual(
new Set(['require-trailing-spaces', 'space-around-link', 'no-multiple-blank-lines'])
)
})
})
161 changes: 80 additions & 81 deletions dist/index.js

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lint-md/github-action",
"version": "0.2.0",
"version": "0.3.0",
"description": "github action for lint-md",
"main": "dist/index.js",
"scripts": {
Expand Down Expand Up @@ -37,7 +37,7 @@
"devDependencies": {
"@actions/core": "^1.11.1",
"@actions/github": "^9.1.1",
"@lint-md/core": "^2.1.2",
"@lint-md/core": "^2.3.0",
"@types/jest": "^30.0.0",
"@typescript-eslint/eslint-plugin": "^8.61.0",
"@typescript-eslint/parser": "^8.61.0",
Expand Down
39 changes: 16 additions & 23 deletions src/lint-md-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import * as fs from 'fs'
import * as path from 'path'
import * as core from '@actions/core'
import { lintMarkdown, LintMdRulesConfig } from '@lint-md/core'
import { lintMarkdown, type LintMdRulesConfig, type LintReportItem } from '@lint-md/core'
import { glob } from 'glob'

interface LintConfig {
Expand All @@ -18,17 +18,9 @@ interface LintConfig {
extensions?: string[]
}

interface LintResultItem {
loc: { start: { line: number; column: number }; end: { line: number; column: number } }
message: string
name: string
content: string
severity: number
}

interface FileLintResult {
path: string
errors: LintResultItem[]
errors: LintReportItem[]
}

async function loadMdFiles(
Expand Down Expand Up @@ -120,7 +112,7 @@ export class LintMdAction {
if (result.lintResult.length > 0) {
this.fileResults.push({
path: file,
errors: result.lintResult as LintResultItem[],
errors: result.lintResult,
})
}
}
Expand All @@ -137,20 +129,21 @@ export class LintMdAction {
}

showErrorOrPassInfo() {
if (this.isPass()) {
core.info('\nMarkdown Lint free! 🎉')
} else {
for (const fileResult of this.fileResults) {
for (const error of fileResult.errors) {
const message = `[${error.name}] ${error.message} (${fileResult.path}:${error.loc.start.line}:${error.loc.start.column})`
if (error.severity === 2) {
core.error(message)
} else {
core.warning(message)
}
for (const fileResult of this.fileResults) {
for (const error of fileResult.errors) {
const message = `[${error.name}] ${error.message} (${fileResult.path}:${error.loc.start.line}:${error.loc.start.column})`
if (error.severity === 2) {
core.error(message)
} else {
core.warning(message)
}
}
core.setFailed('\nThere are some lint errors in your files 😭...')
}

if (this.isPass()) {
core.info('\nMarkdown lint passed! 🎉')
} else {
core.setFailed('\nThere are lint issues in your files 😭...')
}
}

Expand Down