The code is a JavaScript function named collectRoutes
that collects and processes URL paths as part of a routing mechanism, using functions from the ../Core
module to generate Google Sheets templates. The function iterates over the URL paths, removes duplicates, and creates new templates associated with each link, returning a promise that resolves when all links have been processed.
npm run import -- "find known routes to sheets"
var importer = require('../Core')
var wrapTemplate = importer.import("output google sheet template")
var getTemplateProperties = importer.import("google sheet template properties")
var getTemplateByUrl = importer.import("convert sheet helper functions")
var TRIM = /^\s*\/\s*|\s*\/\s*$/ig;
var isFiltered = (url) => url.split('/').length > 1
// combine with "getSections" by using fake "{{> url/path}}" include
function collectRoutes(routes, properties, templates, rendered) {
var local = routes.concat(properties['render'] || [])
.filter((link, i, arr) => !rendered.includes(link)
// protocol means it's absolute remote path and not to try to generate it
&& arr.indexOf(link) == i && !link.includes(':'))
local.forEach(link => rendered[rendered.length] = link)
var promises = local
// promise in series so there is no data collisions
.map(link => resolve => {
var trimmedBase = (properties['base'] || '').replace(TRIM, '')
link = link.replace(/#.*$/, '') // remove hash #
link = link.replace(TRIM, '') // remove extra slashes
if(link.substr(0, trimmedBase.length) === trimmedBase) {
link = link.substr(trimmedBase.length).replace(TRIM, '');
}
// any part of a path can contain the reference to a page template
var key = getTemplateByUrl(templates, link);
var newProps = Object.assign({}, properties);
// create a temporary template to filter by
newProps[key + '-' + key + '-link'] = link;
templates[key + '-' + key] = {template: {rows: [[
isFiltered(link)
? `{{> ${key}/${key}-${key}-link}}`
: `{{> ${key}}}`
]]}}
return getTemplateProperties(key + '-' + key, newProps, templates)
.then(() => wrapTemplate(link, key, newProps[key + '-' + key], newProps))
.then(page => {
var pages = {};
pages[link] = page;
resolve(pages)
})
})
return importer.runAllPromises(promises)
.then(results => results.reduce((obj, r) => Object.assign(obj, r), {}))
}
module.exports = collectRoutes;
/**
* Import required modules
*/
const { importer, TRIM } = require('../Core');
/**
* Import helper functions
*/
const { getTemplateProperties, wrapTemplate, getTemplateByUrl } = importer.import([
'google sheet template properties',
'output google sheet template',
'convert sheet helper functions'
]);
/**
* Check if a URL is filtered
*
* @param {string} url - The URL to check
* @returns {boolean} Whether the URL is filtered
*/
const isFiltered = (url) => url.split('/').length > 1;
/**
* Collect routes by combining 'routes' and 'properties' with fake include templates
*
* @param {array} routes - The list of routes
* @param {object} properties - The properties object
* @param {object} templates - The templates object
* @param {object} rendered - The rendered object
* @returns {Promise<object>} A promise resolving to the collected routes
*/
function collectRoutes(routes, properties, templates, rendered) {
const local = routes
.concat(properties.render || [])
.filter((link, i, arr) =>!rendered.includes(link) && arr.indexOf(link) === i &&!link.includes(':'));
local.forEach((link) => rendered[rendered.length] = link);
const promises = local.map((link) => (resolve) => {
const trimmedBase = (properties.base || '').replace(TRIM, '');
const filteredLink = link.replace(/#.*$/, '').replace(TRIM, '');
if (filteredLink.startsWith(trimmedBase)) {
filteredLink = filteredLink.substring(trimmedBase.length).replace(TRIM, '');
}
const key = getTemplateByUrl(templates, filteredLink);
const newProps = {...properties };
newProps[key + '-' + key + '-link'] = link;
templates[key + '-' + key] = {
template: {
rows: [
isFiltered(link)
? `{{> ${key}/${key}-${key}-link}}`
: `{{> ${key}}}`
]
}
};
return getTemplateProperties(key + '-' + key, newProps, templates)
.then(() => wrapTemplate(link, key, newProps[key + '-' + key], newProps))
.then((page) => {
const pages = {};
pages[link] = page;
resolve(pages);
});
});
return importer.runAllPromises(promises)
.then((results) => results.reduce((obj, r) => ({...obj,...r }), {}));
}
module.exports = collectRoutes;
Code Breakdown
The code imports several modules using the require
function:
importer
: imports the entire ../Core
modulewrapTemplate
, getTemplateProperties
, and getTemplateByUrl
: imports specific functions from the ../Core
module:
wrapTemplate
: returns an output Google Sheets templategetTemplateProperties
: returns properties for a Google Sheets templategetTemplateByUrl
: returns a Google Sheets template by URLA regular expression TRIM
is defined to remove leading and trailing slashes from a string. The isFiltered
function checks if a URL has more than one path segment.
collectRoutes
FunctionThe collectRoutes
function takes four arguments: routes
, properties
, templates
, and rendered
. It appears to be part of a routing mechanism, collecting and processing URL paths.
Here's a step-by-step breakdown:
routes
and properties['render']
arrays, filters out any duplicate links, and adds them to the rendered
array.#
) from the linkbase
path (if set). If so, removes the base path.getTemplateByUrl
function to get a template key associated with the linknewProps
by copying properties
and adding a new key-value pair with the link as the value{{> template }}
syntaxgetTemplateProperties
to get properties for the new templatewrapTemplate
to generate the templatethen
to process the result of wrapTemplate
The collectRoutes
function returns a promise that resolves when all links have been processed.
The code uses Markdown formatting for comments. Unfortunately, the code does not contain any explicit function or variable documentation.