Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 59 additions & 12 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import * as pluginUtils from './loader/utils.js';
import * as utils from './utils.js';
import log from '../logging.js';
import * as oembedUtils from './oembed.js';
import { getOembed } from './oembed.js';
import * as oembedUtils from './plugins/system/oembed/oembedUtils.js';
import * as pluginLoader from './loader/pluginLoader.js';
import requestWrapper from './request.js';
import { cache } from './cache.js';
Expand All @@ -22,14 +23,16 @@
templates = pluginLoader._templates,
PLUGIN_METHODS = pluginUtils.PLUGIN_METHODS;

// Keys must be synced with `lib/loader/utils.js: DEFAULT_PARAMS`.
const utilsModules = {
utils: utils,
htmlUtils: htmlUtils,
metaUtils: metaUtils,
utils,
htmlUtils,
metaUtils,
oembedUtils,
plugins: pluginsModules,
prepareLinkMedia: mediaPlugin.prepareLink,
log: log,
cache: cache
log,
cache
};

/*
Expand Down Expand Up @@ -944,19 +947,65 @@
for(var key in r.data) {
var v = r.data[key];

// TODO: work with arrays?
if (v !== '' && v !== null && ((typeof v === 'string' && !/^\s+$/.test(v)) || typeof v === 'number' || typeof v === 'object')) {

// Check meta plugins order.
allResults.vars._sources = allResults.vars._sources || {};

var prevOrder = null, nextOrder = null, pluginId = allResults.vars._sources[key];
var prevOrder = null, nextOrder = null, prevPluginId = allResults.vars._sources[key];

if (pluginId && plugins[pluginId] && plugins[r.method.pluginId]) {
prevOrder = plugins[pluginId].order;
if (prevPluginId && plugins[prevPluginId] && plugins[r.method.pluginId]) {
prevOrder = plugins[prevPluginId].order;
nextOrder = plugins[r.method.pluginId].order;
}

if (!prevOrder || !nextOrder || prevOrder < nextOrder) {
var oldValue = allResults.vars[key];

var newValueOverrides = !prevOrder || !nextOrder || prevOrder < nextOrder;

if (typeof v === 'object' && typeof oldValue === 'object' || Array.isArray(oldValue)) {

var isArrayMode = Array.isArray(v) || Array.isArray(oldValue);

if (isArrayMode) {
// Join arrays mode.
if (!Array.isArray(v)) {
v = [v];
}
if (!Array.isArray(oldValue)) {
oldValue = [oldValue];
}

// Keep order by priority.
if (newValueOverrides) {
allResults.vars[key] = oldValue.concat(v);
} else {
allResults.vars[key] = v.concat(oldValue);
}

allResults.vars[key] = utils.filterSameObjects(allResults.vars[key]);

} else {
// Merge object mode.
if (newValueOverrides) {
// Extend existing object.
Object.assign(oldValue, v);
} else {
// Extend with old values on top.
allResults.vars[key] = Object.assign(v, oldValue);
}
}

if (!Array.isArray(allResults.vars._sources[key])) {
// Convert sources list to array.
allResults.vars._sources[key] = [allResults.vars._sources[key]];
}
allResults.vars._sources[key].push(r.method.pluginId);

} else if (newValueOverrides) {

// Assign new non object value.
allResults.vars[key] = v;
allResults.vars._sources[key] = r.method.pluginId;
}
Expand Down Expand Up @@ -2028,6 +2077,4 @@
}, cb);
};

var getOembed = oembedUtils.getOembed;

export { getOembed };
17 changes: 17 additions & 0 deletions lib/loader/pluginLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,23 @@
return;
}

// Log custom plugin attributes.
// for(var plugin_key in plugin) {
// if (PLUGINS_FIELDS.indexOf(plugin_key) === -1
// && PLUGIN_METHODS.indexOf(plugin_key) === -1
// && ['provides',
// 'listed',
// 'tests',
// 're',
// 'notPlugin',
// 'highestPriority',
// 'lowestPriority',
// 'startIteration',
// 'finishIteration'].indexOf(plugin_key) === -1) {
// console.log('--- unknown plugin attr', getFileName(bits[bits.length - 1]), plugin_key)
// }
// }

// Check post plugin.
// TODO: only prapereLink method abailable.
// TODO: exclude prapereLink method from other plugins.
Expand Down
2 changes: 2 additions & 0 deletions lib/loader/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
"utils",
"htmlUtils",
"metaUtils",
"oembedUtils",
"plugins",
"prepareLinkMedia",
"log",
"cache"
];
Expand Down
5 changes: 2 additions & 3 deletions lib/plugins/system/oembed/autoDiscovery.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as oembedUtils from './oembedUtils.js';
const RE = CONFIG.IGNORE_DOMAINS_RE || CONFIG.BLACKLIST_DOMAINS_RE;

export default {
Expand All @@ -9,7 +8,7 @@ export default {
* "__" two underscores means super mandatory param. Without that param plugin dependencies will not be searched.
*
* */
getData: function(meta, __noOembedLinks) {
getData: function(meta, oembedUtils, __noOembedLinks) {

var oembedLinks = oembedUtils.findOembedLinks(null, meta);

Expand All @@ -31,4 +30,4 @@ export default {
}

}
};
};
6 changes: 2 additions & 4 deletions lib/plugins/system/oembed/knownEndpoints.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import * as oembedUtils from './oembedUtils.js';

export default {

provides: ['oembedLinks', '__noOembedLinks'],

getData: function(url) {
getData: function(url, oembedUtils) {
var oembedLinks = oembedUtils.findOembedLinks(url);
return {
oembedLinks: oembedLinks,
__noOembedLinks: !oembedLinks || !oembedLinks.length || null // null - means value not in context.
};
}
};
};
3 changes: 1 addition & 2 deletions lib/plugins/system/oembed/oembed.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as oembedUtils from './oembedUtils.js';
import * as cheerio from 'cheerio';

import * as entities from 'entities';
Expand Down Expand Up @@ -113,7 +112,7 @@ export default {

getIframe: _getOembedIframe, // available via export for fallbacks as plugins['oembed'].getIframe(obj)

getData: function(url, oembedLinks, options, cb) {
getData: function(url, oembedLinks, oembedUtils, options, cb) {

var href = oembedLinks[0].href;

Expand Down
11 changes: 11 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1496,3 +1496,14 @@ export function isBlocked(url, options, cb) {
}
});
}

export function filterSameObjects(array) {
const seen = new Set();
return array.filter(obj => {
// Generate JSON with sorted keys.
const key = JSON.stringify(Object.fromEntries(Object.entries(obj).sort(([a], [b]) => a.localeCompare(b))));
if (seen.has(key)) return false;
seen.add(key);
return true;
});
}
6 changes: 2 additions & 4 deletions plugins/meta/noindex-robots.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import * as oembedUtils from '../../lib/plugins/system/oembed/oembedUtils.js';

export default {

getData: function(url, meta, __noOembedLinks, options, cb) {
getData: function(url, meta, oembedUtils, __noOembedLinks, options, cb) {

return cb(
meta.robots
Expand All @@ -19,4 +17,4 @@ export default {
} : null);
}

};
};
Loading