Skip to content

Commit 076874f

Browse files
committed
Allow custom template engines
1 parent b0148bd commit 076874f

4 files changed

Lines changed: 11 additions & 20 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ npm install pdf-made-easy
6363

6464
### CLI
6565

66-
- [ ] Custom template engine via a `getTemplateRenderer` method in the config file
66+
- [x] Custom template engine via a `getTemplateRenderer` method in the config file
6767
- [ ] Auto-reloading preview of the generated PDF via a `--preview` or `-p` option
6868
- [ ] Using a separate stylesheet via a `--style` or `-s` option
6969
- [ ] Serving local assets for PDF embedding via `--serve` and `--port` options (ideas for implementation: [1](https://stackoverflow.com/a/67505480), [2](https://github.com/puppeteer/puppeteer/issues/1643))

cli.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@ import { findUp, pathExists } from "find-up";
99
import { hideBin } from "yargs/helpers";
1010
import yargs from "yargs/yargs";
1111

12-
import {
13-
absolutizePath,
14-
build,
15-
develop,
16-
getDefaultTemplateRenderer
17-
} from "./index.js";
12+
import { absolutizePath, build, develop } from "./index.js";
1813

1914
const DEFAULT_CONFIG_FILENAME = "pme.config.mjs";
2015

@@ -81,8 +76,7 @@ async function handle(args) {
8176
try {
8277
await (command === "build" ? build : develop)({
8378
...args,
84-
options: await getConfig(config, DEFAULT_CONFIG_FILENAME),
85-
getTemplateRenderer: getDefaultTemplateRenderer
79+
options: await getConfig(config, DEFAULT_CONFIG_FILENAME)
8680
});
8781
} catch ({ message }) {
8882
console.error(`Error: ${message}`);

index.d.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ export interface PMEConfig<T = LiquidOptions> {
4343
* The options passed to {@link PDFRenderer.render}.
4444
*/
4545
pdfOptions?: PDFOptions;
46+
47+
/**
48+
* Sets up and returns a function that renders HTML from a template string and data
49+
* object.
50+
*/
51+
getTemplateRenderer?(options?: T): RenderTemplate;
4652
}
4753

4854
/**
@@ -54,12 +60,6 @@ export interface CommandArgs<T = LiquidOptions> extends BaseArgs {
5460
* The options passed down to the template and PDF renderers.
5561
*/
5662
options?: PMEConfig<T>;
57-
58-
/**
59-
* Sets up and returns a function that renders HTML from a template string and data
60-
* object.
61-
*/
62-
getTemplateRenderer?(options?: T): RenderTemplate;
6363
}
6464

6565
/**

index.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,7 @@ export async function getBuilder() {
9292
let isClosed = false;
9393

9494
return {
95-
async build(
96-
{ data, template, output, options, getTemplateRenderer },
97-
rootDir = process.cwd()
98-
) {
95+
async build({ data, template, output, options }, rootDir = process.cwd()) {
9996
if (isClosed) {
10097
return;
10198
}
@@ -112,7 +109,7 @@ export async function getBuilder() {
112109

113110
const html = await renderHTML(
114111
absolutizePath(template, rootDir),
115-
(getTemplateRenderer ?? getDefaultTemplateRenderer)(
112+
(options?.getTemplateRenderer ?? getDefaultTemplateRenderer)(
116113
// TODO: Fix type error
117114
// @ts-expect-error
118115
options?.templateOptions

0 commit comments

Comments
 (0)