From 500b103c03f96ef3daa294ca69d0f6a8e6021440 Mon Sep 17 00:00:00 2001 From: Khaled Garbaya Date: Tue, 26 Sep 2017 17:52:10 +0200 Subject: [PATCH] feat(courses): Add view helpers --- app.js | 8 ++++++++ helpers.js | 17 +++++++++++++++++ package.json | 2 ++ views/course.pug | 4 ++-- views/mixins/_copyModule.pug | 2 +- views/mixins/_courseCard.pug | 2 +- views/mixins/_lesson.pug | 2 +- 7 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 helpers.js diff --git a/app.js b/app.js index fd77e17..db6db70 100644 --- a/app.js +++ b/app.js @@ -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() diff --git a/helpers.js b/helpers.js new file mode 100644 index 0000000..5c5547c --- /dev/null +++ b/helpers.js @@ -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, '#') +} + diff --git a/package.json b/package.json index d1e9055..a9c1a72 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/views/course.pug b/views/course.pug index 0eb679e..7b76611 100644 --- a/views/course.pug +++ b/views/course.pug @@ -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)} diff --git a/views/mixins/_copyModule.pug b/views/mixins/_copyModule.pug index b410bca..8842b08 100644 --- a/views/mixins/_copyModule.pug +++ b/views/mixins/_copyModule.pug @@ -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)} diff --git a/views/mixins/_courseCard.pug b/views/mixins/_courseCard.pug index 3ea3f39..5d11caf 100644 --- a/views/mixins/_courseCard.pug +++ b/views/mixins/_courseCard.pug @@ -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 diff --git a/views/mixins/_lesson.pug b/views/mixins/_lesson.pug index 52b7402..a36394a 100644 --- a/views/mixins/_lesson.pug +++ b/views/mixins/_lesson.pug @@ -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}`)