forked from supreetpal/gdoc-markdown-converter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarkdown.gs
More file actions
613 lines (532 loc) · 21.1 KB
/
markdown.gs
File metadata and controls
613 lines (532 loc) · 21.1 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
/*
Usage:
Adding this script to your doc:
- Tools > Script Manager > New
- Select "Blank Project", then paste this code in and save.
Running the script:
- Add-ons > ConverToMarkDown
- Select "Add To Drive" function.
- The document will be converted into a MD file and it will be placed in the folder called "Common"
- You can also view or donwload the converted file from the pop up, once the document is converted.
*/
/**
* Creates a menu entry in the Google Docs UI when the document is opened.
* This method is only used by the regular add-on, and is never called by
* the mobile add-on version.
*
* @param {object} e The event parameter for a simple onOpen trigger. To
* determine which authorization mode (ScriptApp.AuthMode) the trigger is
* running in, inspect e.authMode.
*/
function onOpen(e) {
DocumentApp.getUi().createAddonMenu()
.addItem('Add MD to Drive', 'ConvertToMarkdown')
.addItem('Download MD file', 'downloadMdFile')
.addToUi();
}
/**
* Runs when the add-on is installed.
* This method is only used by the regular add-on, and is never called by
* the mobile add-on version.
*
* @param {object} e The event parameter for a simple onInstall trigger. To
* determine which authorization mode (ScriptApp.AuthMode) the trigger is
* running in, inspect e.authMode. (In practice, onInstall triggers always
* run in AuthMode.FULL, but onOpen triggers may be AuthMode.LIMITED or
* AuthMode.NONE.)
*/
/*
// This was superceded.
function onInstall(e) {
onOpen(e);
}
*/
function onInstall(e) {
//When the document is first installed, clear out the user preferences set already
var scriptProperties = PropertiesService.getUserProperties();
//Change this value to change the default output folder. This value can be overridden by user at any time using 'Change Output Folder' menu
var defaultOutputFolder = "Google Docs to Markdown";
//Setting the intial value of common folder if not present
//if (typeof scriptProperties.getProperty('DEFAULT_OUTPUT_FOLDER') == 'undefined' || scriptProperties.getProperty('DEFAULT_OUTPUT_FOLDER') == null || scriptProperties.getProperty('DEFAULT_OUTPUT_FOLDER') === "")
scriptProperties.setProperty('DEFAULT_OUTPUT_FOLDER', defaultOutputFolder); // Changing this value affects the default common folder name
//Setting the intial value of common folder if not present
//if (typeof scriptProperties.getProperty('COMMON_FOLDER') == 'undefined' || scriptProperties.getProperty('COMMON_FOLDER') == null || scriptProperties.getProperty('COMMON_FOLDER') == "")
scriptProperties.setProperty('COMMON_FOLDER', defaultOutputFolder); // Changing this value affects the common folder name
onOpen(e);
}
/**
* Creates a menu entry in the Google Docs UI when the document is opened.
*
* @param {object} e The event parameter for a simple onOpen trigger. To
* determine which authorization mode (ScriptApp.AuthMode) the trigger is
* running in, inspect e.authMode.
*/
/*
function onOpen(e) {
DocumentApp.getUi().createAddonMenu()
.addItem('Add To Drive', 'ConvertToMarkdown')
.addItem('Download MD file', 'downloadMdFile')
.addToUi();
}
*/
/**
* Runs when the add-on is installed.
*
* @param {object} e The event parameter for a simple onInstall trigger. To
* determine which authorization mode (ScriptApp.AuthMode) the trigger is
* running in, inspect e.authMode. (In practice, onInstall triggers always
* run in AuthMode.FULL, but onOpen triggers may be AuthMode.LIMITED or
* AuthMode.NONE.)
*/
function onInstall(e) {
//When the document is first installed, clear out the user preferences set already
var scriptProperties = PropertiesService.getUserProperties();
//Change this value to change the default output folder. This value can be overridden by user at any time using 'Change Output Folder' menu
var defaultOutputFolder = "Google Docs to Markdown";
//Setting the intial value of common folder if not present
//if (typeof scriptProperties.getProperty('DEFAULT_OUTPUT_FOLDER') == 'undefined' || scriptProperties.getProperty('DEFAULT_OUTPUT_FOLDER') == null || scriptProperties.getProperty('DEFAULT_OUTPUT_FOLDER') === "")
scriptProperties.setProperty('DEFAULT_OUTPUT_FOLDER', defaultOutputFolder); // Changing this value affects the default common folder name
//Setting the intial value of common folder if not present
//if (typeof scriptProperties.getProperty('COMMON_FOLDER') == 'undefined' || scriptProperties.getProperty('COMMON_FOLDER') == null || scriptProperties.getProperty('COMMON_FOLDER') == "")
scriptProperties.setProperty('COMMON_FOLDER', defaultOutputFolder); // Changing this value affects the common folder name
onOpen(e);
}
function doGet() {
ConvertToMarkdown();
}
/* This function converts the google document into mark down and saves it in the common folder as configured by the user.
It will also save any inline images in the same folder where converted MD file is placed */
function ConvertToMarkdown() {
var text = "";
var inSrc = false;
var inClass = false;
var globalImageCounter = 0;
var globalListCounters = {};
// edbacher: added a variable for indent in src <pre> block. Let style sheet do margin.
var srcIndent = "";
var attachments = [];
var file;
var commonFolder;
var folder;
var folderName;
var blob;
var content_type;
var suffix;
var image_name;
var photo;
// try {
var numChildren = DocumentApp.getActiveDocument().getActiveSection().getNumChildren();
// Logger.log('numChildren: ' + numChildren);
var folder = getCurrentFolder();
var prev_type = 'PARAGRAPH';
var prev_heading = 'Normal';
var child_heading = '';
// var isCode = false;
// Walk through all the child elements of the doc.
for (var i = 0; i < numChildren; i++) {
var child = DocumentApp.getActiveDocument().getActiveSection().getChild(i);
var child_type = child.getType();
if (child_type == 'UNSUPPORTED') continue;
if (child_type == 'PARAGRAPH') {
child_heading = child.getHeading();
} else {
child_heading = '';
}
// Logger.log('type: ' + child_type + '/' + child_heading);
var result = processParagraph(i, child, inSrc, globalImageCounter, globalListCounters);
globalImageCounter += (result && result.images) ? result.images.length : 0;
if (result !== null) {
inSrc = result.inSrc;
// Logger.log('inSrc: ' + inSrc);
if (inClass) {
text += result.text + "\n\n";
} else if (inSrc) {
// text += (srcIndent + escapeHTML(result.text) + "\n");
text += result.text + '\n';
} else if (result.text && result.text.length > 0) {
// Logger.log(child_type + '/' + child_heading + '**' + prev_type + '/' + prev_heading);
if (child_type == 'PARAGRAPH') {
if (prev_type == 'LIST_ITEM') {
text += '\n';
}
if (child_heading == 'Normal' && result.text.charAt(0) != '#') {
text += result.text + '\n\n';
} else {
text += result.text + '\n';
}
} else if (child_type == 'LIST_ITEM') {
text += result.text + '\n';
} else {
text += result.text + '\n\n';
}
// set previous type info
prev_type = child_type;
prev_heading = child_heading;
}
if (result.images && result.images.length > 0) {
for (var j = 0; j < result.images.length; j++) {
attachments.push({
"fileName": result.images[j].name,
"mimeType": result.images[j].type,
"content": result.images[j].bytes
});
}
}
} else if (inSrc) { // support empty lines inside source code
text += '\n';
}
}
//Checking if MD file with same name is already present inside that folder. If present it will override the older file. If not it will create the new file.
var filename = DocumentApp.getActiveDocument().getName() + '.md';
removeExistingFiles(folder, filename);
file = DriveApp.createFile(filename, text, 'text/plain');
folder.addFile(file)
//If there are any attachments in the file, it has to be saved in the same directory.
//Due to this issue [http://code.google.com/p/google-apps-script-issues/issues/detail?id=1239], image files are created using blob. and replaced using Drive API.
if (attachments.length > 0) {
for (var iterator = 0; iterator < attachments.length; iterator++) {
blob = attachments[iterator].content;
content_type = blob.getContentType()
Logger.log('content_type: ' + content_type);
suffix = content_type.split("/")[1] //e.g gif/jpg or png
blob.setName('test.png'); // Invent a name, the blob seems to need it?
// try {
var image_name = "image_" + iterator + "." + suffix;
Logger.log('image_name: ' + image_name);
removeExistingFiles(folder, image_name);
photo = folder.createFile(blob);
photo.setName(image_name);
// } catch (e) {
// throw ("Error in saving attached images : " + e);
// }
}
}
// } catch (e) {
// var errorMsg = "";
// //While displaying error message, we display the last converted text, so that the users can know, after which line the conversion failed.
// //Check if there is any last converted text. If so take the last sentence from the converted text. If not, just display the error message.
// if (text != null && text.length != 0 && text.trim() !== "") {
// var sentence = text.split(".");
// if (sentence.length > 1) {
// errorMsg = "Error after the line : \"" + sentence[sentence.length - 2] + "\".\n\n" + e;
// } else if (sentence.length == 1) {
// errorMsg = "Error after the line : \"" + sentence[sentence.length - 1] + "\".\n\n" + e;
// } else if (sentence.length == 0) {
// errorMsg = "Error after the text : \"" + text + "\".\n\n" + e;;
// }
// } else {
// errorMsg = e;
// }
// //Showing the error message in alert window.
// DocumentApp.getUi().alert("Error", errorMsg, DocumentApp.getUi().ButtonSet.OK);
// }
// return file;
}
/* This function converts the current open document into an MD file and saves it in the common folder. It will then show you the pop up to download the MD file */
function downloadMdFile() {
var file = ConvertToMarkdown();
DocumentApp.getUi().showDialog(
HtmlService
.createHtmlOutput('<a href="https://docs.google.com/a/fusioncharts.com/uc?export=download&id=' + file.getId() + '">Download the converted MD file</a>')
.setTitle('Download Link for MD file')
.setWidth(400 /* pixels */ )
.setHeight(100 /* pixels */ ));
}
/* This function returns the current folder. */
function getCurrentFolder() {
// get the active document
var doc = DocumentApp.getActiveDocument();
// get the parent folders, could be more than one
var directParents = DriveApp.getFileById(doc.getId()).getParents();
var folderCount = 0;
while(directParents.hasNext()) {
folderCount++;
var currentFolder = directParents.next();
}
if (folderCount == 1) {
return currentFolder;
}
Logger.log('Found more than one parent folder.');
return null;
}
/* This function removes previous verions of the file */
function removeExistingFiles(folder, filename) {
Logger.log(folder.getName());
Logger.log(filename)
var files = folder.getFilesByName(filename);
while (files.hasNext()) {
var file = files.next();
file.setTrashed(true);
}
}
function escapeHTML(text) {
return text.replace(/</g, '<').replace(/>/g, '>');
}
// Process each child element (not just paragraphs).
function processParagraph(index, element, inSrc, imageCounter, listCounters) {
// Logger.log('element type: ' + element.getType());
// First, check for things that require no processing.
if (element.getNumChildren() == 0) {
return null;
}
// Punt on TOC.
if (element.getType() === DocumentApp.ElementType.TABLE_OF_CONTENTS) {
return {
"text": "[[TOC]]"
};
}
// Set up for real results.
var result = {};
result.inSrc = inSrc;
var pOut = "";
var textElements = [];
var imagePrefix = "image_";
var child_heading = '';
var cell_text = '';
// Start source code block
if (!inSrc) {
if (element.getType() === DocumentApp.ElementType.PARAGRAPH) {
if (element.getText().slice(0, 3) == '```') {
inSrc = true;
}
}
}
// Process Table
if (element.getType() === DocumentApp.ElementType.TABLE) {
var nRows = element.getNumChildren();
var nCols = element.getChild(0).getNumCells();
for (var row = 0; row < nRows; row++) {
textElements.push("|");
// process this row
for (var col = 0; col < nCols; col++) {
var cell = element.getChild(row).getChild(col);
var num_para = cell.getNumChildren();
// add initial space
textElements.push(' ');
// process each paragraph
for (var para_num = 0; para_num < num_para; para_num++) {
var cell_elem = cell.getChild(para_num);
var cell_elem_type = cell_elem.getType();
// add break between paragraphs
if (para_num > 0) textElements.push('<br>');
if (cell_elem_type == 'PARAGRAPH') {
var cell_result = processParagraph(index, cell_elem, inSrc, imageCounter, listCounters);
if (cell_result) {
cell_text = cell_result.text;
// remove bold from heading paragraphs
if (row == 0) {
cell_text = cell_text.replace(/\*\*/g, '');
}
} else {
cell_text = ' ';
}
textElements.push(cell_text);
} else {
Logger.log('unsupported element: ' + cell_elem_type);
textElements.push('<NOT_PARA>');
}
}
// add final space and cell divider
textElements.push(' |');
}
// add new line, except for last row
if (row < nRows - 1) {
textElements.push("\n");
}
// process the delimiter row
if (row == 0) {
textElements.push("|");
// process this row
for (var col = 0; col < nCols; col++) {
textElements.push(":---|");
}
textElements.push("\n");
}
}
}
// Process various types (ElementType).
for (var i = 0; i < element.getNumChildren(); i++) {
var t = element.getChild(i).getType();
// Logger.log('element child type: ' + t);
if (t === DocumentApp.ElementType.TABLE_ROW) {
// do nothing: already handled TABLE_ROW
} else if (t === DocumentApp.ElementType.TEXT) {
var txt = element.getChild(i);
pOut += txt.getText();
textElements.push(txt);
} else if (t === DocumentApp.ElementType.INLINE_IMAGE) {
result.images = result.images || [];
var img = element.getChild(i);
var blob = img.getBlob();
var alt_title = img.getAltTitle() || 'no alt title';
Logger.log('alt_title: ' + alt_title);
var contentType = blob.getContentType();
var extension = "";
if (/\/png$/.test(contentType)) {
extension = ".png";
} else if (/\/gif$/.test(contentType)) {
extension = ".gif";
} else if (/\/jpe?g$/.test(contentType)) {
extension = ".jpg";
} else {
throw "Unsupported image type: " + contentType;
}
var name = imagePrefix + imageCounter + extension;
imageCounter++;
textElements.push('');
result.images.push({
"bytes": blob,
"type": contentType,
"name": name
});
} else if (t === DocumentApp.ElementType.PAGE_BREAK) {
// ignore
} else if (t === DocumentApp.ElementType.HORIZONTAL_RULE) {
// textElements.push('* * *\n');
textElements.push('-------');
} else if (t === DocumentApp.ElementType.FOOTNOTE) {
textElements.push(' (NOTE: ' + element.getChild(i).getFootnoteContents().getText() + ')');
} else {
//throw "Paragraph "+index+" of type "+element.getType()+" has an unsupported child: "
//+t+" "+(element.getChild(i)["getText"] ? element.getChild(i).getText():'')+" index="+result;
throw "Unsupported format in current file :" + t + " " + (element.getChild(i)["getText"] ? element.getChild(i).getText() : '') + ". Cannot be converted into an MD file";
}
}
if (textElements.length == 0) {
// Isn't result empty now?
return result;
}
prefix = findPrefix(inSrc, element, listCounters);
var pOut = '';
for (var i = 0; i < textElements.length; i++) {
pOut += processTextElement(inSrc, textElements[i]);
}
// correct multiple, concatenated bold formatting
pOut = pOut.replace('****', '');
// remove bold formatting from headings
if (prefix.charAt(0) == '#' || pOut.slice(0, 3) == '**#') {
if (pOut.slice(0, 2) == '**' && pOut.slice(-2) == '**') {
pOut = pOut.slice(2, -2);
}
}
// replace Unicode quotation marks
pOut = pOut.replace('\u201d', '"').replace('\u201c', '"');
// replace smart quotes here
// replace non-breaking spaces
pOut = pOut.replace('\u00a0', ' ');
// remove heading numbers on appendix headings
if (prefix.charAt(0) == '#') {
var heading = pOut;
var matches = /^[\d\.]+\s(Appendix.*)/.exec(pOut);
if (matches) {
heading = matches[1];
}
pOut = heading;
}
// End source code block
if (result.inSrc) { // check the original so that you don't immediately turn off the block
if (element.getType() === DocumentApp.ElementType.PARAGRAPH) {
if (element.getText().slice(0, 3) == '```') {
inSrc = false;
}
}
}
// setup return variable
result.text = prefix + pOut;
result.inSrc = inSrc;
// return result
return result;
}
// Add correct prefix to list items.
function findPrefix(inSrc, element, listCounters) {
var prefix = "";
if (!inSrc) {
if (element.getType() === DocumentApp.ElementType.PARAGRAPH) {
var paragraphObj = element;
switch (paragraphObj.getHeading()) {
// Add a # for each heading level. No break, so we accumulate the right number.
case DocumentApp.ParagraphHeading.HEADING6:
prefix += "#";
case DocumentApp.ParagraphHeading.HEADING5:
prefix += "#";
case DocumentApp.ParagraphHeading.HEADING4:
prefix += "#";
case DocumentApp.ParagraphHeading.HEADING3:
prefix += "#";
case DocumentApp.ParagraphHeading.HEADING2:
prefix += "#";
case DocumentApp.ParagraphHeading.HEADING1:
prefix += "# ";
default:
}
} else if (element.getType() === DocumentApp.ElementType.LIST_ITEM) {
var listItem = element;
var nesting = listItem.getNestingLevel()
for (var i = 0; i < nesting; i++) {
prefix += " ";
}
var gt = listItem.getGlyphType();
// Bullet list (<ul>):
if (gt === DocumentApp.GlyphType.BULLET || gt === DocumentApp.GlyphType.HOLLOW_BULLET || gt === DocumentApp.GlyphType.SQUARE_BULLET) {
prefix += "* ";
} else {
// Ordered list (<ol>):
var key = listItem.getListId() + '.' + listItem.getNestingLevel();
var counter = listCounters[key] || 0;
counter++;
listCounters[key] = counter;
prefix += counter + ". ";
}
}
}
return prefix;
}
function processTextElement(inSrc, txt) {
if (typeof(txt) === 'string') {
return txt;
}
var pOut = txt.getText();
if (!txt.getTextAttributeIndices) {
return pOut;
}
var attrs = txt.getTextAttributeIndices();
var lastOff = pOut.length;
for (var i = attrs.length - 1; i >= 0; i--) {
var off = attrs[i];
var url = txt.getLinkUrl(off);
var font = txt.getFontFamily(off);
if (url) { // start of link
if (i >= 1 && attrs[i - 1] == off - 1 && txt.getLinkUrl(attrs[i - 1]) === url) {
// detect links that are in multiple pieces because of errors on formatting:
i -= 1;
off = attrs[i];
url = txt.getLinkUrl(off);
}
pOut = pOut.substring(0, off) + '[' + pOut.substring(off, lastOff) + '](' + url + ')' + pOut.substring(lastOff);
} else if (font) {
if (!inSrc && (font == 'Source Code Pro' || font == 'Consolas')) {
var code_font = font;
while (i >= 1 && txt.getFontFamily(attrs[i - 1]) && txt.getFontFamily(attrs[i - 1]) == code_font) {
// detect fonts that are in multiple pieces because of errors on formatting:
i -= 1;
off = attrs[i];
}
pOut = pOut.substring(0, off) + '`' + pOut.substring(off, lastOff) + '`' + pOut.substring(lastOff);
}
}
// Logger.log(txt.getText() + ' //' + off + ': ' + txt.isBold() + '/' + txt.isBold(off));
if (txt.isBold(off)) {
var d1 = d2 = "**";
if (txt.isItalic(off)) {
// edbacher: changed this to handle bold italic properly.
d1 = "**_";
d2 = "_**";
}
pOut = pOut.substring(0, off) + d1 + pOut.substring(off, lastOff) + d2 + pOut.substring(lastOff);
} else if (txt.isItalic(off)) {
pOut = pOut.substring(0, off) + '_' + pOut.substring(off, lastOff) + '_' + pOut.substring(lastOff);
}
lastOff = off;
}
return pOut;
}