@financial-times/dotcom-server-handlebars 7.2.7 → 7.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/node/PageKitHandlebars.d.ts +46 -0
- package/dist/node/findPartialFiles.d.ts +2 -0
- package/dist/node/helpers/array.d.ts +1 -0
- package/dist/node/helpers/capture.d.ts +2 -0
- package/dist/node/helpers/concat.d.ts +1 -0
- package/dist/node/helpers/dateformat.d.ts +1 -0
- package/dist/node/helpers/encode.d.ts +1 -0
- package/dist/node/helpers/ifAll.d.ts +1 -0
- package/dist/node/helpers/ifEquals.d.ts +1 -0
- package/dist/node/helpers/ifEqualsSome.d.ts +1 -0
- package/dist/node/helpers/ifSome.d.ts +1 -0
- package/dist/node/helpers/json.d.ts +1 -0
- package/dist/node/helpers/renderReactComponent.d.ts +3 -0
- package/dist/node/helpers/resize.d.ts +1 -0
- package/dist/node/helpers/slice.d.ts +1 -0
- package/dist/node/helpers/unlessAll.d.ts +1 -0
- package/dist/node/helpers/unlessEquals.d.ts +1 -0
- package/dist/node/helpers/unlessSome.d.ts +1 -0
- package/dist/node/helpers.d.ts +16 -0
- package/dist/node/index.d.ts +3 -0
- package/dist/node/loadFileContents.d.ts +1 -0
- package/dist/tsconfig.tsbuildinfo +3320 -0
- package/package.json +6 -4
- package/src/PageKitHandlebars.ts +0 -142
- package/src/findPartialFiles.ts +0 -25
- package/src/helpers/README.md +0 -213
- package/src/helpers/array.ts +0 -8
- package/src/helpers/capture.ts +0 -9
- package/src/helpers/concat.ts +0 -8
- package/src/helpers/dateformat.ts +0 -13
- package/src/helpers/encode.ts +0 -15
- package/src/helpers/ifAll.ts +0 -12
- package/src/helpers/ifEquals.ts +0 -12
- package/src/helpers/ifEqualsSome.ts +0 -13
- package/src/helpers/ifSome.ts +0 -12
- package/src/helpers/json.ts +0 -16
- package/src/helpers/renderReactComponent.ts +0 -34
- package/src/helpers/resize.ts +0 -20
- package/src/helpers/slice.ts +0 -22
- package/src/helpers/unlessAll.ts +0 -12
- package/src/helpers/unlessEquals.ts +0 -12
- package/src/helpers/unlessSome.ts +0 -12
- package/src/helpers.ts +0 -16
- package/src/index.ts +0 -3
- package/src/loadFileContents.ts +0 -5
- package/src/types.d.ts +0 -11
package/package.json
CHANGED
|
@@ -1,16 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@financial-times/dotcom-server-handlebars",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.3.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/node/index.js",
|
|
6
6
|
"types": "src/index.ts",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
9
|
-
"tsc": "../../node_modules/.bin/tsc --incremental",
|
|
10
9
|
"clean": "npm run clean:dist && npm run clean:node_modules",
|
|
11
10
|
"clean:dist": "rm -rf dist",
|
|
12
11
|
"clean:node_modules": "rm -rf node_modules",
|
|
13
|
-
"build:node": "
|
|
12
|
+
"build:node": "tsc",
|
|
14
13
|
"build": "npm run build:node",
|
|
15
14
|
"dev": "npm run build:node -- --watch",
|
|
16
15
|
"preinstall": "[ \"$INIT_CWD\" != \"$PWD\" ] || npm_config_yes=true npx check-engine"
|
|
@@ -31,6 +30,9 @@
|
|
|
31
30
|
"node": ">= 14.0.0",
|
|
32
31
|
"npm": "7.x || 8.x"
|
|
33
32
|
},
|
|
33
|
+
"files": [
|
|
34
|
+
"dist/"
|
|
35
|
+
],
|
|
34
36
|
"repository": {
|
|
35
37
|
"type": "git",
|
|
36
38
|
"repository": "https://github.com/Financial-Times/dotcom-page-kit.git",
|
|
@@ -43,4 +45,4 @@
|
|
|
43
45
|
"devDependencies": {
|
|
44
46
|
"check-engine": "^1.10.1"
|
|
45
47
|
}
|
|
46
|
-
}
|
|
48
|
+
}
|
package/src/PageKitHandlebars.ts
DELETED
|
@@ -1,142 +0,0 @@
|
|
|
1
|
-
import path from 'path'
|
|
2
|
-
import mixinDeep from 'mixin-deep'
|
|
3
|
-
import Handlebars, { HelperDelegate, TemplateDelegate } from 'handlebars'
|
|
4
|
-
import findPartialFiles from './findPartialFiles'
|
|
5
|
-
import loadFileContents from './loadFileContents'
|
|
6
|
-
import { TRenderCallback, TPartialTemplates, TFilePaths } from './types'
|
|
7
|
-
|
|
8
|
-
export type TPageKitHandlebarsOptions = {
|
|
9
|
-
/**
|
|
10
|
-
* An instance of Handlebars to use.
|
|
11
|
-
* @default require('handlebars')
|
|
12
|
-
*/
|
|
13
|
-
handlebars?: typeof Handlebars
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Current working directory.
|
|
17
|
-
* @default process.cwd()
|
|
18
|
-
*/
|
|
19
|
-
rootDirectory?: string
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Additional helper functions to register with Handlebars.
|
|
23
|
-
* @default {}
|
|
24
|
-
*/
|
|
25
|
-
helpers?: {
|
|
26
|
-
[key: string]: HelperDelegate
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Partial templates to register with Handlebars.
|
|
31
|
-
* @default {}
|
|
32
|
-
*/
|
|
33
|
-
partials?: TPartialTemplates
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* A list of directories and patterns used to dynamically find and load partial template files.
|
|
37
|
-
* @default { './views/partials': '**\/*.{hbs,html}' }
|
|
38
|
-
*/
|
|
39
|
-
partialPaths?: TFilePaths
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Enable caching of template files to reduce filesystem I/O
|
|
43
|
-
* @default process.env.NODE_ENV !== 'development
|
|
44
|
-
*/
|
|
45
|
-
cache?: boolean
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// By default NODE_ENV will be undefined so be explicit
|
|
49
|
-
const nodeEnv = process.env.NODE_ENV || 'development'
|
|
50
|
-
|
|
51
|
-
const defaultOptions: TPageKitHandlebarsOptions = {
|
|
52
|
-
cache: nodeEnv !== 'development',
|
|
53
|
-
rootDirectory: process.cwd(),
|
|
54
|
-
helpers: {},
|
|
55
|
-
partials: {},
|
|
56
|
-
partialPaths: {
|
|
57
|
-
'./views/partials': '**/*.{hbs,html}',
|
|
58
|
-
'./bower_components': '*/{templates,server/templates,components,partials,views}/**/*.{hbs,html}',
|
|
59
|
-
'./node_modules/@financial-times':
|
|
60
|
-
'*/{templates,server/templates,components,partials,views}/**/*.{hbs,html}'
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export class PageKitHandlebars {
|
|
65
|
-
public options: TPageKitHandlebarsOptions
|
|
66
|
-
public engine: this['renderView']
|
|
67
|
-
|
|
68
|
-
private cache: Map<string, any> = new Map()
|
|
69
|
-
|
|
70
|
-
constructor(userOptions: TPageKitHandlebarsOptions = {}) {
|
|
71
|
-
this.options = mixinDeep({}, defaultOptions, userOptions)
|
|
72
|
-
|
|
73
|
-
// Create a point for Express to mount as a view engine
|
|
74
|
-
this.engine = this.renderView.bind(this)
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
loadPartials(): TPartialTemplates {
|
|
78
|
-
let partials: TFilePaths = this.cache.get('__partials__')
|
|
79
|
-
|
|
80
|
-
if (partials === undefined) {
|
|
81
|
-
partials = findPartialFiles(this.options.rootDirectory, this.options.partialPaths)
|
|
82
|
-
|
|
83
|
-
if (this.options.cache) {
|
|
84
|
-
this.cache.set('__partials__', partials)
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
const templates = {}
|
|
89
|
-
|
|
90
|
-
Object.keys(partials).forEach((name) => {
|
|
91
|
-
const filePath = partials[name]
|
|
92
|
-
templates[name] = this.loadTemplate(filePath)
|
|
93
|
-
})
|
|
94
|
-
|
|
95
|
-
return templates
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
loadTemplate(filePath: string): TemplateDelegate {
|
|
99
|
-
if (path.isAbsolute(filePath) === false) {
|
|
100
|
-
filePath = path.resolve(this.options.rootDirectory, filePath)
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
let template: TemplateDelegate = this.cache.get(filePath)
|
|
104
|
-
|
|
105
|
-
if (template === undefined) {
|
|
106
|
-
const contents = loadFileContents(filePath)
|
|
107
|
-
const hbs = this.options.handlebars || Handlebars
|
|
108
|
-
|
|
109
|
-
template = hbs.compile(contents)
|
|
110
|
-
|
|
111
|
-
if (this.options.cache) {
|
|
112
|
-
this.cache.set(filePath, template)
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
return template
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
render(template: string | TemplateDelegate, templateContext: any): string {
|
|
120
|
-
if (typeof template === 'string') {
|
|
121
|
-
template = this.loadTemplate(template)
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
const html = template(templateContext, {
|
|
125
|
-
helpers: this.options.helpers,
|
|
126
|
-
partials: { ...this.options.partials, ...this.loadPartials() }
|
|
127
|
-
})
|
|
128
|
-
|
|
129
|
-
return html.trim()
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
// This method is intended to be mounted by Express and used as a view engine.
|
|
133
|
-
// <https://expressjs.com/en/guide/using-template-engines.html>
|
|
134
|
-
renderView(templatePath: string, templateContext: any, callback: TRenderCallback): void {
|
|
135
|
-
try {
|
|
136
|
-
const html = this.render(templatePath, templateContext)
|
|
137
|
-
callback(null, html)
|
|
138
|
-
} catch (error) {
|
|
139
|
-
callback(error)
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
}
|
package/src/findPartialFiles.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import path from 'path'
|
|
2
|
-
import glob from 'glob'
|
|
3
|
-
import { TFilePaths } from './types'
|
|
4
|
-
|
|
5
|
-
export default function loadPartialFiles(cwd: string, partialPaths: TFilePaths): TFilePaths {
|
|
6
|
-
const partialFiles = {}
|
|
7
|
-
|
|
8
|
-
Object.keys(partialPaths).forEach((partialPath) => {
|
|
9
|
-
const globPattern = partialPaths[partialPath]
|
|
10
|
-
const baseDirectory = path.resolve(cwd, partialPath)
|
|
11
|
-
|
|
12
|
-
const matchingFiles = glob.sync(globPattern, {
|
|
13
|
-
cwd: baseDirectory
|
|
14
|
-
})
|
|
15
|
-
|
|
16
|
-
matchingFiles.forEach((file) => {
|
|
17
|
-
const extension = path.extname(file)
|
|
18
|
-
const name = file.replace(extension, '')
|
|
19
|
-
|
|
20
|
-
partialFiles[name] = path.join(baseDirectory, file)
|
|
21
|
-
})
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
return partialFiles
|
|
25
|
-
}
|
package/src/helpers/README.md
DELETED
|
@@ -1,213 +0,0 @@
|
|
|
1
|
-
# Handlebars Helpers
|
|
2
|
-
|
|
3
|
-
This package contains a suite of helpers to enable the migration of applications from [n-handlebars] to Page Kit. Not every helper provided by n-handlebars has been ported over; any helpers we could not find usage of, are used only within [n-ui], or have dependencies on other Handlebars components have not been included.
|
|
4
|
-
|
|
5
|
-
[n-ui]: https://github.com/Financial-Times/n-ui/
|
|
6
|
-
[n-handlebars]: https://github.com/Financial-Times/n-handlebars
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
## Usage
|
|
10
|
-
|
|
11
|
-
When using this package you can import the `helpers` property and provide it as an option when creating a new `PageKitHandlebars` instance. Please note that helpers will not be appended to the global Handlebars instance.
|
|
12
|
-
|
|
13
|
-
```js
|
|
14
|
-
const { PageKitHandlebars, helpers } = require('@financial-times/dotcom-server-handlebars')
|
|
15
|
-
const renderer = new PageKitHandlebars({ helpers })
|
|
16
|
-
```
|
|
17
|
-
|
|
18
|
-
Alternatively if you want to use the helpers with an existing Handlebars instance:
|
|
19
|
-
|
|
20
|
-
```js
|
|
21
|
-
const Handlebars = require('handlebars')
|
|
22
|
-
const { helpers } = require('@financial-times/dotcom-server-handlebars')
|
|
23
|
-
|
|
24
|
-
Handlebars.registerHelper(helpers)
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
## Block helpers
|
|
29
|
-
|
|
30
|
-
### capture
|
|
31
|
-
|
|
32
|
-
Captures the string inside of the opening and closing tags and assigns it to a variable.
|
|
33
|
-
|
|
34
|
-
Example:
|
|
35
|
-
|
|
36
|
-
```hbs
|
|
37
|
-
{{#capture "myOutput"}}I am being captured.{{/capture}}
|
|
38
|
-
|
|
39
|
-
{{myOutput}}
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
### dateformat
|
|
43
|
-
|
|
44
|
-
Formats a [date object] using the [dateformat] library. If no format is specified it will default to the `isoUtcDateTime` format.
|
|
45
|
-
|
|
46
|
-
Example:
|
|
47
|
-
|
|
48
|
-
```hbs
|
|
49
|
-
{{#dateformat}}{{date}}{{/dateformat}}
|
|
50
|
-
{{#dateformat "fullDate"}}{{ date }}{{/dateformat}}
|
|
51
|
-
{{#dateformat "dddd, d mmmm, yyyy"}}{{ date }}{{/dateformat}}
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
[date object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
|
|
55
|
-
[dateformat]: https://www.npmjs.com/package/dateformat
|
|
56
|
-
|
|
57
|
-
### ifAll
|
|
58
|
-
|
|
59
|
-
Outputs the content if all of the parameters are [truthy].
|
|
60
|
-
|
|
61
|
-
Example:
|
|
62
|
-
|
|
63
|
-
```hbs
|
|
64
|
-
{{#ifAll foo bar baz}}All parameters are truthy{{else}}A parameter is falsy{{/ifAll}}
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
[truthy]: https://developer.mozilla.org/en-US/docs/Glossary/Truthy
|
|
68
|
-
|
|
69
|
-
### ifEquals
|
|
70
|
-
|
|
71
|
-
Outputs the content if all of the parameters are [strictly equal]. The first parameter is used as the control and all parameters are tested against it.
|
|
72
|
-
|
|
73
|
-
Example:
|
|
74
|
-
|
|
75
|
-
```hbs
|
|
76
|
-
{{#ifEquals foo bar}}Parameters are all equal{{else}}A parameter does not match{{/ifEquals}}
|
|
77
|
-
```
|
|
78
|
-
|
|
79
|
-
[strictly equal]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness
|
|
80
|
-
|
|
81
|
-
### ifEqualsSome
|
|
82
|
-
|
|
83
|
-
Outputs the content if some of the parameters are [strictly equal]. The first parameter is used as the control and all parameters are tested against it.
|
|
84
|
-
|
|
85
|
-
Example:
|
|
86
|
-
|
|
87
|
-
```hbs
|
|
88
|
-
{{#ifEqualsSome foo bar baz}}Some parameters are all equal{{else}}No parameters match{{/ifEqualsSome}}
|
|
89
|
-
```
|
|
90
|
-
|
|
91
|
-
### ifSome
|
|
92
|
-
|
|
93
|
-
Outputs the content if at least one of the parameters is [truthy].
|
|
94
|
-
|
|
95
|
-
Example:
|
|
96
|
-
|
|
97
|
-
```hbs
|
|
98
|
-
{{#ifSome foo bar baz}}A parameter is truthy{{else}}All parameters are falsy{{/ifSome}}
|
|
99
|
-
```
|
|
100
|
-
|
|
101
|
-
### renderReactComponent
|
|
102
|
-
|
|
103
|
-
Outputs a JSX component specified by its local path (relative to the root of the app consuming `dotcom-server-handlebars`) or the package name of the node module, with specification of a named export if required, as well as any other props.
|
|
104
|
-
|
|
105
|
-
Example:
|
|
106
|
-
|
|
107
|
-
```hbs
|
|
108
|
-
{{{renderReactComponent localPath="views/components/ComponentWithHbsOutput" title="This is a React component"}}}
|
|
109
|
-
|
|
110
|
-
{{{renderReactComponent package="@financial-times/dotcom-ui-header" namedExport="LogoOnly" variant="large-logo"}}}
|
|
111
|
-
```
|
|
112
|
-
|
|
113
|
-
### resize
|
|
114
|
-
|
|
115
|
-
Deliver an image via the [Origami Image Service] and resize it to the specified width. Additional named parameters will be appended to the URL query string.
|
|
116
|
-
|
|
117
|
-
Example:
|
|
118
|
-
|
|
119
|
-
```hbs
|
|
120
|
-
<img src="{{#resize 640}}{{image}}{{/resize}}" />
|
|
121
|
-
<img src="{{#resize 640 fit="contain"}}{{image}}{{/resize}}" />
|
|
122
|
-
```
|
|
123
|
-
|
|
124
|
-
[Origami Image Service]: https://www.ft.com/__origami/service/image/v2/
|
|
125
|
-
|
|
126
|
-
### slice
|
|
127
|
-
|
|
128
|
-
Iterate over a subset of items. Accepts an `offset` and `limit` parameter.
|
|
129
|
-
|
|
130
|
-
Example:
|
|
131
|
-
|
|
132
|
-
```hbs
|
|
133
|
-
{{#slice iterable offset="2"}}{{this}}{{/slice}}
|
|
134
|
-
{{#slice iterable offset="4" limit="2"}}{{this}}{{/slice}}
|
|
135
|
-
```
|
|
136
|
-
|
|
137
|
-
### unlessAll
|
|
138
|
-
|
|
139
|
-
Outputs the content if all of the parameters are [falsy].
|
|
140
|
-
|
|
141
|
-
Example:
|
|
142
|
-
|
|
143
|
-
```hbs
|
|
144
|
-
{{#unlessAll foo bar baz}}All parameters are falsy{{else}}A parameter is truthy{{/unlessAll}}
|
|
145
|
-
```
|
|
146
|
-
|
|
147
|
-
[falsy]: https://developer.mozilla.org/en-US/docs/Glossary/Falsy
|
|
148
|
-
|
|
149
|
-
### unlessEquals
|
|
150
|
-
|
|
151
|
-
Outputs the content if any parameters are _not_ [strictly equal].
|
|
152
|
-
|
|
153
|
-
Example:
|
|
154
|
-
|
|
155
|
-
```hbs
|
|
156
|
-
{{#unlessEquals foo bar}}Parameters are not equal{{else}}All parameters match{{/unlessEquals}}
|
|
157
|
-
```
|
|
158
|
-
|
|
159
|
-
### unlessSome
|
|
160
|
-
|
|
161
|
-
Outputs the content if any of the parameters are [falsy].
|
|
162
|
-
|
|
163
|
-
Example:
|
|
164
|
-
|
|
165
|
-
```hbs
|
|
166
|
-
{{#unlessSome foo bar baz}}A parameter is falsy{{else}}All parameters are truthy{{/unlessSome}}
|
|
167
|
-
```
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
## Inline helpers
|
|
171
|
-
|
|
172
|
-
### array
|
|
173
|
-
|
|
174
|
-
Converts the given parameters into a single array.
|
|
175
|
-
|
|
176
|
-
Example:
|
|
177
|
-
|
|
178
|
-
```hbs
|
|
179
|
-
{{array foo bar baz}}
|
|
180
|
-
{{>partial parameter=(array foo bar baz)}}
|
|
181
|
-
```
|
|
182
|
-
|
|
183
|
-
### concat
|
|
184
|
-
|
|
185
|
-
Concatenates multiple parameters into a single string.
|
|
186
|
-
|
|
187
|
-
Example:
|
|
188
|
-
|
|
189
|
-
```hbs
|
|
190
|
-
{{concat "Welcome to " name}}
|
|
191
|
-
{{>partial parameter=(concat "Welcome to " name)}}
|
|
192
|
-
```
|
|
193
|
-
|
|
194
|
-
### encode
|
|
195
|
-
|
|
196
|
-
Encodes a uniform resource identifer (URI) using [`encodeURIComponent()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) or [`encodeURI()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI).
|
|
197
|
-
|
|
198
|
-
Example:
|
|
199
|
-
|
|
200
|
-
```hbs
|
|
201
|
-
{{encode text}}
|
|
202
|
-
{{encode text mode="uri"}}
|
|
203
|
-
```
|
|
204
|
-
|
|
205
|
-
### json
|
|
206
|
-
|
|
207
|
-
JSON stringifies the given parameter. Please note that this will error if you try to output the `@root` context when used in an Express application as this may contain secret information.
|
|
208
|
-
|
|
209
|
-
Example:
|
|
210
|
-
|
|
211
|
-
```hbs
|
|
212
|
-
{{{json data}}}
|
|
213
|
-
```
|
package/src/helpers/array.ts
DELETED
package/src/helpers/capture.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { HelperOptions } from 'handlebars'
|
|
2
|
-
|
|
3
|
-
export default function capture(name: string, options: HelperOptions) {
|
|
4
|
-
if (this.hasOwnProperty(name)) {
|
|
5
|
-
throw Error(`Template data property "${name}" has already been declared.`)
|
|
6
|
-
} else {
|
|
7
|
-
this[name] = options.fn(this)
|
|
8
|
-
}
|
|
9
|
-
}
|
package/src/helpers/concat.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import formatter from 'dateformat'
|
|
2
|
-
import { HelperOptions } from 'handlebars'
|
|
3
|
-
|
|
4
|
-
export default function dateformat(...args) {
|
|
5
|
-
if (args.length > 2) {
|
|
6
|
-
throw Error('Incorrect number of parameters provided')
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
const options = args.pop() as HelperOptions
|
|
10
|
-
const format = args[0] || 'isoUtcDateTime'
|
|
11
|
-
|
|
12
|
-
return formatter(options.fn(this), format)
|
|
13
|
-
}
|
package/src/helpers/encode.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { HelperOptions } from 'handlebars'
|
|
2
|
-
|
|
3
|
-
export default function encode(...args) {
|
|
4
|
-
if (args.length !== 2) {
|
|
5
|
-
throw Error('Incorrect number of parameters provided')
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
const options = args.pop() as HelperOptions
|
|
9
|
-
|
|
10
|
-
if (options.hash.mode === 'uri') {
|
|
11
|
-
return encodeURI(args[0])
|
|
12
|
-
} else {
|
|
13
|
-
return encodeURIComponent(args[0])
|
|
14
|
-
}
|
|
15
|
-
}
|
package/src/helpers/ifAll.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { HelperOptions } from 'handlebars'
|
|
2
|
-
|
|
3
|
-
export default function ifAll(...args) {
|
|
4
|
-
if (args.length < 2) {
|
|
5
|
-
throw Error('At least one parameter must be provided')
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
const options = args.pop() as HelperOptions
|
|
9
|
-
const condition = args.every(Boolean)
|
|
10
|
-
|
|
11
|
-
return condition ? options.fn(this) : options.inverse(this)
|
|
12
|
-
}
|
package/src/helpers/ifEquals.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { HelperOptions } from 'handlebars'
|
|
2
|
-
|
|
3
|
-
export default function ifEquals(...args) {
|
|
4
|
-
if (args.length < 3) {
|
|
5
|
-
throw Error('At least two parameters must be provided')
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
const options = args.pop() as HelperOptions
|
|
9
|
-
const condition = args.every((item) => item === args[0])
|
|
10
|
-
|
|
11
|
-
return condition ? options.fn(this) : options.inverse(this)
|
|
12
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { HelperOptions } from 'handlebars'
|
|
2
|
-
|
|
3
|
-
export default function ifEqualsSome(...args) {
|
|
4
|
-
if (args.length < 4) {
|
|
5
|
-
throw Error('At least three parameters must be provided')
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
const options = args.pop() as HelperOptions
|
|
9
|
-
const control = args.shift()
|
|
10
|
-
const condition = args.some((item) => item === control)
|
|
11
|
-
|
|
12
|
-
return condition ? options.fn(this) : options.inverse(this)
|
|
13
|
-
}
|
package/src/helpers/ifSome.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { HelperOptions } from 'handlebars'
|
|
2
|
-
|
|
3
|
-
export default function ifSome(...args) {
|
|
4
|
-
if (args.length < 2) {
|
|
5
|
-
throw Error('At least one parameter must be provided')
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
const options = args.pop() as HelperOptions
|
|
9
|
-
const condition = args.some(Boolean)
|
|
10
|
-
|
|
11
|
-
return condition ? options.fn(this) : options.inverse(this)
|
|
12
|
-
}
|
package/src/helpers/json.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
export default function json(...args) {
|
|
2
|
-
if (args.length !== 2) {
|
|
3
|
-
throw Error('Incorrect number of parameters provided')
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
// The second parameter will always be an instance of HelperOptions
|
|
7
|
-
const target = args[0]
|
|
8
|
-
|
|
9
|
-
// Do not allow users to output the whole @root context
|
|
10
|
-
// <https://github.com/Financial-Times/n-handlebars/pull/65>
|
|
11
|
-
if (target && target.hasOwnProperty('_locals')) {
|
|
12
|
-
throw Error('For security reasons you may not use the JSON helper to output the entire view context')
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
return JSON.stringify(target)
|
|
16
|
-
}
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import path from 'path'
|
|
2
|
-
import React from 'react'
|
|
3
|
-
import ReactDOMServer from 'react-dom/server'
|
|
4
|
-
|
|
5
|
-
export default function renderReactComponent({ hash }) {
|
|
6
|
-
let modulePath
|
|
7
|
-
|
|
8
|
-
if (hash.hasOwnProperty('package')) {
|
|
9
|
-
// `paths` argument provided to ensure package lookup is made
|
|
10
|
-
// within context of consuming app rather than dependency.
|
|
11
|
-
modulePath = require.resolve(hash.package, { paths: [process.cwd()] })
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
if (hash.hasOwnProperty('localPath')) {
|
|
15
|
-
// localPath is relative to root of app consuming dotcom-server-handlebars.
|
|
16
|
-
modulePath = path.resolve(hash.localPath)
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
if (!modulePath) {
|
|
20
|
-
throw new Error('You must specify a "package" or "localPath" argument to load a module')
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const importedModule = require(modulePath)
|
|
24
|
-
|
|
25
|
-
const Component = hash.hasOwnProperty('namedExport')
|
|
26
|
-
? importedModule[hash.namedExport]
|
|
27
|
-
: importedModule.__esModule
|
|
28
|
-
? importedModule.default
|
|
29
|
-
: importedModule
|
|
30
|
-
|
|
31
|
-
const props = { ...this, ...hash }
|
|
32
|
-
|
|
33
|
-
return ReactDOMServer.renderToStaticMarkup(React.createElement(Component, props))
|
|
34
|
-
}
|
package/src/helpers/resize.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { HelperOptions } from 'handlebars'
|
|
2
|
-
import querystring from 'querystring'
|
|
3
|
-
|
|
4
|
-
const host = 'https://www.ft.com/__origami/service/image/v2/images/raw'
|
|
5
|
-
|
|
6
|
-
const defaults = { source: 'next', fit: 'scale-down' }
|
|
7
|
-
|
|
8
|
-
export default function resize(...args) {
|
|
9
|
-
if (args.length !== 2) {
|
|
10
|
-
throw Error('Incorrect number of parameters provided')
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
const options = args.pop() as HelperOptions
|
|
14
|
-
const image = options.fn(this)
|
|
15
|
-
const width = args[0]
|
|
16
|
-
|
|
17
|
-
const query = querystring.stringify({ width, ...defaults, ...options.hash })
|
|
18
|
-
|
|
19
|
-
return `${host}/${encodeURIComponent(image)}?${query}`
|
|
20
|
-
}
|
package/src/helpers/slice.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { HelperOptions } from 'handlebars'
|
|
2
|
-
|
|
3
|
-
export default function slice(...args) {
|
|
4
|
-
if (args.length !== 2) {
|
|
5
|
-
throw Error('Incorrect number of parameters provided')
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
const options = args.pop() as HelperOptions
|
|
9
|
-
|
|
10
|
-
const offset = parseInt(options.hash.offset, 10) || 0
|
|
11
|
-
const limit = parseInt(options.hash.limit, 10) || 1
|
|
12
|
-
|
|
13
|
-
const slicedItems = Array.from(args[0]).slice(offset, offset + limit)
|
|
14
|
-
|
|
15
|
-
let contents = ''
|
|
16
|
-
|
|
17
|
-
slicedItems.forEach((item) => {
|
|
18
|
-
contents += options.fn(item)
|
|
19
|
-
})
|
|
20
|
-
|
|
21
|
-
return contents
|
|
22
|
-
}
|
package/src/helpers/unlessAll.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { HelperOptions } from 'handlebars'
|
|
2
|
-
|
|
3
|
-
export default function unlessAll(...args) {
|
|
4
|
-
if (args.length < 2) {
|
|
5
|
-
throw Error('At least one parameter must be provided')
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
const options = args.pop() as HelperOptions
|
|
9
|
-
const condition = args.some(Boolean) === false
|
|
10
|
-
|
|
11
|
-
return condition ? options.fn(this) : options.inverse(this)
|
|
12
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { HelperOptions } from 'handlebars'
|
|
2
|
-
|
|
3
|
-
export default function unlessEquals(...args) {
|
|
4
|
-
if (args.length < 3) {
|
|
5
|
-
throw Error('At least two parameters must be provided')
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
const options = args.pop() as HelperOptions
|
|
9
|
-
const condition = args.every((item) => item === args[0]) === false
|
|
10
|
-
|
|
11
|
-
return condition ? options.fn(this) : options.inverse(this)
|
|
12
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { HelperOptions } from 'handlebars'
|
|
2
|
-
|
|
3
|
-
export default function unlessSome(...args) {
|
|
4
|
-
if (args.length < 2) {
|
|
5
|
-
throw Error('At least one parameter must be provided')
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
const options = args.pop() as HelperOptions
|
|
9
|
-
const condition = args.every(Boolean) === false
|
|
10
|
-
|
|
11
|
-
return condition ? options.fn(this) : options.inverse(this)
|
|
12
|
-
}
|
package/src/helpers.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
export { default as array } from './helpers/array'
|
|
2
|
-
export { default as capture } from './helpers/capture'
|
|
3
|
-
export { default as concat } from './helpers/concat'
|
|
4
|
-
export { default as dateformat } from './helpers/dateformat'
|
|
5
|
-
export { default as encode } from './helpers/encode'
|
|
6
|
-
export { default as ifAll } from './helpers/ifAll'
|
|
7
|
-
export { default as ifEquals } from './helpers/ifEquals'
|
|
8
|
-
export { default as ifEqualsSome } from './helpers/ifEqualsSome'
|
|
9
|
-
export { default as ifSome } from './helpers/ifSome'
|
|
10
|
-
export { default as json } from './helpers/json'
|
|
11
|
-
export { default as renderReactComponent } from './helpers/renderReactComponent'
|
|
12
|
-
export { default as resize } from './helpers/resize'
|
|
13
|
-
export { default as slice } from './helpers/slice'
|
|
14
|
-
export { default as unlessAll } from './helpers/unlessAll'
|
|
15
|
-
export { default as unlessEquals } from './helpers/unlessEquals'
|
|
16
|
-
export { default as unlessSome } from './helpers/unlessSome'
|