I'm trying to use two Handlebar Helpers on the {{body}} but only one of them is working? I'm following Traversy media's Node.js App from Scratch youtube course here https://youtu.be/SBvmnHTQIPY at 1:44:10. I triple checked my code against his using diffchecker. Here are the two helpers I'm using on the template index.hbs:
<p>{{stripTags (truncate body 150)}}</p>
But, once I add "150" to truncate, the helper 'stripTags' stops working and shows the html but does truncate it to 150 characters. from this:
J Story 2 I'm baby marfa coloring book church-key, vape leggings 8-bit hashtag umami tbh mixtape chillwave. Butcher cliche unicorn, yr VHS bicycle rights try-hard drinking vinegar. Woke vaporware art party subway tile. Small batch celiac kombucha readymade, chia ramps hoodie artisan banjo kale chips. Austin semiotics wayfarers pinterest, four dollar toast gochujang humblebrag leggings.Offal 8-bit sustainable skateboard quinoa.
to this:
J Story 2 <p style="box-sizing: inherit; margin-bottom: 1.5em; color: rgb(4, 4, 2); font-family: "Helvetica Neue", Helvetica, Arial, Frutiger,...
Handlebar Helper code:
const moment = require('moment')
module.exports = {
formatDate: function (date, format) {
return moment(date).utc().format(format)
},
truncate: function (str, len) {
if (str.length > len && str.length > 0) {
let new_str = str + ' '
new_str = str.substr(0, len)
new_str = str.substr(0, new_str.lastIndexOf(' '))
new_str = new_str.length > 0 ? new_str : str.substr(0, len)
return new_str + '...'
}
return str
},
stripTags: function (input) {
return input.replace(/<(?:.|\n)*?>/gm, '')
},
}
App.js
const path = require('path')
const express = require('express')
const mongoose = require('mongoose')
const dotenv = require('dotenv')
const morgan = require('morgan')
const exphbs = require('express-handlebars')
const passport = require('passport')
const session = require('express-session')
const MongoStore = require('connect-mongo')(session)
const connectDB = require('./config/db')
//Load config
dotenv.config({path: './config/config.env' })
// Passport config
require('./config/passport')(passport)
connectDB()
const app = express()
//Body parser
app.use(express.urlencoded({ extended: false }))
app.use(express.json())
// Logging
if(process.env.NODE_ENV === 'development') {
app.use(morgan('dev'))
}
// Handlebars Helpers
const { formatDate, stripTags, truncate, } = require('./helpers/hbs')
// Handlebars
app.engine('.hbs', exphbs({
helpers: {
formatDate,
stripTags,
truncate,
},
defaultLayout: 'main',
extname: '.hbs',
})
)
app.set('view engine', '.hbs')