8000 [pull] main from remark-embedder:main by pull[bot] · Pull Request #15 · mikestecker/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[pull] main from remark-embedder:main #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
23 changes: 14 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -451,15 +451,20 @@ Thanks goes to these people ([emoji key][emojis]):
<!-- prettier-ignore-start -->
<!-- markdownlint-disable -->
<table>
<tr>
<td align="center"><a href="https://kentcdodds.com"><img src="https://avatars.githubusercontent.com/u/1500684?v=3?s=100" width="100px;" alt=""/><br /><sub><b>Kent C. Dodds</b></sub></a><br /><a href="https://github.com/remark-embedder/core/commits?author=kentcdodds" title="Code">💻</a> <a href="https://github.com/remark-embedder/core/commits?author=kentcdodds" title="Documentation">📖</a> <a href="#infra-kentcdodds" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a> <a href="https://github.com/remark-embedder/core/commits?author=kentcdodds" title="Tests">⚠️</a></td>
<td align="center"><a href="https://michaeldeboey.be"><img src="https://avatars3.githubusercontent.com/u/6643991?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Michaël De Boey</b></sub></a><br /><a href="https://github.com/remark-embedder/core/issues?q=author%3AMichaelDeBoey" title="Bug reports">🐛</a> <a href="https://github.com/remark-embedder/core/commits?author=MichaelDeBoey" title="Code">💻</a> <a href="https://github.com/remark-embedder/core/commits?author=MichaelDeBoey" title="Documentation">📖</a> <a href="https://github.com/remark-embedder/core/commits?author=MichaelDeBoey" title="Tests">⚠️</a> <a href="https://github.com/remark-embedder/core/pulls?q=is%3Apr+reviewed-by%3AMichaelDeBoey" title="Reviewed Pull Requests">👀</a></td>
<td align="center"><a href="https://github.com/johnstonmatt"><img src="https://avatars1.githubusercontent.com/u/5455542?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Matt Johnston</b></sub></a><br /><a href="https://github.com/remark-embedder/core/commits?author=johnstonmatt" title="Documentation">📖</a></td>
<td align="center"><a href="https://www.osiux.ws/"><img src="https://avatars2.githubusercontent.com/u/204463?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Eduardo Reveles</b></sub></a><br /><a href="https://github.com/remark-embedder/core/commits?author=osiux" title="Code">💻</a></td>
<td align="center"><a href="http://wooorm.com"><img src="https://avatars0.githubusercontent.com/u/944406?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Titus</b></sub></a><br /><a href="https://github.com/remark-embedder/core/pulls?q=is%3Apr+reviewed-by%3Awooorm" title="Reviewed Pull Requests">👀</a></td>
<td align="center"><a href="https://bradgarropy.com"><img src="https://avatars2.githubusercontent.com/u/11336745?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Brad Garropy</b></sub></a><br /><a href="https://github.com/remark-embedder/core/commits?author=bradgarropy" title="Code">💻</a> <a href="https://github.com/remark-embedder/core/issues?q=author%3Abradgarropy" title="Bug reports">🐛</a></td>
<td align="center"><a href="https://github.com/mikestecker"><img src="https://avatars.githubusercontent.com/u/407465?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Mike Stecker</b></sub></a><br /><a href="https://github.com/remark-embedder/core/commits?author=mikestecker" title="Code">💻</a> <a href="https://github.com/remark-embedder/core/commits?author=mikestecker" title="Tests">⚠️</a> <a href="https://github.com/remark-embedder/core/commits?author=mikestecker" title="Documentation">📖</a></td>
</tr>
<tbody>
<tr>
<td align="center" valign="top" width="14.28%"><a href="https://kentcdodds.com"><img src="https://avatars.githubusercontent.com/u/1500684?v=3?s=100" width="100px;" alt="Kent C. Dodds"/><br /><sub><b>Kent C. Dodds</b></sub></a><br /><a href="https://github.com/remark-embedder/core/commits?author=kentcdodds" title="Code">💻</a> <a href="https://github.com/remark-embedder/core/commits?author=kentcdodds" title="Documentation">📖</a> <a href="#infra-kentcdodds" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a> <a href="https://github.com/remark-embedder/core/commits?author=kentcdodds" title="Tests">⚠️</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://michaeldeboey.be"><img src="https://avatars3.githubusercontent.com/u/6643991?v=4?s=100" width="100px;" alt="Michaël De Boey"/><br /><sub><b>Michaël De Boey</b></sub></a><br /><a href="https://github.com/remark-embedder/core/issues?q=author%3AMichaelDeBoey" title="Bug reports">🐛</a> <a href="https://github.com/remark-embedder/core/commits?author=MichaelDeBoey" title="Code">💻</a> <a href="https://github.com/remark-embedder/core/commits?author=MichaelDeBoey" title="Documentation">📖</a> <a href="https://github.com/remark-embedder/core/commits?author=MichaelDeBoey" title="Tests">⚠️</a> <a href="https://github.com/remark-embedder/core/pulls?q=is%3Apr+reviewed-by%3AMichaelDeBoey" title="Reviewed Pull Requests">👀</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/johnstonmatt"><img src="https://avatars1.githubusercontent.com/u/5455542?v=4?s=100" width="100px;" alt="Matt Johnston"/><br /><sub><b>Matt Johnston</b></sub></a><br /><a href="https://github.com/remark-embedder/core/commits?author=johnstonmatt" title="Documentation">📖</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://www.osiux.ws/"><img src="https://avatars2.githubusercontent.com/u/204463?v=4?s=100" width="100px;" alt="Eduardo Reveles"/><br /><sub><b>Eduardo Reveles</b></sub></a><br /><a href="https://github.com/remark-embedder/core/commits?author=osiux" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="http://wooorm.com"><img src="https://avatars0.githubusercontent.com/u/944406?v=4?s=100" width="100px;" alt="Titus"/><br /><sub><b>Titus</b></sub></a><br /><a href="https://github.com/remark-embedder/core/pulls?q=is%3Apr+reviewed-by%3Awooorm" title="Reviewed Pull Requests">👀</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://bradgarropy.com"><img src="https://avatars2.githubusercontent.com/u/11336745?v=4?s=100" width="100px;" alt="Brad Garropy"/><br /><sub><b>Brad Garropy</b></sub></a><br /><a href="https://github.com/remark-embedder/core/commits?author=bradgarropy" title="Code">💻</a> <a href="https://github.com/remark-embedder/core/issues?q=author%3Abradgarropy" title="Bug reports">🐛</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/mikestecker"><img src="https://avatars.githubusercontent.com/u/407465?v=4?s=100" width="100px;" alt="Mike Stecker"/><br /><sub><b>Mike Stecker</b></sub></a><br /><a href="https://github.com/remark-embedder/core/commits?author=mikestecker" title="Code">💻</a> <a href="https://github.com/remark-embedder/core/commits?author=mikestecker" title="Tests">⚠️</a> <a href="https://github.com/remark-embedder/core/commits?author=mikestecker" title="Documentation">📖</a></td>
</tr>
<tr>
<td align="center" valign="top" width="14.28%"><a href="https://blog.mono0x.net/"><img src="https://avatars.githubusercontent.com/u/231380?v=4?s=100" width="100px;" alt="mono"/><br /><sub><b>mono</b></sub></a><br /><a href="https://github.com/remark-embedder/core/commits?author=mono0x" title="Code">💻</a> <a href="https://github.com/remark-embedder/core/commits?author=mono0x" title="Tests">⚠️</a></td>
</tr>
</tbody>
</table>

<!-- markdownlint-restore -->
Expand Down
37 changes: 33 additions & 4 deletions src/__tests__/index.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -279,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 => `<div>${html}</div>`)
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(
`<div><iframe src="https://some-site.com/"></iframe></div>`,
)

expect(getHTMLMock).not.toHaveBeenCalled()
})

test('can handle errors', async () => {
consoleError.mockImplementationOnce(() => {})
const transformer = getTransformer({
Expand Down
38 changes: 19 additions & 19 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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 = unknown> = Type
Expand Down Expand Up @@ -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
}
}

Expand Down
0