- Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathzendesk.mjs
1136 lines (1016 loc) · 38.7 KB
/
zendesk.mjs
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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
// Read environment variables
varauthorized=true;
constZENDESK_USER=process.env.ZENDESK_USER;
constZENDESK_PASS=process.env.ZENDESK_PASS;
letzendeskApiLimit;
if(ZENDESK_USER&&ZENDESK_PASS){
console.log('Zendesk credentials found.');
zendeskApiLimit=400;
}else{
console.log('Zendesk credentials not found.');
zendeskApiLimit=200;
}
console.log(`API requests per minute: ${zendeskApiLimit}`);
constAlgoliaID=process.env.ALGOLIA_APPLICATION_ID;
constAlgoliaSecret=process.env.ALGOLIA_INDEXER_KEY;
constAlgoliaIndexName=process.env.ALGOLIA_INDEX;
// Define and parse command-line options
import{Command}from'commander';
constprogram=newCommand();
program
.name('zendesk')
.description('Push Markdown content to Zendesk Help Center')
.argument('<root>','Root directory')
.argument('<articles>','Glob pattern for article markdown files relative to root directory (must be inside)')
.argument('<url>','URL for target Zendesk locale, e.g. https://arduino.zendesk.com/api/v2/help_center/en-us')
.option('-d, --deploy','Deploy changes',false)
.option('-v, --verbose','enable verbose output',false)
.option('--cache-read [path]','read cached data',false)
.option('--cache-save [path]','save cached data',false)
.option('--html-save','save rendered HTML to disk',false)
.option('--skip-algolia','skip all Algolia actions',false)
.option('--html-diff','print rendered HTML diff',false)
.option('-w, --wait <delay>','delay in seconds before fetching data')
.option('-u, --syncIndex','check the entire search index for changes')
program.parse();
constroot=program.args[0];
constarticlePattern=program.args[1];
consttarget_url=program.args[2];
constdeployChanges=program.opts().deploy;
constverbose=program.opts().verbose;
constcacheRead=program.opts().cacheRead;
constcacheSave=program.opts().cacheSave;
consthtmlSave=program.opts().htmlSave;
consthtmlDiff=program.opts().htmlDiff;// TODO
constwait=program.opts().wait;
constsyncIndex=program.opts().syncIndex;
varskipAlgolia=program.opts().skipAlgolia;
// Set up Zendesk client
import{createClientascreateZendeskClient}from'node-zendesk';
constclient=createZendeskClient({
username: ZENDESK_USER,
password: ZENDESK_PASS,
remoteUri: target_url,
disableGlobalState: true,
helpcenter: true,
throttle: {
window: 60,
limit: zendeskApiLimit
}
});
// Algolia
importalgoliasearchfrom'algoliasearch';
letalgoliaIndex;
if(!skipAlgolia){
algoliaIndex=algoliasearch(AlgoliaID,AlgoliaSecret)
.initIndex(AlgoliaIndexName);
try{
varalgoliaExists=awaitalgoliaIndex.exists();
if(algoliaExists){
console.log('Algolia index exists.');
}
}catch(error){
console.log('Algolia index does not exist, and will not be updated!');
skipAlgolia=true;
}
}
// Empty line
console.log();
// HTML
import*ashtmlparser2from"htmlparser2";
import{render}from'dom-serializer';
import{minify}from'html-minifier';
import{convert}from'html-to-text';
// Markdown
importhljsfrom'highlight.js';// https://highlightjs.org/
importMarkdownItfrom'markdown-it';
importmarkdownItFootnotesfrom'markdown-it-footnote';
importmarkdownItAnchorfrom'markdown-it-anchor';
importmarkdownItAttrsfrom'markdown-it-attrs';
importmarkdownItGitHubAlertsfrom'markdown-it-github-alerts';
constmd=newMarkdownIt({
html: true,
smartquotes: true,
typographer: true,
quotes: '“”‘’',
highlight: function(str,lang){
if(lang&&hljs.getLanguage(lang)){
try{
returnhljs.highlight(str,{
language: lang
}).value;
}catch(__){}
}
return'';// use external default escaping
}
})
.use(markdownItGitHubAlerts,{
classPrefix: 'callout',
icons: {
note: '<span class="callout-icon callout-icon-note"></span>',
tip: '<span class="callout-icon callout-icon-tip"></span>',
important: '<span class="callout-icon callout-icon-important"></span>',
warning: '<span class="callout-icon callout-icon-warning"></span>',
caution: '<span class="callout-icon callout-icon-caution"></span>'
}
})
.use(markdownItAnchor,{
tabIndex: false
})
.use(markdownItAttrs,{
allowedAttributes: ['id','class'],
slugify: uslug
})
.use(markdownItFootnotes);
importfmfrom'front-matter';
// Other imports
importfgfrom'fast-glob';
importclcfrom'cli-color';
importpathfrom'path';
import{fileURLToPath}from'url'
const__dirname=path.dirname(fileURLToPath(import.meta.url));
importcolumnifyfrom'columnify';
importfsfrom'fs';
constfsPromises=fs.promises;
importfetchfrom'node-fetch';
importFormDatafrom'form-data';
importuslugfrom'uslug';
importdifffrom'fast-diff';
/* Run main function */
main();
asyncfunctionmain(){
letzendeskCategories,
zendeskSections,
zendeskArticles,
localDirPaths,
localArticles,
zendeskAttachments;
if(wait){
console.log(clc.underline(`Waiting for ${wait} second(s)...`));
awaitdelay(wait*1000);
console.log('Done.\n');
}
console.log(clc.underline('Reading and parsing local content...'));
awaitPromise.all([
exTime(fg('**',{
onlyDirectories: true,
cwd: root,
deep: 2
})).then(result=>{
console.log(`Read ${result.data.length} directory paths in ${result.exTime} ms.`);
returnresult.data;
}),
exTime(fg(articlePattern,{
cwd: root
}))
.then(result=>{
console.log(`Found ${result.data.length} Markdown files in ${result.exTime} ms.`);
returnexTime(Promise.all(parseMarkdown(root,result.data)));
})
.then(result=>{
console.log(`Parsed ${result.data.length} Markdown files in ${result.exTime} ms.`);
returnresult.data;
})
]).then(results=>{
localDirPaths=results[0];
localArticles=results[1];
});
if(cacheRead){
console.log(clc.underline('\nReading cached data...'));
vardata=awaitreadCache(path.join(__dirname,'/.cache'));
zendeskCategories=data.categories;
zendeskSections=data.sections;
zendeskArticles=data.articles;
zendeskAttachments=data.attachments;
console.log('Done reading cache.')
}else{
console.log(clc.underline('\nFetching categories and sections...'));
try{
awaitPromise.all([
exTime(client.categories.list()).then(result=>{
console.log(`Fetched ${result.data.length} categories in ${result.exTime} ms.`);
returnresult.data;
}),
exTime(client.sections.list()).then(result=>{
console.log(`Fetched ${result.data.length} sections in ${result.exTime} ms.`);
returnresult.data;
})
]).then(results=>{
zendeskCategories=results[0];
zendeskSections=results[1];
});
}catch(error){
console.error('Error occured fetching data from Zendesk.')
throwerror;
}
console.log(clc.underline('\nFetching articles...'));
try{
awaitPromise.all([
exTime(client.articles.list()).then(result=>{
console.log(`Fetched ${result.data.length} articles in ${result.exTime} ms.`);
returnresult.data;
})
]).then(results=>{
zendeskArticles=results[0];
});
}catch(error){
console.error('Error occured fetching data from Zendesk.')
throwerror;
}
console.log(clc.underline('\nFetching article attachments...'));
try{
awaitPromise.all([
exTime(getAllAttachmentsSync(zendeskArticles)).then(result=>{
console.log(`Fetched ${result.data.length} article attachment lists in ${result.exTime} ms.`);
returnresult.data;
})
]).then(results=>{
zendeskAttachments=results[0];
});
}catch(error){
console.error('Error occured fetching data from Zendesk.')
throwerror;
}
}
// VALIDATION: Directory structure should match Zendesk categories and sections
console.log(clc.underline('\nVerifying categories and sections...'));
constpositionRows=getPositionRows(zendeskCategories,zendeskSections,localDirPaths);
// Print results
console.log(columnify(positionRows.sort(comparePositionRow))+'\n');
// Articles
console.log(clc.underline('Checking articles for changes...'));
vararticles=getArticles(zendeskCategories,zendeskSections,zendeskArticles,localArticles,zendeskAttachments);
// Print changes
printChanges(zendeskCategories,zendeskSections,articles);
// Deploy changes unless --dry-run option is set
if(deployChanges){
console.log(clc.underline('\n'+'Deploying changes...'));
awaitdeploy(zendeskSections,articles);
console.log('Done');
}
// saveAllIndex(zendeskSections, articles);
if(syncIndex){
console.log('\n'+clc.underline('Syncing search index...'));
awaitsaveAllSearchObjects(zendeskSections,articles);
awaitdeleteOrphanedSearchObjects(articles);
}
// Save cache
if(cacheSave){
console.log('\n'+clc.underline('Saving cache...'));
vardata={
"categories": zendeskCategories,
"sections": zendeskSections,
"articles": zendeskArticles,
"attachments": zendeskAttachments
}
saveCacheSync(path.join(__dirname,'/.cache'),data);
}
}
/* --- END MAIN --- */
// Abstract article object
functiongetArticles(zendeskCategories,zendeskSections,zendeskArticles,localArticles,zendeskAttachments){
vararticles=[];
for(constlocalArticleoflocalArticles){
constlevelNames=getLevelsFromPath(localArticle.filepath);
constsectionID=getPositionID(zendeskCategories,zendeskSections, ...levelNames);
varmd=localArticle;
md.section_id=sectionID;
varzd=zendeskArticles.find(a=>a.id==localArticle.attributes.id);
if(md&&md.attributes.id&&!zd){
thrownewError(`Article ID ${md.attributes.id} in ${md.filepath} was not found in Zendesk!`);
}
vararticleAttachments=zendeskAttachments.find(za=>za&&za.article_id==localArticle.attributes.id);
if(articleAttachments){
articleAttachments=articleAttachments.attachments;// TODO..
}else{
articleAttachments=[];
}
articles.push({
"md": md,
"zd": zd,
"attachments": articleAttachments
});
}
varremovedArticles=zendeskArticles.filter(zendeskArticle=>!localArticles.some(localArticle=>localArticle.attributes.id==zendeskArticle.id));
for(constzendeskArticleofremovedArticles){
if(!zendeskArticle.draft){
articles.push({
"md": null,
"zd": zendeskArticle,
"attachments": null// whatever
});
}
}
returnarticles;
}
functionprintChanges(zendeskCategories,zendeskSections,articles){
varoffset=" - ";
// Sort articles into arrays
varnewArticles=[];
varunchangedArticles=[];
varupdatedArticles=[];
varremovedArticles=[];
for(constaofarticles){
if(a.md&&!a.zd){
newArticles.push(a);
}elseif(a.md&&a.zd){
if(hasChanges(a)){
updatedArticles.push(a)
}else{
unchangedArticles.push(a)
}
}else{
removedArticles.push(a);
}
}
// NEW ARTICLES
for(constaofnewArticles){
console.log(`${clc.cyan('[NEW]')}${clc.yellow('['+a.md.filepath+']')}`);
constattachmentReplacements=getAttachmentReplacements(a);
for(constattachmentofattachmentReplacements){
if(!attachment.target){
console.log(offset+'Uploading attachment: '+attachment.src);
}
}
console.log(offset+`"${a.md.attributes.title}" will be published as new article`);
}
// UPDATED ARTICLES
for(constaofupdatedArticles){
console.log(`${clc.green('[UPDATED]')}${clc.yellow('['+a.zd.html_url+']')}`)
// Check title
if(a.md.attributes.title!=a.zd.title){
console.log(offset+`${'title'}: ${clc.bgRed(a.zd.title)}${clc.bgGreen(a.md.attributes.title)}`);
}
// Check position
varoldPosition=getPositionNames(zendeskCategories,zendeskSections,a.zd.section_id).join(' > ');
varnewPosition=getPositionNames(zendeskCategories,zendeskSections,a.md.section_id).join(' > ');
if(newPosition!=oldPosition){
console.log(offset+`${'Position'}: ${clc.bgRed(oldPosition)}${clc.bgGreen(newPosition)}`);
}
// Check attachments and article body
constattachmentReplacements=getAttachmentReplacements(a);
constnewAttachmentReplacements=attachmentReplacements.filter(ar=>ar.target==null);
if(newAttachmentReplacements.length>0){
for(constnewAttachmentofnewAttachmentReplacements){
console.log(offset+'Uploading attachment: '+newAttachment.src);
}
console.log(offset+'Article body will be updated with new attachment URLs and any other changes.')
}else{
// Check body
constlocal=htmlparser2.parseDocument(md.render(a.md.body),{
decodeEntities: true
});
constremote=htmlparser2.parseDocument(a.zd.body,{
decodeEntities: true
});
for(constimgElementofhtmlparser2.DomUtils.filter(e=>e.name=='img',local)){
varreplacement=attachmentReplacements.find(ar=>ar.src==imgElement.attribs.src);
if(replacement){
imgElement.attribs.src=replacement.target;
}
}
constlocalRender=minify(render(local,{
encodeEntities: true
}),{
continueOnParseError: true,
collapseWhitespace: true
});
constremoteRender=minify(render(remote,{
encodeEntities: true
}),{
continueOnParseError: true,
collapseWhitespace: true
});
if(localRender!=remoteRender){
console.log(offset+'Article body will be updated.')
}
}
}
// UNCHANGED ARTICLES
if(verbose){
for(constaofunchangedArticles){
console.log(`${clc.blackBright('[UNCHANGED]')}${clc.yellow('['+a.zd.html_url+']')}`)
}
}
// REMOVED ARTICLES
for(constaofremovedArticles){
console.log(`${clc.red('[REMOVED]')}${clc.yellow('['+a.zd.html_url+']')}`)
console.log(offset+`"${a.zd.title}" will be removed`);
}
vartotalArticles=newArticles.length+updatedArticles.length+unchangedArticles.length+removedArticles.length;
varsummary=`🔍 ${totalArticles} Total 📝 ${clc.cyan(newArticles.length+' New')} 📤 ${clc.green(updatedArticles.length+' Update')} ❌ ${clc.red(removedArticles.length+' Removed')} 💤 ${clc.blackBright(unchangedArticles.length+' Unchanged')}`;
console.log(summary);
}
functioncreateArticle(article){
varsection_id=article.md.section_id;
// Create article draft without body
vararticleData={
"draft": true,
"body": null,
"locale": "en-us",
"permission_group_id": 1127974,
"title": article.md.attributes.title,
"user_segment_id": null
};
returnclient.articles.create(section_id,{
"article": articleData,
"notify_subscribers": false
}).then(result=>{
console.log(`[OK] Created "${result.title}" (${result.html_url})`);
article.zd=result;
// Add article ID and save to disk
article.md.attributes.id=result.id;
saveArticle(article);
// Return the article
returnarticle;
})
}
functioncreateAttachments(article){
constnewAttachments=getAttachmentReplacements(article).filter(attachment=>attachment.target==null);
returnPromise.all(newAttachments.map(newAttachment=>{
varattachmentPath=`content/${path.dirname(article.md.filepath)}/${newAttachment.src}`;
returncreateArticleAttachment(article.md.attributes.id,attachmentPath)// returns json
.then(result=>{
console.log('[OK] Uploading:'+result.display_file_name+' => '+result.content_url);
console.log(result);
article.attachments.push(result);
})
.catch(error=>{
if(error.statusCode){
console.log(`[${error.statusCode}] ${attachmentPath}`);
}else{
console.log(`[ER] ${attachmentPath}`);
throwerror;
}
});
}))
}
functiongetArticleUpdates(a){
varupdates={};
// Check position
if(a.md.section_id!=a.zd.section_id){
updates.section_id=a.md.section_id;
}
if(Object.keys(updates).length>0){
returnupdates;
}else{
returnnull;
}
}
functiongetTranslationUpdates(a){
varupdates={};
// Check title
if(a.md.attributes.title!=a.zd.title){
updates.title=a.md.attributes.title;
}
// Check draft
vardraft=(a.md.attributes.draft==true);// TODO: Simplify?
if(draft!=a.zd.draft){
updates.draft=draft;
}
// Check attachments and article body
constattachmentReplacements=getAttachmentReplacements(a);
varrenderedHTML=makeHTML(a.md.body,attachmentReplacements,false);
if(renderedHTML!=a.zd.body){
updates.body=makeHTML(a.md.body,attachmentReplacements,true);
}
if(Object.keys(updates).length>0){
returnupdates;
}else{
returnnull;
}
}
asyncfunctiondeploy(zendeskSections,articles){
// Sort articles into arrays
varnewArticles=[];
varupdatedArticles=[];
varremovedArticles=[];
for(constaofarticles){
if(a.md&&!a.zd){
newArticles.push(a);
}elseif(a.md&&a.zd){
if(hasChanges(a)){
updatedArticles.push(a)
}
}else{
removedArticles.push(a);
}
}
// Create any new articles as empty drafts (update them with the others)
constcreatedArticles=awaitPromise.all(newArticles.map(a=>createArticle(a)));
updatedArticles=updatedArticles.concat(createdArticles);
// For every article in Zendesk with changes...
awaitPromise.all(updatedArticles.map(asynca=>{
// Create any new attachments
awaitcreateAttachments(a);
// Make any updates to translation
// https://developer.zendesk.com/api-reference/help_center/help-center-api/translations/
consttranslationUpdates=getTranslationUpdates(a);
if(translationUpdates){
// console.log(`Updating translation for article "${a.zd.title}"...`);
try{
varresult=awaitclient.translations.updateForArticle(a.zd.id,'en-us',translationUpdates)
console.log(`[OK] Updating translation "${result.title}" (${result.html_url})`);
}catch(error){
console.log(`Error updating translation for ${a.zd.html_url}`);
console.log(error)
throwerror;
}
}
// Make any updates to article
// https://developer.zendesk.com/api-reference/help_center/help-center-api/articles/
constarticleUpdates=getArticleUpdates(a);
if(articleUpdates){
// console.log(`Updating article ${a.zd.title}...`);
try{
console.log(a.zd);
result=awaitclient.articles.update(a.zd.id,articleUpdates);
console.log(`[OK] Updating "${result.title}" (${result.html_url})`)
}catch(error){
console.log(`Error sending update to ${a.zd.html_url}`);
throwerror;
}
}
// Update Algolia
if((translationUpdates||articleUpdates)&&!skipAlgolia){
varsectionName=zendeskSections.find(s=>s.id==a.zd.section_id).name;
varcontentClearText=convert(a.zd.body,{
selectors: [
{selector: 'hr',format: 'skip'},
{selector: '[id]',format: 'skip'},// Lazy way to exclude empty a elements for anchoring
{selector: 'a',options: {ignoreHref: true}}
]
});
vardescriptionClearText;
if(contentClearText.length<150){
descriptionClearText=contentClearText;
}else{
descriptionClearText=contentClearText.substring(0,149)+'…';
}
try{
algoliaIndex.saveObject({
"objectID": a.zd.url,
"title": a.zd.title,
"documentation_type": "Help Center",
"category_of_helpcenter": sectionName,
"environment": "support.arduino.cc",
"language": "en",
"language_pretty": "English",
"content": contentClearText,
"description": descriptionClearText,
"url": a.zd.html_url,
}).wait();
}catch(error){
console.error("Couldn't save object in Algolia");
if(verbose){
console.error(error);
}
}
}
}));
// Delete unused attachments
for(constarticleofupdatedArticles){
constarticleAttachments=article.attachments;
constarticleReplacements=getAttachmentReplacements(article);
for(constarticleAttachmentofarticleAttachments){
if(!articleReplacements.some(ar=>ar.target==articleAttachment.content_url&&ar.src!=null)){
console.log(`Attachment ${articleAttachment.id} will be deleted from ${articleAttachment.content_url}`)
client.articleattachments.delete(articleAttachment.id)
.then(result=>{
console.log('Deleted attachment:'+articleAttachment.content_url)
})
.catch(error=>{
console.log(`[${error.statusCode}] Deleting attachment (id: ${articleAttachment.id}) ${articleAttachment.content_url}`);
})
}
}
}
// Archive articles not present on disk (do this synchronously to avoid error responses)
for(constarticleofremovedArticles){
try{
awaitclient.articles.delete(article.zd.id);
console.log(`[OK] Archiving article "${article.zd.title}" (${article.zd.html_url})`);
}catch(error){
console.error(`[${error.statusCode}] Archiving article "${article.zd.title}" (${article.zd.html_url})`);
}
if(!skipAlgolia){
awaitalgoliaIndex.deleteObject(article.zd.url);
}
}
}
functionhasChanges(article){
consttrackedAttributes=["title","section_id","body","draft"];
if(!article.md){
thrownewError(`Can't compare ${article.zd.html_url}, no local source to compare to!`);
}
if(!article.zd){
thrownewError(`Can't compare ${article.md.filepath}, no Zendesk target to compare to!`);
}
if(article.md.attributes.title!=article.zd.title){
returntrue;
}
if(article.md.section_id!=article.zd.section_id){
returntrue;
}
if(article.md.attributes.draft&&!article.zd.draft){
returntrue;
}
constattachmentReplacements=getAttachmentReplacements(article);
if(attachmentReplacements.some(attachment=>attachment.target==null)){
returntrue;
}
// Render body and compare
letlocalHTML=makeHTML(article.md.body,attachmentReplacements,true);
letzdHTML=article.zd.body;
if(htmlSave){
lethtmlFilePath=article.md.filepath.concat('.html')
fs.writeFileSync(root+'/'+htmlFilePath,localHTML,function(err){
if(err){
returnconsole.log(err);
}
});
}
if(!compareHTML(localHTML,zdHTML)){
returntrue;
}
// No changes found
returnfalse;
}
functioncompareHTML(a,b){
constlocal=htmlparser2.parseDocument(a,{
decodeEntities: true
});
constremote=htmlparser2.parseDocument(b,{
decodeEntities: true
});
constlocalRender=minify(render(local,{
encodeEntities: true
}),{
continueOnParseError: true,
collapseWhitespace: true
});
constremoteRender=minify(render(remote,{
encodeEntities: true
}),{
continueOnParseError: true,
collapseWhitespace: true
});
return(localRender==remoteRender);
}
functionmakeHTML(markdown,attachmentReplacements,encodeEntities){
consthtml=htmlparser2.parseDocument(md.render(markdown));
for(constimgElementofhtmlparser2.DomUtils.filter(e=>e.name=='img',html)){
varreplacement=attachmentReplacements.find(ar=>ar.src==imgElement.attribs.src);
if(replacement){
imgElement.attribs.src=replacement.target;
}
}
returnrender(html,{
xmlMode: false,
encodeEntities: encodeEntities
})
}
functiongetAttachmentReplacements(article){
vararticleAttachments=article.attachments;
letattachmentReplacements=[];
consthtml=htmlparser2.parseDocument(md.render(article.md.body));
for(constimgElementofhtmlparser2.DomUtils.filter(e=>e.name=='img',html)){
varsrc=imgElement.attribs.src;
if(src.startsWith('img')){
varproject_img_path=root+'/'+path.dirname(article.md.filepath)+'/'+src;
varstats=fs.statSync(project_img_path)
varfileSizeInBytes=stats.size;
varzdAttachment=articleAttachments.find(a=>a.display_file_name==path.basename(src)&&fileSizeInBytes==a.size);
if(zdAttachment){// Match found
attachmentReplacements.push({
"src": src,
"target": zdAttachment.content_url// .replace('/' + zdAttachment.file_name, '')
});
// Remove processed
zdAttachment.used=true;
}else{
// Not found
attachmentReplacements.push({
"src": src,
"target": null
});
}
}
}
// Check for unused attachments
varunusedArticleAttachments=articleAttachments.filter(aa=>!attachmentReplacements.some(ar=>ar.target==aa.content_url));
for(varaofunusedArticleAttachments){
attachmentReplacements.push({
"src": null,
"target": a.content_url
});
}
returnattachmentReplacements;
}
// NOTE: Does not work for subsections
functiongetPositionRow(zendeskCategories,zendeskSections,dirPath){
varpositionLevels=dirPath.split('/');
varzendeskCategory=zendeskCategories.find(c=>c.name==positionLevels[0]);
// If no category is found
if(!zendeskCategory){
return{
category: positionLevels[0],
section: '',
source: clc.yellow(dirPath),
target: clc.red('Not found.')
};
}elseif(positionLevels.length==1){// Category found, no section in path
return{
category: positionLevels[0],
section: '',
source: clc.green(dirPath),
target: clc.green(zendeskCategory.html_url.split('-').slice(0,2).join('-'))
};
}
varzendeskSection=zendeskSections.find(s=>s.name==positionLevels[1]&&s.category_id==zendeskCategory.id);
if(!zendeskSection){
return{
category: positionLevels[0],
section: positionLevels[1],
source: clc.yellow(dirPath),
target: clc.red('Not found.')
};
}else{// Section found
return{
category: positionLevels[0],
section: positionLevels[1],
source: clc.green(dirPath),
target: clc.green(zendeskCategory.html_url.split('-').slice(0,2).join('-'))
};
}
}
functiongetPositionRows(zendeskCategories,zendeskSections,localDirPaths){
constpositionRows=[];
for(constdirpathoflocalDirPaths){
positionRows.push(getPositionRow(zendeskCategories,zendeskSections,dirpath));
}
// Zendesk categories
for(constzendeskCategoryofzendeskCategories){
// console.log(zCategory);
if(!localDirPaths.find(dirPath=>dirPath==zendeskCategory.name)){
positionRows.push({
category: dirPath,
section: '',
source: clc.red('Not found'),
target: clc.yellow(zendeskCategory.html_url.split('-').slice(0,2).join('-'))
})
}
}
// Zendesk sections
for(constzendeskSectionofzendeskSections){
constcategory_id=zendeskSection.category_id;
constcategory_name=zendeskCategories.find(c=>c.id==zendeskSection.category_id).name;
constsection_name=zendeskSection.name;
if(!localDirPaths.find(dirPath=>dirPath==`${category_name}/${section_name}`)){
positionRows.push({
category: category_name,
section: section_name,
source: clc.red('Not found'),
target: clc.yellow(zendeskCategory.html_url.split('-').slice(0,2).join('-'))
})
}
}
returnpositionRows;
}
functionparseMarkdown(base,filePaths){
varpromises=filePaths.map(filePath=>fsPromises.readFile(path.join(base,filePath),'utf8')
.then(data=>{
vararticle=fm(data);
article.filepath=filePath;
returnarticle;
}));
returnpromises;
}
functioncomparePositionRow(a,b){
if(a.category>b.category){
return1;
}
if(a.category<b.category){
return-1;
}
if(a.section==null||a.section>b.section){
return1;
}
if(b.section==null||b.section>a.section){
return1;
}
// a and b are the same position
return0;
}
functiongetLevelsFromPath(path){
returnpath.split('/').slice(0,2);
}
functiongetPositionID(zendeskCategories,zendeskSections, ...levelNames){
constcategoryName=levelNames[0];
constcategory=zendeskCategories.find(c=>c.name==categoryName);
if(levelNames.length==1){
returncategory.id;
}
constsectionName=levelNames[1];
constsection=zendeskSections.find(s=>s.name==sectionName&&s.category_id==category.id);
returnsection.id;
}
functiongetPositionNames(zendeskCategories,zendeskSections,id){
constsection=zendeskSections.find(s=>s.id==id);
if(section){
constcategory=zendeskCategories.find(c=>c.id==section.category_id);
return[category.name,section.name];
}else{
// No section was found, look for category instead
returnzendeskCategories.find(c=>c.id==id);
}
}
functiondelay(ms){
returnnewPromise(resolve=>{
setTimeout(resolve,ms);
});
}
functiongetAllAttachments(localArticles){
varattachment_promises=[];
for(constlocalArticleoflocalArticles){
constid=localArticle.attributes.id;
if(id){
attachment_promises.push(client.articleattachments.list(id)
.then(result=>{
return{
"article_id": id,
"attachments": result.article_attachments
};
})
.catch((error)=>{
if(error.statusCode!=404){
console.error(error);
}
}));
}
}
returnPromise.all(attachment_promises);
}
asyncfunctiongetAllAttachmentsSync(zendeskArticles){
varattachmentLists=[];
for(constzendeskArticleofzendeskArticles){
constid=zendeskArticle.id;
constdraft=zendeskArticle.draft;// Will fail for drafts unless authenticated
if(id){
if(verbose){
console.log('Fetching attachments for article '+id);
}
varresult=awaitclient.articleattachments.list(id);
if(!result.article_attachments){
console.log(`Warning: No article attachment array for article with ID ${id}`);
console.log(result);
}else{
attachmentLists.push({
"article_id": id,
"attachments": result.article_attachments
})
}
}
}
returnattachmentLists;
}
functionsaveCacheSync(filePath,data){
fs.writeFileSync(filePath,JSON.stringify(data),function(err){
if(err){
returnconsole.log(err);
}
});
}
functionreadCache(filePath){
returnnewPromise(function(resolve,reject){
fs.readFile(filePath,'utf8',function(err,data){
if(err){
returnconsole.log(err);
}else{
resolve(JSON.parse(data));
}
});
});
}