File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11
22import React from 'react' ;
3+ import { MarkdownLink } from './markdown-link' ;
34
45export 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 ) ) }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff 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,
You can’t perform that action at this time.
0 commit comments