Skip to content

Commit

Permalink
Patch handlebars, eliminate hbs global variable
Browse files Browse the repository at this point in the history
  • Loading branch information
toBeOfUse committed Jan 26, 2020
1 parent 7ed8269 commit c173440
Show file tree
Hide file tree
Showing 10 changed files with 579 additions and 177 deletions.
13 changes: 13 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"type": "node",
"request": "attach",
"name": "Attach",
"restart": true,
"stopOnEntry": false,
"port": 5757
},
{
Expand All @@ -20,5 +22,16 @@
],
"port": 5757
},
{
"type": "node",
"request": "launch",
"name": "Launch via yarn",
"runtimeExecutable": "yarn",
"restart": true,
"runtimeArgs": [
"debug"
],
"port": 5757
},
]
}
11 changes: 0 additions & 11 deletions app/emailer.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,17 +175,6 @@ async function sendUpdateEmail (user) {
emailLog('info:\n' + JSON.stringify(info, null, 4) + '\n')
}
})
/*
//not for production, i only have this here bc i can't actually send emails and then look at them:
console.log(info);
var emailHTML = await hbs.render('./views/emails/update.handlebars', info.context);
fs.writeFile(user.username + 'Email.html', emailHTML, err => {
if (err) {
emailLog('could not log text of email that was just sent to ' + user.username);
emailLog('reason given: ' + err);
}
});
*/
} else {
emailLog('\nlooks like ' + user.username + ' had no unread notifications! no email will be forthcoming')
}
Expand Down
40 changes: 23 additions & 17 deletions app/postingToSweet.js
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,8 @@ module.exports = function (app) {
? '<span class="author-display-name"><a href="/' + req.user.username + '">' + req.user.displayName + '</a></span><span class="author-username">@' + req.user.username + '</span>'
: '<span class="author-username"><a href="/' + req.user.username + '">@' + req.user.username + '</a></span>'

hbs.render('./views/partials/comment_dynamic.handlebars', {
app.render('partials/comment_dynamic', {
layout: false,
image: image,
name: name,
username: req.user.username,
Expand All @@ -641,14 +642,16 @@ module.exports = function (app) {
comment_id: commentId.toString(),
post_id: post._id.toString(),
depth: depth
}, (err, html) => {
if (err) {
throw Error('could not render new comment html\n' + err)
}
const result = {
comment: html
}
res.contentType('json')
res.send(JSON.stringify(result))
})
.then(async html => {
const result = {
comment: html
}
res.contentType('json')
res.send(JSON.stringify(result))
})
})
.catch((err) => {
console.log('Database error: ' + err)
Expand Down Expand Up @@ -850,21 +853,24 @@ module.exports = function (app) {
// This post has been written by the logged in user - we good
var isCommunityPost = post.type === 'community'
var content = await helper.renderHTMLContent(post, true)
hbs.render('./views/partials/posteditormodal.handlebars', {
app.render('partials/posteditormodal', {
layout: false,
contentWarnings: post.contentWarnings,
privacy: post.privacy,
isCommunityPost: isCommunityPost,
isDraft: post.type === 'draft',
postID: post._id.toString()
}, (err, html) => {
if (err) {
throw Error('could not render post editor modal\n' + err)
}
var result = {
editor: html,
content: content
}
res.contentType('json')
res.send(JSON.stringify(result))
})
.then(async html => {
var result = {
editor: html,
content: content
}
res.contentType('json')
res.send(JSON.stringify(result))
})
} else {
res.send('Hold up there scout')
}
Expand Down
2 changes: 1 addition & 1 deletion app/utilityFunctionsMostlyText.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const fs = require('fs')
const sharp = require('sharp')
const path = require('path')
const sanitizeHtml = require('sanitize-html')
// const hbs
const hbs = require('./viewEngine.js')
const User = require('./models/user')

// these requires are not in server.js bc they're only used here
Expand Down
47 changes: 47 additions & 0 deletions app/viewEngine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// View engine (Handlebars)
const handlebars = require('handlebars')
const expressHandlebars = require('express-handlebars')
const helpers = require('handlebars-helpers')()
const { allowInsecurePrototypeAccess } = require('@handlebars/allow-prototype-access')
const hbs = expressHandlebars.create({
handlebars: allowInsecurePrototypeAccess(handlebars),
defaultLayout: 'main',
partialsDir: ['views/partials/', 'views/partials/scriptPartials/'],
helpers: {
...helpers,
...{
plural: function (number, text) {
var singular = number === 1
// If no text parameter was given, just return a conditional s.
if (typeof text !== 'string') return singular ? '' : 's'
// Split with regex into group1/group2 or group1(group3)
var match = text.match(/^([^()\/]+)(?:\/(.+))?(?:\((\w+)\))?/)
// If no match, just append a conditional s.
if (!match) return text + (singular ? '' : 's')
// We have a good match, so fire away
return (
// Singular case
(singular && match[1]) ||
// Plural case: 'bagel/bagels' --> bagels
match[2] ||
// Plural case: 'bagel(s)' or 'bagel' --> bagels
match[1] + (match[3] || 's')
)
},
buildComment (comment, depth) {
if (!depth) depth = 1
var tree = []
tree.push({
comment: comment,
depth: depth
})
comment.replies.forEach((r) => {
depth = depth + 1
tree.comment.replies.depth = depth
})
return tree
}
}
}
})
module.exports = hbs
2 changes: 1 addition & 1 deletion app/viewingSweet.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const Relationship = require('./models/relationship')
const Community = require('./models/community')
const Post = require('./models/post')
const Tag = require('./models/tag')
const Image = require('./models/image')
const helper = require('./utilityFunctionsMostlyText')
const notifier = require('./notifier')
const globals = require('../config/globals')
Expand Down Expand Up @@ -400,7 +401,6 @@ module.exports = function (app) {
const followedUserData = []
Relationship.find({ fromUser: req.user._id, value: 'follow' }).populate('toUser').then((followedUsers) => {
followedUsers.forEach(relationship => {
console.log(relationship)
var follower = {
key: helper.escapeHTMLChars(relationship.toUser.displayName ? relationship.toUser.displayName + ' (' + '@' + relationship.toUser.username + ')' : '@' + relationship.toUser.username),
value: relationship.toUser.username,
Expand Down
4 changes: 2 additions & 2 deletions config/globals.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
maximumCommentDepth: 5
};
maximumCommentDepth: 5
}
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"author": "Raphael Kabo",
"license": "GPL-3.0-only",
"dependencies": {
"@handlebars/allow-prototype-access": "^1.0.1",
"@sendgrid/mail": "^6.3.1",
"autolinker": "^3.0.5",
"bcrypt-nodejs": "0.0.3",
Expand Down Expand Up @@ -51,9 +52,6 @@
"shortid": "^2.2.14",
"web-push": "^3.3.5"
},
"resolutions": {
"handlebars": "v4.5.3"
},
"devDependencies": {
"eslint": "^6.8.0",
"eslint-config-standard": "^14.1.0",
Expand Down
49 changes: 5 additions & 44 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// Initialization ======================================================================
const express = require('express')
const handlebars = require('express-handlebars')
const app = express()
const port = process.env.PORT || 8686
const passport = require('passport')
const flash = require('connect-flash')
require('handlebars-helpers')()

const hbs = require('./app/viewEngine.js')
app.engine('handlebars', hbs.engine)
app.set('view engine', 'handlebars')

const compression = require('compression')
app.use(compression())
Expand All @@ -27,47 +29,6 @@ app.use(bodyParser()) // get information from html forms
const mongoSanitize = require('express-mongo-sanitize') // sanitize information recieved from html forms
app.use(mongoSanitize())

// View engine (Handlebars)
hbs = handlebars.create({
defaultLayout: 'main',
partialsDir: ['views/partials/', 'views/partials/scriptPartials/'],
helpers: {
plural: function (number, text) {
var singular = number === 1
// If no text parameter was given, just return a conditional s.
if (typeof text !== 'string') return singular ? '' : 's'
// Split with regex into group1/group2 or group1(group3)
var match = text.match(/^([^()\/]+)(?:\/(.+))?(?:\((\w+)\))?/)
// If no match, just append a conditional s.
if (!match) return text + (singular ? '' : 's')
// We have a good match, so fire away
return (
// Singular case
(singular && match[1]) ||
// Plural case: 'bagel/bagels' --> bagels
match[2] ||
// Plural case: 'bagel(s)' or 'bagel' --> bagels
match[1] + (match[3] || 's')
)
},
buildComment (comment, depth) {
if (!depth) depth = 1
var tree = []
tree.push({
comment: comment,
depth: depth
})
comment.replies.forEach((r) => {
depth = depth + 1
tree.comment.replies.depth = depth
})
return tree
}
}
})
app.engine('handlebars', hbs.engine)
app.set('view engine', 'handlebars')

// Static files
app.use(express.static('public'))

Expand Down Expand Up @@ -173,7 +134,7 @@ require('autolinker')
require('node-schedule')
require('./config/globals')

const writeMorganToSeparateFile = true // change to write full request log to stdout instead of a separate file
const writeMorganToSeparateFile = false // change to write full request log to stdout instead of a separate file

let stream
if (writeMorganToSeparateFile) {
Expand Down
Loading

0 comments on commit c173440

Please sign in to comment.