Skip to content

Commit

Permalink
Only match URLs that don’t have a custom label (delucis#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
delucis authored May 4, 2024
1 parent 732a6cf commit 93a5ca7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
7 changes: 7 additions & 0 deletions .changeset/large-dolls-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@astro-community/astro-embed-integration': patch
---

Fixes an issue where the integration would match links that take up an entire paragraph even if they included a custom link.

For example, `[Check it out!](https://example.com)` would be converted to a `<LinkPreview>` component instead of rendering an `<a>` tag. This is now fixed and only plain URLs like `https://example.com` will match.
19 changes: 16 additions & 3 deletions packages/astro-embed-integration/remark-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Node, select, selectAll } from 'unist-util-select';
import { type Node, select, selectAll } from 'unist-util-select';
import twitterMatcher from '@astro-community/astro-embed-twitter/matcher';
import vimeoMatcher from '@astro-community/astro-embed-vimeo/matcher';
import youtubeMatcher from '@astro-community/astro-embed-youtube/matcher';
Expand Down Expand Up @@ -58,9 +58,22 @@ export default function createPlugin({
const link: Link | null = select(':scope > link:only-child', paragraph);
if (!link) return;

const { url } = link;
const { url, children } = link;
// We’re only interested in HTTP links
if (!url?.startsWith('http')) return;
if (!url || !url.startsWith('http')) return;
// URLs should have a single child
if (!children || children.length !== 1) return;

// The child should be a text node with a value matching the URL
const child = children[0];
if (
!child ||
child.type !== 'text' ||
!('value' in child) ||
child.value !== url
) {
return;
}

const component = getComponent(url);
if (component) {
Expand Down

0 comments on commit 93a5ca7

Please sign in to comment.