Skip to content

Commit

Permalink
add remark-lint-no-thematic-break package
Browse files Browse the repository at this point in the history
  • Loading branch information
sshakndr committed Dec 24, 2024
1 parent be3ac1c commit 74d181f
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/remark-lint-no-thematic-break/fixtures/000.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
ignores an empty document

---


---


---
11 changes: 11 additions & 0 deletions packages/remark-lint-no-thematic-break/fixtures/001.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
ignores a document that does not contain thematic breaks

---

# ---
*a**b***

---


---
21 changes: 21 additions & 0 deletions packages/remark-lint-no-thematic-break/fixtures/002.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
sends a message if a document contains thematic breaks

---

> ---
- - -
***
* * *
___
_ _ _

---

1:3-1:6: Thematic breaks are not allowed.
2:1-2:6: Thematic breaks are not allowed.
3:1-3:4: Thematic breaks are not allowed.
4:1-4:6: Thematic breaks are not allowed.
5:1-5:4: Thematic breaks are not allowed.
6:1-6:6: Thematic breaks are not allowed.

---
47 changes: 47 additions & 0 deletions packages/remark-lint-no-thematic-break/lib/main.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {readFile, readdir} from "node:fs/promises"
import path from "node:path"
import remarkLint from "remark-lint"
import remarkParse from "remark-parse"
import remarkStringify from "remark-stringify"
import {unified} from "unified"
import {test} from "uvu"
import {equal as eq} from "uvu/assert"
import {remarkLintNoThematicBreak} from "./main.ts"

for (const [n, v, e] of await list()) {
test(n, async () => {
const f = await unified()
.use(remarkParse)
.use(remarkLint)
.use(remarkLintNoThematicBreak)
.use(remarkStringify)
.process(v)
const a = f.messages.map(String)
eq(a, e)
})
}

test.run()

type TestCase = [string, string, string[]]

async function list(): Promise<TestCase[]> {
const a: TestCase[] = []
const d = await readdir("fixtures")
for (const n of d) {
const e = path.extname(n)
const p = path.basename(n, e)
const s = await readFile(`fixtures/${n}`, "utf8")
const c = create(p, s)
a.push(c)
}
return a
}

function create(p: string, s: string): TestCase {
let [n, m, h] = s.split("\n---\n")
n = n.slice(0, -1)
m = m.slice(1).slice(0, -1)
h = h.slice(1).slice(0, -1)
return [`${p}: ${n}`, m, h.split("\n").filter((f) => f !== "")]
}
26 changes: 26 additions & 0 deletions packages/remark-lint-no-thematic-break/lib/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {
isParentNode,
isThematicBreakNode,
} from "@onlyoffice/mdast-util-is-node"
import {type RemarkLintRule} from "@onlyoffice/remark-lint"
import {type Node} from "mdast"
import {lintRule} from "unified-lint-rule"
import {type VFile} from "vfile"

export const remarkLintNoThematicBreak: RemarkLintRule<Node> =
lintRule("@onlyoffice:no-thematic-break", rule)

function rule(t: Node, f: VFile): void {
if (isThematicBreakNode(t)) {
f.message("Thematic breaks are not allowed.", {ancestors: [t]})
return
}

if (!isParentNode(t)) {
return
}

for (const c of t.children) {
rule(c, f)
}
}
34 changes: 34 additions & 0 deletions packages/remark-lint-no-thematic-break/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "@onlyoffice/remark-lint-no-thematic-break",
"type": "module",
"private": true,
"main": "dist/main.js",
"scripts": {
"build:code": "esbuild lib/* --outdir=dist",
"build:types": "tsc --project tsconfig.emit.json",
"build": "pnpm build:types && pnpm build:code",
"clean": "rimraf dist node_modules",
"test:types": "tsc",
"test:unit": "c8 --config ../../c8.config.json tsx node_modules/uvu/bin.js lib ^.*\\.test\\.ts$",
"test": "pnpm test:types && pnpm test:unit"
},
"dependencies": {
"@onlyoffice/mdast-util-is-node": "workspace:^",
"@onlyoffice/remark-lint": "workspace:^",
"@types/mdast": "4.0.3",
"unified-lint-rule": "3.0.0",
"vfile": "6.0.1"
},
"devDependencies": {
"c8": "9.1.0",
"esbuild": "0.23.0",
"remark-lint": "10.0.0",
"remark-parse": "11.0.0",
"remark-stringify": "11.0.0",
"rimraf": "6.0.1",
"tsx": "4.16.5",
"typescript": "5.4.5",
"unified": "11.0.5",
"uvu": "0.5.6"
}
}
8 changes: 8 additions & 0 deletions packages/remark-lint-no-thematic-break/tsconfig.emit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.emit.json",
"compilerOptions": {
"rootDir": "lib",
"outDir": "dist"
},
"include": ["lib"]
}
4 changes: 4 additions & 0 deletions packages/remark-lint-no-thematic-break/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../../tsconfig.base.json",
"include": ["lib"]
}
49 changes: 49 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit 74d181f

Please sign in to comment.