// article.js // Copyright (C) 2021 Digital Telepresence, LLC // License: Apache-2.0 'use strict'; const fs = require('fs'); const { SiteService } = require('../../lib/site-lib'); const mongoose = require('mongoose'); const Article = mongoose.model('Article'); const marked = require('marked'); class ArticleService extends SiteService { constructor (dtp) { super(dtp, module.exports); this.populateArticle = [ { path: 'channel', select: 'slug name images.icon status stats links', }, { path: 'author', select: '_id username username_lc displayName picture', }, ]; } async start ( ) { this.markedRenderer = new marked.Renderer(); } async getById (articleId) { const article = await Article .findById(articleId) .populate(this.populateArticle) .lean(); return article; } async getForChannel (channel, pagination) { const articles = await Article .find({ channel: channel._id }) .sort({ created: -1 }) .skip(pagination.skip) .limit(pagination.cpp) .populate(this.populateArticle) .lean(); return articles; } async renderMarkdown (documentFile) { const markdown = await fs.promises.readFile(documentFile, 'utf8'); return marked(markdown, { renderer: this.markedRenderer }); } } module.exports = { slug: 'article', name: 'article', create: (dtp) => { return new ArticleService(dtp); }, };