-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwizard-utils.ts
More file actions
139 lines (120 loc) · 3.96 KB
/
wizard-utils.ts
File metadata and controls
139 lines (120 loc) · 3.96 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import type { DataAnswerSource, DataQuestionSource, FileOutputConfig, WizardAnswer, WizardStep } from "@/types/wizard"
const buildFileSlug = (filename?: string, fallback?: string) => {
const base = filename ?? fallback ?? ""
const trimmed = base.trim()
if (!trimmed) {
return (fallback ?? "file").toLowerCase()
}
const withoutLeadingDot = trimmed.replace(/^\./, "")
const noExtension = withoutLeadingDot.replace(/\.[^./]+$/, "")
const normalizedSeparators = noExtension.replace(/[\\/]+/g, "-")
const collapsed = normalizedSeparators.replace(/[^a-zA-Z0-9]+/g, "-")
const hyphenReduced = collapsed.replace(/-+/g, "-")
const trimmedHyphen = hyphenReduced.replace(/^-|-$/g, "")
const slug = trimmedHyphen.toLowerCase()
if (slug.length === 0) {
return (fallback ?? "file").toLowerCase()
}
return slug
}
/**
* Maps a data answer source to a wizard answer format
*/
export const mapAnswerSourceToWizard = (answer: DataAnswerSource): WizardAnswer => {
const infoLines: string[] = []
if (answer.pros && answer.pros.length > 0) {
infoLines.push(`Pros: ${answer.pros.join(", ")}`)
}
if (answer.cons && answer.cons.length > 0) {
infoLines.push(`Cons: ${answer.cons.join(", ")}`)
}
const isDisabled = answer.disabled ?? (answer.enabled === false)
const disabledLabel = answer.disabledLabel ?? (answer.enabled === false ? "Soon" : undefined)
return {
value: answer.value,
label: answer.label,
icon: answer.icon,
example: answer.example,
infoLines: infoLines.length > 0 ? infoLines : undefined,
docs: answer.docs,
tags: answer.tags,
isDefault: answer.isDefault,
disabled: isDisabled,
disabledLabel,
filename: answer.filename,
format: answer.format,
enabled: answer.enabled,
}
}
/**
* Builds a wizard step from a question set
*/
export const buildStepFromQuestionSet = (
id: string,
title: string,
questions: DataQuestionSource[]
): WizardStep => ({
id,
title,
questions: questions.map((question) => ({
id: question.id,
question: question.question,
allowMultiple: question.allowMultiple,
responseKey: question.responseKey,
isReadOnlyOnSummary: question.isReadOnlyOnSummary,
enableFilter: question.enableFilter,
answers: question.answers.map(mapAnswerSourceToWizard),
freeText: question.freeText,
})),
})
export const buildFileOptionsFromQuestion = (
question?: DataQuestionSource | null
): FileOutputConfig[] => {
if (!question) {
return []
}
return question.answers
.filter((answer) => answer.enabled !== false)
.map((answer) => ({
id: answer.value,
label: answer.label,
filename: answer.filename ?? answer.label,
format: answer.format ?? "markdown",
slug: buildFileSlug(answer.filename, answer.value ?? answer.label),
enabled: answer.enabled,
icon: answer.icon,
docs: answer.docs,
isDefault: answer.isDefault,
isLegacy: answer.isLegacy,
}))
}
const formatLabelMap: Record<string, string> = {
markdown: "Markdown",
json: "JSON",
"cursor-rules-json": "JSON",
zip: "ZIP Archive",
}
const formatMimeTypeMap: Record<string, string> = {
markdown: "text/markdown",
json: "application/json",
"cursor-rules-json": "application/json",
zip: "application/zip",
}
/**
* Converts a stored format identifier into a human-friendly label
*/
export const getFormatLabel = (format?: string) => {
if (!format) {
return null
}
return formatLabelMap[format] ?? format
}
/**
* Returns the browser mime-type associated with a stored format identifier
*/
export const getMimeTypeForFormat = (format?: string) => {
if (!format) {
return "text/plain"
}
return formatMimeTypeMap[format] ?? "text/plain"
}