1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-03-03 15:32:30 +02:00
joplin/ReactNativeClient/lib/TemplateUtils.js
Laurent Cozic 71efff6827
Linter update (#1777)
* Update eslint config

* Applied linter to lib

* Applied eslint config to CliClient/app

* Removed prettier due to https://github.com/prettier/prettier/pull/4765

* First pass on test units

* Applied linter config to test units

* Applied eslint config to clipper

* Applied to plugin dir

* Applied to root of ElectronClient

* Applied on RN root

* Applied on CLI root

* Applied on Clipper root

* Applied config to tools

* test hook

* test hook

* test hook

* Added pre-commit hook

* Applied rule no-trailing-spaces

* Make sure root packages are installed when installing sub-dir

* Added doc
2019-07-30 09:35:42 +02:00

63 lines
1.7 KiB
JavaScript

const { shim } = require('lib/shim.js');
const { time } = require('lib/time-utils.js');
const Mustache = require('mustache');
const TemplateUtils = {};
// new template variables can be added here
// If there are too many, this should be moved to a new file
const view = {
date: time.formatMsToLocal(new Date().getTime(), time.dateFormat()),
time: time.formatMsToLocal(new Date().getTime(), time.timeFormat()),
datetime: time.formatMsToLocal(new Date().getTime()),
custom_datetime: () => {
return (text, render) => {
return render(time.formatMsToLocal(new Date().getTime(), text));
};
},
};
// Mustache escapes strings (including /) with the html code by default
// This isn't useful for markdown so it's disabled
Mustache.escape = text => {
return text;
};
TemplateUtils.render = function(input) {
return Mustache.render(input, view);
};
TemplateUtils.loadTemplates = async function(filePath) {
let templates = [];
let files = [];
if (await shim.fsDriver().exists(filePath)) {
try {
files = await shim.fsDriver().readDirStats(filePath);
} catch (error) {
let msg = error.message ? error.message : '';
msg = 'Could not read template names from ' + filePath + '\n' + msg;
error.message = msg;
throw error;
}
files.forEach(async file => {
if (file.path.endsWith('.md')) {
try {
let fileString = await shim.fsDriver().readFile(filePath + '/' + file.path, 'utf-8');
templates.push({ label: file.path, value: fileString });
} catch (error) {
let msg = error.message ? error.message : '';
msg = 'Could not load template ' + file.path + '\n' + msg;
error.message = msg;
throw error;
}
}
});
}
return templates;
};
module.exports = TemplateUtils;