<commit_before>const querystring = require('querystring')
const express = require('express')
const passport = require('passport');
const _ = require('underscore')

const Model = require('../model')
const config = require('../config')

const router = express.Router()

// category data
router.use((req, res, next) => {
	Model.SeriesDB.then(db => {
		db.findAll()
		.then(seriesSet => {
			var categories = _.map(
				_.groupBy(seriesSet, series => { return series.category }), 
				seriesSet => {
					return {
						title: config.categories[seriesSet[0].category],
						series: seriesSet
					}
				}
			)

			res.locals.categories =  _.map(config.categories, (name, slug) => {
				return _.findWhere(categories, {title: name})
			})
			next()
		})
	})
})

//login
router.get('/login', (req, res) => {
	res.render('login')
})

router.post('/login', (req, res, next) => {
	passport.authenticate('local', {
		successRedirect: '/admin',
		failureRedirect: '/login',
		failureFlash: true
	})(req, res, next)
})

router.get('/logout', (req, res) => {
	req.logout()
	res.redirect('/')
})

router.get(/^\/(?:page\/([^\\\/]+?))?(?:\/(?=$))?$/i, (req, res) => {
	var page = normalize_page(req.params[0])
	Model.PostDB.then(Post => {
		Post.count().then(count => {
			res.locals.pagination = pagination(Math.ceil(count/5), page)
			res.locals.pagination.base_url = "/page/"
		})
	})
	Model.PostDB.then(Post => {
		return Post.findAll({
			offset: (page-1)*5,
			limit: 5,
			order: 'published_date DESC',
		})
		.then(posts => {
			posts = _.map(posts, function(post){
				var content = post.content.replace(/<.*?>/g, '')
				content = content.replace(/# Korean Only/g, '')
				content = content.replace(/# With English( Translations)?/g, '')
				content = content.split(' ').splice(0, 60).join(' ')
				post.content = markdown(content) 
				return post
			})
			res.render('index', {
				posts: posts,
			})
		})
	})
})

//single
router.get('/:slug', (req, res) => {
	Model.PostDB.then(Post => {
		return Post.find({ where: {slug: normalize_slug(req.params.slug)}})
		.then(post => {
			res.render('single', {
				page_title: post.title,
				title: post.title,
				content: markdown(post.content),
			})
		})
	})
})

function normalize_slug(slug) {
	if (!slug.includes('%')) {
		slug = querystring.escape(slug)
	}
	return slug.toLowerCase()
}

const md = require('markdown-it')({
	html: true,
	breaks: true,
})

function markdown(content) {
	return md.render(content)<commit_msg>Added 404 handling.<commit_after>const querystring = require('querystring')
const express = require('express')
const passport = require('passport');
const _ = require('underscore')

const Model = require('../model')
const config = require('../config')

const router = express.Router()

// category data
router.use((req, res, next) => {
	Model.SeriesDB.then(db => {
		db.findAll()
		.then(seriesSet => {
			var categories = _.map(
				_.groupBy(seriesSet, series => { return series.category }), 
				seriesSet => {
					return {
						title: config.categories[seriesSet[0].category],
						series: seriesSet
					}
				}
			)

			res.locals.categories =  _.map(config.categories, (name, slug) => {
				return _.findWhere(categories, {title: name})
			})
			next()
		})
	})
})

//login
router.get('/login', (req, res) => {
	res.render('login')
})

router.post('/login', (req, res, next) => {
	passport.authenticate('local', {
		successRedirect: '/admin',
		failureRedirect: '/login',
		failureFlash: true
	})(req, res, next)
})

router.get('/logout', (req, res) => {
	req.logout()
	res.redirect('/')
})

// index
router.get(/^\/(?:page\/([^\\\/]+?))?(?:\/(?=$))?$/i, (req, res) => {
	var page = normalize_page(req.params[0])
	Model.PostDB.then(Post => {
		Post.count().then(count => {
			res.locals.pagination = pagination(Math.ceil(count/5), page)
			res.locals.pagination.base_url = "/page/"
		})
	})
	Model.PostDB.then(Post => {
		return Post.findAll({
			offset: (page-1)*5,
			limit: 5,
			order: 'published_date DESC',
		})
		.then(posts => {
			posts = _.map(posts, function(post){
				var content = post.content.replace(/<.*?>/g, '')
				content = content.replace(/# Korean Only/g, '')
				content = content.replace(/# With English( Translations)?/g, '')
				content = content.split(' ').splice(0, 60).join(' ')
				post.content = markdown(content) 
				return post
			})
			res.render('index', {
				posts: posts,
			})
		})
	})
})

//single
router.get('/:slug', (req, res, next) => {
	Model.PostDB.then(Post => {
		return Post.find({ where: {slug: normalize_slug(req.params.slug)}})
		.then(post => {
			res.render('single', {
				page_title: post.title,
				title: post.title,
				content: markdown(post.content),
			})
		})
		.catch(err => {
			err.status = 404
			next(err)
		})
	})
})

function normalize_slug(slug) {
	if (!slug.includes('%')) {
		slug = querystring.escape(slug)
	}
	return slug.toLowerCase()
}

const md = require('markdown-it')({
	html: true,
	breaks: true,
})

function markdown(content) {
	return md.render(content)
{'fraction_non_alphanumeric': 0.20741183502689778, 'fraction_numerical': 0.004781829049611476, 'mean_word_length': 3.4229074889867843, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5680556', 'n_tokens_mistral': 2032, 'n_tokens_neox': 1786, 'n_words': 329}