( files: ImportFile[], )
| 159 | } |
| 160 | |
| 161 | export function parseSnippetsLabFiles( |
| 162 | files: ImportFile[], |
| 163 | ): SnippetImportParseResult { |
| 164 | const snippets: SnippetImportCandidate[] = [] |
| 165 | const warnings: SnippetImportParseResult['warnings'] = [] |
| 166 | |
| 167 | for (const file of files) { |
| 168 | const parsed = parseJsonFile(file) |
| 169 | const contents = getContents(parsed) |
| 170 | |
| 171 | if (!contents) { |
| 172 | warnings.push({ |
| 173 | code: 'snippetslab.invalidExport', |
| 174 | source: file.name, |
| 175 | }) |
| 176 | continue |
| 177 | } |
| 178 | |
| 179 | const folderPathMap = buildFolderPathMap( |
| 180 | getRecordArray(contents, 'folders'), |
| 181 | ) |
| 182 | const tagMap = buildTagMap(getRecordArray(contents, 'tags')) |
| 183 | const smartGroups = getRecordArray(contents, 'smartGroups') |
| 184 | |
| 185 | if (smartGroups.length) { |
| 186 | warnings.push({ |
| 187 | code: 'snippetslab.smartGroupsSkipped', |
| 188 | source: file.name, |
| 189 | }) |
| 190 | } |
| 191 | |
| 192 | getRecordArray(contents, 'snippets').forEach((record, index) => { |
| 193 | const sourceId |
| 194 | = readString(record, 'uuid') ?? `${file.name}:${index + 1}` |
| 195 | const fragments = getRecordArray(record, 'fragments') |
| 196 | const contents = fragments |
| 197 | .map((fragment, fragmentIndex) => { |
| 198 | const value = readString(fragment, 'content') |
| 199 | if (!value) { |
| 200 | return null |
| 201 | } |
| 202 | |
| 203 | const attachments = fragment.attachments |
| 204 | if (Array.isArray(attachments) && attachments.length) { |
| 205 | warnings.push({ |
| 206 | code: 'snippetslab.attachmentsSkipped', |
| 207 | source: `${file.name}/${sourceId}`, |
| 208 | }) |
| 209 | } |
| 210 | |
| 211 | return { |
| 212 | label: normalizeImportEntryName( |
| 213 | readString(fragment, 'title'), |
| 214 | `Fragment ${fragmentIndex + 1}`, |
| 215 | ), |
| 216 | language: normalizeLexer(fragment.language), |
| 217 | value, |
| 218 | } |
no test coverage detected