2017-07-07 13:28:29 -04:00
/ * *
* Copyright ( c ) 2017 - present , Facebook , Inc .
*
2017-10-05 14:14:49 -04:00
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree .
2017-07-07 13:28:29 -04:00
* /
2017-07-10 19:38:35 -04:00
const CWD = process . cwd ( ) ;
2017-07-07 13:28:29 -04:00
2017-07-10 19:38:35 -04:00
const Metadata = require ( "../core/metadata.js" ) ;
const fs = require ( "fs" ) ;
2017-07-07 13:28:29 -04:00
let languages ;
2017-07-10 19:38:35 -04:00
if ( fs . existsSync ( CWD + "/languages.js" ) ) {
languages = require ( CWD + "/languages.js" ) ;
2017-07-07 13:28:29 -04:00
} else {
2017-07-10 19:38:35 -04:00
languages = [
{
enabled : true ,
name : "English" ,
tag : "en"
}
] ;
2017-07-07 13:28:29 -04:00
}
2017-08-15 19:55:38 -04:00
// returns data broken up into categories for a sidebar
2017-07-24 16:33:58 -04:00
function readCategories ( sidebar ) {
2017-07-07 13:28:29 -04:00
const enabledLanguages = [ ] ;
languages . filter ( lang => lang . enabled ) . map ( lang => {
enabledLanguages . push ( lang . tag ) ;
} ) ;
const allCategories = { } ;
for ( let k = 0 ; k < enabledLanguages . length ; ++ k ) {
const language = enabledLanguages [ k ] ;
2017-08-03 13:25:01 -04:00
const metadatas = [ ] ;
Object . keys ( Metadata ) . forEach ( id => {
const metadata = Metadata [ id ] ;
if ( metadata . sidebar === sidebar && metadata . language === language ) {
metadatas . push ( metadata ) ;
}
2017-07-07 13:28:29 -04:00
} ) ;
// Build a hashmap of article_id -> metadata
const articles = { } ;
for ( let i = 0 ; i < metadatas . length ; ++ i ) {
const metadata = metadatas [ i ] ;
articles [ metadata . id ] = metadata ;
}
// Build a hashmap of article_id -> previous_id
const previous = { } ;
for ( let i = 0 ; i < metadatas . length ; ++ i ) {
const metadata = metadatas [ i ] ;
if ( metadata . next ) {
if ( ! articles [ metadata . next ] ) {
throw new Error (
2017-08-09 19:17:39 -04:00
metadata . version
? ` Improper sidebars file for version ${ metadata . version } . Make sure that all documents with ids specified in this version's sidebar file exist and that no ids are repeated. `
: ` Improper sidebars.json file. Make sure that documents with the ids specified in sidebars.json exist and that no ids are repeated. `
2017-07-07 13:28:29 -04:00
) ;
}
previous [ articles [ metadata . next ] . id ] = metadata . id ;
}
}
// Find the first element which doesn't have any previous
let first = null ;
for ( let i = 0 ; i < metadatas . length ; ++ i ) {
const metadata = metadatas [ i ] ;
if ( ! previous [ metadata . id ] ) {
first = metadata ;
break ;
}
}
const categories = [ ] ;
let currentCategory = null ;
let metadata = first ;
let i = 0 ;
while ( metadata && i ++ < 1000 ) {
if ( ! currentCategory || metadata . category !== currentCategory . name ) {
currentCategory && categories . push ( currentCategory ) ;
currentCategory = {
name : metadata . category ,
2017-07-10 19:38:35 -04:00
links : [ ]
2017-07-07 13:28:29 -04:00
} ;
}
currentCategory . links . push ( metadata ) ;
metadata = articles [ metadata . next ] ;
}
categories . push ( currentCategory ) ;
allCategories [ language ] = categories ;
}
2017-07-25 19:30:49 -04:00
return allCategories ;
2017-07-07 13:28:29 -04:00
}
module . exports = readCategories ;