dsh-tablelint 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +39 -2
- package/bin/cli.js +27 -0
- package/cordis.patch.yml +4 -0
- package/package.json +45 -4
- package/src/index.js +125 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 zjukop3
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,40 @@
|
|
|
1
|
-
#
|
|
1
|
+
# dsh-tablelint
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A ragged table renders correctly in the editor that produced it and broken everywhere it is published. Cell counts are cheap to compare and expensive to notice by eye.
|
|
4
|
+
|
|
5
|
+
<!-- example:start -->
|
|
6
|
+
## 真实输入输出
|
|
7
|
+
|
|
8
|
+
输入:
|
|
9
|
+
|
|
10
|
+
```markdown
|
|
11
|
+
| 项目 | 数量 |
|
|
12
|
+
| --- | --- |
|
|
13
|
+
| 一 | 2 |
|
|
14
|
+
| 三 | 4 | 6 |
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
真实运行(逐字输出,由测试与工具保持同步):
|
|
18
|
+
|
|
19
|
+
```text
|
|
20
|
+
$ tablelint report-table.md
|
|
21
|
+
error column-count line 4: 3 cells in a 2-column table
|
|
22
|
+
fail: 1 errors, 0 warnings
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
出口码:1。
|
|
26
|
+
<!-- example:start -->
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
npx tablelint report.md # human output, exit 1 on findings
|
|
30
|
+
npx tablelint report.md --json # machine output for a pipeline
|
|
31
|
+
npx tablelint report.md --warnings-fail
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
生成报告的 Markdown 表格检查:行内单元格数与表头不一致、分隔行与表头数量不符、表头重名或为空、只有表头没有数据行。
|
|
35
|
+
|
|
36
|
+
Exit codes: `0` clean, `1` findings, `2` usage or read error. In DSH the same function is behind `POST /tablelint/api` with `{"text":"…"}` or `{"path":"…"}`, so the panel and CI agree by construction.
|
|
37
|
+
|
|
38
|
+
Part of [dsh-verify-kit](https://github.com/zjukop3/dsh-verify-kit): five small deterministic checks, each one verb, each runnable without DSH.
|
|
39
|
+
|
|
40
|
+
MIT.
|
package/bin/cli.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Exit codes: 0 clean, 1 findings, 2 usage or read error. The same function the
|
|
3
|
+
// host route calls, so a check that passes here cannot fail in the panel.
|
|
4
|
+
import { readFile } from 'node:fs/promises'
|
|
5
|
+
import { lintTables } from '../src/index.js'
|
|
6
|
+
|
|
7
|
+
const args = process.argv.slice(2)
|
|
8
|
+
const target = args.find(argument => !argument.startsWith('--'))
|
|
9
|
+
if (!target) {
|
|
10
|
+
process.stderr.write('usage: tablelint <file.md> [--warnings-fail]\n')
|
|
11
|
+
process.exit(2)
|
|
12
|
+
}
|
|
13
|
+
const asJson = args.includes('--json')
|
|
14
|
+
try {
|
|
15
|
+
const result = await lintTables(await readFile(target, 'utf8'))
|
|
16
|
+
if (asJson) {
|
|
17
|
+
process.stdout.write(`${JSON.stringify(result, null, 2)}\n`)
|
|
18
|
+
} else {
|
|
19
|
+
for (const finding of result.findings) process.stdout.write(`${finding.level} ${finding.rule} ${finding.message}\n`)
|
|
20
|
+
process.stdout.write(`${result.ok ? 'ok' : 'fail'}: ${result.errors} errors, ${result.warnings} warnings\n`)
|
|
21
|
+
}
|
|
22
|
+
const failed = !result.ok || (args.includes('--warnings-fail') && result.warnings > 0)
|
|
23
|
+
process.exit(failed ? 1 : 0)
|
|
24
|
+
} catch (error) {
|
|
25
|
+
process.stderr.write(`${error.message}\n`)
|
|
26
|
+
process.exit(2)
|
|
27
|
+
}
|
package/cordis.patch.yml
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,47 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-tablelint",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Markdown table check for generated reports: rows with the wrong cell count, a delimiter row that disagrees with the header, duplicate or empty headers, and tables with no rows.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./src/index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./src/index.js",
|
|
9
|
+
"./package.json": "./package.json"
|
|
10
|
+
},
|
|
11
|
+
"bin": {
|
|
12
|
+
"tablelint": "./bin/cli.js"
|
|
13
|
+
},
|
|
14
|
+
"dsh": {
|
|
15
|
+
"bundle": {
|
|
16
|
+
"patch": "./cordis.patch.yml"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": ">=22.19.0"
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"src",
|
|
24
|
+
"bin",
|
|
25
|
+
"cordis.patch.yml",
|
|
26
|
+
"README.md",
|
|
27
|
+
"LICENSE"
|
|
28
|
+
],
|
|
29
|
+
"keywords": [
|
|
30
|
+
"dsh",
|
|
31
|
+
"dsh-plugin",
|
|
32
|
+
"deepseek-harness",
|
|
33
|
+
"markdown",
|
|
34
|
+
"table",
|
|
35
|
+
"lint",
|
|
36
|
+
"report",
|
|
37
|
+
"表格检查",
|
|
38
|
+
"交付检查"
|
|
39
|
+
],
|
|
40
|
+
"license": "MIT",
|
|
41
|
+
"author": "zjukop3",
|
|
42
|
+
"repository": {
|
|
43
|
+
"type": "git",
|
|
44
|
+
"url": "git+https://github.com/zjukop3/dsh-verify-kit.git"
|
|
45
|
+
},
|
|
46
|
+
"homepage": "https://github.com/zjukop3/dsh-verify-kit/tree/main/packages/dsh-tablelint"
|
|
47
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
// Ragged markdown tables are the most common defect in generated reports: they
|
|
2
|
+
// render fine in the editor that produced them and broken everywhere else.
|
|
3
|
+
// This counts cells, not vibes.
|
|
4
|
+
|
|
5
|
+
import { readFile } from 'node:fs/promises'
|
|
6
|
+
|
|
7
|
+
export const name = 'tablelint'
|
|
8
|
+
export const inject = ['webServer']
|
|
9
|
+
|
|
10
|
+
export function splitCells(line) {
|
|
11
|
+
const trimmed = line.trim().replace(/^\|/, '').replace(/\|$/, '')
|
|
12
|
+
const cells = []
|
|
13
|
+
let current = ''
|
|
14
|
+
let escaped = false
|
|
15
|
+
for (const character of trimmed) {
|
|
16
|
+
if (escaped) {
|
|
17
|
+
current += character
|
|
18
|
+
escaped = false
|
|
19
|
+
continue
|
|
20
|
+
}
|
|
21
|
+
if (character === '\\') {
|
|
22
|
+
escaped = true
|
|
23
|
+
continue
|
|
24
|
+
}
|
|
25
|
+
if (character === '|') {
|
|
26
|
+
cells.push(current.trim())
|
|
27
|
+
current = ''
|
|
28
|
+
continue
|
|
29
|
+
}
|
|
30
|
+
current += character
|
|
31
|
+
}
|
|
32
|
+
cells.push(current.trim())
|
|
33
|
+
return cells
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function tablesOf(text) {
|
|
37
|
+
const lines = text.split('\n')
|
|
38
|
+
const tables = []
|
|
39
|
+
let fence = false
|
|
40
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
41
|
+
const line = lines[index]
|
|
42
|
+
if (/^\s*(```|~~~)/.test(line)) {
|
|
43
|
+
fence = !fence
|
|
44
|
+
continue
|
|
45
|
+
}
|
|
46
|
+
if (fence) continue
|
|
47
|
+
const isRow = line.trim().startsWith('|') && line.includes('|', 1)
|
|
48
|
+
const next = lines[index + 1] || ''
|
|
49
|
+
const isDelimiter = /^\s*\|?[\s:|-]+\|[\s:|-]*$/.test(next) && next.includes('-')
|
|
50
|
+
if (!isRow || !isDelimiter) continue
|
|
51
|
+
const header = splitCells(line)
|
|
52
|
+
const delimiter = splitCells(next)
|
|
53
|
+
const rows = []
|
|
54
|
+
let cursor = index + 2
|
|
55
|
+
while (cursor < lines.length && lines[cursor].trim().startsWith('|')) {
|
|
56
|
+
rows.push({ line: cursor + 1, cells: splitCells(lines[cursor]) })
|
|
57
|
+
cursor += 1
|
|
58
|
+
}
|
|
59
|
+
tables.push({ line: index + 1, header, delimiter, rows })
|
|
60
|
+
index = cursor - 1
|
|
61
|
+
}
|
|
62
|
+
return tables
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function lintTables(text) {
|
|
66
|
+
const tables = tablesOf(text)
|
|
67
|
+
const findings = []
|
|
68
|
+
for (const table of tables) {
|
|
69
|
+
if (table.header.length < 2) {
|
|
70
|
+
findings.push({ level: 'warn', rule: 'single-column', message: `line ${table.line}: table has one column, which is a list wearing a table` })
|
|
71
|
+
}
|
|
72
|
+
if (table.delimiter.length !== table.header.length) {
|
|
73
|
+
findings.push({
|
|
74
|
+
level: 'error',
|
|
75
|
+
rule: 'delimiter-count',
|
|
76
|
+
message: `line ${table.line + 1}: ${table.delimiter.length} delimiter cells under ${table.header.length} header cells`,
|
|
77
|
+
})
|
|
78
|
+
}
|
|
79
|
+
const headerKeys = table.header.map(cell => cell.toLowerCase())
|
|
80
|
+
const duplicates = headerKeys.filter((cell, index) => cell && headerKeys.indexOf(cell) !== index)
|
|
81
|
+
for (const duplicate of new Set(duplicates)) {
|
|
82
|
+
findings.push({ level: 'warn', rule: 'duplicate-header', message: `line ${table.line}: two columns are both named "${duplicate}"` })
|
|
83
|
+
}
|
|
84
|
+
if (table.header.some(cell => cell === '')) {
|
|
85
|
+
findings.push({ level: 'warn', rule: 'empty-header', message: `line ${table.line}: a column has no header` })
|
|
86
|
+
}
|
|
87
|
+
for (const row of table.rows) {
|
|
88
|
+
if (row.cells.length !== table.header.length) {
|
|
89
|
+
findings.push({
|
|
90
|
+
level: 'error',
|
|
91
|
+
rule: 'column-count',
|
|
92
|
+
message: `line ${row.line}: ${row.cells.length} cells in a ${table.header.length}-column table`,
|
|
93
|
+
})
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if (table.rows.length === 0) {
|
|
97
|
+
findings.push({ level: 'warn', rule: 'empty-table', message: `line ${table.line}: header and delimiter with no rows` })
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
const errors = findings.filter(finding => finding.level === 'error').length
|
|
101
|
+
return { ok: errors === 0, tables: tables.length, errors, warnings: findings.length - errors, findings }
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function apply(ctx, config = {}) {
|
|
105
|
+
const prefix = config.path || '/tablelint/api'
|
|
106
|
+
const handler = async (req, res) => {
|
|
107
|
+
const send = (status, body) => {
|
|
108
|
+
res.statusCode = status
|
|
109
|
+
res.setHeader('content-type', 'application/json; charset=utf-8')
|
|
110
|
+
res.end(JSON.stringify(body))
|
|
111
|
+
}
|
|
112
|
+
try {
|
|
113
|
+
if (req.method !== 'POST') return send(405, { error: 'POST only' })
|
|
114
|
+
const chunks = []
|
|
115
|
+
for await (const chunk of req) chunks.push(chunk)
|
|
116
|
+
const body = JSON.parse(Buffer.concat(chunks).toString('utf8') || '{}')
|
|
117
|
+
const text = typeof body.text === 'string' && body.text ? body.text : body.path ? await readFile(body.path, 'utf8') : ''
|
|
118
|
+
if (!text) return send(400, { error: 'text or path is required' })
|
|
119
|
+
return send(200, lintTables(text))
|
|
120
|
+
} catch (error) {
|
|
121
|
+
return send(400, { error: error.message || 'request failed' })
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
ctx.effect(() => ctx.webServer.register({ kind: 'prefix', path: prefix, handler }))
|
|
125
|
+
}
|