| 146 | } |
| 147 | |
| 148 | async doConvert ( |
| 149 | inputFiles: FileData[], |
| 150 | inputFormat: FileFormat, |
| 151 | outputFormat: FileFormat |
| 152 | ): Promise<FileData[]> { |
| 153 | const outputFiles: FileData[] = []; |
| 154 | const converterEntry = textToSourceHandler.converters.find( |
| 155 | ([format]) => format.internal === outputFormat.internal |
| 156 | ); |
| 157 | |
| 158 | if (!converterEntry) { |
| 159 | throw new Error(`could not find a textToSource converter to convert to ${outputFormat.mime}`); |
| 160 | } |
| 161 | |
| 162 | const [, converter] = converterEntry; |
| 163 | |
| 164 | for (const inputFile of inputFiles) { |
| 165 | const text = new TextDecoder().decode(inputFile.bytes).replaceAll(/\r?\n/g, "\n"); |
| 166 | |
| 167 | const converted = converter(text); |
| 168 | |
| 169 | const bytes = new TextEncoder().encode(converted); |
| 170 | const name = inputFile.name.replace(/\.txt$/i, `.${outputFormat.extension}`); |
| 171 | outputFiles.push({ bytes, name }); |
| 172 | } |
| 173 | return outputFiles; |
| 174 | } |
| 175 | |
| 176 | } |
| 177 | |