-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HTML File Path Support #32
Conversation
- Add a string path to an HTML template file instead of a direct template Resolves craftamap#6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh nice, thanks for taking care of the issue.
Left some small comments. :^)
src/index.ts
Outdated
const compiledTemplateFn = htmlTemplate.toString().endsWith('.html') | ||
? lodashTemplate(fs.readFileSync(htmlTemplate).toString()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let us also check if the file exists using fs.exists
.
I also think some people are still calling their html files just .htm
.
src/index.ts
Outdated
const compiledTemplateFn = htmlTemplate.toString().endsWith('.html') | ||
? lodashTemplate(fs.readFileSync(htmlTemplate).toString()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can safely convert renderTemplate to be async, and use await fs.readFile
from the promises package instead of renaming all the references of fs to promises.
In regards to both comments, |
Maybe the smartest thing would be to switch the whole import to import fs from 'fs' and use According to the docs, |
- Rework renderTemplate function a bit - Check if htmlTemplate prop exists, if it does and points to a valid file, read that file - If it doesn't read a valid file, attempt to load the raw html string, and if that's invalid, load the default html fallback - Add a logging function to indicate whether the path reads a valid file
Sounds good, let me know what you think of the latest commit. |
src/index.ts
Outdated
const template = (htmlTemplate && fs.existsSync(htmlTemplate) | ||
? await fs.promises.readFile(htmlTemplate) | ||
: htmlTemplate || '').toString() | ||
const isValid = template.startsWith('<!DOCTYPE html>') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like this is a bit too error prone. For example, the templates in the README don't work with this anymore. Can we leave this out for now? We can create a issue for checking the validity of a document in the future. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good now! I'm unsure about the doctype check, let's add a issue for this.
Thanks for your help on this issue, it's really appreciated!
Np, error check removed in latest commit. |
Let's go, will be released within the next days :) |
Resolves #6