Skip to content

Commit 2d07854

Browse files
committed
chore(npm): update dependencies
1 parent a0d4d66 commit 2d07854

File tree

5 files changed

+141
-3
lines changed

5 files changed

+141
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@athenna/view",
3-
"version": "5.2.0",
3+
"version": "5.3.0",
44
"description": "The Athenna template engine. Built on top of Edge.js.",
55
"license": "MIT",
66
"author": "João Lenon <lenon@athenna.io>",

src/types/TagContract.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* @athenna/view
3+
*
4+
* (c) João Lenon <lenon@athenna.io>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
import type { TagContract as EdgeTagContract } from 'edge.js/types'
11+
12+
export type TagContract = Omit<EdgeTagContract, 'tagName'>

src/types/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* @athenna/view
3+
*
4+
* (c) João Lenon <lenon@athenna.io>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
export * from '#src/types/TagContract'

src/views/ViewImpl.ts

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,20 @@ import { Edge } from 'edge.js'
1111
import { debug } from '#src/debug'
1212
import { Config } from '@athenna/config'
1313
import { resolve, isAbsolute } from 'node:path'
14-
import { Is, File, Path } from '@athenna/common'
14+
import type { TagContract } from '#src/types/TagContract'
15+
import { Is, File, Path, Macroable } from '@athenna/common'
1516
import { EmptyComponentException } from '#src/exceptions/EmptyComponentException'
1617
import { NotFoundComponentException } from '#src/exceptions/NotFoundComponentException'
1718
import { AlreadyExistComponentException } from '#src/exceptions/AlreadyExistComponentException'
1819

19-
export class ViewImpl {
20+
export class ViewImpl extends Macroable {
2021
/**
2122
* Edge instance that is handling all the views.
2223
*/
2324
public edge: Edge
2425

2526
public constructor() {
27+
super()
2628
this.edge = Edge.create(Config.get('view.edge', {}))
2729
}
2830

@@ -152,6 +154,54 @@ export class ViewImpl {
152154
return this
153155
}
154156

157+
/**
158+
* Add a new tag to templates. Just like @component
159+
* @if, etc.
160+
*
161+
* @example
162+
* ```ts
163+
* import type { TagContract } from '@athenna/view'
164+
*
165+
* const reverseTagOptions: TagContract = {
166+
* block: false,
167+
* seekable: true,
168+
* compile(parser, buffer, token) {
169+
* buffer.outputRaw('Hello from reverse tag')
170+
* }
171+
* }
172+
*
173+
* View.addTag('reverse', reverseTagOptions)
174+
*
175+
* const output = await View.renderRaw('@reverse()') // 'Hello from reverse tag'
176+
* ```
177+
*/
178+
public addTag(name: string, options: TagContract) {
179+
this.edge.registerTag({ tagName: name, ...options })
180+
181+
return this
182+
}
183+
184+
/**
185+
* Remove some tag from views registered using
186+
* "addTag" method.
187+
*
188+
* @example
189+
* ```ts
190+
* View
191+
* .addTag('reverse', { ... })
192+
* .removeTag('reverse')
193+
* ```
194+
*/
195+
public removeTag(name: string) {
196+
if (!this.edge.tags[name]) {
197+
return this
198+
}
199+
200+
delete this.edge.tags[name]
201+
202+
return this
203+
}
204+
155205
/**
156206
* Create a new view disk. View disks can be used
157207
* to register multiple views at the same time.

tests/unit/views/ViewImplTest.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,72 @@ export default class ViewImplTest {
194194
}
195195
}
196196

197+
@Test()
198+
public async shouldBeAbleToAddTagsToBeUsedByAllViews({ assert }: Context) {
199+
View.addTag('reverse', {
200+
block: false,
201+
seekable: true,
202+
compile(_, buffer) {
203+
buffer.outputRaw('Hello from reverse tag')
204+
}
205+
})
206+
207+
const content = await View.renderRaw('@reverse()')
208+
209+
assert.deepEqual(content, 'Hello from reverse tag')
210+
}
211+
212+
@Test()
213+
public async shouldBeAbleToRemoveGlobalTags({ assert }: Context) {
214+
View.addTag('reverse', {
215+
block: false,
216+
seekable: true,
217+
compile(_, buffer) {
218+
buffer.outputRaw('Hello from reverse tag')
219+
}
220+
})
221+
222+
{
223+
const content = await View.renderRaw('@reverse()')
224+
225+
assert.deepEqual(content, 'Hello from reverse tag')
226+
}
227+
228+
View.removeTag('reverse')
229+
230+
{
231+
const content = await View.renderRaw('@reverse()')
232+
233+
assert.deepEqual(content, '@reverse()')
234+
}
235+
}
236+
237+
@Test()
238+
public async shouldBeAbleToRemoveGlobalTagsThatDoesNotExist({ assert }: Context) {
239+
View.addTag('reverse', {
240+
block: false,
241+
seekable: true,
242+
compile(_, buffer) {
243+
buffer.outputRaw('Hello from reverse tag')
244+
}
245+
})
246+
247+
{
248+
const content = await View.renderRaw('@reverse()')
249+
250+
assert.deepEqual(content, 'Hello from reverse tag')
251+
}
252+
253+
View.removeTag('reverse')
254+
View.removeTag('not-found')
255+
256+
{
257+
const content = await View.renderRaw('@reverse()')
258+
259+
assert.deepEqual(content, '@reverse()')
260+
}
261+
}
262+
197263
@Test()
198264
public async shouldBeAbleToCreateViewDisks({ assert }: Context) {
199265
View.createViewDisk('test', Path.fixtures('views/admin'))

0 commit comments

Comments
 (0)