|
31 | 31 | pluginsList = [],
|
32 | 32 | postPluginsList = [],
|
33 | 33 | pluginsByDomain = {};
|
| 34 | + |
| 35 | + var pluginsListSortedByModifiedTime; |
34 | 36 |
|
35 | 37 | export {
|
36 | 38 | plugins as _plugins,
|
|
98 | 100 | }
|
99 | 101 | }
|
100 | 102 |
|
| 103 | + function fillModifiedDate() { |
| 104 | + for(var i = 0; i < pluginsList.length; i++) { |
| 105 | + var plugin = pluginsList[i]; |
| 106 | + plugin.modifiedWithMixins = plugin.getPluginLastModifiedDate(); |
| 107 | + } |
| 108 | + |
| 109 | + pluginsListSortedByModifiedTime = [...pluginsList]; |
| 110 | + pluginsListSortedByModifiedTime.sort((p1, p2) => { |
| 111 | + // Sort desc by modified date. |
| 112 | + return p2.modifiedWithMixins - p1.modifiedWithMixins; |
| 113 | + }); |
| 114 | + } |
| 115 | + |
101 | 116 | function getFileName(filenameWithExt) {
|
102 | 117 | return filenameWithExt.replace(/\.(js|ejs)$/i, "");
|
103 | 118 | }
|
|
304 | 319 | var stat = fs.statSync(pluginPath);
|
305 | 320 | pluginDeclaration.modified = new Date(stat.mtime);
|
306 | 321 | pluginDeclaration.getPluginLastModifiedDate = getPluginLastModifiedDate;
|
| 322 | + pluginDeclaration.pluginMatchesUrl = pluginMatchesUrl; |
307 | 323 |
|
308 | 324 | // If no mixins - mark domain plugin 'asks to mixin all generic plugins'.
|
309 | 325 | if (plugin.mixins) {
|
|
445 | 461 | return record;
|
446 | 462 | };
|
447 | 463 |
|
| 464 | + export function getDomainForPlugin(uri) { |
| 465 | + return uri.split('/')[2] |
| 466 | + // Skip www. for domain search. |
| 467 | + .replace(/^www\./i, "") |
| 468 | + .toLowerCase(); |
| 469 | + } |
| 470 | + |
| 471 | + export function hasNewPluginForUri(timeAfter, uri) { |
| 472 | + if (typeof timeAfter !== 'number') { |
| 473 | + return; |
| 474 | + } |
| 475 | + |
| 476 | + var i = 0, |
| 477 | + plugin, match; |
| 478 | + |
| 479 | + const domain = getDomainForPlugin(uri); |
| 480 | + |
| 481 | + while (i < pluginsListSortedByModifiedTime.length |
| 482 | + && (plugin = pluginsListSortedByModifiedTime[i]) |
| 483 | + // Dot not check next plugins if older, because they are all older (SortedByModifiedTime desc). |
| 484 | + && plugin.modifiedWithMixins > timeAfter |
| 485 | + // Find first plugin match (newest). |
| 486 | + && !(match = plugin.pluginMatchesUrl(domain, uri))) { |
| 487 | + |
| 488 | + i++; |
| 489 | + } |
| 490 | + |
| 491 | + // Return plugin if match found. |
| 492 | + return match && plugin; |
| 493 | + }; |
| 494 | + |
| 495 | + function pluginMatchesUrl(domain, uri) { |
| 496 | + const plugin = this; |
| 497 | + if (plugin.domain) { |
| 498 | + |
| 499 | + // Match only by regexp. Used in specific cases where domain changes (like national domain). |
| 500 | + |
| 501 | + var match = null, j = 0, res = plugin.re; |
| 502 | + while (!match && j < res.length) { |
| 503 | + match = uri.match(res[j]); |
| 504 | + j++; |
| 505 | + } |
| 506 | + if (match) { |
| 507 | + // Store match for plugin. |
| 508 | + return match; |
| 509 | + } else if (res.length) { |
| 510 | + // Skip plugin with unmatched re. |
| 511 | + return; |
| 512 | + } |
| 513 | + |
| 514 | + // Straight match by domain. |
| 515 | + |
| 516 | + // Positive match on plugin.domain="domain.com", domain="sub.domain.com" |
| 517 | + // Positive match on plugin.domain="domain.com", domain="domain.com" |
| 518 | + var idx = domain.indexOf(plugin.domain); |
| 519 | + |
| 520 | + if (idx === -1 || ((idx > 0) && domain.charAt(idx - 1) !== '.')) { |
| 521 | + // Break if not found, or not dot separation. |
| 522 | + return; |
| 523 | + } |
| 524 | + |
| 525 | + if (idx > 0) { |
| 526 | + var subdomain = domain.substring(0, idx - 1); |
| 527 | + if (subdomain === 'blog') { |
| 528 | + // Skip "blog.*.*" blog page for domain plugin without re. |
| 529 | + return; |
| 530 | + } |
| 531 | + } |
| 532 | + |
| 533 | + var match = (idx + plugin.domain.length) === domain.length; |
| 534 | + |
| 535 | + if (match) { |
| 536 | + // TODO: use `true` as `null`. |
| 537 | + return true; |
| 538 | + } |
| 539 | + } |
| 540 | + } |
| 541 | + |
448 | 542 | async function scanModulesForPlugins() {
|
449 | 543 |
|
450 | 544 | // Scan node_modules dir.
|
|
513 | 607 | }
|
514 | 608 | }
|
515 | 609 |
|
| 610 | + fillModifiedDate(); |
| 611 | + |
516 | 612 | console.log('Iframely plugins loaded:')
|
517 | 613 | console.log(' - custom domains:', pluginsList.filter(function(p) { return p.domain; }).length);
|
518 | 614 | console.log(' - generic & meta:', pluginsList.filter(function(p) { return !p.domain && !p.custom; }).length);
|
|
0 commit comments