feat(courses): Add view helpers

This commit is contained in:
Khaled Garbaya
2017-09-26 17:52:10 +02:00
committed by Benedikt Rötsch
parent c650dd3f1c
commit 500b103c03
7 changed files with 32 additions and 5 deletions

8
app.js
View File

@@ -1,6 +1,7 @@
require('dotenv').config({ path: 'variables.env' })
const express = require('express')
const path = require('path')
const helpers = require('./helpers')
// const favicon = require('serve-favicon')
const logger = require('morgan')
const cookieParser = require('cookie-parser')
@@ -27,6 +28,12 @@ app.use(bodyParser.urlencoded({ extended: false }))
app.use(cookieParser())
app.use(express.static(path.join(__dirname, 'public')))
// Pass custo helpers to all our templates
app.use(function (req, res, next) {
res.locals.helpers = helpers
next()
})
app.use('/', index)
app.use('/courses', courses)
app.use('/categories', categories)
@@ -52,6 +59,7 @@ app.use(function (err, req, res, next) {
res.render('error')
})
// app.use()
// init the contentful client
initClient()

17
helpers.js Normal file
View File

@@ -0,0 +1,17 @@
const marked = require('marked')
// Parse markdown text
exports.markdown = (content) => {
return marked(removeIvalidDataURL(content), {sanitize: true})
}
// Dump is a handy debugging function we can use to sort of "console.log" our data
exports.dump = (obj) => JSON.stringify(obj, null, 2)
// Evil users might try to add base64 url data to execute js
// so we should take care of that
function removeIvalidDataURL (content) {
let regex = /data:\S+;base64\S*/gm
return content.replace(regex, '#')
}

View File

@@ -17,6 +17,8 @@
"debug": "~2.2.0",
"dotenv": "^4.0.0",
"express": "~4.14.0",
"jstransformer-markdown-it": "^2.0.0",
"marked": "^0.3.6",
"morgan": "~1.7.0",
"normalize.css": "^7.0.0",
"pug": "~2.0.0-beta6",

View File

@@ -16,6 +16,6 @@ block content
if lesson
h2= lesson.fields.title
p= lesson.fields.description
p !{helpers.markdown(lesson.fields.description)}
else
p= course.fields.description
p !{helpers.markdown(course.fields.description)}

View File

@@ -3,6 +3,6 @@ mixin copyModule(module)
.module__copy
h1.module__copy__title #{module.fields.title}
h3.module__copy__headline #{module.fields.headline}
div.module__copy__copy #{module.fields.copy}
div.module__copy__copy !{helpers.markdown(module.fields.copy)}

View File

@@ -7,7 +7,7 @@ mixin courseCard(course = {fields: {title: '', description: '', categories: [],
.course-card__title
h2= course.fields.title
.course-card__description
p= course.fields.shortDescription
p !{helpers.markdown(course.fields.shortDescription)}
.course-card__link
a(href=`/courses/${course.fields.slug}`) view course

View File

@@ -1,7 +1,7 @@
mixin lesson(lesson)
.lesson
h1.lesson__tilte #{lesson.fields.title}
div.lesson__shortDescription #{lesson.fields.shortDescription}
div.lesson__shortDescription !{helpers.markdown(lesson.fields.shortDescription)}
img.lesson__image(src=`${lesson.fields.image.fields.file.url}` alt=`${lesson.fields.image.fields.title}`)