import { Inject, Injectable } from '@nestjs/common'; import { Repository } from 'typeorm'; import { Document } from './document.entity'; import { TokenService } from 'src/modules/utility/services/token.service'; import { marked } from 'marked'; @Injectable() export class DocumentService { constructor( @Inject('DOCUMENT_CACHE') private documentStorage: string, @Inject('DOCUMENT_REPOSITORY') private documentRepository: Repository, private tokenService: TokenService, ) {} public slugify(text: string): string { return text .toString() .normalize('NFD') // split an accented letter in the base letter and the accent .replace(/[\u0300-\u036f]/g, '') // remove all previously split accents .toLowerCase() .trim() .replace(/\s+/g, '-') .replace(/[^\w\-]+/g, '') .replace(/\-\-+/g, '-'); } public async render(input: string): Promise { return new Promise((resolve, reject) => { marked.parse(input, (err, result) => { if (err) { return reject(err); } resolve(result); }); }); } public async getDocumentBySlug( slug: string, ): Promise { const doc = await this.documentRepository.findOne({ where: { slug } }); const html = await this.render(doc.body); return { ...doc, html, }; } public async getDocumentByID( id: number, ): Promise { const doc = await this.documentRepository.findOne({ where: { id } }); const html = await this.render(doc.body); return { ...doc, html, }; } }