UNPKG

2.85 kBJavaScriptView Raw
1"use strict";
2/*
3 * @adonisjs/sink
4 *
5 * (c) Harminder Virk <virk@adonisjs.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10var __importDefault = (this && this.__importDefault) || function (mod) {
11 return (mod && mod.__esModule) ? mod : { "default": mod };
12};
13Object.defineProperty(exports, "__esModule", { value: true });
14exports.MarkdownRenderer = void 0;
15const open_1 = __importDefault(require("open"));
16const path_1 = require("path");
17const os_1 = require("os");
18const fs_extra_1 = require("fs-extra");
19const Styles_1 = require("./Styles");
20/**
21 * Markdown renderer for opening the instructions md file inside the
22 * terminal or the browser.
23 */
24class MarkdownRenderer {
25 constructor(mdFileAbsPath, packageName) {
26 this.mdFileAbsPath = mdFileAbsPath;
27 this.packageName = packageName;
28 }
29 /**
30 * Generates HTML with the markdown processed code
31 */
32 generateHtml(contents) {
33 return `<html>
34 <head>
35 <style type="text/css">${Styles_1.css}</style>
36 </head>
37 <body>
38 <article class="markdown-body">
39 <h1> Setup instructions for
40 <a href="https://npmjs.org/package/${this.packageName}" target="_blank">${this.packageName}</a>
41 </h1>
42 ${contents}
43 </article>
44 </body>
45 </html>`;
46 }
47 /**
48 * Opens the html contents by writing it to a temporary
49 * file and opens up the file inside the browser.
50 */
51 async openContentsInBrowser(html) {
52 const filePath = (0, path_1.join)((0, os_1.tmpdir)(), `adonis-${new Date().getTime()}.html`);
53 await (0, fs_extra_1.outputFile)(filePath, html);
54 await (0, open_1.default)(filePath, { wait: false });
55 }
56 /**
57 * Converts markdown to HTML and opens it up inside the browser
58 */
59 async renderInBrowser() {
60 const { marked, Renderer } = await import('marked');
61 const renderer = new Renderer();
62 try {
63 const contents = await (0, fs_extra_1.readFile)(this.mdFileAbsPath, 'utf-8');
64 const html = this.generateHtml(marked.setOptions({ renderer })(contents.trim()));
65 await this.openContentsInBrowser(html);
66 }
67 catch (error) { }
68 }
69 /**
70 * Writes markdown in the terminal
71 */
72 async renderInTerminal() {
73 const { marked } = await import('marked');
74 const { default: TerminalRenderer } = await import('marked-terminal');
75 const renderer = new TerminalRenderer();
76 try {
77 const contents = await (0, fs_extra_1.readFile)(this.mdFileAbsPath, 'utf-8');
78 console.log(marked.setOptions({ renderer })(contents.trim()).trim());
79 }
80 catch (error) { }
81 }
82}
83exports.MarkdownRenderer = MarkdownRenderer;