icynet-auth-server/src/modules/objects/document/document.service.ts

63 lines
1.6 KiB
TypeScript

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<Document>,
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<string> {
return new Promise((resolve, reject) => {
marked.parse(input, (err, result) => {
if (err) {
return reject(err);
}
resolve(result);
});
});
}
public async getDocumentBySlug(
slug: string,
): Promise<Document & { html: string }> {
const doc = await this.documentRepository.findOne({ where: { slug } });
const html = await this.render(doc.body);
return {
...doc,
html,
};
}
public async getDocumentByID(
id: number,
): Promise<Document & { html: string }> {
const doc = await this.documentRepository.findOne({ where: { id } });
const html = await this.render(doc.body);
return {
...doc,
html,
};
}
}