mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-24 19:33:02 +03:00
Moved @tryghost/tpl
to Framework repo
refs 3cc8da4a7c
- the package has been moved into the framework repo in the referenced
commit, so the files can be deleted here
This commit is contained in:
parent
20aad599fd
commit
669ef01d1b
@ -1,6 +0,0 @@
|
||||
module.exports = {
|
||||
plugins: ['ghost'],
|
||||
extends: [
|
||||
'plugin:ghost/node'
|
||||
]
|
||||
};
|
@ -1,21 +0,0 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2013-2021 Ghost Foundation
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
@ -1,53 +0,0 @@
|
||||
# Tpl
|
||||
|
||||
## Install
|
||||
|
||||
`npm install @tryghost/tpl --save`
|
||||
|
||||
or
|
||||
|
||||
`yarn add @tryghost/tpl`
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
```
|
||||
const tpl = require('@tryghost/tpl');
|
||||
messages = {
|
||||
myError: 'Something terrible happened to {something}'
|
||||
};
|
||||
|
||||
console.error(tpl(messages.myError, {something: 'The thing'}));
|
||||
```
|
||||
|
||||
* Takes strings like 'Your site is now available on {url}' and interpolates them with passed in data
|
||||
* Will ignore double or triple braces like {{get}} or {{{content}}}
|
||||
* Can handle escaped braces e.g. \\\\{\\\\{{helpername}\\\\}\\\\}
|
||||
* There's a simple bare minimum escaping needed to make {{{helpername}}} work with interpolation e.g. {\\\\{{helpername}}}
|
||||
|
||||
|
||||
## Develop
|
||||
|
||||
This is a mono repository, managed with [lerna](https://lernajs.io/).
|
||||
|
||||
Follow the instructions for the top-level repo.
|
||||
1. `git clone` this repo & `cd` into it as usual
|
||||
2. Run `yarn` to install top-level dependencies.
|
||||
|
||||
|
||||
## Run
|
||||
|
||||
- `yarn dev`
|
||||
|
||||
|
||||
## Test
|
||||
|
||||
- `yarn lint` run just eslint
|
||||
- `yarn test` run lint and tests
|
||||
|
||||
|
||||
|
||||
|
||||
# Copyright & License
|
||||
|
||||
Copyright (c) 2013-2021 Ghost Foundation - Released under the [MIT license](LICENSE).
|
@ -1 +0,0 @@
|
||||
module.exports = require('./lib/tpl');
|
@ -1,27 +0,0 @@
|
||||
const template = require('lodash.template');
|
||||
const interpolate = /(?<!{){([^{]+?)}/g;
|
||||
|
||||
/**
|
||||
* Template function
|
||||
* Takes strings like 'Your site is now available on {url}' and interpolates them with passed in data
|
||||
* Will ignore double or triple braces like {{get}} or {{{helpername}}}
|
||||
* Can handle escaped braces e.g. \\{\\{{helpername}\\}\\}
|
||||
* But there's a simple bare minimum escaping needed to make {{{helpername}}} work e.g. {\\{{helpername}}}
|
||||
*
|
||||
*
|
||||
* @param {String} string - string with optional {data properties}
|
||||
* @param {Object} [data] - optional data to interpolate
|
||||
* @returns {string} the interpolated string
|
||||
*/
|
||||
module.exports = (string, data) => {
|
||||
if (!data) {
|
||||
return string;
|
||||
}
|
||||
|
||||
// We replace any escaped left braces with the unicode character so we can swap it back later
|
||||
let processedString = string.replace(/\\{/g, '\\U+007B');
|
||||
// Let lodash do its thing
|
||||
processedString = template(processedString, {interpolate})(data);
|
||||
// Replace our swapped out left braces and any escaped right braces
|
||||
return processedString.replace(/\\U\+007B/g, '{').replace(/\\}/g, '}');
|
||||
};
|
@ -1,30 +0,0 @@
|
||||
{
|
||||
"name": "@tryghost/tpl",
|
||||
"version": "0.1.5",
|
||||
"repository": "https://github.com/TryGhost/Utils/tree/master/packages/tpl",
|
||||
"author": "Ghost Foundation",
|
||||
"license": "MIT",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"dev": "echo \"Implement me!\"",
|
||||
"test": "NODE_ENV=testing c8 --check-coverage mocha './test/**/*.test.js'",
|
||||
"lint": "eslint . --ext .js --cache",
|
||||
"posttest": "yarn lint"
|
||||
},
|
||||
"files": [
|
||||
"index.js",
|
||||
"lib"
|
||||
],
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
"devDependencies": {
|
||||
"c8": "7.9.0",
|
||||
"mocha": "9.1.2",
|
||||
"should": "13.2.3",
|
||||
"sinon": "11.1.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"lodash.template": "^4.5.0"
|
||||
}
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
module.exports = {
|
||||
plugins: ['ghost'],
|
||||
extends: [
|
||||
'plugin:ghost/test'
|
||||
]
|
||||
};
|
@ -1,13 +0,0 @@
|
||||
// Switch these lines once there are useful utils
|
||||
// const testUtils = require('./utils');
|
||||
require('./utils');
|
||||
|
||||
describe('Lodash Template', function () {
|
||||
it('Does not get clobbered by this lib', function () {
|
||||
require('../lib/tpl');
|
||||
let _ = require('lodash');
|
||||
|
||||
// @ts-ignore
|
||||
_.templateSettings.interpolate.should.eql(/<%=([\s\S]+?)%>/g);
|
||||
});
|
||||
});
|
@ -1,96 +0,0 @@
|
||||
// Switch these lines once there are useful utils
|
||||
// const testUtils = require('./utils');
|
||||
require('./utils');
|
||||
const tpl = require('../');
|
||||
|
||||
describe('tpl', function () {
|
||||
it('Can handle a plain string', function () {
|
||||
const string = 'My template';
|
||||
const result = tpl(string);
|
||||
|
||||
result.should.eql('My template');
|
||||
});
|
||||
|
||||
it('Can handle a string with data', function () {
|
||||
const string = 'Go visit {url}';
|
||||
const data = {url: 'https://example.com'};
|
||||
|
||||
let result = tpl(string, data);
|
||||
|
||||
result.should.eql('Go visit https://example.com');
|
||||
});
|
||||
|
||||
it('Can mix interpolation handlebars in the same message', function () {
|
||||
const string = '{{#get}} helper took {totalMs}ms to complete';
|
||||
const data = {
|
||||
totalMs: '500'
|
||||
};
|
||||
|
||||
let result = tpl(string, data);
|
||||
result.should.eql('{{#get}} helper took 500ms to complete');
|
||||
});
|
||||
|
||||
it('Can mix interpolation with handlebars-block helpers without escaping', function () {
|
||||
const string = '{{#{helperName}}} helper took {totalMs}ms to complete';
|
||||
|
||||
const data = {
|
||||
helperName: 'get',
|
||||
totalMs: '500'
|
||||
};
|
||||
|
||||
let result = tpl(string, data);
|
||||
result.should.eql('{{#get}} helper took 500ms to complete');
|
||||
});
|
||||
|
||||
it('ignores 3 braces', function () {
|
||||
const string = 'The {{{helperName}}} helper is not available.';
|
||||
const data = {
|
||||
helperName: 'get',
|
||||
totalMs: '500'
|
||||
};
|
||||
let result = tpl(string, data);
|
||||
result.should.eql('The {{{helperName}}} helper is not available.');
|
||||
});
|
||||
|
||||
it('has a simple bare minimum escaping needed', function () {
|
||||
const string = 'The {\\{{helperName}}} helper is not available.';
|
||||
const data = {
|
||||
helperName: 'get',
|
||||
totalMs: '500'
|
||||
};
|
||||
let result = tpl(string, data);
|
||||
result.should.eql('The {{get}} helper is not available.');
|
||||
});
|
||||
|
||||
it('Can handle escaped left braces', function () {
|
||||
const string = 'The \\{\\{{helperName}}} helper is not available.';
|
||||
const data = {
|
||||
helperName: 'get',
|
||||
totalMs: '500'
|
||||
};
|
||||
let result = tpl(string, data);
|
||||
result.should.eql('The {{get}} helper is not available.');
|
||||
});
|
||||
|
||||
it('Can handle escaped right braces as well', function () {
|
||||
const string = 'The \\{\\{{helperName}\\}\\} helper is not available.';
|
||||
const data = {
|
||||
helperName: 'get',
|
||||
totalMs: '500'
|
||||
};
|
||||
let result = tpl(string, data);
|
||||
result.should.eql('The {{get}} helper is not available.');
|
||||
});
|
||||
|
||||
it('Returns a sensible error if data is missing', function () {
|
||||
const string = '{helperName} helper took {totalMs}ms to complete';
|
||||
const data = {
|
||||
totalMs: '500'
|
||||
};
|
||||
|
||||
let resultFn = () => {
|
||||
tpl(string, data);
|
||||
};
|
||||
resultFn.should.throw('helperName is not defined');
|
||||
});
|
||||
});
|
@ -1,11 +0,0 @@
|
||||
/**
|
||||
* Custom Should Assertions
|
||||
*
|
||||
* Add any custom assertions to this file.
|
||||
*/
|
||||
|
||||
// Example Assertion
|
||||
// should.Assertion.add('ExampleAssertion', function () {
|
||||
// this.params = {operator: 'to be a valid Example Assertion'};
|
||||
// this.obj.should.be.an.Object;
|
||||
// });
|
@ -1,11 +0,0 @@
|
||||
/**
|
||||
* Test Utilities
|
||||
*
|
||||
* Shared utils for writing tests
|
||||
*/
|
||||
|
||||
// Require overrides - these add globals for tests
|
||||
require('./overrides');
|
||||
|
||||
// Require assertions - adds custom should assertions
|
||||
require('./assertions');
|
@ -1,10 +0,0 @@
|
||||
// This file is required before any test is run
|
||||
|
||||
// Taken from the should wiki, this is how to make should global
|
||||
// Should is a global in our eslint test config
|
||||
global.should = require('should').noConflict();
|
||||
should.extend();
|
||||
|
||||
// Sinon is a simple case
|
||||
// Sinon is a global in our eslint test config
|
||||
global.sinon = require('sinon');
|
Loading…
Reference in New Issue
Block a user