-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
327 additions
and
185 deletions.
There are no files selected for viewing
18 changes: 0 additions & 18 deletions
18
...ocsoc-mail-merge/output/previews/dual.white.amphibian/Kishan__nunjucks__Preview-HTML.html
This file was deleted.
Oops, something went wrong.
10 changes: 0 additions & 10 deletions
10
...erge/output/previews/dual.white.amphibian/Kishan__nunjucks__Preview-Markdown.md
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
...l-merge/src/engines/nunjucks/getFields.ts → ...erge/src/engines/nunjucks-md/getFields.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
packages/email/docsoc-mail-merge/src/engines/nunjucks-md/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { promises as fs } from "fs"; | ||
import nunjucks from "nunjucks"; | ||
|
||
import { renderMarkdownToHtml } from "../../markdown/toHtml"; | ||
import { CSVRecord } from "../../util/types"; | ||
import { TemplateEngineOptions } from "../types"; | ||
import { TemplateEngine } from "../types"; | ||
import getTemplateFields from "./getFields"; | ||
import { assertIsNunjucksTemplateOptions, NunjucksMarkdownTemplateOptions } from "./types"; | ||
|
||
/** | ||
* A Nunjucks Markdown template engine | ||
* | ||
* Given two templates: | ||
* 1. A Markdown template that contains Nunjucks variables | ||
* 2. An HTML template that will be used to wrap the rendered Markdown | ||
* | ||
* We produce two previews: | ||
* 1. A Markdown preview, where the Nunjucks variables are expanded in the Markdown template | ||
* 2. An HTML preview, where the Markdown preview is rendered to HTML and embedded in the HTML template. | ||
* | ||
* The HTML preview __must__ contain a `{{ content }}` variable that will be replaced with the rendered Markdown | ||
* | ||
* The HTML preview is what will be sent as the final email, however we provide | ||
* the Markdown preview for editing purposes - you can edit it, trigger a re-render, | ||
* and see the changes in the HTML preview that will be sent | ||
*/ | ||
export default class NunjucksMarkdownEngine extends TemplateEngine { | ||
private loadedTemplate?: string; | ||
private templateOptions: NunjucksMarkdownTemplateOptions; | ||
|
||
constructor(templateOptions: TemplateEngineOptions) { | ||
super(); | ||
assertIsNunjucksTemplateOptions(templateOptions); | ||
this.templateOptions = templateOptions; | ||
} | ||
|
||
async loadTemplate() { | ||
this.loadedTemplate = await fs.readFile(this.templateOptions.templatePath, "utf-8"); | ||
} | ||
|
||
extractFields() { | ||
if (!this.loadedTemplate) { | ||
throw new Error("Template not loaded"); | ||
} | ||
|
||
return getTemplateFields(this.loadedTemplate); | ||
} | ||
|
||
async renderPreview(record: CSVRecord) { | ||
if (!this.loadedTemplate) { | ||
throw new Error("Template not loaded"); | ||
} | ||
|
||
const templateCompiled = nunjucks.compile( | ||
this.loadedTemplate, | ||
nunjucks.configure({ | ||
throwOnUndefined: true, | ||
}), | ||
); | ||
const htmlWrapper = await fs.readFile(this.templateOptions.rootHtmlTemplate, "utf-8"); | ||
const htmlWrapperCompiled = nunjucks.compile(htmlWrapper, nunjucks.configure({ autoescape: false })); | ||
|
||
// Render the Markdown template with the record, so that we have something to preview | ||
const expanded = templateCompiled.render({ | ||
name: record["name"], | ||
}); | ||
// Render the MD to HTML | ||
const html = renderMarkdownToHtml(expanded); | ||
|
||
// Wrap the rendered markdown html in the wrapper | ||
const wrapped = htmlWrapperCompiled.render({ content: html }); | ||
|
||
// Return both | ||
return [ | ||
{ | ||
name: "Preview-Markdown.md", | ||
content: expanded, // you can edit this and re-render | ||
metadata: {}, | ||
}, | ||
{ | ||
name: "Preview-HTML.html", | ||
content: wrapped, // this is what will be sent - do not edit it, re-rendering will overwrite it | ||
metadata: {}, | ||
}, | ||
]; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
packages/email/docsoc-mail-merge/src/engines/nunjucks-md/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { TemplateEngineOptions } from "../types"; | ||
|
||
/** | ||
* Nunjucks template options | ||
*/ | ||
export interface NunjucksMarkdownTemplateOptions { | ||
/** Path to the Markdown template to use to produce __editable__ previews of emails */ | ||
templatePath: string; | ||
/** Path to the root HTML template to use to produce __final__ emails - this is what the final rendered markdown from {@link NunjucksMarkdownTemplateOptions.templatePath} will be embedded into */ | ||
rootHtmlTemplate: string; | ||
[key: string]: string; | ||
} | ||
|
||
/** | ||
* Asserts that the given options are valid Nunjucks template options & throws an error if they are not | ||
*/ | ||
export function assertIsNunjucksTemplateOptions( | ||
options: TemplateEngineOptions, | ||
): asserts options is NunjucksMarkdownTemplateOptions { | ||
if (!options["templatePath"] || typeof options["templatePath"] !== "string") { | ||
throw new Error("Invalid template option"); | ||
} | ||
if (!options["rootHtmlTemplate"] || typeof options["rootHtmlTemplate"] !== "string") { | ||
throw new Error("Invalid rootHtmlTemplate option"); | ||
} | ||
} |
67 changes: 0 additions & 67 deletions
67
packages/email/docsoc-mail-merge/src/engines/nunjucks/index.ts
This file was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
packages/email/docsoc-mail-merge/src/engines/nunjucks/types.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.