From 6442da93a5c4af54934d09e60830ff01af2761ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Belin?= Date: Sat, 9 Nov 2024 23:49:51 +0100 Subject: [PATCH] Use the `basedir` option to resolve the template path --- src/pug.coffee | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/pug.coffee b/src/pug.coffee index 0b506aa..935035a 100644 --- a/src/pug.coffee +++ b/src/pug.coffee @@ -1,3 +1,4 @@ +import {extname, isAbsolute, join} from "node:path" import {compile, compileFile, render, renderFile} from "pug" import {htmlToPdf} from "./puppeteer.js" @@ -5,10 +6,15 @@ import {htmlToPdf} from "./puppeteer.js" export pug = (application, rendererOptions = {}) -> renderer = Object.freeze {compile, compileFile, render, renderFile} + # Resolves the path of the specified view. + resolvePath = (view) -> + view += ".pug" unless extname view + if isAbsolute(view) or not rendererOptions.basedir? then view else join rendererOptions.basedir, view + # Renders the specified view. _render = (view, locals = {}, renderingOptions = {}) -> data = {rendererOptions..., @state..., locals...} - html = await Promise.resolve renderer.renderFile view, data + html = await Promise.resolve renderer.renderFile resolvePath(view), data if renderingOptions.writeResponse ? yes @body = html @type = "html" @@ -17,7 +23,7 @@ export pug = (application, rendererOptions = {}) -> # Renders the specified view as a PDF document. _renderPdf = (view, locals = {}, renderingOptions = {}) -> data = {rendererOptions..., @state..., locals...} - html = await Promise.resolve renderer.renderFile view, data + html = await Promise.resolve renderer.renderFile resolvePath(view), data pdf = await htmlToPdf html, browser: rendererOptions.browser, pdf: renderingOptions if renderingOptions.writeResponse ? yes @body = pdf