0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-15 19:12:38 +00:00

simplify getThemeBundle() by using just one loop

Also, removes need for special handling of the "first" theme.
This commit is contained in:
Trevor Buckner
2024-07-15 16:38:19 -04:00
parent 4951b9bf1a
commit 484b0a6dff
2 changed files with 28 additions and 57 deletions

View File

@@ -79,7 +79,7 @@ app.get('/robots.txt', (req, res)=>{
// Theme // Theme
// Path for User Themes // Path for User Themes
app.get('/theme/:id', asyncHandler(getBrew('theme', false)), asyncHandler(getThemeBundle)); app.get('/theme/:id', asyncHandler(getThemeBundle));
// Path for Static Themes // Path for Static Themes
app.get('/theme/:renderer/:id', asyncHandler(getThemeBundle)); app.get('/theme/:renderer/:id', asyncHandler(getThemeBundle));

View File

@@ -262,69 +262,40 @@ const api = {
User theme the value will come from the User Theme metadata. User theme the value will come from the User Theme metadata.
*/ */
let parentReq = {}; let parentReq = {};
const completeStyles = []; let currentTheme;
const completeStyles = [];
const completeSnippets = []; const completeSnippets = [];
if(!req.params.renderer) {
// If this is not set, our *first* theme is a User theme.
const finalChildBrew = req.brew;
// Break up the frontmatter
splitTextStyleAndMetadata(finalChildBrew);
// If there is anything in the snippets member, append it to the snippets array.
if(finalChildBrew?.snippets) completeSnippets.push(JSON.parse(finalChildBrew.snippets));
// If there is anything in the styles member, append it to the styles array with labelling.
if(finalChildBrew?.style) completeStyles.push(`/* From Brew: ${req.protocol}://${req.get('host')}/share/${req.params.id} */\n\n${finalChildBrew.style}`);
// Set up the simulated request we are using for the parent-walking. while (req.params.id) {
// This is our loop control. console.log(`loading theme ID ${req.params.id}`)
parentReq = { //=== User Themes ===//
params : { if(!isStaticTheme(req.params.renderer, req.params.id)) {
id : finalChildBrew.theme, await api.getBrew('share')(req, res, ()=>{});
// This is the only value needed for the User themes. This is the shareId of the theme. currentTheme = req.brew;
}, splitTextStyleAndMetadata(currentTheme);
renderer : finalChildBrew.renderer
// We set this for use later when checking for Static theme inheritance. // If there is anything in the snippets or style members, append them to the appropriate array
}; if(currentTheme?.snippets) completeSnippets.push(JSON.parse(currentTheme.snippets));
while ((parentReq.params.id) && (!isStaticTheme(finalChildBrew.renderer, parentReq.params.id))) { if(currentTheme?.style) completeStyles.push(`/* From Brew: ${req.protocol}://${req.get('host')}/share/${req.params.id} */\n\n${currentTheme.style}`);
await api.getBrew('share')(parentReq, res, ()=>{});
// Load the referenced Brew req.params.id = currentTheme.theme;
splitTextStyleAndMetadata(parentReq); req.params.renderer = currentTheme.renderer;
// break up the meta data }
if(parentReq?.snippets) completeSnippets.push(JSON.parse(parentReq.snippets)); //=== Static Themes ===//
// If there is anything in the snippets member, append it to the snippets array. else {
if(parentReq?.style) {
completeStyles.push(`/* From Brew: ${req.protocol}://${req.get('host')}/share/${parentReq.params.id} */\n\n${parentReq.style}`); // NOTE: This currently makes NO attempt to do anything with Static theme Snippets. Loading of static snippets remains unchanged.
// If there is anything in the styles member, append it to the styles array with labelling. const localStyle = `@import url(\"/themes/${req.params.renderer}/${req.params.id}/style.css\");`;
} completeStyles.push(`/* From Theme ${req.params.id} */\n\n${localStyle}`);
// Update the loop object to point to this theme's parent
parentReq.params.id = parentReq?.theme; req.params.id = Themes[req.params.renderer][req.params.id].baseTheme;
} }
} else {
// If the first theme wasn't a User theme, set up the loop control object
// This is done the same as above for consistant logic.
parentReq = {
params : {
id : req.params.id,
// This is the name of the theme
},
renderer : req.params.renderer
// The renderer is needed for the static pathing.
};
}
while ((parentReq.params.id) && (isStaticTheme(parentReq.renderer, parentReq.params.id))) {
// If we have a static path
const localStyle = fs.readFileSync(path.join(__dirname, '../build/themes/', parentReq.renderer, parentReq.params.id, 'style.css')).toString();
// Read the Theme's style.css from the filesystem
completeStyles.push(`/* From Theme ${parentReq.params.id} */\n\n${localStyle}`);
// Label and append the themes style to the styles array.
parentReq.params.id = Themes[parentReq.renderer][parentReq.params.id].baseTheme;
// NOTE: This currently makes NO attempt to do anything with Static theme Snippets. Loading of static snippets remains unchanged.
} }
const returnObj = { const returnObj = {
// Reverse the order of the arrays so they are listed oldest parent to youngest child.
styles : completeStyles.reverse(), styles : completeStyles.reverse(),
// Reverse the order of the styles array so they are rendered oldest aprent to youngest child. snippets : completeSnippets.reverse()
snippets : completeSnippets
}; };
res.setHeader('Content-Type', 'text/json'); res.setHeader('Content-Type', 'text/json');