-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathcode-language.ts
More file actions
66 lines (53 loc) · 1.89 KB
/
code-language.ts
File metadata and controls
66 lines (53 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import type { Code, Root } from 'mdast'
import { visit } from 'unist-util-visit'
const BASH_PREFIX_PATTERN = /^(?:\$ |pip3? |python3? -m |python3? |npm |pnpm |yarn |npx |git |make |curl |wget |docker(?:-compose)? |kubectl |helm |cd |ls |cat |cp |mv |rm |mkdir |chmod |export |set |echo )/m
const PYTHON_PATTERN = /(?:^|\n)(?:from [\w.]+ import |import [\w.]+|def \w+\(|class \w+|with open\(|print\(|if __name__ == ['"]__main__['"]|for \w+ in |try:|except )/
const SQL_PATTERN = /^(?:select|insert\s+into|update|delete\s+from|create\s+table|alter\s+table|with\s+\w+\s+as)\b/im
const TYPESCRIPT_PATTERN = /(?:^|\n)(?:interface \w+|type \w+\s*=|import type |export type |export interface |const \w+:\s|:\s(?:string|number|boolean|Record<|Array<)|as const\b)/
const JAVASCRIPT_PATTERN = /(?:^|\n)(?:const |let |var |function \w+\(|export default |export function |module\.exports|import .* from |=>)/
const YAML_LINE_PATTERN = /^(\s*-\s+)?[\w"'./-]+:\s*.+$/m
function looksLikeJson(value: string) {
try {
JSON.parse(value)
return true
} catch {
return false
}
}
export function inferMarkdownCodeLanguage(value: string): string | undefined {
const trimmed = value.trim()
if (!trimmed) {
return undefined
}
if (looksLikeJson(trimmed)) {
return 'json'
}
if (PYTHON_PATTERN.test(trimmed)) {
return 'python'
}
if (BASH_PREFIX_PATTERN.test(trimmed)) {
return 'bash'
}
if (SQL_PATTERN.test(trimmed)) {
return 'sql'
}
if (TYPESCRIPT_PATTERN.test(trimmed)) {
return 'ts'
}
if (JAVASCRIPT_PATTERN.test(trimmed)) {
return 'javascript'
}
if (trimmed.includes(':') && YAML_LINE_PATTERN.test(trimmed)) {
return 'yaml'
}
return undefined
}
export function remarkInferCodeLanguage() {
return (tree: Root) => {
visit(tree, 'code', (node: Code) => {
if (!node.lang) {
node.lang = inferMarkdownCodeLanguage(node.value)
}
})
}
}