Skip to content

Commit c44a0dd

Browse files
mono0xkentcdodds
andauthored
fix: apply handleHTML to cached HTML data (#124)
Co-authored-by: Kent C. Dodds <[email protected]>
1 parent 8b804e8 commit c44a0dd

File tree

2 files changed

+47
-16
lines changed

2 files changed

+47
-16
lines changed

src/__tests__/index.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,37 @@ test('handleHTML gets null', async () => {
277277
)
278278
})
279279

280+
test('handleHTML works when requests are cached', async () => {
281+
const myCache = new Map()
282+
const transformer = getTransformer()
283+
const getHTMLMock = transformer.getHTML as Mock
284+
await (
285+
await import('remark')
286+
)
287+
.remark()
288+
.use(remarkEmbedder, {cache: myCache, transformers: [transformer]})
289+
.use((await import('remark-html')).default, {sanitize: false})
290+
.process('https://some-site.com')
291+
292+
expect(getHTMLMock).toHaveBeenCalledTimes(1)
293+
getHTMLMock.mockClear()
294+
295+
const handleHTML = vi.fn(html => `<div>${html}</div>`)
296+
const result = await (
297+
await import('remark')
298+
)
299+
.remark()
300+
.use(remarkEmbedder, {cache: myCache, transformers: [transformer], handleHTML})
301+
.use((await import('remark-html')).default, {sanitize: false})
302+
.process(`[https://some-site.com](https://some-site.com)`)
303+
304+
expect(result.toString()).toMatchInlineSnapshot(
305+
`<div><iframe src="https://some-site.com/"></iframe></div>`,
306+
)
307+
308+
expect(getHTMLMock).not.toHaveBeenCalled()
309+
})
310+
280311
test('can handle errors', async () => {
281312
consoleError.mockImplementationOnce(() => {})
282313
const transformer = getTransformer({

src/index.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -129,26 +129,26 @@ const remarkEmbedder: Plugin<[RemarkEmbedderOptions]> = ({
129129
const cacheKey = `remark-embedder:${transformer.name}:${url}`
130130
let html: GottenHTML | undefined = await cache?.get(cacheKey)
131131

132-
if (!html) {
133-
try {
132+
try {
133+
if (!html) {
134134
html = await transformer.getHTML(url, config)
135135
html = html?.trim() ?? null
136136
await cache?.set(cacheKey, html)
137+
}
137138

138-
// optional handleHTML transform function
139-
if (handleHTML) {
140-
html = await handleHTML(html, {url, transformer, config})
141-
html = html?.trim() ?? null
142-
}
143-
} catch (e: unknown) {
144-
if (handleError) {
145-
const error = e as Error
146-
console.error(`${errorMessageBanner}\n\n${error.message}`)
147-
html = await handleError({error, url, transformer, config})
148-
html = html?.trim() ?? null
149-
} else {
150-
throw e
151-
}
139+
// optional handleHTML transform function
140+
if (handleHTML) {
141+
html = await handleHTML(html, {url, transformer, config})
142+
html = html?.trim() ?? null
143+
}
144+
} catch (e: unknown) {
145+
if (handleError) {
146+
const error = e as Error
147+
console.error(`${errorMessageBanner}\n\n${error.message}`)
148+
html = await handleError({error, url, transformer, config})
149+
html = html?.trim() ?? null
150+
} else {
151+
throw e
152152
}
153153
}
154154

0 commit comments

Comments
 (0)