-
Notifications
You must be signed in to change notification settings - Fork 0
/
google-doc-exporter.js
87 lines (75 loc) · 2.26 KB
/
google-doc-exporter.js
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
import { google } from "googleapis";
import { memoize } from "lodash-es";
import * as cheerio from "cheerio";
import { CacheWithExpiration } from "./cache.js";
import "dotenv/config.js";
const { CLIENT_EMAIL, PRIVATE_KEY } = process.env;
const CACHE_EXPIRATION_IN_SECONDS =
parseInt(process.env.CACHE_EXPIRATION_IN_SECONDS, 10) || 1 * 60 * 60; // 1 hour
const auth = new google.auth.JWT({
email: CLIENT_EMAIL,
key: PRIVATE_KEY,
scopes: ["https://www.googleapis.com/auth/drive.readonly"],
});
const drive = google.drive({
version: "v3",
auth,
});
const getGoogleDocName = (fileId) => {
return drive.files.get({ fileId }).then((res) => {
return res.data.name;
});
};
const exportGoogleDoc = async (fileId) => {
const [filename, body] = await Promise.all([
getGoogleDocName(fileId),
getBody(fileId),
]);
return {
title: filename.replace("MonComptePro - ", ""),
body: cleanHtmlContent(body),
};
};
const getBody = async (fileId) => {
console.log(`Downloading doc ${fileId}...`);
const response = await drive.files.export(
{
fileId: fileId,
mimeType: "text/html",
},
{ responseType: "stream" },
);
const content = await new Promise((resolve, reject) => {
let htmlContent = "";
response.data
.on("data", (data) => {
htmlContent += data.toString();
})
.on("end", () => {
console.log(`Doc ${fileId} downloaded!`);
resolve(htmlContent);
})
.on("error", (err) => {
reject(err);
});
});
const regex = /@import url\(https:\/\/themes\.googleusercontent\.com[^)]*\)/;
return content.replace(regex, "");
};
export const cleanHtmlContent = (html) => {
const $ = cheerio.load(html);
$("[style]").removeAttr("style");
$("[class]").removeAttr("class");
$("p").has("span:empty:first-child:last-child").remove();
$("p:empty").remove();
$("table").addClass("fr-table");
$("table tr:first-of-type td").each(function(index, el) {
const newCell = $(el).contents().unwrap().wrap('<th>').closest('th').attr('scope', 'col')
$(el).replaceWith(newCell)
})
return $("body").html() || "";
};
export const exportMemoizedGoogleDoc = memoize(exportGoogleDoc);
exportMemoizedGoogleDoc.cache = new CacheWithExpiration(
CACHE_EXPIRATION_IN_SECONDS * 1000,
);