Skip to content
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

provide JavaScript Layout Templates' data and permalink class members with some useful info #1082

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Engines/JavaScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ class JavaScript extends TemplateEngine {
}
}

async getExtraDataFromFile(inputPath) {
async getExtraDataFromFile(inputPath, template) {
let inst = this.getInstanceFromInputPath(inputPath);
if (inst && "data" in inst) {
// get extra data from `data` method,
// either as a function or getter or object literal
let result = await (typeof inst.data === "function"
? inst.data()
? inst.data(inputPath, this, template)
: inst.data);
if (typeof result !== "object") {
throw new JavaScriptTemplateInvalidDataFormatError(
Expand Down
2 changes: 1 addition & 1 deletion src/Engines/TemplateEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class TemplateEngine {
return true;
}

getExtraDataFromFile(inputPath) {
getExtraDataFromFile(inputPath, template) {
return {};
}

Expand Down
29 changes: 28 additions & 1 deletion src/Template.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,15 @@ class Template extends TemplateContent {

let permalink = data[this.config.keys.permalink];
if (permalink) {
permalink = await this.mkString(permalink, data, this);
debugDev(`_getLink()::mkString() -> permalink = ${permalink}, extraOutputSubdirectory = ${this.extraOutputSubdirectory}`);
// render variables inside permalink front matter, bypass markdown
let permalinkValue;
if (!this.config.dynamicPermalinks || data.dynamicPermalink === false) {
debugDev("Not using dynamicPermalinks, using %o", permalink);
permalinkValue = permalink;
} else {
permalinkValue = await super.render(permalink, data, true);
permalinkValue = await super.render(permalink, data, /* bypassMarkdown */ true);
debug(
"Rendering permalink for %o: %s becomes %o",
this.inputPath,
Expand Down Expand Up @@ -154,6 +156,7 @@ class Template extends TemplateContent {
// TODO instead of htmlIOException, do a global search to check if output path = input path and then add extra suffix
async getOutputLink(data) {
let link = await this._getLink(data);
debugDev(`getOutputLink() -> ${link.toString()}`)
return link.toString();
}

Expand Down Expand Up @@ -733,6 +736,30 @@ class Template extends TemplateContent {
}
return contents;
}

// ripped from Engines/JavaScript/_getInstance() and tweaked:
//
// String, Buffer, Promise
// Function, Class
// Object
async mkString(mod, data) {
if (typeof mod === "string" || mod instanceof Buffer) {
return mod.toString();
}
else if (mod.then) {
debugDev(`mkString with promise: ${mod}`);
return mod.toString();
} else if (typeof mod === "function") {
//debugDev(`mkString with function: ${mod.toString()}`);
if (mod.prototype && "render" in mod.prototype) {
return await (new mod()).render(data, this.config, this);
} else {
return await mod(data, this.config, this);
}
} else if ("render" in mod) {
return await mod.render(data, this.config, this);
}
}
}

module.exports = Template;
4 changes: 2 additions & 2 deletions src/TemplateContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class TemplateContent {
await this.read();
}

let extraData = await this.engine.getExtraDataFromFile(this.inputPath);
let extraData = await this.engine.getExtraDataFromFile(this.inputPath, this);
let data = TemplateData.mergeDeep({}, this.frontMatter.data, extraData);
return TemplateData.cleanupData(data);
}
Expand Down Expand Up @@ -174,7 +174,7 @@ class TemplateContent {
debugDev("%o getCompiledTemplate function created", this.inputPath);
return fn;
} catch (e) {
debug(`Having trouble compiling template ${this.inputPath}: %O`, str);
debug(`Having trouble compiling template ${this.inputPath}: %O --> %O`, str, e);
throw new TemplateContentCompileError(
`Having trouble compiling template ${this.inputPath}`,
e
Expand Down