|
| 1 | +import { markdownTable } from 'markdown-table'; |
| 2 | + |
| 3 | +import MarkdownIt from 'markdown-it'; |
| 4 | + |
| 5 | +import hljs from 'highlight.js'; |
| 6 | + |
| 7 | +import { create } from 'html-pdf'; |
| 8 | + |
| 9 | +import { Doc, File, Method } from 'doxdox-core'; |
| 10 | + |
| 11 | +const md = new MarkdownIt({ |
| 12 | + html: true, |
| 13 | + linkify: true, |
| 14 | + highlight: function (str, lang) { |
| 15 | + if (lang && hljs.getLanguage(lang)) { |
| 16 | + try { |
| 17 | + return hljs.highlight(str, { language: lang }).value; |
| 18 | + } catch (_) {} |
| 19 | + } |
| 20 | + |
| 21 | + return ''; |
| 22 | + } |
| 23 | +}); |
| 24 | + |
| 25 | +const renderMethod = (method: Method) => `<div class="mb-5"><a name="${ |
| 26 | + method.slug |
| 27 | +}" /> |
| 28 | +
|
| 29 | +<h2 class="method-name">${method.fullName}</h2> |
| 30 | +
|
| 31 | +${method.description ? md.render(method.description) : ''} |
| 32 | +
|
| 33 | +<h3>Parameters</h3> |
| 34 | +
|
| 35 | +<div class="table-responsive"> |
| 36 | +${md |
| 37 | + .render( |
| 38 | + markdownTable([ |
| 39 | + ['Name', 'Types', 'Description'], |
| 40 | + ...method.params.map(({ name, types, description }) => [ |
| 41 | + name, |
| 42 | + `<code>${types.join('</code>, <code>')}</code>`, |
| 43 | + description |
| 44 | + ]) |
| 45 | + ]) |
| 46 | + ) |
| 47 | + .replace('<table>', '<table class="table">')} |
| 48 | +</div> |
| 49 | +
|
| 50 | +<h3>Returns</h3> |
| 51 | +
|
| 52 | +${method.returns.map( |
| 53 | + param => `<p><code>${param.types.join('</code>, <code>')}</code></p> |
| 54 | +
|
| 55 | +<p>${param.description}</p>` |
| 56 | +)} |
| 57 | +
|
| 58 | +</div> |
| 59 | +`; |
| 60 | + |
| 61 | +const renderFile = (file: File) => |
| 62 | + `${file.methods.map(method => renderMethod(method)).join('')}`; |
| 63 | + |
| 64 | +export default async (doc: Doc): Promise<Buffer> => { |
| 65 | + return new Promise(resolve => { |
| 66 | + create( |
| 67 | + `<!DOCTYPE html> |
| 68 | + <html> |
| 69 | + <head> |
| 70 | + <meta charset="utf-8" /> |
| 71 | + <meta name="viewport" content="initial-scale=1" /> |
| 72 | + <title>${doc.name}${ |
| 73 | + doc.description ? ` - ${doc.description}` : '' |
| 74 | + }</title> |
| 75 | + <link |
| 76 | + href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" |
| 77 | + rel="stylesheet" |
| 78 | + integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" |
| 79 | + crossorigin="anonymous" |
| 80 | + /> |
| 81 | + <link |
| 82 | + rel="stylesheet" |
| 83 | + href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.4.0/styles/github.min.css" |
| 84 | + integrity="sha512-0aPQyyeZrWj9sCA46UlmWgKOP0mUipLQ6OZXu8l4IcAmD2u31EPEy9VcIMvl7SoAaKe8bLXZhYoMaE/in+gcgA==" |
| 85 | + crossorigin="anonymous" |
| 86 | + referrerpolicy="no-referrer" |
| 87 | + /> |
| 88 | + <style> |
| 89 | + .pkg-name { |
| 90 | + font-size: 3.5rem; |
| 91 | + } |
| 92 | +
|
| 93 | + .pkg-description { |
| 94 | + font-size: 1.5rem; |
| 95 | + font-weight: 200; |
| 96 | + } |
| 97 | +
|
| 98 | + .method-name { |
| 99 | + position: relative; |
| 100 | + } |
| 101 | + </style> |
| 102 | + </head> |
| 103 | + <body> |
| 104 | + <div class="bg-dark text-white"> |
| 105 | + <div class="container p-5"> |
| 106 | + <h1 class="pkg-name">${doc.name}</h1> |
| 107 | +
|
| 108 | + ${ |
| 109 | + doc.description |
| 110 | + ? `<p class="pkg-description">${doc.description}</p>` |
| 111 | + : '' |
| 112 | + } |
| 113 | + </div> |
| 114 | + </div> |
| 115 | +
|
| 116 | + <div class="container p-5"> |
| 117 | + ${doc.files |
| 118 | + .filter(file => file.methods.length) |
| 119 | + .map(file => renderFile(file)) |
| 120 | + .join('')} |
| 121 | + </div> |
| 122 | +
|
| 123 | + <footer> |
| 124 | + <div class="container p-5 text-center text-muted"> |
| 125 | + <p> |
| 126 | + Documentation generated with |
| 127 | + <a href="https://github.com/docsbydoxdox/doxdox">doxdox</a>. |
| 128 | + </p> |
| 129 | + <p> |
| 130 | + Generated on |
| 131 | + ${new Date().toDateString()} ${new Date().toTimeString()} |
| 132 | + </p> |
| 133 | + </div> |
| 134 | + </footer> |
| 135 | + </body> |
| 136 | + </html> |
| 137 | + `, |
| 138 | + { format: 'Letter' } |
| 139 | + ).toBuffer((_: any, buffer: Buffer) => { |
| 140 | + resolve(buffer); |
| 141 | + }); |
| 142 | + }); |
| 143 | +}; |
0 commit comments