mirror of
https://github.com/tldr-pages/tldr.git
synced 2024-11-10 11:29:59 +03:00
62731a8b64
Previously they were stored separately in their own arrays. This made them disconnected, and impossible to know if a page belonged to which platform and language combination. This PR adds an array of objects called `targets` which contains platform/language pairs.
82 lines
1.8 KiB
JavaScript
82 lines
1.8 KiB
JavaScript
'use strict';
|
|
|
|
const glob = require('glob');
|
|
|
|
function parsePlatform(pagefile) {
|
|
return pagefile.split(/\//)[1];
|
|
}
|
|
|
|
function parsePagename(pagefile) {
|
|
return pagefile.split(/\//)[2].replace(/\.md$/, '');
|
|
}
|
|
|
|
function parseLanguage(pagefile) {
|
|
let pagesFolder = pagefile.split(/\//)[0];
|
|
return pagesFolder == 'pages' ? 'en' : pagesFolder.replace(/^pages\./, '');
|
|
}
|
|
|
|
function buildPagesIndex(files) {
|
|
let reducer = function (index, file) {
|
|
let os = parsePlatform(file);
|
|
let page = parsePagename(file);
|
|
let language = parseLanguage(file);
|
|
|
|
if (index[page]) {
|
|
if (!index[page].platform.includes(os)) {
|
|
index[page].platform.push(os);
|
|
}
|
|
|
|
if (!index[page].language.includes(language)) {
|
|
index[page].language.push(language);
|
|
}
|
|
|
|
const targets = index[page].targets;
|
|
const exists = targets.some((t) => {return t.platform === os && t.language === language});
|
|
if (!exists) {
|
|
targets.push({os, language})
|
|
}
|
|
} else {
|
|
index[page] = {
|
|
name: page,
|
|
platform: [os],
|
|
language: [language],
|
|
targets: [{os, language}]
|
|
};
|
|
}
|
|
|
|
return index;
|
|
};
|
|
|
|
let obj = files.reduce(reducer, {});
|
|
|
|
return Object.keys(obj)
|
|
.sort()
|
|
.map(function(page) {
|
|
return {
|
|
name: page,
|
|
platform: obj[page].platform,
|
|
language: obj[page].language,
|
|
targets: obj[page].targets
|
|
};
|
|
});
|
|
}
|
|
|
|
function saveIndex(index) {
|
|
let indexFile = {
|
|
commands: index
|
|
};
|
|
|
|
console.log(JSON.stringify(indexFile));
|
|
}
|
|
|
|
glob('pages*/**/*.md', function (er, files) {
|
|
if (er !== null) {
|
|
console.error('ERROR finding pages!');
|
|
console.error(er);
|
|
return;
|
|
}
|
|
|
|
let index = buildPagesIndex(files);
|
|
saveIndex(index);
|
|
});
|