1 | import * as _ from 'lodash';
|
2 |
|
3 | const decache = require('decache');
|
4 |
|
5 | export 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 |
|
21 | return { text: text, bold: true };
|
22 | };
|
23 |
|
24 | renderer.em = text => {
|
25 |
|
26 | this.convertedTokens.push({ text: text, italics: true });
|
27 | return text;
|
28 | };
|
29 |
|
30 | renderer.paragraph = text => {
|
31 |
|
32 | return text;
|
33 | };
|
34 |
|
35 |
|
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 |
|
53 | const tokens = this.markedInstance.lexer(stringToConvert);
|
54 |
|
55 | const pdfmakeData = this.markedInstance.Parser.parse(tokens);
|
56 |
|
57 | const result = {
|
58 | text: this.convertedTokens
|
59 | };
|
60 | return result;
|
61 | }
|
62 | }
|
63 |
|
64 | export default MarkdownToPDFEngine.getInstance();
|