Skip to content
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
19 changes: 16 additions & 3 deletions src/plugins/rehype/external-links.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
import rehypeExternalLinks, { type Options } from "rehype-external-links";
import type { Element } from "hast";

function hasImgChild(node: Element): boolean {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hard to imagine ever intentionally linking over more than just a single image child, but I guess in those cases you also probably don't want . Fair enough!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I admittedly stole it from rehype-external-links themselves - they have it in their tests: https://github.com/rehypejs/rehype-external-links/blob/b0ada86b178e135d55368ffa47b915db17219716/test.js#L388-L398

return node.children.some(
(child) => child.type === "element" && child.tagName === "img",
);
}

export const externalLinkArrow = " ↗";

export const rehypeExternalLinksOptions = {
content: {
type: "text",
value: " ↗",
content: (element) => {
if (!hasImgChild(element)) {
return {
type: "text",
value: externalLinkArrow,
};
}
},
contentProperties: {
class: "external-link",
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/rehype/heading-slugs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { toString } from "hast-util-to-string";
import { visit } from "unist-util-visit";
import GithubSlugger from "github-slugger";
import { rehypeExternalLinksOptions } from "./external-links";
import { externalLinkArrow } from "./external-links";
import type { Root } from "hast";
import type { MdxTextExpression } from "mdast-util-mdx-expression";

Expand Down Expand Up @@ -34,7 +34,7 @@ export default function () {
} else {
if (!element.properties.id) {
const string = toString(element)
.replaceAll(rehypeExternalLinksOptions.content.value, "")
.replaceAll(externalLinkArrow, "")
.trimEnd();

element.properties.id = slugs.slug(string);
Expand Down
27 changes: 26 additions & 1 deletion src/plugins/rehype/index.node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ describe("heading-slugs", () => {
.data("settings", {
fragment: true,
})
.use([rehypeParse, rehypeHeadingSlugs, rehypeStringify])
.use([
rehypeParse,
rehypeExternalLinks,
rehypeHeadingSlugs,
rehypeStringify,
])
.process(html);

return file.toString();
Expand All @@ -32,6 +37,16 @@ describe("heading-slugs", () => {

expect(text).toMatchInlineSnapshot(`"<h2 id="bar">foo</h2>"`);
});

test("does not add arrow if image children", async () => {
const text = await process(
'<h2 id="bar"><a href="https://example.com">foo</a></h2>',
);

expect(text).toMatchInlineSnapshot(
`"<h2 id="bar"><a href="https://example.com" target="_blank" rel="noopener">foo<span class="external-link"> ↗</span></a></h2>"`,
);
});
});

describe("external-links", () => {
Expand Down Expand Up @@ -59,6 +74,16 @@ describe("external-links", () => {

expect(text).toMatchInlineSnapshot(`"<a href="/">foo</a>"`);
});

test("does not add arrow if image children", async () => {
const text = await process(
'<a href="https://example.com"><img src="/image.jpg" /></a>',
);

expect(text).toMatchInlineSnapshot(
`"<a href="https://example.com" target="_blank" rel="noopener"><img src="/image.jpg"></a>"`,
);
});
});

describe("autolink-headings", () => {
Expand Down
6 changes: 2 additions & 4 deletions src/util/props.node.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, test } from "vitest";
import { generateDescription } from "./props";
import { rehypeExternalLinksOptions } from "~/plugins/rehype/external-links";
import { externalLinkArrow } from "~/plugins/rehype/external-links";

describe("description", () => {
describe("markdown", () => {
Expand All @@ -13,10 +13,8 @@ describe("description", () => {
});

test("removes external link icon", async () => {
const icon = rehypeExternalLinksOptions.content.value;

const desc = await generateDescription({
markdown: `[links${icon}](/) and **${icon}stuff**`,
markdown: `[links${externalLinkArrow}](/) and **${externalLinkArrow}stuff**`,
});

expect(desc).toEqual("links and \\*\\*stuff\\*\\*");
Expand Down
6 changes: 2 additions & 4 deletions src/util/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { parse } from "node-html-parser";
import he from "he";
import { remark } from "remark";
import strip from "strip-markdown";
import { rehypeExternalLinksOptions } from "~/plugins/rehype/external-links";
import { externalLinkArrow } from "~/plugins/rehype/external-links";

type TableOfContentsItems = NonNullable<StarlightRouteData["toc"]>["items"];

Expand Down Expand Up @@ -84,7 +84,5 @@ export async function generateDescription({
if (paragraph) description = he.decode(paragraph.innerText);
}

return description
?.replaceAll(rehypeExternalLinksOptions.content.value, "")
.trim();
return description?.replaceAll(externalLinkArrow, "").trim();
}
4 changes: 2 additions & 2 deletions src/util/sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { AstroGlobal } from "astro";
import type { StarlightRouteData } from "@astrojs/starlight/route-data";

import { getEntry, getCollection } from "astro:content";
import { rehypeExternalLinksOptions } from "~/plugins/rehype/external-links";
import { externalLinkArrow } from "~/plugins/rehype/external-links";

type Link = Extract<StarlightRouteData["sidebar"][0], { type: "link" }> & {
order?: number;
Expand Down Expand Up @@ -280,7 +280,7 @@ async function handleLink(link: Link): Promise<Link> {
if (frontmatter.external_link && !frontmatter.sidebar.group?.hideIndex) {
return {
...link,
label: link.label.concat(rehypeExternalLinksOptions.content.value),
label: link.label.concat(externalLinkArrow),
href: frontmatter.external_link,
badge: frontmatter.external_link.startsWith("/api")
? {
Expand Down