@@ -2,11 +2,17 @@ import { Fragment, type ReactNode } from 'react';
22
33// A small, safe Markdown renderer for member articles: headings (## and ###),
44// paragraphs, bullet and numbered lists, block quotes, fenced code blocks,
5- // and inline `code`, **bold**, *italic* and [links](https://...). It builds
6- // React elements and never inserts HTML, so nothing an author writes can run
7- // as script. Only https links are made clickable.
5+ // simple tables, and inline `code`, **bold**, *italic* and
6+ // [links](https://...). It builds React elements and never inserts HTML, so
7+ // nothing an author writes can run as script. Only https links and links to
8+ // pages on this site (starting with "/") are made clickable.
89
9- const INLINE = / ( ` [ ^ ` ] + ` ) | ( \* \* [ ^ * ] + \* \* ) | ( \* [ ^ * ] + \* ) | ( \[ [ ^ \] ] + \] \( h t t p s : \/ \/ [ ^ ) \s ] + \) ) / g;
10+ /** "Writing a good PR" → "writing-a-good-pr", for heading links */
11+ export function headingId ( text : string ) : string {
12+ return text . toLowerCase ( ) . replace ( / [ ` * ] / g, '' ) . replace ( / [ ^ a - z 0 - 9 ] + / g, '-' ) . replace ( / ^ - | - $ / g, '' ) ;
13+ }
14+
15+ const INLINE = / ( ` [ ^ ` ] + ` ) | ( \* \* (?: \[ [ ^ \] ] + \] \( [ ^ ) \s ] + \) | [ ^ * ] ) + \* \* ) | ( \* [ ^ * ] + \* ) | ( \[ [ ^ \] ] + \] \( (?: h t t p s : \/ \/ | \/ (? ! \/ ) ) [ ^ ) \s ] * \) ) / g;
1016
1117function inline ( text : string ) : ReactNode [ ] {
1218 const parts : ReactNode [ ] = [ ] ;
@@ -16,11 +22,13 @@ function inline(text: string): ReactNode[] {
1622 const token = match [ 0 ] ;
1723 const key = parts . length ;
1824 if ( token . startsWith ( '`' ) ) parts . push ( < code key = { key } className = "rounded bg-muted px-1 py-0.5 text-sm" > { token . slice ( 1 , - 1 ) } </ code > ) ;
19- else if ( token . startsWith ( '**' ) ) parts . push ( < strong key = { key } > { token . slice ( 2 , - 2 ) } </ strong > ) ;
25+ else if ( token . startsWith ( '**' ) ) parts . push ( < strong key = { key } > { inline ( token . slice ( 2 , - 2 ) ) } </ strong > ) ;
2026 else if ( token . startsWith ( '*' ) ) parts . push ( < em key = { key } > { token . slice ( 1 , - 1 ) } </ em > ) ;
2127 else {
2228 const [ , label , href ] = token . match ( / ^ \[ ( [ ^ \] ] + ) \] \( ( [ ^ ) ] + ) \) $ / ) ! ;
23- parts . push ( < a key = { key } href = { href } target = "_blank" rel = "nofollow ugc noopener noreferrer" className = "text-primary underline" > { label } </ a > ) ;
29+ parts . push ( href . startsWith ( '/' )
30+ ? < a key = { key } href = { href } className = "text-primary underline" > { label } </ a >
31+ : < a key = { key } href = { href } target = "_blank" rel = "nofollow ugc noopener noreferrer" className = "text-primary underline" > { label } </ a > ) ;
2432 }
2533 last = match . index + token . length ;
2634 }
@@ -42,10 +50,10 @@ export default function Markdown({ text }: { text: string }) {
4250 i ++ ;
4351 blocks . push ( < pre key = { key } className = "overflow-x-auto rounded-md bg-muted p-3 text-sm" > < code > { code . join ( '\n' ) } </ code > </ pre > ) ;
4452 } else if ( / ^ # # # \s / . test ( line ) ) {
45- blocks . push ( < h3 key = { key } className = "mt-6 text-lg font-semibold" > { inline ( line . slice ( 4 ) ) } </ h3 > ) ;
53+ blocks . push ( < h3 key = { key } id = { headingId ( line . slice ( 4 ) ) } className = "mt-6 scroll-mt-20 text-lg font-semibold" > { inline ( line . slice ( 4 ) ) } </ h3 > ) ;
4654 i ++ ;
4755 } else if ( / ^ # # ? \s / . test ( line ) ) {
48- blocks . push ( < h2 key = { key } className = "mt-8 text-xl font-semibold" > { inline ( line . replace ( / ^ # # ? \s / , '' ) ) } </ h2 > ) ;
56+ blocks . push ( < h2 key = { key } id = { headingId ( line . replace ( / ^ # # ? \s / , '' ) ) } className = "mt-8 scroll-mt-20 text-xl font-semibold" > { inline ( line . replace ( / ^ # # ? \s / , '' ) ) } </ h2 > ) ;
4957 i ++ ;
5058 } else if ( / ^ \s * [ - * ] \s / . test ( line ) ) {
5159 const items : string [ ] = [ ] ;
@@ -55,6 +63,21 @@ export default function Markdown({ text }: { text: string }) {
5563 const items : string [ ] = [ ] ;
5664 while ( i < lines . length && / ^ \s * \d + \. \s / . test ( lines [ i ] ) ) items . push ( lines [ i ++ ] . replace ( / ^ \s * \d + \. \s / , '' ) ) ;
5765 blocks . push ( < ol key = { key } className = "list-decimal space-y-1 pl-6" > { items . map ( ( item , j ) => < li key = { j } > { inline ( item ) } </ li > ) } </ ol > ) ;
66+ } else if ( line . startsWith ( '|' ) ) {
67+ const rows : string [ ] [ ] = [ ] ;
68+ while ( i < lines . length && lines [ i ] . startsWith ( '|' ) ) {
69+ const cells = lines [ i ++ ] . replace ( / ^ \| | \| $ / g, '' ) . split ( '|' ) . map ( cell => cell . trim ( ) ) ;
70+ if ( ! cells . every ( cell => / ^ : ? - + : ? $ / . test ( cell ) ) ) rows . push ( cells ) ;
71+ }
72+ const [ head , ...body ] = rows ;
73+ blocks . push (
74+ < div key = { key } className = "overflow-x-auto" >
75+ < table className = "w-full border-collapse text-sm" >
76+ < thead > < tr > { head . map ( ( cell , j ) => < th key = { j } className = "border-b p-2 text-left font-semibold" > { inline ( cell ) } </ th > ) } </ tr > </ thead >
77+ < tbody > { body . map ( ( row , r ) => < tr key = { r } > { row . map ( ( cell , j ) => < td key = { j } className = "border-b p-2 align-top" > { inline ( cell ) } </ td > ) } </ tr > ) } </ tbody >
78+ </ table >
79+ </ div > ,
80+ ) ;
5881 } else if ( line . startsWith ( '>' ) ) {
5982 const quote : string [ ] = [ ] ;
6083 while ( i < lines . length && lines [ i ] . startsWith ( '>' ) ) quote . push ( lines [ i ++ ] . replace ( / ^ > \s ? / , '' ) ) ;
@@ -63,7 +86,7 @@ export default function Markdown({ text }: { text: string }) {
6386 i ++ ;
6487 } else {
6588 const paragraph : string [ ] = [ ] ;
66- while ( i < lines . length && lines [ i ] . trim ( ) && ! / ^ ( ` ` ` | # { 2 , 3 } \s | \s * [ - * ] \s | \s * \d + \. \s | > ) / . test ( lines [ i ] ) ) paragraph . push ( lines [ i ++ ] ) ;
89+ while ( i < lines . length && lines [ i ] . trim ( ) && ! / ^ ( ` ` ` | # { 2 , 3 } \s | \s * [ - * ] \s | \s * \d + \. \s | > | \| ) / . test ( lines [ i ] ) ) paragraph . push ( lines [ i ++ ] ) ;
6790 blocks . push ( < p key = { key } > { paragraph . map ( ( part , j ) => < Fragment key = { j } > { j > 0 && ' ' } { inline ( part ) } </ Fragment > ) } </ p > ) ;
6891 }
6992 }
0 commit comments