-
Notifications
You must be signed in to change notification settings - Fork 366
Expand file tree
/
Copy pathenvelope.ts
More file actions
100 lines (84 loc) · 2.93 KB
/
Copy pathenvelope.ts
File metadata and controls
100 lines (84 loc) · 2.93 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
import type { FileData, FileFormat, FormatHandler } from "../FormatHandler.ts";
import { parseODT, parseODP, parseODS } from "./envelope/parseODF.js";
import parseDOCX from "./envelope/parseDOCX.js";
import parsePPTX from "./envelope/parsePPTX.js";
import parseXLSX from "./envelope/parseXLSX.js";
import CommonFormats, { Category } from "src/CommonFormats.ts";
class envelopeHandler implements FormatHandler {
public name: string = "envelope";
public supportedFormats: FileFormat[] = [
CommonFormats.DOCX.builder("docx").allowFrom(),
// Currently, Pancoc handles PPTX and XLSX better than Envelope.
// CommonFormats.PPTX.builder("pptx").allowFrom(),
// CommonFormats.XLSX.builder("xlsx").allowFrom(),
{
name: "OpenDocument Text",
format: "odt",
extension: "odt",
mime: "application/vnd.oasis.opendocument.text",
from: true,
to: false,
internal: "odt",
category: Category.DOCUMENT,
lossless: false
},
{
name: "OpenDocument Presentation",
format: "odp",
extension: "odp",
mime: "application/vnd.oasis.opendocument.presentation",
from: true,
to: false,
internal: "odp",
category: Category.PRESENTATION,
lossless: false
},
{
name: "OpenDocument Spreadsheet",
format: "ods",
extension: "ods",
mime: "application/vnd.oasis.opendocument.spreadsheet",
from: true,
to: false,
internal: "ods",
category: Category.SPREADSHEET,
lossless: false
},
// Technically not "lossless", but it's about as close as we'll ever get
CommonFormats.HTML.supported("html", false, true, true)
];
public ready: boolean = true;
async init () {
this.ready = true;
}
async doConvert (
inputFiles: FileData[],
inputFormat: FileFormat,
outputFormat: FileFormat
): Promise<FileData[]> {
if (outputFormat.internal !== "html") throw new TypeError(`Unsupported output format: ${outputFormat.internal}`);
let parser: (bytes: Uint8Array) => Promise<string>;
switch (inputFormat.internal) {
case "odt": parser = parseODT; break;
case "odp": parser = parseODP; break;
case "ods": parser = parseODS; break;
case "docx": parser = parseDOCX; break;
case "pptx": parser = parsePPTX; break;
case "xlsx": parser = parseXLSX; break;
default: throw new TypeError(`Unsupported input format: ${inputFormat.internal}`);
}
const outputFiles: FileData[] = [];
const encoder = new TextEncoder();
for (const inputFile of inputFiles) {
const html = `<div style="background: #fff">
${await parser(inputFile.bytes)}
</div>`;
const bytes = encoder.encode(html);
const baseName = inputFile.name.split(".").slice(0, -1).join(".");
const name = baseName + "." + outputFormat.extension;
outputFiles.push({ bytes, name });
}
return outputFiles;
}
}
export default envelopeHandler;