From 4638c3e1d9a6eb94f769f297c5f05a48b9770d4f Mon Sep 17 00:00:00 2001 From: Sean Robertson Date: Fri, 18 Jun 2021 11:24:20 +1200 Subject: [PATCH 1/4] Update server.js Initial commit of incorporating `brew.style` into /source and /download pages --- server.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/server.js b/server.js index 1d93e5401..2258dc074 100644 --- a/server.js +++ b/server.js @@ -113,7 +113,7 @@ app.get('/source/:id', asyncHandler(async (req, res)=>{ const brew = await getBrewFromId(req.params.id, 'share'); const replaceStrings = { '&': '&', '<': '<', '>': '>' }; - let text = brew.text; + let text = `\`\`\`css\n${brew.style}\n\`\`\`\n\n${brew.text}`; for (const replaceStr in replaceStrings) { text = text.replaceAll(replaceStr, replaceStrings[replaceStr]); } @@ -132,8 +132,9 @@ app.get('/download/:id', asyncHandler(async (req, res)=>{ 'Cache-Control' : 'no-cache', 'Content-Type' : 'text/plain', 'Content-Disposition' : `attachment; filename="${fileName}.txt"` - }); - res.status(200).send(brew.text); + }); + let text = `\`\`\`css\n${brew.style}\n\`\`\`\n\n${brew.text}`; + res.status(200).send(text); })); //User Page From 68ed6019f66afc983d9830b1a15974c4b72a5b21 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Sat, 19 Jun 2021 15:50:48 +1200 Subject: [PATCH 2/4] Add new access type (`raw`) and simplify adding other types in the future. Add functionality for `raw` access to skip splitting Style data from Brew Content. --- server.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/server.js b/server.js index 2258dc074..ee2d239ec 100644 --- a/server.js +++ b/server.js @@ -11,9 +11,11 @@ const sanitizeFilename = require('sanitize-filename'); const asyncHandler = require('express-async-handler'); const dedent = require('dedent-tabs').default; +const brewAccessTypes = ['edit', 'share', 'raw']; + //Get the brew object from the HB database or Google Drive const getBrewFromId = asyncHandler(async (id, accessType)=>{ - if(accessType !== 'edit' && accessType !== 'share') + if(!brewAccessTypes.includes(accessType)) throw ('Invalid Access Type when getting brew'); let brew; if(id.length > 12) { @@ -27,6 +29,10 @@ const getBrewFromId = asyncHandler(async (id, accessType)=>{ brew = sanitizeBrew(brew, accessType === 'edit' ? false : true); //Split brew.text into text and style + //unless the Access Type is RAW, in which case return immediately + if(accessType == 'raw') { + return brew; + } if(brew.text.startsWith('```css')) { const index = brew.text.indexOf('```\n\n'); brew.style = brew.text.slice(7, index - 1); @@ -37,7 +43,7 @@ const getBrewFromId = asyncHandler(async (id, accessType)=>{ /* Any CSS here will apply to your document! */ .myExampleClass { - color: black; + color: black; }`; } return brew; @@ -110,10 +116,10 @@ app.get('/robots.txt', (req, res)=>{ //Source page app.get('/source/:id', asyncHandler(async (req, res)=>{ - const brew = await getBrewFromId(req.params.id, 'share'); + const brew = await getBrewFromId(req.params.id, 'raw'); const replaceStrings = { '&': '&', '<': '<', '>': '>' }; - let text = `\`\`\`css\n${brew.style}\n\`\`\`\n\n${brew.text}`; + let text = brew.text; for (const replaceStr in replaceStrings) { text = text.replaceAll(replaceStr, replaceStrings[replaceStr]); } @@ -123,7 +129,7 @@ app.get('/source/:id', asyncHandler(async (req, res)=>{ //Download brew source page app.get('/download/:id', asyncHandler(async (req, res)=>{ - const brew = await getBrewFromId(req.params.id, 'share'); + const brew = await getBrewFromId(req.params.id, 'raw'); const prefix = 'HB - '; let fileName = sanitizeFilename(`${prefix}${brew.title}`).replaceAll(' ', ''); @@ -132,8 +138,8 @@ app.get('/download/:id', asyncHandler(async (req, res)=>{ 'Cache-Control' : 'no-cache', 'Content-Type' : 'text/plain', 'Content-Disposition' : `attachment; filename="${fileName}.txt"` - }); - let text = `\`\`\`css\n${brew.style}\n\`\`\`\n\n${brew.text}`; + }); + const text = brew.text; res.status(200).send(text); })); From 8e304fa483eac34930b085690e4a80a93c71d699 Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Sun, 20 Jun 2021 14:30:00 -0400 Subject: [PATCH 3/4] tab --- server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server.js b/server.js index ee2d239ec..06d2c9b38 100644 --- a/server.js +++ b/server.js @@ -43,7 +43,7 @@ const getBrewFromId = asyncHandler(async (id, accessType)=>{ /* Any CSS here will apply to your document! */ .myExampleClass { - color: black; + color: black; }`; } return brew; From 9b97e0dd87ee862c4afceaf910caf5f09a6b421c Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Sun, 20 Jun 2021 14:32:23 -0400 Subject: [PATCH 4/4] redundant variable --- server.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/server.js b/server.js index 06d2c9b38..7753165c0 100644 --- a/server.js +++ b/server.js @@ -139,8 +139,7 @@ app.get('/download/:id', asyncHandler(async (req, res)=>{ 'Content-Type' : 'text/plain', 'Content-Disposition' : `attachment; filename="${fileName}.txt"` }); - const text = brew.text; - res.status(200).send(text); + res.status(200).send(brew.text); })); //User Page