-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1ef72ca
commit 9c396c1
Showing
4 changed files
with
136 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
const fse = require("fs-extra"); | ||
const glob = require("glob"); | ||
|
||
const getConfig = require("./config"); | ||
const processor = require("./processor"); | ||
|
||
const config = getConfig(); | ||
|
||
/** | ||
* Converts content of meta.json into array of height data, which are objects containing 'path' and 'height' of an interactive example | ||
* Any examples for which height couldn't be determined are skipped | ||
* @param metaContent - Content of meta.json as an JS object | ||
* @return {{path: string, height: String}[]} array of height data | ||
*/ | ||
function getPagesHeightData(metaContent) { | ||
const pageArray = Object.values(metaContent.pages); | ||
const allHeights = pageArray.flatMap(p => getPageHeightData(p)); | ||
return allHeights.filter(p => p !== undefined); | ||
} | ||
|
||
/** | ||
* Returns object holding 'path' and 'height' of interactive example provided as an argument in form of an object which can be found in meta.json files | ||
* 'path' is relative path to interactive example file name, for example: pages/css/function-calc.html | ||
* 'height' is YARI class name used for setting height of that interactive example | ||
* @param page - Object describing single interactive example | ||
* @return {{path: string, height: String}|undefined} height data - object holding 'path' and 'height' of interactive example | ||
*/ | ||
function getPageHeightData(page) { | ||
const height = getPageHeight(page); | ||
if(height !== undefined) { | ||
return { | ||
'path': `pages/${page.type}/${page.fileName}`, | ||
'height': height | ||
} | ||
} else { | ||
return undefined; | ||
} | ||
} | ||
|
||
/** | ||
* Returns YARI class name used for setting height of interactive example provided as an argument in form of an object which can be found in meta.json files | ||
* @param page - Object describing single interactive example | ||
* @return {String|undefined} height - YARI class name used for setting height of interactive example | ||
*/ | ||
function getPageHeight(page) { | ||
if(page.height !== undefined) { | ||
return page.height; | ||
} | ||
|
||
switch(page.type) { | ||
case 'css': | ||
return '';// All examples have the same height | ||
case 'tabbed': | ||
case 'webapi-tabbed': | ||
console.error(`MDN-BOB: (heightBuilder.js) Missing height property for ${page.fileName}`); | ||
return undefined; | ||
case 'js': | ||
// Yari expects those values for JS editor: 'shorter', '', 'taller' | ||
return processor.getJSPageHeight(page.exampleCode); | ||
case 'wat': | ||
// Yari expects those values for WAT editor: 'tabbed-shorter', 'tabbed-standard', 'tabbed-taller' | ||
return 'tabbed-' + processor.getWatPageHeight(page.watExampleCode); | ||
default: | ||
console.error(`MDN-BOB: (heightBuilder.js) Unsupported page type ${page.type}`); | ||
return undefined; | ||
} | ||
} | ||
|
||
/** | ||
* Builds height-data.json containing array of objects with path and height properties, which can be used for setting height of interactive example | ||
*/ | ||
function buildHeightData() { | ||
return new Promise((resolve) => { | ||
const metaJSONArray = glob.sync(config.metaGlob, {}); | ||
const heightData = []; | ||
|
||
for (const metaJson of metaJSONArray) { | ||
const file = fse.readJsonSync(metaJson); | ||
|
||
const metaHeights = getPagesHeightData(file); | ||
heightData.push(...metaHeights); | ||
} | ||
|
||
const jsonData = JSON.stringify(heightData, null, 4); | ||
fse.outputFileSync(config.heightData, jsonData); | ||
|
||
resolve("MDN-BOB: Height Data was successfully constructed"); | ||
}); | ||
} | ||
|
||
module.exports = { | ||
buildHeightData, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters