Skip to content

Commit

Permalink
temp: add remark-lint-no-unicode package
Browse files Browse the repository at this point in the history
  • Loading branch information
vanyauhalin committed Dec 6, 2024
1 parent b647397 commit 91477d2
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 40 deletions.
2 changes: 1 addition & 1 deletion packages/remark-lint-no-unicode/fixtures/001.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ignores fully clear document
ignores a document that does not contain forbidden characters

---

Expand Down
10 changes: 4 additions & 6 deletions packages/remark-lint-no-unicode/fixtures/002.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
finds forbidden characters in a document
ignores forbidden characters in inline code and fenced code blocks

---

# abcd
`abcd`

```
abcd
abcd
```

---

1:6-1:7: Forbidden character "d" (see https://symbl.cc/en/0064/).
3:4-3:5: Forbidden character "d" (see https://symbl.cc/en/0064/).
4:4-4:5: Forbidden character "d" (see https://symbl.cc/en/0064/).

---
8 changes: 4 additions & 4 deletions packages/remark-lint-no-unicode/fixtures/003.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
ignores forbidden characters in code
sends an abstract message if a document contains forbidden characters

---

`abcd`
# abcd

```
abcd
```

---

1:6-1:7: Character "d" (U+0064) is not allowed. Refer to "https://symbl.cc/en/0064/" for information regarding this character.
3:4-3:5: Character "d" (U+0064) is not allowed. Refer to "https://symbl.cc/en/0064/" for information regarding this character.

---
11 changes: 11 additions & 0 deletions packages/remark-lint-no-unicode/fixtures/004.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
sends a concrete message if a document contains forbidden characters

---

ab c

---

1:3-1:4: No-Break Space character " " (U+00A0) is not allowed. Consider replacing it with a Space character " " (U+0020). Refer to "https://symbl.cc/en/00A0/" and "https://symbl.cc/en/0020/" for information regarding these characters.

---
78 changes: 49 additions & 29 deletions packages/remark-lint-no-unicode/lib/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import {
isLiteralNode,
isParentNode,
} 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 Position} from "unist"
import {type MessageOptions, type VFile} from "vfile"

const hs = hints([
["00A0", "Use a regular space instead of no-break space"],
["00A0", "No-Break Space", "0020", "a Space"],
])

export const remarkLintNoUnicode: any =
export const remarkLintNoUnicode: RemarkLintRule<Node, string[]> =
lintRule("@onlyoffice:no-unicode", rule)

function rule(t: Node, f: VFile, o: string[]): void {
Expand All @@ -36,42 +37,61 @@ function rule(t: Node, f: VFile, o: string[]): void {
const c = t.value[i]

const u = unicode(c)
if (!u) {
continue
}

if (o.includes(u)) {
continue
}

const m = message(c, u)
const p: MessageOptions = {ancestors: [t]}
const r = reason(u)

const p: MessageOptions = {ancestors: [t]}
if (t.position) {
p.place = position(t.position, t.value, i)
p.place = place(t.position, t.value, i)
}

f.message(m, p)
f.message(r, p)
}
}

function hints(t: [string, string][]): Record<string, string> {
const h: Record<string, string> = {}
function unicode(s: string): string {
const p = s.codePointAt(0)
if (p === undefined) {
return ""
}

const u = p.toString(16).toUpperCase()

for (const [u, m] of t) {
h[u] = `${m} (see ${ref(u)}).`
switch (u.length) {
case 1:
return `000${u}`
case 2:
return `00${u}`
case 3:
return `0${u}`
default:
return u
}
}

return h
function character(u: string): string {
const n = Number.parseInt(u, 16)
return String.fromCodePoint(n)
}

function message(v: string, u: string): string {
function reason(u: string): string {
let m = hs[u]

if (!m) {
m = `Forbidden character "${v}" (see ${ref(u)}).`
m = abstract(u)
}

return m
}

function position(p: Position, a: string, i: number): Position {
function place(p: Position, a: string, i: number): Position {
let l = p.start.line
let c = p.start.column

Expand All @@ -98,22 +118,22 @@ function position(p: Position, a: string, i: number): Position {
}
}

function unicode(s: string): string {
const p = s.codePointAt(0)
if (p === undefined) {
return ""
}
const u = p.toString(16).toUpperCase()
switch (u.length) {
case 1:
return `000${u}`
case 2:
return `00${u}`
case 3:
return `0${u}`
default:
return u
function hints(t: [string, string, string, string][]): Record<string, string> {
const h: Record<string, string> = {}

for (const [au, an, bu, bn] of t) {
h[au] = concrete(au, an, bu, bn)
}

return h
}

function abstract(u: string): string {
return `Character "${character(u)}" (U+${u}) is not allowed. Refer to "${ref(u)}" for information regarding this character.`
}

function concrete(au: string, an: string, bu: string, bn: string): string {
return `${an} character "${character(au)}" (U+${au}) is not allowed. Consider replacing it with ${bn} character "${character(bu)}" (U+${bu}). Refer to "${ref(au)}" and "${ref(bu)}" for information regarding these characters.`
}

function ref(u: string): string {
Expand Down
1 change: 1 addition & 0 deletions packages/remark-lint-no-unicode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
},
"dependencies": {
"@onlyoffice/mdast-util-is-node": "workspace:^",
"@onlyoffice/remark-lint": "workspace:^",
"@types/mdast": "4.0.3",
"@types/unist": "3.0.3",
"unified-lint-rule": "3.0.0",
Expand Down
3 changes: 3 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 91477d2

Please sign in to comment.