Skip to content

Commit d6aa96f

Browse files
committed
fix(pdf): build images and supported extensions default empty
1 parent 3c93051 commit d6aa96f

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

src/use/pdf.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ export const usePDF = () => {
127127

128128
const image = (entity: Entity) => {
129129
if (entity.external?.image?.alignment === 'full') {
130-
if (entity.raw.includes('svg')) {
130+
if (entity.external.image.name.includes('svg')) {
131131
return {
132132
svg: entity.raw,
133133
width:
@@ -161,7 +161,7 @@ export const usePDF = () => {
161161
],
162162
}
163163
} else {
164-
if (entity.raw.includes('svg')) {
164+
if (entity.external?.image?.name.includes('svg')) {
165165
return {
166166
svg: entity.raw,
167167
width: entity.external?.image?.size.width,

src/use/raw.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,12 @@ export const useRaw = () => {
337337

338338
const make = () => {
339339
const image = (entity: Entity) => {
340+
if (!support().images(entity.external?.image?.name as string)) {
341+
return `<div class="flex wb-text text-xl items-end w-full justify-center py-5">
342+
<svg id="unsupported-extension-image" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="h-7 w-7" preserveAspectRatio="xMidYMid meet" viewBox="0 0 20 20"><g stroke-width="1.5" fill="none"><path d="M12 8v4" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"></path><path d="M12 16.01l.01-.011" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"></path><path d="M9 3H4v3" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"></path><path d="M4 11v2" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"></path><path d="M20 11v2" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"></path><path d="M15 3h5v3" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"></path><path d="M9 21H4v-3" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"></path><path d="M15 21h5v-3" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"></path></g></svg>
343+
</div>`
344+
}
345+
340346
return `<div class="flex wb-text text-xl items-end w-full justify-center py-5">
341347
<svg
342348
xmlns="http://www.w3.org/2000/svg"
@@ -658,6 +664,19 @@ export const useRaw = () => {
658664
})
659665
}
660666

667+
const support = () => {
668+
const images = (file: string) => {
669+
return (
670+
file.includes('svg') ||
671+
file.includes('png') ||
672+
file.includes('jpeg') ||
673+
file.includes('jpg')
674+
)
675+
}
676+
677+
return { images }
678+
}
679+
661680
return {
662681
html,
663682
caret,
@@ -668,6 +687,7 @@ export const useRaw = () => {
668687
style,
669688
normalize,
670689
copy,
690+
support,
671691
}
672692
}
673693

test/raw/raw_v2.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,4 +387,27 @@ describe('Raw v2 - Make', () => {
387387
})
388388
).toContain(file_name)
389389
})
390+
391+
it('should not create image because unsupported extension', () => {
392+
const file_name = 'foo.bw'
393+
394+
expect(
395+
make.image({
396+
type: 'image',
397+
raw: 'dsaKDSADaskASHDGYasdahbdasnASD...',
398+
createdAt: format.actually(),
399+
updatedAt: format.actually(),
400+
external: {
401+
image: {
402+
name: file_name,
403+
size: {
404+
width: 0,
405+
height: 0,
406+
},
407+
alignment: 'full',
408+
},
409+
},
410+
})
411+
).toContain('id="unsupported-extension-image"')
412+
})
390413
})

0 commit comments

Comments
 (0)