Skip to content

Commit 1e41fc0

Browse files
committed
Added new pdf plugin.
1 parent a05ad38 commit 1e41fc0

File tree

11 files changed

+287
-0
lines changed

11 files changed

+287
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ A template for creating your own renderer [doxdox-renderer-template](./packages/
285285
| ---------------------------------------------------------------------- | -------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- |
286286
| [doxdox-renderer-dash](./packages/doxdox-renderer-dash/) | Dash renderer for doxdox. | [![NPM version](https://img.shields.io/npm/v/doxdox-renderer-dash?style=flat-square)](https://www.npmjs.org/package/doxdox-renderer-dash) |
287287
| [doxdox-renderer-github-wiki](./packages/doxdox-renderer-github-wiki/) | GitHub Wiki renderer for doxdox. | [![NPM version](https://img.shields.io/npm/v/doxdox-renderer-github-wiki?style=flat-square)](https://www.npmjs.org/package/doxdox-renderer-github-wiki) |
288+
| [doxdox-renderer-pdf](./packages/doxdox-renderer-pdf/) | PDF renderer for doxdox. | [![NPM version](https://img.shields.io/npm/v/doxdox-renderer-pdf?style=flat-square)](https://www.npmjs.org/package/doxdox-renderer-pdf) |
288289

289290
## Questions
290291

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"./packages/doxdox-renderer-github-wiki",
1212
"./packages/doxdox-renderer-json",
1313
"./packages/doxdox-renderer-markdown",
14+
"./packages/doxdox-renderer-pdf",
1415
"./packages/doxdox-renderer-template",
1516
"./packages/doxdox-cli",
1617
"./packages/doxdox"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules/
2+
3+
coverage/
4+
5+
dist/
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
coverage/
2+
3+
src/
4+
5+
.prettierignore
6+
.prettierrc
7+
8+
tsconfig.json
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist/
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"arrowParens": "avoid",
3+
"singleQuote": true,
4+
"tabWidth": 4,
5+
"trailingComma": "none",
6+
"overrides": [
7+
{
8+
"files": ["*.json", ".*rc", "*.md"],
9+
"options": {
10+
"tabWidth": 2
11+
}
12+
}
13+
]
14+
}

packages/doxdox-renderer-pdf/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2022 Scott Doxey
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# doxdox-renderer-pdf
2+
3+
> PDF renderer for doxdox.
4+
5+
[![NPM version](https://img.shields.io/npm/v/doxdox-renderer-pdf?style=flat-square)](https://www.npmjs.org/package/doxdox-renderer-pdf)
6+
[![NPM downloads per month](https://img.shields.io/npm/dm/doxdox-renderer-pdf?style=flat-square)](https://www.npmjs.org/package/doxdox-renderer-pdf)
7+
[![doxdox documentation](https://img.shields.io/badge/doxdox-documentation-%23E85E95?style=flat-square)](https://doxdox.org)
8+
9+
## Install
10+
11+
```bash
12+
$ npm install [email protected] [email protected] --save-dev
13+
```
14+
15+
## Usage
16+
17+
```bash
18+
$ doxdox -r pdf -o docs.pdf
19+
```
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"name": "doxdox-renderer-pdf",
3+
"description": "PDF renderer for doxdox.",
4+
"version": "4.0.0-preview.1",
5+
"exports": "./dist/index.js",
6+
"types": "./dist/index.d.ts",
7+
"type": "module",
8+
"engines": {
9+
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
10+
},
11+
"dependencies": {
12+
"highlight.js": "11.4.0",
13+
"html-pdf": "3.0.1",
14+
"markdown-it": "12.3.2",
15+
"markdown-table": "3.0.2"
16+
},
17+
"peerDependencies": {
18+
"doxdox-core": "4.0.0-preview.1"
19+
},
20+
"peerDependenciesMeta": {
21+
"doxdox-core": {
22+
"optional": false
23+
}
24+
},
25+
"devDependencies": {
26+
"@types/jest": "27.4.0",
27+
"@types/html-pdf": "3.0.0",
28+
"@types/node": "17.0.13",
29+
"jest": "27.4.7",
30+
"ts-jest": "27.1.3",
31+
"typescript": "4.5.5"
32+
},
33+
"scripts": {
34+
"test": "NODE_OPTIONS=--experimental-vm-modules jest --runInBand --passWithNoTests",
35+
"build": "rm -rf dist/ && tsc"
36+
},
37+
"keywords": [
38+
"doxdox",
39+
"documentation",
40+
"template"
41+
],
42+
"jest": {
43+
"collectCoverage": true,
44+
"extensionsToTreatAsEsm": [
45+
".ts"
46+
],
47+
"globals": {
48+
"ts-jest": {
49+
"useESM": true
50+
}
51+
},
52+
"transform": {
53+
"^.+\\.ts$": "ts-jest"
54+
}
55+
},
56+
"homepage": "https://github.com/docsbydoxdox/doxdox",
57+
"repository": {
58+
"type": "git",
59+
"url": "git://github.com/docsbydoxdox/doxdox.git"
60+
}
61+
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"compilerOptions": {
3+
"target": "esnext",
4+
"module": "es2020",
5+
"esModuleInterop": true,
6+
"moduleResolution": "node",
7+
"declaration": true,
8+
"outDir": "./dist",
9+
"strict": true
10+
},
11+
"include": ["./src", "./src/.d.ts"],
12+
"exclude": ["./dist", "**/*.test.ts"]
13+
}

0 commit comments

Comments
 (0)