Skip to content

Commit

Permalink
support fallback tag in NIP-94
Browse files Browse the repository at this point in the history
  • Loading branch information
syusui-s committed Jul 4, 2024
1 parent 753ff32 commit 018a6db
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
16 changes: 16 additions & 0 deletions nip94.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('generateEventTemplate', () => {
image: 'https://example.com/image.jpg',
summary: 'Lorem ipsum',
alt: 'Image alt text',
fallback: ['https://fallback1.example.com/image.jpg', 'https://fallback2.example.com/image.jpg'],
}

const expectedEventTemplate: EventTemplate = {
Expand All @@ -40,6 +41,8 @@ describe('generateEventTemplate', () => {
['image', 'https://example.com/image.jpg'],
['summary', 'Lorem ipsum'],
['alt', 'Image alt text'],
['fallback', 'https://fallback1.example.com/image.jpg'],
['fallback', 'https://fallback2.example.com/image.jpg'],
],
}

Expand Down Expand Up @@ -71,6 +74,7 @@ describe('validateEvent', () => {
['image', 'https://example.com/image.jpg'],
['summary', 'Lorem ipsum'],
['alt', 'Image alt text'],
['fallback', 'https://fallback.example.com/image.jpg'],
],
},
sk,
Expand Down Expand Up @@ -100,6 +104,7 @@ describe('validateEvent', () => {
['image', 'https://example.com/image.jpg'],
['summary', 'Lorem ipsum'],
['alt', 'Image alt text'],
['fallback', 'https://fallback.example.com/image.jpg'],
],
},
sk,
Expand Down Expand Up @@ -129,6 +134,7 @@ describe('validateEvent', () => {
['image', 'https://example.com/image.jpg'],
['summary', 'Lorem ipsum'],
['alt', 'Image alt text'],
['fallback', 'https://fallback.example.com/image.jpg'],
],
},
sk,
Expand Down Expand Up @@ -158,6 +164,7 @@ describe('validateEvent', () => {
['image', 'https://example.com/image.jpg'],
['summary', 'Lorem ipsum'],
['alt', 'Image alt text'],
['fallback', 'https://fallback.example.com/image.jpg'],
],
},
sk,
Expand All @@ -181,6 +188,7 @@ describe('validateEvent', () => {
['image', 'https://example.com/image.jpg'],
['summary', 'Lorem ipsum'],
['alt', 'Image alt text'],
['fallback', 'https://fallback.example.com/image.jpg'],
],
},
sk,
Expand All @@ -204,6 +212,7 @@ describe('validateEvent', () => {
['image', 'https://example.com/image.jpg'],
['summary', 'Lorem ipsum'],
['alt', 'Image alt text'],
['fallback', 'https://fallback.example.com/image.jpg'],
],
},
sk,
Expand All @@ -227,6 +236,7 @@ describe('validateEvent', () => {
['image', 'https://example.com/image.jpg'],
['summary', 'Lorem ipsum'],
['alt', 'Image alt text'],
['fallback', 'https://fallback.example.com/image.jpg'],
],
},
sk,
Expand Down Expand Up @@ -259,6 +269,7 @@ describe('validateEvent', () => {
['image', 'https://example.com/image.jpg'],
['summary', 'Lorem ipsum'],
['alt', 'Image alt text'],
['fallback', 'https://fallback.example.com/image.jpg'],
],
},
sk,
Expand Down Expand Up @@ -288,6 +299,7 @@ describe('validateEvent', () => {
['image', 'https://example.com/image.jpg'],
['summary', 'Lorem ipsum'],
['alt', 'Image alt text'],
['fallback', 'https://fallback.example.com/image.jpg'],
],
},
sk,
Expand Down Expand Up @@ -319,6 +331,8 @@ describe('parseEvent', () => {
['image', 'https://example.com/image.jpg'],
['summary', 'Lorem ipsum'],
['alt', 'Image alt text'],
['fallback', 'https://fallback1.example.com/image.jpg'],
['fallback', 'https://fallback2.example.com/image.jpg'],
],
},
sk,
Expand All @@ -340,6 +354,7 @@ describe('parseEvent', () => {
image: 'https://example.com/image.jpg',
summary: 'Lorem ipsum',
alt: 'Image alt text',
fallback: ['https://fallback1.example.com/image.jpg', 'https://fallback2.example.com/image.jpg'],
})
})

Expand All @@ -364,6 +379,7 @@ describe('parseEvent', () => {
['image', 'https://example.com/image.jpg'],
['summary', 'Lorem ipsum'],
['alt', 'Image alt text'],
['fallback', 'https://fallback.example.com/image.jpg'],
],
},
sk,
Expand Down
10 changes: 10 additions & 0 deletions nip94.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ export type FileMetadataObject = {
* Optional: A description for accessibility, providing context or a brief description of the file.
*/
alt?: string

/**
* Optional: fallback URLs in case url fails.
*/
fallback?: string[]
}

/**
Expand Down Expand Up @@ -104,6 +109,7 @@ export function generateEventTemplate(fileMetadata: FileMetadataObject): EventTe
if (fileMetadata.image) eventTemplate.tags.push(['image', fileMetadata.image])
if (fileMetadata.summary) eventTemplate.tags.push(['summary', fileMetadata.summary])
if (fileMetadata.alt) eventTemplate.tags.push(['alt', fileMetadata.alt])
if (fileMetadata.fallback) fileMetadata.fallback.forEach(url => eventTemplate.tags.push(['fallback', url]))

return eventTemplate
}
Expand Down Expand Up @@ -194,6 +200,10 @@ export function parseEvent(event: Event): FileMetadataObject {
case 'alt':
fileMetadata.alt = value
break
case 'fallback':
fileMetadata.fallback ??= []
fileMetadata.fallback.push(value)
break
}
}

Expand Down

0 comments on commit 018a6db

Please sign in to comment.