-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommands.ts
More file actions
55 lines (48 loc) · 1.35 KB
/
commands.ts
File metadata and controls
55 lines (48 loc) · 1.35 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
import { parseFrontmatter } from './frontmatter.js'
import { walkDir } from './walk-dir.js'
export interface CommandFrontmatter {
name: string
description: string
argumentHint: string
}
export interface CommandInfo {
name: string
file: string
category?: string
}
export function findCommandsInDir(dir: string, maxDepth = 2): CommandInfo[] {
const entries = walkDir(dir, {
maxDepth,
filter: (e) => !e.isDirectory && e.name.endsWith('.md'),
})
return entries.map((entry) => {
const baseName = entry.name.replace(/\.md$/, '')
const commandName = entry.category
? `/${entry.category}:${baseName}`
: `/${baseName}`
return {
name: commandName,
file: entry.path,
category: entry.category,
}
})
}
export function extractCommandFrontmatter(content: string): CommandFrontmatter {
const { data, parseError } = parseFrontmatter<{
name?: string
description?: string
'argument-hint'?: string
}>(content)
const argumentHintRaw =
!parseError && typeof data['argument-hint'] === 'string'
? data['argument-hint']
: ''
return {
name: !parseError && typeof data.name === 'string' ? data.name : '',
description:
!parseError && typeof data.description === 'string'
? data.description
: '',
argumentHint: argumentHintRaw.replace(/^["']|["']$/g, ''),
}
}