mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-14 10:42:38 +00:00
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.
This commit is contained in:
20
server.js
20
server.js
@@ -11,9 +11,11 @@ const sanitizeFilename = require('sanitize-filename');
|
|||||||
const asyncHandler = require('express-async-handler');
|
const asyncHandler = require('express-async-handler');
|
||||||
const dedent = require('dedent-tabs').default;
|
const dedent = require('dedent-tabs').default;
|
||||||
|
|
||||||
|
const brewAccessTypes = ['edit', 'share', 'raw'];
|
||||||
|
|
||||||
//Get the brew object from the HB database or Google Drive
|
//Get the brew object from the HB database or Google Drive
|
||||||
const getBrewFromId = asyncHandler(async (id, accessType)=>{
|
const getBrewFromId = asyncHandler(async (id, accessType)=>{
|
||||||
if(accessType !== 'edit' && accessType !== 'share')
|
if(!brewAccessTypes.includes(accessType))
|
||||||
throw ('Invalid Access Type when getting brew');
|
throw ('Invalid Access Type when getting brew');
|
||||||
let brew;
|
let brew;
|
||||||
if(id.length > 12) {
|
if(id.length > 12) {
|
||||||
@@ -27,6 +29,10 @@ const getBrewFromId = asyncHandler(async (id, accessType)=>{
|
|||||||
|
|
||||||
brew = sanitizeBrew(brew, accessType === 'edit' ? false : true);
|
brew = sanitizeBrew(brew, accessType === 'edit' ? false : true);
|
||||||
//Split brew.text into text and style
|
//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')) {
|
if(brew.text.startsWith('```css')) {
|
||||||
const index = brew.text.indexOf('```\n\n');
|
const index = brew.text.indexOf('```\n\n');
|
||||||
brew.style = brew.text.slice(7, index - 1);
|
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! */
|
/* Any CSS here will apply to your document! */
|
||||||
|
|
||||||
.myExampleClass {
|
.myExampleClass {
|
||||||
color: black;
|
color: black;
|
||||||
}`;
|
}`;
|
||||||
}
|
}
|
||||||
return brew;
|
return brew;
|
||||||
@@ -110,10 +116,10 @@ app.get('/robots.txt', (req, res)=>{
|
|||||||
|
|
||||||
//Source page
|
//Source page
|
||||||
app.get('/source/:id', asyncHandler(async (req, res)=>{
|
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 = { '&': '&', '<': '<', '>': '>' };
|
const replaceStrings = { '&': '&', '<': '<', '>': '>' };
|
||||||
let text = `\`\`\`css\n${brew.style}\n\`\`\`\n\n${brew.text}`;
|
let text = brew.text;
|
||||||
for (const replaceStr in replaceStrings) {
|
for (const replaceStr in replaceStrings) {
|
||||||
text = text.replaceAll(replaceStr, replaceStrings[replaceStr]);
|
text = text.replaceAll(replaceStr, replaceStrings[replaceStr]);
|
||||||
}
|
}
|
||||||
@@ -123,7 +129,7 @@ app.get('/source/:id', asyncHandler(async (req, res)=>{
|
|||||||
|
|
||||||
//Download brew source page
|
//Download brew source page
|
||||||
app.get('/download/:id', asyncHandler(async (req, res)=>{
|
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 - ';
|
const prefix = 'HB - ';
|
||||||
|
|
||||||
let fileName = sanitizeFilename(`${prefix}${brew.title}`).replaceAll(' ', '');
|
let fileName = sanitizeFilename(`${prefix}${brew.title}`).replaceAll(' ', '');
|
||||||
@@ -132,8 +138,8 @@ app.get('/download/:id', asyncHandler(async (req, res)=>{
|
|||||||
'Cache-Control' : 'no-cache',
|
'Cache-Control' : 'no-cache',
|
||||||
'Content-Type' : 'text/plain',
|
'Content-Type' : 'text/plain',
|
||||||
'Content-Disposition' : `attachment; filename="${fileName}.txt"`
|
'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);
|
res.status(200).send(text);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user