Skip to content

Commit c6d65a1

Browse files
committed
add md link
1 parent 14cb3cd commit c6d65a1

3 files changed

Lines changed: 39 additions & 2 deletions

File tree

components/block-table.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
import React from 'react';
3+
import { MarkdownLink } from './markdown-link';
34

45
export default function BlockTable({ value }: { value: { rows: { _key: string; cells: string[] }[] } }) {
56
const { rows } = value;
@@ -12,7 +13,9 @@ export default function BlockTable({ value }: { value: { rows: { _key: string; c
1213
{rows.map((row) => (
1314
<tr key={row._key}>
1415
{row.cells.map((cell, cellIndex) => (
15-
<td key={cellIndex}>{cell}</td>
16+
<td key={cellIndex}>
17+
<MarkdownLink text={cell} />
18+
</td>
1619
))}
1720
</tr>
1821
))}

components/markdown-link.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
import React from 'react';
3+
import Link from 'next/link';
4+
5+
export function MarkdownLink({ text }: { text: string }) {
6+
if (!text) return null;
7+
const regex = /\[([^\]]+)\]\(([^)]+)\)/g;
8+
const parts = text.split(regex);
9+
10+
return (
11+
<>
12+
{parts.map((part, i) => {
13+
if (i % 3 === 1) {
14+
const href = parts[i + 1];
15+
return (
16+
<Link key={i} href={href} rel="noreferrer noopener" target="_blank">
17+
{part}
18+
</Link>
19+
);
20+
}
21+
if (i % 3 === 2) {
22+
return null;
23+
}
24+
return part;
25+
})}
26+
</>
27+
);
28+
}

sanity/components/pastehandler.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,13 @@ function deserializeTableElement(
154154
}
155155

156156
const rows = Array.from(el.querySelectorAll('tr')).map((tr) => {
157-
const cells = Array.from(tr.querySelectorAll('th, td')).map((td) => td.textContent);
157+
const cells = Array.from(tr.querySelectorAll('th, td')).map((td) => {
158+
const link = td.querySelector('a');
159+
if (link) {
160+
return `[${link.textContent}](${link.href})`;
161+
}
162+
return td.textContent;
163+
});
158164
return {
159165
_type: 'row',
160166
cells,

0 commit comments

Comments
 (0)