UNPKG

1.89 kBPlain TextView Raw
1import * as _ from 'lodash';
2
3const decache = require('decache');
4
5export class MarkdownToPDFEngine {
6 private static instance: MarkdownToPDFEngine;
7
8 private markedInstance;
9
10 private convertedTokens = [];
11
12 private constructor() {
13 decache('marked');
14 const { marked } = require('marked');
15 this.markedInstance = marked;
16
17 const renderer = new this.markedInstance.Renderer();
18
19 renderer.strong = text => {
20 // console.log('MarkdownToPDFEngine strong: ', text);
21 return { text: text, bold: true };
22 };
23
24 renderer.em = text => {
25 // console.log('MarkdownToPDFEngine em: ', text);
26 this.convertedTokens.push({ text: text, italics: true });
27 return text;
28 };
29
30 renderer.paragraph = text => {
31 // console.log('MarkdownToPDFEngine paragraph: ', text);
32 return text;
33 };
34
35 // TODO Add custom parser... -> https://github.com/markedjs/marked/issues/504
36
37 this.markedInstance.setOptions({
38 renderer: renderer,
39 gfm: true,
40 breaks: false
41 });
42 }
43 public static getInstance() {
44 if (!MarkdownToPDFEngine.instance) {
45 MarkdownToPDFEngine.instance = new MarkdownToPDFEngine();
46 }
47 return MarkdownToPDFEngine.instance;
48 }
49
50 public convert(stringToConvert: string) {
51 this.convertedTokens = [];
52 // console.log('MarkdownToPDFEngine convert: ', stringToConvert);
53 const tokens = this.markedInstance.lexer(stringToConvert);
54 // console.log(tokens);
55 const pdfmakeData = this.markedInstance.Parser.parse(tokens);
56 // console.log(this.convertedTokens);
57 const result = {
58 text: this.convertedTokens
59 };
60 return result;
61 }
62}
63
64export default MarkdownToPDFEngine.getInstance();