// markdown.js
// Copyright (C) 2022 DTP Technologies, LLC
// License: Apache-2.0

'use strict';

const fs = require('fs');

const { SiteService } = require('../../lib/site-lib');

const marked = require('marked');

class MarkdownService extends SiteService {

  constructor (dtp) {
    super(dtp, module.exports);
  }

  async start ( ) {
    await super.start();
    this.markedRenderer = new marked.Renderer();
  }

  async renderFile (documentFile) {
    const markdown = await fs.promises.readFile(documentFile, 'utf8');
    return this.render(markdown);
  }

  async render (markdown) {
    return marked.parse(markdown, { renderer: this.markedRenderer });
  }
}

module.exports = {
  slug: 'markdown',
  name: 'markdown',
  create: (dtp) => { return new MarkdownService(dtp); },
};