@learnpack/learnpack 5.0.144 → 5.0.148

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/ui/app.tar.gz CHANGED
Binary file
@@ -125,3 +125,71 @@ export const checkAndFixSidebar = async (
125
125
 
126
126
  return false
127
127
  }
128
+
129
+ export const checkAndFixSidebarPure = async (
130
+ sidebarJson: Record<string, Record<string, string>>,
131
+ exercises: any[],
132
+ rigoToken: string,
133
+ autoFix = true
134
+ ): Promise<{
135
+ valid: boolean
136
+ fixedSidebar?: Record<string, Record<string, string>>
137
+ }> => {
138
+ const exerciseTranslations: Set<string> = new Set()
139
+ for (const e of exercises) {
140
+ for (const lang of Object.keys(e.translations || {})) {
141
+ exerciseTranslations.add(lang)
142
+ }
143
+ }
144
+
145
+ let hasErrors = false
146
+ const fixedSidebar: Record<string, Record<string, string>> = {
147
+ ...sidebarJson,
148
+ }
149
+
150
+ for (const exercise of exercises) {
151
+ const slug = exercise.slug
152
+ const existingEntry = fixedSidebar[slug]
153
+
154
+ if (!existingEntry) {
155
+ hasErrors = true
156
+ fixedSidebar[slug] = { us: exercise.slug }
157
+ continue
158
+ }
159
+
160
+ for (const lang of exerciseTranslations) {
161
+ if (!Object.prototype.hasOwnProperty.call(existingEntry, lang)) {
162
+ hasErrors = true
163
+ }
164
+ }
165
+ }
166
+
167
+ if (hasErrors && autoFix) {
168
+ if (!rigoToken) {
169
+ Console.error("No Rigobot token provided!")
170
+ return { valid: false }
171
+ }
172
+
173
+ Console.warning(
174
+ "Filling sidebar JSON with missing translations or exercises"
175
+ )
176
+
177
+ const response = await fillSidebarJSON(rigoToken, {
178
+ needed_translations: JSON.stringify([...exerciseTranslations]),
179
+ sidebar_json: JSON.stringify(fixedSidebar),
180
+ })
181
+
182
+ const newSidebarJson = JSON.parse(response.parsed.new_sidebar_file)
183
+
184
+ Console.info("Sidebar JSON was fixed")
185
+ return {
186
+ valid: true,
187
+ fixedSidebar: newSidebarJson,
188
+ }
189
+ }
190
+
191
+ return {
192
+ valid: !hasErrors,
193
+ fixedSidebar: hasErrors ? fixedSidebar : undefined,
194
+ }
195
+ }