# All files / src/Site index.ts

30.12% Statements 194/644
23.93% Branches 45/188
24.44% Functions 33/135
30.66% Lines 188/613

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    1001
    1002
    1003
    1004
    1005
    1006
    1007
    1008
    1009
    1010
    1011
    1012
    1013
    1014
    1015
    1016
    1017
    1018
    1019
    1020
    1021
    1022
    1023
    1024
    1025
    1026
    1027
    1028
    1029
    1030
    1031
    1032
    1033
    1034
    1035
    1036
    1037
    1038
    1039
    1040
    1041
    1042
    1043
    1044
    1045
    1046
    1047
    1048
    1049
    1050
    1051
    1052
    1053
    1054
    1055
    1056
    1057
    1058
    1059
    1060
    1061
    1062
    1063
    1064
    1065
    1066
    1067
    1068
    1069
    1070
    1071
    1072
    1073
    1074
    1075
    1076
    1077
    1078
    1079
    1080
    1081
    1082
    1083
    1084
    1085
    1086
    1087
    1088
    1089
    1090
    1091
    1092
    1093
    1094
    1095
    1096
    1097
    1098
    1099
    1100
    1101
    1102
    1103
    1104
    1105
    1106
    1107
    1108
    1109
    1110
    1111
    1112
    1113
    1114
    1115
    1116
    1117
    1118
    1119
    1120
    1121
    1122
    1123
    1124
    1125
    1126
    1127
    1128
    1129
    1130
    1131
    1132
    1133
    1134
    1135
    1136
    1137
    1138
    1139
    1140
    1141
    1142
    1143
    1144
    1145
    1146
    1147
    1148
    1149
    1150
    1151
    1152
    1153
    1154
    1155
    1156
    1157
    1158
    1159
    1160
    1161
    1162
    1163
    1164
    1165
    1166
    1167
    1168
    1169
    1170
    1171
    1172
    1173
    1174
    1175
    1176
    1177
    1178
    1179
    1180
    1181
    1182
    1183
    1184
    1185
    1186
    1187
    1188
    1189
    1190
    1191
    1192
    1193
    1194
    1195
    1196
    1197
    1198
    1199
    1200
    1201
    1202
    1203
    1204
    1205
    1206
    1207
    1208
    1209
    1210
    1211
    1212
    1213
    1214
    1215
    1216
    1217
    1218
    1219
    1220
    1221
    1222
    1223
    1224
    1225
    1226
    1227
    1228
    1229
    1230
    1231
    1232
    1233
    1234
    1235
    1236
    1237
    1238
    1239
    1240
    1241
    1242
    1243
    1244
    1245
    1246
    1247
    1248
    1249
    1250
    1251
    1252
    1253
    1254
    1255
    1256
    1257
    1258
    1259
    1260
    1261
    1262
    1263
    1264
    1265
    1266
    1267
    1268
    1269
    1270
    1271
    1272
    1273
    1274
    1275
    1276
    1277
    1278
    1279
    1280
    1281
    1282
    1283
    1284
    1285
    1286
    1287
    1288
    1289
    1290
    1291
    1292
    1293
    1294
    1295
    1296
    1297
    1298
    1299
    1300
    1301
    1302
    1303
    1304
    1305
    1306
    1307
    1308
    1309
    1310
    1311
    1312
    1313
    1314
    1315
    1316
    1317
    1318
    1319
    1320
    1321
    1322
    1323
    1324
    1325
    1326
    1327
    1328
    1329
    1330
    1331
    1332
    1333
    1334
    1335
    1336
    1337
    1338
    1339
    1340
    1341
    1342
    1343
    1344
    1345
    1346
    1347
    1348
    1349
    1350
    1351
    1352
    1353
    1354
    1355
    1356
    1357
    1358
    1359
    1360
    1361
    1362
    1363
    1364
    1365
    1366
    1367
    1368
    1369
    1370
    1371
    1372
    1373
    1374
    1375
    1376
    1377
    1378
    1379
    1380
    1381
    1382
    1383
    1384
    1385
    1386
    1387
    1388
    1389
    1390
    1391
    1392
    1393
    1394
    1395
    1396
    1397
    1398
    1399
    1400
    1401
    1402
    1403
    1404
    1405
    1406
    1407
    1408
    1409
    1410
    1411
    1412
    1413
    1414
    1415
    1416
    1417
    1418
    1419
    1420
    1421
    1422
    1423
    1424
    1425
    1426
    1427
    1428
    1429
    1430
    1431
    1432
    1433
    1434
    1435
    1436
    1437
    1438
    1439
    1440
    1441
    1442
    1443
    1444
    1445
    1446
    1447
    1448
    1449
    1450
    1451
    1452
    1453
    1454
    1455
    1456
    1457
    1458
    1459
    1460
    1461
    1462
    1463
    1464
    1465
    1466
    1467
    1468
    1469
    1470
    1471
    1472
    1473
    1474
    1475
    1476
    1477
    1478
    1479
    1480
    1481
    1482
    1483
    1484
    1485
    1486
    1487
    1488
    1489
    1490
    1491
    1492
    1493
    1494
    1495
    1496
    1497
    1498
    1499
    1500
    1501
    1502
    1503
    1504
    1505
    1506
    1507
    1508
    1509
    1510
    1511
    1512
    1513
    1514
    1515
    1516
    1517
    1518
    1519
    1520
    1521
    1522
    1523
    1524
    1525
    1526
    1527
    1528
    1529
    1530
    1531
    1532
    1533
    1534
    1535
    1536
    1537
    1538
    1539
    1540
    1541
    1542
    1543
    1544
    1545
    1546
    1547
    1548
    1549
    1550
    1551
    15521x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x
    1x
    1x
    1x
    1x
    1x

    1x
    1x
    1x

    1x

    1x

    1x
    1x
    1x
    1x
    1x

    1x
    1x

    1x
    1x

    1x

    1x
    1x

    17x

    1x

    1x

    1x
    1x

    1x

    38x

    38x
    38x
    38x

    38x

    38x
    38x
    38x

    38x
    38x
    38x
    38x
    38x

    38x
    38x
    38x

    38x
    38x

    38x
    38x

    29x
    29x

    10x
    10x

    8x
    14x

    8x
    8x
    4x
    1x
    8x
    1x

    12x
    10x
    9x

    7x
    7x
    11x
    11x

    7x
    7x
    7x
    6x

    3x
    17x
    9x

    9x
    3x

    3x

    3x

    3x
    3x

    3x
    3x

    3x
    3x

    3x

    2x

    2x
    5x

    5x
    5x

    5x
    5x

    5x
    5x

    5x
    5x
    9x

    9x

    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

    24x

    24x
    24x

    24x
    24x
    24x
    14x

    24x
    24x
    1x

    23x

    23x

    23x
    21x
    21x
    4x

    17x

    17x
    4x

    3x

    13x
    4x

    3x

    9x
    4x

    3x

    5x
    4x

    3x

    1x

    12x

    14x
    14x

    16x
    8x

    8x
    8x
    8x
    4x

    4x
    4x

    14x
    14x

    14x
    14x
    14x

    14x
    14x
    14x
    14x

    14x
    14x
    14x
    14x
    14x
    14x
    14x

    14x

    38x

    38x

    38x

    38x

    38x

    38x

     import cheerio from 'cheerio';
    import fs from 'fs-extra';
    import ignore, { Ignore } from 'ignore';
    import path from 'path';
    import walkSync from 'walk-sync';
    import simpleGit, { SimpleGit } from 'simple-git';
    import Bluebird from 'bluebird';
    import ghpages from 'gh-pages';

    import { Template as NunjucksTemplate } from 'nunjucks';
    import { SiteConfig, SiteConfigPage, SiteConfigStyle } from './SiteConfig';
    import { Page } from '../Page';
    import { PageConfig } from '../Page/PageConfig';
    import { VariableProcessor } from '../variables/VariableProcessor';
    import { VariableRenderer } from '../variables/VariableRenderer';
    import { ExternalManager, ExternalManagerConfig } from '../External/ExternalManager';
    import { SiteLinkManager } from '../html/SiteLinkManager';
    import { PluginManager } from '../plugins/PluginManager';
    import type { FrontMatter } from '../plugins/Plugin';
    import { sequentialAsyncForEach } from '../utils/async';
    import { delay } from '../utils/delay';
    import * as fsUtil from '../utils/fsUtil';
    import * as gitUtil from '../utils/git';
    import * as logger from '../utils/logger';
    import { SITE_CONFIG_NAME, LAZY_LOADING_SITE_FILE_NAME, _ } from './constants';

    // Change when they are migrated to TypeScript
    const ProgressBar = require('../lib/progress');
    const { LayoutManager } = require('../Layout');
    require('../patches/htmlparser2');

    const url = {
      join: path.posix.join,
    };

    const MARKBIND_VERSION = require('../../package.json').version;

    const CONFIG_FOLDER_NAME = '_markbind';
    const SITE_FOLDER_NAME = '_site';
    const TEMP_FOLDER_NAME = '.temp';
    const TEMPLATE_SITE_ASSET_FOLDER_NAME = 'markbind';
    const LAYOUT_SITE_FOLDER_NAME = 'layouts';

    const FAVICON_DEFAULT_PATH = 'favicon.ico';
    const USER_VARIABLES_PATH = '_markbind/variables.md';

    const PAGE_TEMPLATE_NAME = 'page.njk';
    const SITE_DATA_NAME = 'siteData.json';

    const MAX_CONCURRENT_PAGE_GENERATION_PROMISES = 4;

    const LAZY_LOADING_BUILD_TIME_RECOMMENDATION_LIMIT = 30000;
    const LAZY_LOADING_REBUILD_TIME_RECOMMENDATION_LIMIT = 5000;

    function getBootswatchThemePath(theme: string) {
      return require.resolve(`bootswatch/dist/${theme}/bootstrap.min.css`);
    }

    const SUPPORTED_THEMES_PATHS: Record<string, string> = {
      'bootswatch-cerulean': getBootswatchThemePath('cerulean'),
      'bootswatch-cosmo': getBootswatchThemePath('cosmo'),
      'bootswatch-flatly': getBootswatchThemePath('flatly'),
      'bootswatch-journal': getBootswatchThemePath('journal'),
      'bootswatch-litera': getBootswatchThemePath('litera'),
      'bootswatch-lumen': getBootswatchThemePath('lumen'),
      'bootswatch-lux': getBootswatchThemePath('lux'),
      'bootswatch-materia': getBootswatchThemePath('materia'),
      'bootswatch-minty': getBootswatchThemePath('minty'),
      'bootswatch-pulse': getBootswatchThemePath('pulse'),
      'bootswatch-sandstone': getBootswatchThemePath('sandstone'),
      'bootswatch-simplex': getBootswatchThemePath('simplex'),
      'bootswatch-sketchy': getBootswatchThemePath('sketchy'),
      'bootswatch-spacelab': getBootswatchThemePath('spacelab'),
      'bootswatch-united': getBootswatchThemePath('united'),
      'bootswatch-yeti': getBootswatchThemePath('yeti'),
      'bootswatch-zephyr': getBootswatchThemePath('zephyr'),
    };

    const HIGHLIGHT_ASSETS = {
      dark: 'codeblock-dark.min.css',
      light: 'codeblock-light.min.css',
    };

    const MARKBIND_WEBSITE_URL = 'https://markbind.org/';
    const MARKBIND_LINK_HTML = `<a href='${MARKBIND_WEBSITE_URL}'>MarkBind ${MARKBIND_VERSION}</a>`;

    /*
     * A page configuration object.
     */
    type PageCreationConfig = {
      externalScripts: string[],
      frontmatter: FrontMatter,
      layout: string,
      pageSrc: string,
      searchable: boolean,
      faviconUrl?: string,
      glob?: string,
      globExclude?: string
      title?: string,
    };

    type AddressablePage = {
      frontmatter: FrontMatter,
      layout: string,
      searchable: string,
      src: string,
      externalScripts?: string[],
      faviconUrl?: string,
      title?: string,
    };

    type PageGenerationTask = {
      mode: string,
      pages: Page[]
    };

    type PageGenerationContext = {
      startTime: Date,
      numPagesGenerated: number,
      numPagesToGenerate: number,
      isCompleted: boolean,
    };

    type DeployOptions = {
      branch: string,
      message: string,
      repo: string,
      remote: string,
      user?: { name: string; email: string; },
    };

    export class Site {
      dev: boolean;
      rootPath: string;
      outputPath: string;
      tempPath: string;
      siteAssetsDestPath: string;
      pageTemplatePath: string;
      pageTemplate: NunjucksTemplate;
      pages: Page[];
      addressablePages: AddressablePage[];
      addressablePagesSource: string[];
      baseUrlMap: Set<string>;
      forceReload: boolean;
      siteConfig!: SiteConfig;
      siteConfigPath: string;
      variableProcessor!: VariableProcessor;
      pluginManager!: PluginManager;
      siteLinkManager!: SiteLinkManager;
      backgroundBuildMode: string | boolean;
      stopGenerationTimeThreshold: Date;
      postBackgroundBuildFunc: () => void;
      onePagePath: string;
      currentPageViewed: string;
      currentOpenedPages: string[];
      toRebuild: Set<string>;
      externalManager!: ExternalManager;
      // TODO: add LayoutManager when it has been migrated
      layoutManager: any;

      constructor(rootPath: string, outputPath: string, onePagePath: string, forceReload = false,
                  siteConfigPath = SITE_CONFIG_NAME, dev: any, backgroundBuildMode: boolean,
                  postBackgroundBuildFunc: () => void) {
        this.dev = !!dev;

        this.rootPath = rootPath;
        this.outputPath = outputPath;
        this.tempPath = path.join(rootPath, TEMP_FOLDER_NAME);

        // MarkBind assets to be copied
        this.siteAssetsDestPath = path.join(outputPath, TEMPLATE_SITE_ASSET_FOLDER_NAME);

        // Page template path
        this.pageTemplatePath = path.join(__dirname, '../Page', PAGE_TEMPLATE_NAME);
        this.pageTemplate = VariableRenderer.compile(fs.readFileSync(this.pageTemplatePath, 'utf8'));
        this.pages = [];

        // Other properties
        this.addressablePages = [];
        this.addressablePagesSource = [];
        this.baseUrlMap = new Set();
        this.forceReload = forceReload;
        this.siteConfigPath = siteConfigPath;

        // Background build properties
        this.backgroundBuildMode = onePagePath && backgroundBuildMode;
        this.stopGenerationTimeThreshold = new Date();
        this.postBackgroundBuildFunc = postBackgroundBuildFunc || (() => {});

        // Lazy reload properties
        this.onePagePath = onePagePath;
        this.currentPageViewed = onePagePath
          ? path.resolve(this.rootPath, fsUtil.removeExtension(onePagePath))
          : '';
        this.currentOpenedPages = [];
        this.toRebuild = new Set();
      }

      /**
       * Util Methods
       */

      static async rejectHandler(error: unknown, removeFolders: string[]) {
        logger.warn(error);
        try {
          await Promise.all(removeFolders.map(folder => fs.remove(folder)));
        } catch (err) {
          logger.error(`Failed to remove generated files after error!\n${(err as Error).message}`);
        }
      }

      beforeSiteGenerate() {
        this.variableProcessor.invalidateCache();
        this.externalManager.reset();
        this.pluginManager.beforeSiteGenerate();
      }

      /**
       * Changes the site variable of the current page being viewed, building it if necessary.
       * @param normalizedUrl BaseUrl-less and extension-less url of the page
       * @return Boolean of whether the page needed to be rebuilt
       */
      changeCurrentPage(normalizedUrl: string) {
        this.currentPageViewed = path.join(this.rootPath, normalizedUrl);

        Iif (this.toRebuild.has(this.currentPageViewed)) {
          this.beforeSiteGenerate();
          /*
           Lazy loading only builds the page being viewed, but the user may be quick enough
           to trigger multiple page builds before the first one has finished building,
           hence we need to take this into account by using the delayed variant of the method.
           */
          this.rebuildPagesBeingViewed(this.currentPageViewed);
          return true;
        }

        return false;
      }

      /**
       * Changes the list of current opened pages
       * @param normalizedUrls Collection of normalized url of pages taken from the clients
       * ordered from most-to-least recently opened
       */
      changeCurrentOpenedPages(normalizedUrls: string[]) {
        Iif (!this.onePagePath) {
          return;
        }

        const openedPages = normalizedUrls.map(normalizedUrl => path.join(this.rootPath, normalizedUrl));
        this.currentOpenedPages = _.uniq(openedPages);

        if (this.currentOpenedPages.length > 0) {
          logger.info('Current opened pages, from most-to-least recent:');
          this.currentOpenedPages.forEach((pagePath, idx) => {
            logger.info(`${idx + 1}. ${fsUtil.ensurePosix(path.relative(this.rootPath, pagePath))}`);
          });
        } else {
          logger.info('No pages are currently opened');
        }
      }

      /**
       * Read and store the site config from site.json, overwrite the default base URL
       * if it's specified by the user.
       * @param baseUrl user defined base URL (if exists)
       */
      async readSiteConfig(baseUrl?: string) {
        this.siteConfig = await SiteConfig.readSiteConfig(this.rootPath, this.siteConfigPath, baseUrl);
        return this.siteConfig;
      }

      listAssets(fileIgnore: Ignore) {
        const files = walkSync(this.rootPath, { directories: false });
        return fileIgnore.filter(files);
      }

      /**
       * Create a Page object from the site and page creation config.
       */
      createPage(config: PageCreationConfig): Page {
        const sourcePath = path.join(this.rootPath, config.pageSrc);
        const resultPath = path.join(this.outputPath, fsUtil.setExtension(config.pageSrc, '.html'));

        const baseAssetsPath = path.posix.join(
          this.siteConfig.baseUrl || '/', TEMPLATE_SITE_ASSET_FOLDER_NAME,
        );

        const pageConfig = new PageConfig({
          asset: {
            bootstrap: path.posix.join(baseAssetsPath, 'css', 'bootstrap.min.css'),
            externalScripts: _.union(this.siteConfig.externalScripts, config.externalScripts),
            fontAwesome: path.posix.join(baseAssetsPath, 'fontawesome', 'css', 'all.min.css'),
            glyphicons: path.posix.join(baseAssetsPath, 'glyphicons', 'css', 'bootstrap-glyphicons.min.css'),
            octicons: path.posix.join(baseAssetsPath, 'css', 'octicons.css'),
            materialIcons: path.posix.join(baseAssetsPath, 'material-icons', 'material-icons.css'),
            highlight: path.posix.join(baseAssetsPath, 'css',
                                       HIGHLIGHT_ASSETS[this.siteConfig.style.codeTheme]),
            markBindCss: path.posix.join(baseAssetsPath, 'css', 'markbind.min.css'),
            markBindJs: path.posix.join(baseAssetsPath, 'js', 'markbind.min.js'),
            pageNavCss: path.posix.join(baseAssetsPath, 'css', 'page-nav.css'),
            siteNavCss: path.posix.join(baseAssetsPath, 'css', 'site-nav.css'),
            bootstrapUtilityJs: path.posix.join(baseAssetsPath, 'js', 'bootstrap-utility.min.js'),
            polyfillJs: path.posix.join(baseAssetsPath, 'js', 'polyfill.min.js'),
            // We use development Vue when MarkBind is served in 'dev' mode so that hydration issues are reported
            vue: this.dev
              ? 'https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js'
              : path.posix.join(baseAssetsPath, 'js', 'vue.min.js'),
            layoutUserScriptsAndStyles: [],
          },
          baseUrlMap: this.baseUrlMap,
          dev: this.dev,
          faviconUrl: config.faviconUrl,
          frontmatterOverride: config.frontmatter,
          layout: config.layout,
          layoutsAssetPath: path.posix.join(baseAssetsPath, LAYOUT_SITE_FOLDER_NAME),
          pluginManager: this.pluginManager,
          resultPath,
          rootPath: this.rootPath,
          searchable: this.siteConfig.enableSearch && config.searchable,
          siteLinkManager: this.siteLinkManager,
          siteOutputPath: this.outputPath,
          sourcePath,
          src: config.pageSrc,
          title: config.title,
          template: this.pageTemplate,
          variableProcessor: this.variableProcessor,
          addressablePagesSource: this.addressablePagesSource,
          layoutManager: this.layoutManager,
        });
        return new Page(pageConfig, this.siteConfig);
      }

      static printBaseUrlMessage() {
        logger.info('The default base URL of your site is set to /\n'
          + 'You can change the base URL of your site by editing site.json\n'
          + 'Check https://markbind.org/userGuide/siteConfiguration.html for more information.');
      }

      /**
       * Updates the paths to be traversed as addressable pages and returns a list of filepaths to be deleted
       */
      updateAddressablePages() {
        const oldAddressablePagesSources = this.addressablePages.slice().map(page => page.src);
        this.collectAddressablePages();
        const newAddressablePagesSources = this.addressablePages.map(page => page.src);

        return _.difference(oldAddressablePagesSources, newAddressablePagesSources)
          .map(filePath => fsUtil.setExtension(filePath, '.html'));
      }

      getPageGlobPaths(page: SiteConfigPage, pagesExclude: string[]) {
        const pageGlobs = page.glob ?? [];
        return walkSync(this.rootPath, {
          directories: false,
          globs: Array.isArray(pageGlobs) ? pageGlobs : [pageGlobs],
          ignore: [
            CONFIG_FOLDER_NAME,
            SITE_FOLDER_NAME,
            ...pagesExclude.concat(page.globExclude || []),
          ],
        });
      }

      /**
       * Collects the paths to be traversed as addressable pages
       */
      collectAddressablePages() {
        const { pages, pagesExclude } = this.siteConfig;
        const pagesFromSrc = _.flatMap(pages.filter(page => page.src), page => (Array.isArray(page.src)
          ? page.src.map(pageSrc => ({ ...page, src: pageSrc }))
          : [page])) as unknown as AddressablePage[];
        const set = new Set();
        const duplicatePages = pagesFromSrc
          .filter(page => set.size === set.add(page.src).size)
          .map(page => page.src);
        if (duplicatePages.length > 0) {
          throw new Error(`Duplicate page entries found in site config: ${_.uniq(duplicatePages).join(', ')}`);
        }
        const pagesFromGlobs = _.flatMap(pages.filter(page => page.glob),
                                         page => this.getPageGlobPaths(page, pagesExclude)
                                           .map(filePath => ({
                                             src: filePath,
                                             searchable: page.searchable,
                                             layout: page.layout,
                                             frontmatter: page.frontmatter,
                                           }))) as AddressablePage[];
        /*
         Add pages collected from globs and merge properties for pages
         Page properties collected from src have priority over page properties from globs,
         while page properties from later entries take priority over earlier ones.
         */
        const filteredPages: Record<string, AddressablePage> = {};
        pagesFromGlobs.concat(pagesFromSrc).forEach((page) => {
          const filteredPage = _.omitBy(page, _.isUndefined) as AddressablePage;
          filteredPages[page.src] = page.src in filteredPages
            ? { ...filteredPages[page.src], ...filteredPage }
            : filteredPage;
        });
        this.addressablePages = Object.values(filteredPages);
        this.addressablePagesSource.length = 0;
        this.addressablePages.forEach((page) => {
          this.addressablePagesSource.push(fsUtil.removeExtensionPosix(page.src));
        });
      }

      /**
       * Collects the base url map in the site/subsites
       */
      collectBaseUrl() {
        const candidates = walkSync(this.rootPath, { directories: false })
          .filter(x => x.endsWith(this.siteConfigPath))
          .map(x => path.resolve(this.rootPath, x));

        this.baseUrlMap = new Set(candidates.map(candidate => path.dirname(candidate)));
        this.variableProcessor = new VariableProcessor(this.rootPath, this.baseUrlMap);

        this.buildManagers();
      }

      /**
       * Set up the managers used with the configurations.
       */
      buildManagers() {
        const config: ExternalManagerConfig & { externalManager: ExternalManager } = {
          baseUrlMap: this.baseUrlMap,
          baseUrl: this.siteConfig.baseUrl,
          rootPath: this.rootPath,
          outputPath: this.outputPath,
          ignore: this.siteConfig.ignore,
          addressablePagesSource: this.addressablePagesSource,
          variableProcessor: this.variableProcessor,
          intrasiteLinkValidation: this.siteConfig.intrasiteLinkValidation,
          codeLineNumbers: this.siteConfig.style.codeLineNumbers,
          plantumlCheck: this.siteConfig.plantumlCheck,
          headerIdMap: {},
          siteLinkManager: this.siteLinkManager,
          pluginManager: this.pluginManager,
          externalManager: this.externalManager,
        };
        this.siteLinkManager = new SiteLinkManager(config);
        config.siteLinkManager = this.siteLinkManager;

        this.pluginManager = new PluginManager(config, this.siteConfig.plugins, this.siteConfig.pluginsContext);
        config.pluginManager = this.pluginManager;

        this.externalManager = new ExternalManager(config);
        config.externalManager = this.externalManager;

        this.layoutManager = new LayoutManager(config);
      }

      /**
       * Collects the user defined variables map in the site/subsites
       */
      collectUserDefinedVariablesMap() {
        this.variableProcessor.resetUserDefinedVariablesMap();

        this.baseUrlMap.forEach((base) => {
          const userDefinedVariablesPath = path.resolve(base, USER_VARIABLES_PATH);
          let content;
          try {
            content = fs.readFileSync(userDefinedVariablesPath, 'utf8');
          } catch (e) {
            content = '';
            logger.warn((e as Error).message);
          }

          /*
           We retrieve the baseUrl of the (sub)site by appending the relative to the configured base url
           i.e. We ignore the configured baseUrl of the sub sites.
           */
          const siteRelativePathFromRoot = fsUtil.ensurePosix(path.relative(this.rootPath, base));
          const siteBaseUrl = siteRelativePathFromRoot === ''
            ? this.siteConfig.baseUrl
            : path.posix.join(this.siteConfig.baseUrl || '/', siteRelativePathFromRoot);
          this.variableProcessor.addUserDefinedVariable(base, 'baseUrl', siteBaseUrl);
          this.variableProcessor.addUserDefinedVariable(base, 'MarkBind', MARKBIND_LINK_HTML);

          const $ = cheerio.load(content, { decodeEntities: false });
          $('variable,span').each((_index, element) => {
            const name = $(element).attr('name') || $(element).attr('id');

            this.variableProcessor.renderAndAddUserDefinedVariable(base, name, $(element).html());
          });
        });
      }

      /**
       * Collects the user defined variables map in the site/subsites
       * if there is a change in the variables file
       * @param filePaths array of paths corresponding to files that have changed
       */
      collectUserDefinedVariablesMapIfNeeded(filePaths: string[]) {
        const variablesPath = path.resolve(this.rootPath, USER_VARIABLES_PATH);
        Iif (filePaths.includes(variablesPath)) {
          this.collectUserDefinedVariablesMap();
          return true;
        }
        return false;
      }

      /**
       * Generate the website.
       * @param baseUrl user defined base URL (if exists)
       */
      async generate(baseUrl: string | undefined): Promise<any> {
        const startTime = new Date();
        // Create the .tmp folder for storing intermediate results.
        fs.emptydirSync(this.tempPath);
        // Clean the output folder; create it if not exist.
        fs.emptydirSync(this.outputPath);
        const lazyWebsiteGenerationString = this.onePagePath ? '(lazy) ' : '';
        logger.info(`Website generation ${lazyWebsiteGenerationString}started at ${
          startTime.toLocaleTimeString()}`);

        try {
          await this.readSiteConfig(baseUrl);
          this.collectAddressablePages();
          this.collectBaseUrl();
          this.collectUserDefinedVariablesMap();
          await this.buildAssets();
          await (this.onePagePath ? this.lazyBuildSourceFiles() : this.buildSourceFiles());
          await this.copyCoreWebAsset();
          await this.copyBootstrapTheme(false);
          await this.copyFontAwesomeAsset();
          await this.copyOcticonsAsset();
          await this.copyMaterialIconsAsset();
          await this.writeSiteData();
          this.calculateBuildTimeForGenerate(startTime, lazyWebsiteGenerationString);
          Iif (this.backgroundBuildMode) {
            this.backgroundBuildNotViewedFiles();
          }
        } catch (error) {
          await Site.rejectHandler(error, [this.tempPath, this.outputPath]);
        }
      }

      /**
       * Helper function for generate().
       */
      calculateBuildTimeForGenerate(startTime: Date, lazyWebsiteGenerationString: string) {
        const endTime = new Date();
        const totalBuildTime = (endTime.getTime() - startTime.getTime()) / 1000;
        logger.info(`Website generation ${lazyWebsiteGenerationString}complete! Total build time: ${
          totalBuildTime}s`);

        Iif (!this.onePagePath && totalBuildTime > LAZY_LOADING_BUILD_TIME_RECOMMENDATION_LIMIT) {
          logger.info('Your site took quite a while to build...'
              + 'Have you considered using markbind serve -o when writing content to speed things up?');
        }
      }

      /**
       * Build all pages of the site
       */
      async buildSourceFiles() {
        this.beforeSiteGenerate();
        logger.info('Generating pages...');

        try {
          await this.generatePages();
          await fs.remove(this.tempPath);
          logger.info('Pages built');
        } catch (error) {
          await Site.rejectHandler(error, [this.tempPath, this.outputPath]);
        }
      }

      /**
       * Adds all pages except the viewed pages to toRebuild, flagging them for lazy building later.
       */
      async lazyBuildAllPagesNotViewed(viewedPages: string | string[]) {
        const viewedPagesArray = Array.isArray(viewedPages) ? viewedPages : [viewedPages];
        this.pages.forEach((page) => {
          const normalizedUrl = fsUtil.removeExtension(page.pageConfig.sourcePath);
          Iif (!viewedPagesArray.some(viewedPage => normalizedUrl === viewedPage)) {
            this.toRebuild.add(normalizedUrl);
          }
        });
      }

      /**
       * Only build landing page of the site, building more as the author goes to different links.
       */
      async lazyBuildSourceFiles() {
        this.beforeSiteGenerate();
        logger.info('Generating landing page...');

        try {
          await this.generateLandingPage();
          await this.copyLazySourceFiles();
          await fs.remove(this.tempPath);
          await this.lazyBuildAllPagesNotViewed(this.currentPageViewed);
          logger.info('Landing page built, other pages will be built as you navigate to them!');
        } catch (error) {
          await Site.rejectHandler(error, [this.tempPath, this.outputPath]);
        }
      }

      /**
       * Helper function for lazyBuildSourceFiles().
       */
      copyLazySourceFiles() {
        const lazyLoadingSpinnerHtmlFilePath = path.join(__dirname, LAZY_LOADING_SITE_FILE_NAME);
        const outputSpinnerHtmlFilePath = path.join(this.outputPath, LAZY_LOADING_SITE_FILE_NAME);

        return fs.copy(lazyLoadingSpinnerHtmlFilePath, outputSpinnerHtmlFilePath);
      }

      async _rebuildAffectedSourceFiles(filePaths: string | string[]) {
        Iif (this.backgroundBuildMode) {
          this.stopOngoingBuilds();
        }

        const filePathArray = Array.isArray(filePaths) ? filePaths : [filePaths];
        const uniquePaths = _.uniq(filePathArray);
        this.beforeSiteGenerate();

        try {
          await this.layoutManager.updateLayouts(filePaths);
          await this.regenerateAffectedPages(uniquePaths);
          await fs.remove(this.tempPath);
          Iif (this.backgroundBuildMode) {
            this.backgroundBuildNotViewedFiles();
          }
        } catch (error) {
          await Site.rejectHandler(error, [this.tempPath, this.outputPath]);
        }
      }

      async _rebuildPagesBeingViewed(normalizedUrls: string[]) {
        const startTime = new Date();
        const normalizedUrlArray = Array.isArray(normalizedUrls) ? normalizedUrls : [normalizedUrls];
        const uniqueUrls = _.uniq(normalizedUrlArray);
        uniqueUrls.forEach(normalizedUrl => logger.info(
          `Building ${normalizedUrl} as some of its dependencies were changed since the last visit`));

        const pagesToRebuild = this.pages.filter(page =>
          uniqueUrls.some(pageUrl => fsUtil.removeExtension(page.pageConfig.sourcePath) === pageUrl));
        const pageGenerationTask = {
          mode: 'async',
          pages: pagesToRebuild,
        };

        try {
          this._setTimestampVariable();
          await this.runPageGenerationTasks([pageGenerationTask]);
          await this.writeSiteData();
          Site.calculateBuildTimeForRebuildPagesBeingViewed(startTime);
        } catch (err) {
          await Site.rejectHandler(err, [this.tempPath, this.outputPath]);
        }

        await fs.remove(this.tempPath);
      }

      /**
       * Helper function for _rebuildPagesBeingViewed().
       */
      static calculateBuildTimeForRebuildPagesBeingViewed(startTime: Date) {
        const endTime = new Date();
        const totalBuildTime = (endTime.getTime() - startTime.getTime()) / 1000;
        return logger.info(`Lazy website regeneration complete! Total build time: ${totalBuildTime}s`);
      }

      async _backgroundBuildNotViewedFiles() {
        Iif (this.toRebuild.size === 0) {
          return;
        }

        logger.info('Building files that are not viewed in the background...');
        const isCompleted = await this.generatePagesMarkedToRebuild();
        Iif (isCompleted) {
          logger.info('Background building completed!');
          this.postBackgroundBuildFunc();
        }
      }

      /**
       * Generates pages that are marked to be built/rebuilt.
       * @returns A Promise that resolves once all pages are generated.
       */
      async generatePagesMarkedToRebuild(): Promise<boolean> {
        const pagesToRebuild = this.pages.filter((page) => {
          const normalizedUrl = fsUtil.removeExtension(page.pageConfig.sourcePath);
          return this.toRebuild.has(normalizedUrl);
        });

        const pageRebuildTask = {
          mode: 'async',
          pages: pagesToRebuild,
        };
        return this.runPageGenerationTasks([pageRebuildTask]);
      }

      async _rebuildSourceFiles() {
        Iif (this.backgroundBuildMode) {
          this.stopOngoingBuilds();
        }

        logger.info('Pages or site config modified, updating pages...');
        this.beforeSiteGenerate();

        this.layoutManager.removeLayouts();

        const removedPageFilePaths = this.updateAddressablePages();
        try {
          await this.removeAsset(removedPageFilePaths);
          await this.rebuildRequiredPages();
          Iif (this.backgroundBuildMode) {
            this.backgroundBuildNotViewedFiles();
          }
        } catch (error) {
          await Site.rejectHandler(error, [this.tempPath, this.outputPath]);
        }
      }

      /**
       * Helper function for _rebuildSourceFiles().
       */
      async rebuildRequiredPages() {
        Iif (this.onePagePath) {
          this.mapAddressablePagesToPages(this.addressablePages || [], this.getFavIconUrl());

          await this._rebuildPagesBeingViewed(this.currentOpenedPages);
          await this.lazyBuildAllPagesNotViewed(this.currentOpenedPages);
          return;
        }

        logger.warn('Rebuilding all pages...');
        await this.buildSourceFiles();
      }

      async _buildMultipleAssets(filePaths: string | string[]) {
        const filePathArray = Array.isArray(filePaths) ? filePaths : [filePaths];
        const uniquePaths = _.uniq(filePathArray);
        const fileIgnore = ignore().add(this.siteConfig.ignore);
        const fileRelativePaths = uniquePaths.map(filePath => path.relative(this.rootPath, filePath));
        const copyAssets = fileIgnore.filter(fileRelativePaths)
          .map(asset => fs.copy(path.join(this.rootPath, asset), path.join(this.outputPath, asset)));
        await Promise.all(copyAssets);
        logger.info('Assets built');
      }

      async _removeMultipleAssets(filePaths: string | string[]) {
        const filePathArray = Array.isArray(filePaths) ? filePaths : [filePaths];
        const uniquePaths = _.uniq(filePathArray);
        const fileRelativePaths = uniquePaths.map(filePath => path.relative(this.rootPath, filePath));
        const filesToRemove = fileRelativePaths.map(
          fileRelativePath => path.join(this.outputPath, fileRelativePath));
        const removeFiles = filesToRemove.map(asset => fs.remove(asset));
        if (removeFiles.length !== 0) {
          await Promise.all(removeFiles);
          logger.debug('Assets removed');
        }
      }

      async buildAssets() {
        logger.info('Building assets...');
        const outputFolder = path.relative(this.rootPath, this.outputPath);
        const fileIgnore = ignore().add([...this.siteConfig.ignore, outputFolder]);

        // Scan and copy assets (excluding ignore files).
        try {
          const listOfAssets = this.listAssets(fileIgnore);
          const assetsToCopy = listOfAssets.map(asset =>
            fs.copy(path.join(this.rootPath, asset), path.join(this.outputPath, asset)));
          await Promise.all(assetsToCopy);
          logger.info('Assets built');
        } catch (error) {
          await Site.rejectHandler(error, []); // assets won't affect deletion
        }
      }

      async reloadSiteConfig() {
        Iif (this.backgroundBuildMode) {
          this.stopOngoingBuilds();
        }

        const oldSiteConfig = this.siteConfig;
        const oldAddressablePages = this.addressablePages.slice();
        const oldPagesSrc = oldAddressablePages.map(page => page.src);
        await this.readSiteConfig();
        await this.handleIgnoreReload(oldSiteConfig.ignore);
        await this.handlePageReload(oldAddressablePages, oldPagesSrc, oldSiteConfig);
        await this.handleStyleReload(oldSiteConfig.style);
        Iif (this.backgroundBuildMode) {
          this.backgroundBuildNotViewedFiles();
        }
      }

      /**
       * Handles the rebuilding of modified pages
       */
      async handlePageReload(oldAddressablePages: AddressablePage[], oldPagesSrc: string[],
                             oldSiteConfig: SiteConfig) {
        this.collectAddressablePages();

        // Comparator for the _differenceWith comparison below
        const isNewPage = (newPage: AddressablePage, oldPage: AddressablePage) =>
          _.isEqual(newPage, oldPage) || newPage.src === oldPage.src;

        const addedPages = _.differenceWith(this.addressablePages, oldAddressablePages, isNewPage);
        const removedPages = _.differenceWith(oldAddressablePages, this.addressablePages, isNewPage)
          .map(filePath => fsUtil.setExtension(filePath.src as string, '.html'));

        // Checks if any attributes of site.json requiring a global rebuild are modified
        const isGlobalConfigModified = () => !_.isEqual(oldSiteConfig.faviconPath, this.siteConfig.faviconPath)
            || !_.isEqual(oldSiteConfig.titlePrefix, this.siteConfig.titlePrefix)
            || !_.isEqual(oldSiteConfig.titleSuffix, this.siteConfig.titleSuffix)
            || !_.isEqual(oldSiteConfig.style, this.siteConfig.style)
            || !_.isEqual(oldSiteConfig.externalScripts, this.siteConfig.externalScripts)
            || !_.isEqual(oldSiteConfig.globalOverride, this.siteConfig.globalOverride)
            || !_.isEqual(oldSiteConfig.plugins, this.siteConfig.plugins)
            || !_.isEqual(oldSiteConfig.pluginsContext, this.siteConfig.pluginsContext)
            || !_.isEqual(oldSiteConfig.headingIndexingLevel, this.siteConfig.headingIndexingLevel)
            || !_.isEqual(oldSiteConfig.enableSearch, this.siteConfig.enableSearch)
            || !_.isEqual(oldSiteConfig.timeZone, this.siteConfig.timeZone)
            || !_.isEqual(oldSiteConfig.locale, this.siteConfig.locale)
            || !_.isEqual(oldSiteConfig.intrasiteLinkValidation, this.siteConfig.intrasiteLinkValidation)
            || !_.isEqual(oldSiteConfig.plantumlCheck, this.siteConfig.plantumlCheck);

        if (isGlobalConfigModified() || !_.isEmpty(addedPages) || !_.isEmpty(removedPages)) {
          await this.removeAsset(removedPages);
          this.buildManagers();
          await this._rebuildSourceFiles();
          await this.writeSiteData();
        } else {
          // Get pages with edited attributes but with the same src
          const editedPages = _.differenceWith(this.addressablePages, oldAddressablePages, (newPage, oldPage) =>
            _.isEqual(newPage, oldPage) || !oldPagesSrc.includes(newPage.src));
          this.updatePages(editedPages);
          const siteConfigDirectory = path.dirname(path.join(this.rootPath, this.siteConfigPath));
          this.regenerateAffectedPages(editedPages.map(page => path.join(siteConfigDirectory, page.src)));
        }
      }

      /**
       * Creates new pages and replaces the original pages with the updated version
       */
      updatePages(pagesToUpdate: AddressablePage[]) {
        pagesToUpdate.forEach((pageToUpdate) => {
          this.pages.forEach((page, index) => {
            Iif (page.pageConfig.src === pageToUpdate.src) {
              const newPage = this.createNewPage(pageToUpdate, this.getFavIconUrl());
              newPage.resetState();
              this.pages[index] = newPage;
            }
          });
        });
      }

      /**
       * Handles the reloading of ignore attributes
       */
      async handleIgnoreReload(oldIgnore: string[]) {
        const assetsToRemove = _.difference(this.siteConfig.ignore, oldIgnore);

        Iif (!_.isEqual(oldIgnore, this.siteConfig.ignore)) {
          await this._removeMultipleAssets(assetsToRemove);
          this.buildManagers();
          await this.buildAssets();
        }
      }

      /**
       * Handles the reloading of the style attribute if it has been modified
       */
      async handleStyleReload(oldStyle: SiteConfigStyle) {
        Iif (!_.isEqual(oldStyle.bootstrapTheme, this.siteConfig.style.bootstrapTheme)) {
          await this.copyBootstrapTheme(true);
          logger.info('Updated bootstrap theme');
        }
      }

      /**
       * Checks if a specified file path is a dependency of a page
       * @param filePath file path to check
       * @returns whether the file path is a dependency of any of the site's pages
       */
      isDependencyOfPage(filePath: string): boolean {
        return this.pages.some(page => page.isDependency(filePath))
          || fsUtil.ensurePosix(filePath).endsWith(USER_VARIABLES_PATH);
      }

      /**
       * Checks if a specified file path satisfies a src or glob in any of the page configurations.
       * @param filePath file path to check
       * @returns whether the file path is satisfies any glob
       */
      isFilepathAPage(filePath: string): boolean {
        const { pages, pagesExclude } = this.siteConfig;
        const relativeFilePath = fsUtil.ensurePosix(path.relative(this.rootPath, filePath));
        const srcesFromPages = _.flatMap(pages.filter(page => page.src),
                                         page => (Array.isArray(page.src) ? page.src : [page.src]));
        Iif (srcesFromPages.includes(relativeFilePath)) {
          return true;
        }

        const filePathsFromGlobs = _.flatMap(pages.filter(page => page.glob),
                                             page => this.getPageGlobPaths(page, pagesExclude));
        return filePathsFromGlobs.some(fp => fp === relativeFilePath);
      }

      getFavIconUrl() {
        const { baseUrl, faviconPath } = this.siteConfig;

        if (faviconPath) {
          Iif (!fs.existsSync(path.join(this.rootPath, faviconPath))) {
            logger.warn(`${faviconPath} does not exist`);
          }
          return url.join('/', baseUrl, faviconPath);
        } else Iif (fs.existsSync(path.join(this.rootPath, FAVICON_DEFAULT_PATH))) {
          return url.join('/', baseUrl, FAVICON_DEFAULT_PATH);
        }

        return undefined;
      }

      /**
       * Maps an array of addressable pages to an array of Page object
       */
      mapAddressablePagesToPages(addressablePages: AddressablePage[], faviconUrl: string | undefined) {
        this.pages = addressablePages.map(page => this.createNewPage(page, faviconUrl));
      }

      /**
       * Creates and returns a new Page with the given page config details and favicon url
       * @param page config
       * @param faviconUrl of the page
       */
      createNewPage(page: AddressablePage, faviconUrl: string | undefined) {
        return this.createPage({
          faviconUrl,
          pageSrc: page.src,
          title: page.title,
          layout: page.layout,
          frontmatter: page.frontmatter,
          searchable: page.searchable !== 'no',
          externalScripts: page.externalScripts || [],
        });
      }

      stopOngoingBuilds() {
        this.stopGenerationTimeThreshold = new Date();
      }

      /**
       * Runs the supplied page generation tasks according to the specified mode of each task.
       * A page generation task can be a sequential generation or an asynchronous generation.
       * @param pageGenerationTasks Array of page generation tasks
       * @returns A Promise that resolves to a boolean which indicates whether the generation
       * ran to completion
       */
      async runPageGenerationTasks(pageGenerationTasks: PageGenerationTask[]): Promise<boolean> {
        const pagesCount = pageGenerationTasks.reduce((acc, task) => acc + task.pages.length, 0);
        const progressBar = new ProgressBar(`[:bar] :current / ${pagesCount} pages built`, { total: pagesCount });
        progressBar.render();
        logger.setProgressBar(progressBar);

        const startTime = new Date();
        let isCompleted = true;
        await sequentialAsyncForEach(pageGenerationTasks, async (task) => {
          Iif (this.backgroundBuildMode && startTime < this.stopGenerationTimeThreshold) {
            logger.info('Page generation stopped');
            logger.debug('Page generation stopped at generation task queue');
            isCompleted = false;
            return;
          }

          if (task.mode === 'sequential') {
            isCompleted = await this.generatePagesSequential(task.pages, progressBar);
          } else {
            isCompleted = await this.generatePagesAsyncThrottled(task.pages, progressBar) as boolean;
          }

          logger.removeProgressBar();
          this.siteLinkManager.validateAllIntralinks();
        });
        return isCompleted;
      }

      /**
       * Generate pages sequentially. That is, the pages are generated
       * one-by-one in order.
       * @param pages Pages to be generated
       * @param progressBar Progress bar of the overall generation process
       * @returns A Promise that resolves to a boolean which indicates whether the generation
       * ran to completion
       */
      async generatePagesSequential(pages: Page[], progressBar: any): Promise<boolean> {
        const startTime = new Date();
        let isCompleted = true;
        await sequentialAsyncForEach(pages, async (page) => {
          Iif (this.backgroundBuildMode && startTime < this.stopGenerationTimeThreshold) {
            logger.info('Page generation stopped');
            logger.debug('Page generation stopped at sequential generation');
            isCompleted = false;
            return;
          }

          try {
            await page.generate(this.externalManager);
            this.toRebuild.delete(fsUtil.removeExtension(page.pageConfig.sourcePath));
            Iif (this.backgroundBuildMode) {
              await this.writeSiteData(false);
            }
            progressBar.tick();
          } catch (err) {
            logger.error(err);
            throw new Error(`Error while generating ${page.pageConfig.sourcePath}`);
          }
        });
        return isCompleted;
      }

      /**
       * Creates the supplied pages' page generation promises at a throttled rate.
       * This is done to avoid pushing too many callbacks into the event loop at once. (#1245)
       * @param pages Pages to be generated
       * @param progressBar Progress bar of the overall generation process
       * @returns A Promise that resolves to a boolean which indicates whether the generation
       * ran to completion
       */
      generatePagesAsyncThrottled(pages: Page[], progressBar: any): Promise<boolean> {
        return new Promise((resolve, reject) => {
          const context: PageGenerationContext = {
            startTime: new Date(),
            numPagesGenerated: 0,
            numPagesToGenerate: pages.length,
            isCompleted: true,
          };

          // Map pages into array of callbacks for delayed execution
          const pageGenerationQueue = pages.map(page => async () => {
            // Pre-generate guard to ensure no newly executed callbacks start on stop
            Iif (this.backgroundBuildMode && context.startTime < this.stopGenerationTimeThreshold) {
              Iif (context.isCompleted) {
                logger.info('Page generation stopped');
                logger.debug('Page generation stopped at asynchronous generation');
                context.isCompleted = false;
                resolve(false);
              }
              return;
            }

            try {
              await page.generate(this.externalManager);
              this.toRebuild.delete(fsUtil.removeExtension(page.pageConfig.sourcePath));
              Iif (this.backgroundBuildMode) {
                await this.writeSiteData(false);
              }
              this.generateProgressBarStatus(progressBar, context, pageGenerationQueue, resolve);
            } catch (err) {
              logger.error(err);
              reject(new Error(`Error while generating ${page.pageConfig.sourcePath}`));
            }
          });

          /*
           Take the first MAX_CONCURRENT_PAGE_GENERATION_PROMISES callbacks and execute them.
           Whenever a page generation callback resolves,
           it pops the next unprocessed callback off pageGenerationQueue and executes it.
           */
          pageGenerationQueue.splice(0, MAX_CONCURRENT_PAGE_GENERATION_PROMISES)
            .forEach(generatePage => generatePage());
        });
      }

      /**
       * Helper function for generatePagesAsyncThrottled().
       */
      generateProgressBarStatus(progressBar: any, context: PageGenerationContext,
                                pageGenerationQueue: (() => Promise<void>)[], resolve: ((arg0: boolean) => any)) {
        // Post-generate guard to ensure no new callbacks are executed on stop
        Iif (this.backgroundBuildMode && context.startTime < this.stopGenerationTimeThreshold) {
          Iif (context.isCompleted) {
            logger.info('Page generation stopped');
            logger.debug('Page generation stopped at asynchronous generation');
            context.isCompleted = false;
            resolve(false);
          }
          return;
        }
        progressBar.tick();
        context.numPagesGenerated += 1;

        if (pageGenerationQueue.length) {
          pageGenerationQueue.pop()!();
        } else Iif (context.numPagesGenerated === context.numPagesToGenerate) {
          resolve(true);
        }
      }

      /**
       * Renders all pages specified in site configuration file to the output folder
       */
      generatePages() {
        // Run MarkBind include and render on each source file.
        // Render the final rendered page to the output folder.
        const addressablePages = this.addressablePages || [];

        const faviconUrl = this.getFavIconUrl();

        this._setTimestampVariable();
        this.mapAddressablePagesToPages(addressablePages, faviconUrl);

        const pageGenerationTask = {
          mode: 'async',
          pages: this.pages,
        };
        return this.runPageGenerationTasks([pageGenerationTask]);
      }

      /**
       * Renders only the starting page for lazy loading to the output folder.
       */
      async generateLandingPage() {
        const addressablePages = this.addressablePages || [];
        const faviconUrl = this.getFavIconUrl();

        this._setTimestampVariable();
        this.mapAddressablePagesToPages(addressablePages, faviconUrl);

        const landingPage = this.pages.find(page => page.pageConfig.src === this.onePagePath);
        Iif (!landingPage) {
          throw new Error(`${this.onePagePath} is not specified in the site configuration.`);
        }

        await landingPage.generate(this.externalManager);
      }

      async regenerateAffectedPages(filePaths: string[]) {
        const startTime = new Date();

        const shouldRebuildAllPages = this.collectUserDefinedVariablesMapIfNeeded(filePaths) || this.forceReload;
        Iif (shouldRebuildAllPages) {
          logger.warn('Rebuilding all pages as variables file was changed, or the --force-reload flag was set');
        }
        this._setTimestampVariable();

        let openedPagesToRegenerate: Page[] = [];
        const asyncPagesToRegenerate = this.pages.filter((page) => {
          const doFilePathsHaveSourceFiles = filePaths.some(filePath => page.isDependency(filePath));

          Iif (shouldRebuildAllPages || doFilePathsHaveSourceFiles) {
            Iif (this.onePagePath) {
              const normalizedSource = fsUtil.removeExtension(page.pageConfig.sourcePath);
              const openIdx = this.currentOpenedPages.findIndex(pagePath => pagePath === normalizedSource);
              const isRecentlyViewed = openIdx !== -1;

              if (!isRecentlyViewed) {
                this.toRebuild.add(normalizedSource);
              } else {
                openedPagesToRegenerate[openIdx] = page;
              }

              return false;
            }

            return true;
          }

          return false;
        });

        /*
         * As a side effect of doing assignment to an empty array, some elements might be
         * undefined if it has not been assigned to anything. We filter those out here.
         */
        openedPagesToRegenerate = openedPagesToRegenerate.filter(page => page);

        const totalPagesToRegenerate = openedPagesToRegenerate.length + asyncPagesToRegenerate.length;
        Iif (totalPagesToRegenerate === 0) {
          logger.info('No pages needed to be rebuilt');
          return;
        }
        logger.info(`Rebuilding ${totalPagesToRegenerate} pages`);

        const pageGenerationTasks = [];
        Iif (openedPagesToRegenerate.length > 0) {
          const recentPagesGenerationTask = {
            mode: 'sequential',
            pages: openedPagesToRegenerate,
          };
          pageGenerationTasks.push(recentPagesGenerationTask);
        }

        Iif (asyncPagesToRegenerate.length > 0) {
          const asyncPagesGenerationTask = {
            mode: 'async',
            pages: asyncPagesToRegenerate,
          };
          pageGenerationTasks.push(asyncPagesGenerationTask);
        }

        try {
          await this.runPageGenerationTasks(pageGenerationTasks);
          await this.writeSiteData();
          logger.info('Pages rebuilt');
          this.calculateBuildTimeForRegenerateAffectedPages(startTime);
        } catch (error) {
          await Site.rejectHandler(error, []);
        }
      }

      /**
       * Helper function for regenerateAffectedPages().
       */
      calculateBuildTimeForRegenerateAffectedPages(startTime: Date) {
        const endTime = new Date();
        const totalBuildTime = (endTime.getTime() - startTime.getTime()) / 1000;
        logger.info(`Website regeneration complete! Total build time: ${totalBuildTime}s`);
        Iif (!this.onePagePath && totalBuildTime > LAZY_LOADING_REBUILD_TIME_RECOMMENDATION_LIMIT) {
          logger.info('Your pages took quite a while to rebuild...'
              + 'Have you considered using markbind serve -o when writing content to speed things up?');
        }
      }

      /**
       * Copies Font Awesome assets to the assets folder
       */
      async copyFontAwesomeAsset() {
        const faRootSrcPath = path.dirname(require.resolve('@fortawesome/fontawesome-free/package.json'));
        const faCssSrcPath = path.join(faRootSrcPath, 'css', 'all.min.css');
        const faCssDestPath = path.join(this.siteAssetsDestPath, 'fontawesome', 'css', 'all.min.css');
        const faFontsSrcPath = path.join(faRootSrcPath, 'webfonts');
        const faFontsDestPath = path.join(this.siteAssetsDestPath, 'fontawesome', 'webfonts');

        await fs.copy(faCssSrcPath, faCssDestPath);
        await fs.copy(faFontsSrcPath, faFontsDestPath);
      }

      /**
       * Copies Octicon assets to the assets folder
       */
      copyOcticonsAsset() {
        const octiconsCssSrcPath = require.resolve('@primer/octicons/build/build.css');
        const octiconsCssDestPath = path.join(this.siteAssetsDestPath, 'css', 'octicons.css');

        return fs.copy(octiconsCssSrcPath, octiconsCssDestPath);
      }

      /**
       * Copies Google Material Icons assets to the assets folder
       */
      copyMaterialIconsAsset() {
        const materialIconsRootSrcPath = path.dirname(require.resolve('material-icons/package.json'));
        const materialIconsCssAndFontsSrcPath = path.join(materialIconsRootSrcPath, 'iconfont');
        const materialIconsCssAndFontsDestPath = path.join(this.siteAssetsDestPath, 'material-icons');

        return fs.copy(materialIconsCssAndFontsSrcPath, materialIconsCssAndFontsDestPath);
      }

      /**
       * Copies core-web bundles and external assets to the assets output folder
       */
      copyCoreWebAsset() {
        const coreWebRootPath = path.dirname(require.resolve('@markbind/core-web/package.json'));
        const coreWebAssetPath = path.join(coreWebRootPath, 'asset');
        fs.copySync(coreWebAssetPath, this.siteAssetsDestPath);

        const dirsToCopy = ['fonts'];
        const filesToCopy = [
          'js/markbind.min.js',
          'css/markbind.min.css',
        ];

        const copyAllFiles = filesToCopy.map((file) => {
          const srcPath = path.join(coreWebRootPath, 'dist', file);
          const destPath = path.join(this.siteAssetsDestPath, file);
          return fs.copy(srcPath, destPath);
        });

        const copyFontsDir = dirsToCopy.map((dir) => {
          const srcPath = path.join(coreWebRootPath, 'dist', dir);
          const destPath = path.join(this.siteAssetsDestPath, 'css', dir);
          return fs.copy(srcPath, destPath);
        });

        return Promise.all([...copyAllFiles, ...copyFontsDir]);
      }

      /**
       * Copies bootstrapTheme to the assets folder if a valid bootstrapTheme is specified
       * @param isRebuild only true if it is a rebuild
       */
      copyBootstrapTheme(isRebuild: boolean) {
        const { bootstrapTheme } = this.siteConfig.style;

        /**
         * If it is the initial build using the default bootstrapTheme or if the bootstrapTheme specified
         * is not valid, then do nothing.
         */
        Iif ((!isRebuild && !bootstrapTheme)
          || (bootstrapTheme && !_.has(SUPPORTED_THEMES_PATHS, bootstrapTheme))) {
          return _.noop;
        }

        const themeSrcPath = !bootstrapTheme
          ? require.resolve('@markbind/core-web/asset/css/bootstrap.min.css')
          : SUPPORTED_THEMES_PATHS[bootstrapTheme];
        const themeDestPath = path.join(this.siteAssetsDestPath, 'css', 'bootstrap.min.css');

        return fs.copy(themeSrcPath, themeDestPath);
      }

      /**
       * Writes the site data to siteData.json
       * @param verbose Flag to emit logs of the operation
       */
      async writeSiteData(verbose: boolean = true) {
        const siteDataPath = path.join(this.outputPath, SITE_DATA_NAME);
        const siteData = {
          enableSearch: this.siteConfig.enableSearch,
          pages: this.pages.filter(page => page.pageConfig.searchable && page.headings)
            .map(page => ({
              src: page.pageConfig.src,
              title: page.title,
              headings: page.headings,
              headingKeywords: page.keywords,
              frontmatterKeywords: page.frontmatter.keywords,
            })),
        };

        try {
          await fs.outputJson(siteDataPath, siteData, { spaces: 2 });
          Iif (verbose) {
            logger.info('Site data built');
          }
        } catch (error) {
          await Site.rejectHandler(error, [this.tempPath, this.outputPath]);
        }
      }

      deploy(ciTokenVar: string | boolean) {
        const defaultDeployConfig: DeployOptions = {
          branch: 'gh-pages',
          message: 'Site Update.',
          repo: '',
          remote: 'origin',
        };
        process.env.NODE_DEBUG = 'gh-pages';
        return this.generateDepUrl(ciTokenVar, defaultDeployConfig);
      }

      /**
       * Helper function for deploy(). Returns the ghpages link where the repo will be hosted.
       */
      async generateDepUrl(ciTokenVar: boolean | string, defaultDeployConfig: DeployOptions) {
        const publish = Bluebird.promisify(ghpages.publish);
        await this.readSiteConfig();
        const depOptions = await this.getDepOptions(ciTokenVar, defaultDeployConfig, publish);
        return Site.getDepUrl(depOptions);
      }

      /**
       * Helper function for deploy(). Set the options needed to be used by ghpages.publish.
       */
      async getDepOptions(ciTokenVar: boolean | string, defaultDeployConfig: DeployOptions,
                          publish: (basePath: string, options: DeployOptions) => Bluebird<unknown>) {
        const basePath = this.siteConfig.deploy.baseDir || this.outputPath;
        if (!fs.existsSync(basePath)) {
          throw new Error(
            'The site directory does not exist. Please build the site first before deploy.');
        }
        const options: DeployOptions = {
          branch: this.siteConfig.deploy.branch || defaultDeployConfig.branch,
          message: this.siteConfig.deploy.message || defaultDeployConfig.message,
          repo: this.siteConfig.deploy.repo || defaultDeployConfig.repo,
          remote: defaultDeployConfig.remote,
        };
        options.message = options.message.concat(' [skip ci]');

        if (ciTokenVar) {
          const ciToken = _.isBoolean(ciTokenVar) ? 'GITHUB_TOKEN' : ciTokenVar;
          if (!process.env[ciToken]) {
            throw new Error(`The environment variable ${ciToken} does not exist.`);
          }
          const githubToken = process.env[ciToken];
          let repoSlug;

          if (process.env.TRAVIS) {
            repoSlug = Site.extractRepoSlug(options.repo, process.env.TRAVIS_REPO_SLUG);

            options.user = {
              name: 'Deployment Bot',
              email: 'deploy@travis-ci.org',
            };
          } else if (process.env.APPVEYOR) {
            repoSlug = Site.extractRepoSlug(options.repo, process.env.APPVEYOR_REPO_NAME);

            options.user = {
              name: 'AppVeyorBot',
              email: 'deploy@appveyor.com',
            };
          } else if (process.env.GITHUB_ACTIONS) {
            repoSlug = Site.extractRepoSlug(options.repo, process.env.GITHUB_REPOSITORY);

            options.user = {
              name: 'github-actions',
              email: 'github-actions@github.com',
            };
          } else if (process.env.CIRCLECI) {
            repoSlug = Site.extractRepoSlug(
              options.repo,
              `${process.env.CIRCLE_PROJECT_USERNAME}/${process.env.CIRCLE_PROJECT_REPONAME}`,
            );

            options.user = {
              name: 'circleci-bot',
              email: 'deploy@circleci.com',
            };
          } else {
            throw new Error('-c/--ci should only be run in CI environments.');
          }

          options.repo = `https://x-access-token:${githubToken}@github.com/${repoSlug}.git`;
        }

        // Waits for the repo to be updated.
        await publish(basePath, options);
        return options;
      }

      /**
       * Extract repo slug from user-specified repo URL so that we can include the access token
       */
      static extractRepoSlug(repo: string, ciRepoSlug: string | undefined) {
        if (!repo) {
          return ciRepoSlug;
        }
        const repoSlugRegex = /github\.com[:/]([\w-]+\/[\w-.]+)\.git$/;
        const repoSlugMatch = repoSlugRegex.exec(repo);
        if (!repoSlugMatch) {
          throw new Error('-c/--ci expects a GitHub repository.\n'
                + `The specified repository ${repo} is not valid.`);
        }
        const [, repoSlug] = repoSlugMatch;
        return repoSlug;
      }

      /**
       * Helper function for deploy().
       */
      static getDepUrl(options: DeployOptions) {
        const git = simpleGit({ baseDir: process.cwd() });
        return Site.getDeploymentUrl(git, options);
      }

      /**
       * Gets the deployed website's url, returning null if there was an error retrieving it.
       */
      static async getDeploymentUrl(git: SimpleGit, options: DeployOptions) {
        const HTTPS_PREAMBLE = 'https://';
        const SSH_PREAMBLE = 'git@github.com:';
        const GITHUB_IO_PART = 'github.io';

        // https://<name|org name>.github.io/<repo name>/
        function constructGhPagesUrl(remoteUrl: string) {
          Iif (!remoteUrl) {
            return null;
          }
          const parts = remoteUrl.split('/');
          if (remoteUrl.startsWith(HTTPS_PREAMBLE)) {
            // https://github.com/<name|org>/<repo>.git (HTTPS)
            const repoNameWithExt = parts[parts.length - 1];
            const repoName = repoNameWithExt.substring(0, repoNameWithExt.lastIndexOf('.'));
            const name = parts[parts.length - 2].toLowerCase();
            return `https://${name}.${GITHUB_IO_PART}/${repoName}`;
          } else Iif (remoteUrl.startsWith(SSH_PREAMBLE)) {
            // git@github.com:<name|org>/<repo>.git (SSH)
            const repoNameWithExt = parts[parts.length - 1];
            const repoName = repoNameWithExt.substring(0, repoNameWithExt.lastIndexOf('.'));
            const name = parts[0].substring(SSH_PREAMBLE.length);
            return `https://${name}.${GITHUB_IO_PART}/${repoName}`;
          }
          return null;
        }

        const { remote, branch, repo } = options;
        const cnamePromise = gitUtil.getRemoteBranchFile(git, 'blob', remote, branch, 'CNAME');
        const remoteUrlPromise = gitUtil.getRemoteUrl(git, remote);
        const promises = [cnamePromise, remoteUrlPromise];

        try {
          const promiseResults: string[] = await Promise.all(promises) as string[];
          const generateGhPagesUrl = (results: string[]) => {
            const cname = results[0];
            const remoteUrl = results[1];
            if (cname) {
              return cname.trim();
            } else IEif (repo) {
              return constructGhPagesUrl(repo);
            }
            return constructGhPagesUrl(remoteUrl.trim());
          };

          return generateGhPagesUrl(promiseResults);
        } catch (err) {
          logger.error(err);
          return null;
        }
      }

      _setTimestampVariable() {
        const options: Intl.DateTimeFormatOptions = {
          weekday: 'short',
          year: 'numeric',
          month: 'short',
          day: 'numeric',
          timeZone: this.siteConfig.timeZone,
          timeZoneName: 'short',
        };
        const time = new Date().toLocaleTimeString(this.siteConfig.locale, options);
        this.variableProcessor.addUserDefinedVariableForAllSites('timestamp', time);
      }

      /**
       * Build/copy assets that are specified in filePaths
       * @param filePaths a single path or an array of paths corresponding to the assets to build
       */
      buildAsset = delay(this._buildMultipleAssets as () => Bluebird<unknown>, 1000);

      /**
       * Remove assets that are specified in filePaths
       * @param filePaths a single path or an array of paths corresponding to the assets to remove
       */
      removeAsset = delay(this._removeMultipleAssets as () => Bluebird<unknown>, 1000);

      rebuildPagesBeingViewed = delay(this._rebuildPagesBeingViewed as () => Bluebird<unknown>, 1000);

      /**
       * Rebuild pages that are affected by changes in filePaths
       * @param filePaths a single path or an array of paths corresponding to the files that have changed
       */
      rebuildAffectedSourceFiles = delay(this._rebuildAffectedSourceFiles as () => Bluebird<unknown>, 1000);

      /**
       * Rebuild all pages
       */
      rebuildSourceFiles
      = delay(this._rebuildSourceFiles as () => Bluebird<unknown>, 1000) as () => Bluebird<unknown>;

      /**
       * Builds pages that are yet to build/rebuild in the background
       */
      backgroundBuildNotViewedFiles
      = delay(this._backgroundBuildNotViewedFiles as () => Bluebird<unknown>, 1000) as () => Bluebird<unknown>;
    }