@perfood/couch-auth
Version:
Easy and secure authentication for CouchDB/Cloudant. Based on SuperLogin, updated and rewritten in Typescript.
58 lines (57 loc) • 2.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseCompositeTemplate = parseCompositeTemplate;
exports.parseTemplatesDirectly = parseTemplatesDirectly;
const nunjucks_1 = require("nunjucks");
const path_1 = require("path");
/**
* @internal
* Converts basic inline markdown formatting (urls, bold and italic) with
* regular expressions.
* Note that they must be greedy (*?) to format the markdown correctly.
* @param text the markdown text to process
*/
function processMarkdown(text) {
return text
.replace(/(\s)\*\*(.*?)\*\*(\s)/gim, '$1<b>$2</b>$3')
.replace(/(\s)\_(.*?)\_(\s)/gim, '$1<i>$2</i>$3')
.replace(/\[(.*?)\]\((.*?)\)/gim, '<a style="text-decoration: underline; color: #193b92" href="$2">$1</a>')
.replace(/(\s)\*(.*?)\*(\s)/gim, '$1<i>$2</i>$3');
}
/**
* @internal
* Combines a base html template and the content, returning both a html and
* plain text version of the email
*
* @param folderPath template directory
* @param template plain text template whose paragraphs will be formatted into the base template. `.njk` gets appended.
* @param data data that should be passed to the template.
* @param base the base template. Default: `base.njk`
* @returns
*/
function parseCompositeTemplate(folderPath, template, data = {}, base = 'base.njk') {
const baseTemplate = (0, path_1.join)(folderPath, base);
const contentPath = (0, path_1.join)(folderPath, `${template}.njk`);
const plainTextContent = (0, nunjucks_1.render)(contentPath, data);
const contentParagraphs = processMarkdown(plainTextContent).split('\n');
data.paragraphs = contentParagraphs;
return { html: (0, nunjucks_1.render)(baseTemplate, data), text: plainTextContent };
}
/**
* Fallback method: If the `${template}.njk` is not present, this will be called
* to directly render the html (`${template}.html.njk`) and plain text
* (`${template}.text.njk`) templates without additional logic.
*/
function parseTemplatesDirectly(folderPath, template, data = {}) {
let html;
try {
html = (0, nunjucks_1.render)((0, path_1.join)(folderPath, `${template}.html.njk`), data);
}
catch (error) { }
let text;
try {
text = (0, nunjucks_1.render)((0, path_1.join)(folderPath, `${template}.text.njk`), data);
}
catch (error) { }
return { html, text };
}