on master help sign in
markdown: wrap links in next/link
commit99a4a982209d7543f754ada6272c240706652210 authorMatilde Park <matilde.park@urbit.org> dateMon 13 Jun 2022, 14:58 (4 years ago) parentd96103d
2 files changed+13−2
components/Markdown.js+11 components/schema/link.markdoc.js+2−2
components/Markdown.js+11
@@ -1,4 +1,5 @@
11import React from "react";
2+import Link from "next/link";
23import Markdoc, { Ast, Node, Tag, Raw } from "@markdoc/markdoc";
34import { heading } from "./schema/Heading.markdoc";
45import { footnoteRef } from "./schema/footnoteRef.markdoc";
@@ -126,6 +127,14 @@ const superscript = ({ children }) => <sup>{children}</sup>;
126127
127128const CustomFence = ({ children }) => <pre>{children}</pre>;
128129
130+const NextLink = ({ href, target, children }) => {
131+ return (
132+ <Link href={href} passHref>
133+ <a target={target}>{children}</a>
134+ </Link>
135+ );
136+};
137+
129138export function MarkdownParse({ post }) {
130139 const tokeniser = new Markdoc.Tokenizer({ html: true, linkify: true });
131140 const tokens = tokeniser.tokenize(post.content);
@@ -150,6 +159,7 @@ export function MarkdownParse({ post }) {
150159 customFence,
151160 RenderHtml,
152161 superscript,
162+ NextLink,
153163 },
154164 });
155165}
@@ -164,6 +174,7 @@ export default function Markdown({ content }) {
164174 Callout,
165175 CustomFence,
166176 RenderHtml,
177+ NextLink,
167178 },
168179 });
169180}
components/schema/link.markdoc.js+2−2
@@ -15,7 +15,7 @@ function checkIfExternal(attributes) {
1515}
1616
1717export const link = {
18− render: "a",
18+ render: "NextLink",
1919 children: ["strong", "em", "s", "code", "text", "tag"],
2020 attributes: {
2121 href: { type: String, required: true },
@@ -28,6 +28,6 @@ export const link = {
2828
2929 const target = checkIfExternal(attributes);
3030
31− return new Tag(`a`, { ...attributes, target }, children);
31+ return new Tag("NextLink", { ...attributes, target }, children);
3232 },
3333};