UNPKG

course-renderer

Version:

Manages CA School Courses file system storage and HTML conversion

54 lines (44 loc) 1.6 kB
/** * @module post-renderer */ import * as path from 'path' import { pathSanitize } from './path-sanitizer' import { answerGenerator } from './answer-generator' import { transform } from './md-inline' import { anchor } from './anchor' const through = require('through2') /** * post-renderer - post rendering hooks. This allows us to cleanup what the output of the MD render. * The current hooks are * - pathSanitize - this hook hide the internal file structure * - answerGenerator - Generates a question ID for each question and generates an answer key file for the whole courseName */ module.exports = ( options: any ) => ( through.obj((file: any, i: any, cb: any) => { const pathSep = path.sep const chapter = file.path .replace(`${options.dest}${pathSep}`, '') const course = options.course try { let includeList = false; if (/\.facts.json$/.test(file.path)) { includeList = true; } let content = JSON.parse(file.contents.toString()) content = transform(content, includeList) content = anchor(content) content = pathSanitize(content, course, chapter) answerGenerator(content, options.dest, options.sourceCourse, (err: any, data: any) => { if (err) { cb(err) } else { file.contents = new Buffer(JSON.stringify(data, null, 4)) cb(null, file) } }) } catch(e) { cb(e) return; } }))