@cenk1cenk2/md-printer 2.16.1 → 2.17.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/dev.js +1 -1
- package/dist/commands/index.mjs +58 -5
- package/dist/interfaces/pupeteer.mjs +1 -0
- package/oclif.manifest.json +7 -1
- package/package.json +10 -10
package/bin/dev.js
CHANGED
|
@@ -16,7 +16,7 @@ async function main() {
|
|
|
16
16
|
tsNode.register({ project })
|
|
17
17
|
tsConfigPaths.register({
|
|
18
18
|
baseUrl: dirname(project),
|
|
19
|
-
paths: await import(project, {
|
|
19
|
+
paths: await import(project, { with: { type: 'json' } }).then((json) => {
|
|
20
20
|
return json.default.compilerOptions.paths
|
|
21
21
|
})
|
|
22
22
|
})
|
package/dist/commands/index.mjs
CHANGED
|
@@ -9,11 +9,13 @@ import { serveDirectory } from "md-to-pdf/dist/lib/serve-dir.js";
|
|
|
9
9
|
import Nunjucks from "nunjucks";
|
|
10
10
|
import { basename, dirname, extname, isAbsolute, join } from "path";
|
|
11
11
|
import postcss from "postcss";
|
|
12
|
+
import * as Puppeteer from "puppeteer";
|
|
12
13
|
import puppeteer from "puppeteer";
|
|
13
14
|
import showdown from "showdown";
|
|
14
15
|
import { Args, Command, ConfigService, FileSystemService, Flags, JsonParser, MergeStrategy, ParserService, YamlParser, merge } from "@cenk1cenk2/oclif-common";
|
|
15
16
|
|
|
16
17
|
//#region src/commands/index.ts
|
|
18
|
+
const { parsePDFOptions, unitToPixels } = Puppeteer;
|
|
17
19
|
var MDPrinter = class extends Command {
|
|
18
20
|
static description = "Generates a PDF from the given markdown file with the selected HTML template.";
|
|
19
21
|
static flags = {
|
|
@@ -61,7 +63,8 @@ var MDPrinter = class extends Command {
|
|
|
61
63
|
dev: Flags.boolean({
|
|
62
64
|
char: "d",
|
|
63
65
|
description: "Run with Chrome browser instead of publishing the file."
|
|
64
|
-
})
|
|
66
|
+
}),
|
|
67
|
+
continuous: Flags.boolean({ description: "Render PDF as a single continuous page without page breaks." })
|
|
65
68
|
};
|
|
66
69
|
static args = {
|
|
67
70
|
file: Args.string({ description: "File to be processed." }),
|
|
@@ -131,6 +134,7 @@ var MDPrinter = class extends Command {
|
|
|
131
134
|
document_title: ctx.metadata?.document_title ?? this.flags.title ?? this.args.file,
|
|
132
135
|
launch_options: { executablePath: this.flags.browser }
|
|
133
136
|
}]);
|
|
137
|
+
ctx.renderContinuous = this.flags.continuous || ctx.metadata?.continuous === true || ctx.options.continuous === true;
|
|
134
138
|
this.flags.stdout ??= ctx.metadata.stdout;
|
|
135
139
|
if (this.flags.stdout) ctx.options.dest = "stdout";
|
|
136
140
|
if (this.flags["output-filetype"] === "html" || ctx.metadata["output-filetype"] === "html") ctx.options.as_html = true;
|
|
@@ -178,12 +182,61 @@ var MDPrinter = class extends Command {
|
|
|
178
182
|
await this.runMd2Pdf(ctx);
|
|
179
183
|
if (this.browser) await this.browser.close();
|
|
180
184
|
}
|
|
185
|
+
continuousBrowser(browser) {
|
|
186
|
+
return new Proxy(browser, { get: (target, property, receiver) => {
|
|
187
|
+
if (property !== "newPage") {
|
|
188
|
+
const value = Reflect.get(target, property, receiver);
|
|
189
|
+
return typeof value === "function" ? value.bind(target) : value;
|
|
190
|
+
}
|
|
191
|
+
return async () => {
|
|
192
|
+
const page = await browser.newPage();
|
|
193
|
+
const pdf = page.pdf.bind(page);
|
|
194
|
+
page.pdf = async (pdfOptions = {}) => {
|
|
195
|
+
const parsed = parsePDFOptions(pdfOptions, "cm");
|
|
196
|
+
const width = parsed.landscape ? parsed.height : parsed.width;
|
|
197
|
+
const margin = parsed.margin;
|
|
198
|
+
const viewport = page.viewport();
|
|
199
|
+
await page.setViewport({
|
|
200
|
+
width: Math.ceil(Math.max(width - margin.left - margin.right, 1) * unitToPixels.cm),
|
|
201
|
+
height: viewport?.height ?? 800,
|
|
202
|
+
deviceScaleFactor: viewport?.deviceScaleFactor ?? 1
|
|
203
|
+
});
|
|
204
|
+
const contentHeight = await page.evaluate(async () => {
|
|
205
|
+
if ("fonts" in document) await document.fonts.ready;
|
|
206
|
+
await Promise.all(Array.from(document.images).map((image) => {
|
|
207
|
+
if (image.complete) return;
|
|
208
|
+
return new Promise((resolve) => {
|
|
209
|
+
image.addEventListener("load", resolve, { once: true });
|
|
210
|
+
image.addEventListener("error", resolve, { once: true });
|
|
211
|
+
});
|
|
212
|
+
}));
|
|
213
|
+
const bodyTop = document.body.getBoundingClientRect().top;
|
|
214
|
+
const bottom = Math.max(document.body.getBoundingClientRect().bottom, ...Array.from(document.body.querySelectorAll("*")).map((element) => element.getBoundingClientRect().bottom));
|
|
215
|
+
return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight, document.body.getBoundingClientRect().height, document.documentElement.getBoundingClientRect().height, bottom - bodyTop);
|
|
216
|
+
});
|
|
217
|
+
const height = parsePDFOptions({ height: Math.ceil(contentHeight + unitToPixels.cm) }, "cm").height + margin.top + margin.bottom;
|
|
218
|
+
const options = {
|
|
219
|
+
...pdfOptions,
|
|
220
|
+
width: `${width}cm`,
|
|
221
|
+
height: `${height}cm`,
|
|
222
|
+
landscape: false
|
|
223
|
+
};
|
|
224
|
+
delete options.format;
|
|
225
|
+
return pdf(options);
|
|
226
|
+
};
|
|
227
|
+
return page;
|
|
228
|
+
};
|
|
229
|
+
} });
|
|
230
|
+
}
|
|
181
231
|
async runMd2Pdf(ctx) {
|
|
182
232
|
const options = merge(MergeStrategy.EXTEND, { basedir: process.cwd() }, defaultConfig, ctx.options, { devtools: this.flags.dev });
|
|
183
|
-
this.browser
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
233
|
+
if (!this.browser) {
|
|
234
|
+
const browser = await puppeteer.launch({
|
|
235
|
+
devtools: options.devtools,
|
|
236
|
+
...options.launch_options
|
|
237
|
+
});
|
|
238
|
+
this.browser = ctx.renderContinuous ? this.continuousBrowser(browser) : browser;
|
|
239
|
+
}
|
|
187
240
|
const server = await serveDirectory(ctx.options);
|
|
188
241
|
options.port = server.address().port;
|
|
189
242
|
let output;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
package/oclif.manifest.json
CHANGED
|
@@ -139,6 +139,12 @@
|
|
|
139
139
|
"name": "dev",
|
|
140
140
|
"allowNo": false,
|
|
141
141
|
"type": "boolean"
|
|
142
|
+
},
|
|
143
|
+
"continuous": {
|
|
144
|
+
"description": "Render PDF as a single continuous page without page breaks.",
|
|
145
|
+
"name": "continuous",
|
|
146
|
+
"allowNo": false,
|
|
147
|
+
"type": "boolean"
|
|
142
148
|
}
|
|
143
149
|
},
|
|
144
150
|
"hasDynamicHelp": false,
|
|
@@ -151,5 +157,5 @@
|
|
|
151
157
|
"enableJsonFlag": false
|
|
152
158
|
}
|
|
153
159
|
},
|
|
154
|
-
"version": "2.
|
|
160
|
+
"version": "2.17.0"
|
|
155
161
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cenk1cenk2/md-printer",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.17.1",
|
|
4
4
|
"description": "A markdown printer.",
|
|
5
5
|
"module": "dist/index.mjs",
|
|
6
6
|
"type": "module",
|
|
@@ -92,32 +92,32 @@
|
|
|
92
92
|
"yaml": "^2.8.2"
|
|
93
93
|
},
|
|
94
94
|
"devDependencies": {
|
|
95
|
-
"@cenk1cenk2/cz-cc": "^2.2.
|
|
96
|
-
"@cenk1cenk2/eslint-config": "^3.2.
|
|
95
|
+
"@cenk1cenk2/cz-cc": "^2.2.20",
|
|
96
|
+
"@cenk1cenk2/eslint-config": "^3.2.6",
|
|
97
97
|
"@tsconfig/node24": "^24.0.4",
|
|
98
98
|
"@types/fs-extra": "^11.0.4",
|
|
99
|
-
"@types/node": "^24.
|
|
99
|
+
"@types/node": "^24.13.2",
|
|
100
100
|
"@types/nunjucks": "^3.2.6",
|
|
101
101
|
"@types/showdown": "^2.0.6",
|
|
102
|
-
"eslint": "^10.
|
|
102
|
+
"eslint": "^10.6.0",
|
|
103
103
|
"execa": "^9.6.1",
|
|
104
104
|
"globby": "^16.2.0",
|
|
105
105
|
"lint-staged": "^16.4.0",
|
|
106
|
-
"oclif": "^4.23.
|
|
107
|
-
"prettier": "^3.
|
|
106
|
+
"oclif": "^4.23.24",
|
|
107
|
+
"prettier": "^3.9.3",
|
|
108
108
|
"prettier-plugin-jinja-template": "^2.2.0",
|
|
109
109
|
"simple-git-hooks": "^2.13.1",
|
|
110
110
|
"theme-colors": "^0.1.0",
|
|
111
111
|
"tpm": "^1.4.14",
|
|
112
112
|
"ts-node": "^10.9.2",
|
|
113
113
|
"tsconfig-paths": "^4.2.0",
|
|
114
|
-
"tsdown": "^0.
|
|
115
|
-
"typescript": "^
|
|
114
|
+
"tsdown": "^0.22.3",
|
|
115
|
+
"typescript": "^6.0.3"
|
|
116
116
|
},
|
|
117
117
|
"config": {
|
|
118
118
|
"commitizen": {
|
|
119
119
|
"path": "./node_modules/@cenk1cenk2/cz-cc"
|
|
120
120
|
}
|
|
121
121
|
},
|
|
122
|
-
"packageManager": "pnpm@
|
|
122
|
+
"packageManager": "pnpm@11.9.0"
|
|
123
123
|
}
|