Skip to content

Commit

Permalink
Use the basedir option to resolve the template path
Browse files Browse the repository at this point in the history
  • Loading branch information
cedx committed Nov 9, 2024
1 parent 08b74cc commit 6442da9
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/pug.coffee
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import {extname, isAbsolute, join} from "node:path"
import {compile, compileFile, render, renderFile} from "pug"
import {htmlToPdf} from "./puppeteer.js"

# Attaches a view renderer to the context of the specified application.
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"
Expand All @@ -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
Expand Down

0 comments on commit 6442da9

Please sign in to comment.