From 8b804e8145ce878e9fd8997aafb109e38b42a1ef Mon Sep 17 00:00:00 2001 From: mono Date: Thu, 6 Jul 2023 05:10:54 +0900 Subject: [PATCH 1/3] chore: import/consistent-type-specifier-style (#125) --- src/__tests__/index.ts | 6 ++---- src/index.ts | 6 +++--- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/__tests__/index.ts b/src/__tests__/index.ts index 12fe6d0..8baa3c7 100644 --- a/src/__tests__/index.ts +++ b/src/__tests__/index.ts @@ -1,7 +1,5 @@ -import {afterEach, expect, vi, test} from 'vitest' -import type {Mock} from 'vitest' -import remarkEmbedder from '../' -import type {Transformer} from '../' +import {afterEach, expect, vi, test, type Mock} from 'vitest' +import remarkEmbedder, {type Transformer} from '../' // this removes the quotes around strings... const unquoteSerializer = { diff --git a/src/index.ts b/src/index.ts index acb4d2c..c654cdc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ -import type {Element, Root} from 'hast' -import type {Plugin} from 'unified' -import type {Link, Paragraph, Text} from 'mdast' +import {type Element, type Root} from 'hast' +import {type Plugin} from 'unified' +import {type Link, type Paragraph, type Text} from 'mdast' type GottenHTML = string | null type TransformerConfig = Type From c44a0dd5108a967033082a9abd8f96e43f52337a Mon Sep 17 00:00:00 2001 From: mono Date: Thu, 6 Jul 2023 05:11:22 +0900 Subject: [PATCH 2/3] fix: apply `handleHTML` to cached HTML data (#124) Co-authored-by: Kent C. Dodds --- src/__tests__/index.ts | 31 +++++++++++++++++++++++++++++++ src/index.ts | 32 ++++++++++++++++---------------- 2 files changed, 47 insertions(+), 16 deletions(-) diff --git a/src/__tests__/index.ts b/src/__tests__/index.ts index 8baa3c7..0ddeb76 100644 --- a/src/__tests__/index.ts +++ b/src/__tests__/index.ts @@ -277,6 +277,37 @@ test('handleHTML gets null', async () => { ) }) +test('handleHTML works when requests are cached', async () => { + const myCache = new Map() + const transformer = getTransformer() + const getHTMLMock = transformer.getHTML as Mock + await ( + await import('remark') + ) + .remark() + .use(remarkEmbedder, {cache: myCache, transformers: [transformer]}) + .use((await import('remark-html')).default, {sanitize: false}) + .process('https://some-site.com') + + expect(getHTMLMock).toHaveBeenCalledTimes(1) + getHTMLMock.mockClear() + + const handleHTML = vi.fn(html => `
${html}
`) + const result = await ( + await import('remark') + ) + .remark() + .use(remarkEmbedder, {cache: myCache, transformers: [transformer], handleHTML}) + .use((await import('remark-html')).default, {sanitize: false}) + .process(`[https://some-site.com](https://some-site.com)`) + + expect(result.toString()).toMatchInlineSnapshot( + `
`, + ) + + expect(getHTMLMock).not.toHaveBeenCalled() +}) + test('can handle errors', async () => { consoleError.mockImplementationOnce(() => {}) const transformer = getTransformer({ diff --git a/src/index.ts b/src/index.ts index c654cdc..9571560 100644 --- a/src/index.ts +++ b/src/index.ts @@ -129,26 +129,26 @@ const remarkEmbedder: Plugin<[RemarkEmbedderOptions]> = ({ const cacheKey = `remark-embedder:${transformer.name}:${url}` let html: GottenHTML | undefined = await cache?.get(cacheKey) - if (!html) { - try { + try { + if (!html) { html = await transformer.getHTML(url, config) html = html?.trim() ?? null await cache?.set(cacheKey, html) + } - // optional handleHTML transform function - if (handleHTML) { - html = await handleHTML(html, {url, transformer, config}) - html = html?.trim() ?? null - } - } catch (e: unknown) { - if (handleError) { - const error = e as Error - console.error(`${errorMessageBanner}\n\n${error.message}`) - html = await handleError({error, url, transformer, config}) - html = html?.trim() ?? null - } else { - throw e - } + // optional handleHTML transform function + if (handleHTML) { + html = await handleHTML(html, {url, transformer, config}) + html = html?.trim() ?? null + } + } catch (e: unknown) { + if (handleError) { + const error = e as Error + console.error(`${errorMessageBanner}\n\n${error.message}`) + html = await handleError({error, url, transformer, config}) + html = html?.trim() ?? null + } else { + throw e } } From ab2da142c3cddc6ba7d46a18a46cf3fc6f7f6823 Mon Sep 17 00:00:00 2001 From: "allcontributors[bot]" <46447321+allcontributors[bot]@users.noreply.github.com> Date: Wed, 5 Jul 2023 14:12:22 -0600 Subject: [PATCH 3/3] docs: add mono0x as a contributor for code, and test (#126) Co-authored-by: allcontributors[bot] <46447321+allcontributors[bot]@users.noreply.github.com> --- .all-contributorsrc | 14 +++++++++++++- README.md | 23 ++++++++++++++--------- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/.all-contributorsrc b/.all-contributorsrc index a81ef80..1ae33aa 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -83,6 +83,18 @@ "test", "doc" ] + }, + { + "login": "mono0x", + "name": "mono", + "avatar_url": "https://avatars.githubusercontent.com/u/231380?v=4", + "profile": "https://blog.mono0x.net/", + "contributions": [ + "code", + "test" + ] } - ] + ], + "commitType": "docs", + "commitConvention": "angular" } diff --git a/README.md b/README.md index 76a24db..b6e2bf0 100644 --- a/README.md +++ b/README.md @@ -451,15 +451,20 @@ Thanks goes to these people ([emoji key][emojis]): - - - - - - - - - + + + + + + + + + + + + + +

Kent C. Dodds

πŸ’» πŸ“– πŸš‡ ⚠️

MichaΓ«l De Boey

πŸ› πŸ’» πŸ“– ⚠️ πŸ‘€

Matt Johnston

πŸ“–

Eduardo Reveles

πŸ’»

Titus

πŸ‘€

Brad Garropy

πŸ’» πŸ›

Mike Stecker

πŸ’» ⚠️ πŸ“–
Kent C. Dodds
Kent C. Dodds

πŸ’» πŸ“– πŸš‡ ⚠️
MichaΓ«l De Boey
MichaΓ«l De Boey

πŸ› πŸ’» πŸ“– ⚠️ πŸ‘€
Matt Johnston
Matt Johnston

πŸ“–
Eduardo Reveles
Eduardo Reveles

πŸ’»
Titus
Titus

πŸ‘€
Brad Garropy
Brad Garropy

πŸ’» πŸ›
Mike Stecker
Mike Stecker

πŸ’» ⚠️ πŸ“–
mono
mono

πŸ’» ⚠️