Spaces:
Paused
Paused
File size: 5,703 Bytes
f6a5256 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | /**
* Markdown renderer for the nextgen architecture docs.
* Adapted from components/vitis/Markdown.tsx (which is read-only for this
* feature) with GFM table support added — the architecture docs are
* table-heavy comparison matrices.
*/
import Box from '@mui/material/Box';
import Table from '@mui/material/Table';
import TableBody from '@mui/material/TableBody';
import TableCell from '@mui/material/TableCell';
import TableHead from '@mui/material/TableHead';
import TableRow from '@mui/material/TableRow';
import Typography from '@mui/material/Typography';
function Inline({ text }: { text: string }) {
const parts = text.split(/(\*\*[^*]+\*\*|`[^`]+`)/g).filter(Boolean);
return (
<>
{parts.map((p, i) => {
if (p.startsWith('**') && p.endsWith('**')) {
return <strong key={i}>{p.slice(2, -2)}</strong>;
}
if (p.startsWith('`') && p.endsWith('`')) {
return (
<Box
key={i}
component="code"
sx={{ bgcolor: '#232a31', px: 0.5, borderRadius: 0.5, fontFamily: 'monospace', fontSize: '0.9em' }}
>
{p.slice(1, -1)}
</Box>
);
}
return <span key={i}>{p}</span>;
})}
</>
);
}
function splitRow(line: string): string[] {
return line.replace(/^\s*\|/, '').replace(/\|\s*$/, '').split('|').map((c) => c.trim());
}
export default function Markdown({ source }: { source: string }) {
const lines = source.split('\n');
const blocks: React.ReactNode[] = [];
let i = 0;
let key = 0;
while (i < lines.length) {
const line = lines[i];
if (line.startsWith('```')) {
const code: string[] = [];
i += 1;
while (i < lines.length && !lines[i].startsWith('```')) {
code.push(lines[i]);
i += 1;
}
i += 1;
blocks.push(
<Box
key={key++}
component="pre"
sx={{
bgcolor: '#0d1117',
border: '1px solid #232a31',
borderRadius: 1,
p: 1.5,
overflowX: 'auto',
fontSize: 11.5,
fontFamily: 'monospace',
lineHeight: 1.35,
my: 1,
}}
>
{code.join('\n')}
</Box>
);
continue;
}
if (/^\s*\|.*\|\s*$/.test(line) && i + 1 < lines.length && /^\s*\|[\s:|-]+\|\s*$/.test(lines[i + 1])) {
const header = splitRow(line);
i += 2;
const rows: string[][] = [];
while (i < lines.length && /^\s*\|.*\|\s*$/.test(lines[i])) {
rows.push(splitRow(lines[i]));
i += 1;
}
blocks.push(
<Box key={key++} sx={{ overflowX: 'auto', my: 1, border: '1px solid #232a31', borderRadius: 1 }}>
<Table size="small" sx={{ minWidth: 480 }}>
<TableHead>
<TableRow>
{header.map((h, j) => (
<TableCell key={j} sx={{ fontWeight: 800, fontSize: 11, color: '#e0e3e7', bgcolor: '#12171d' }}>
<Inline text={h} />
</TableCell>
))}
</TableRow>
</TableHead>
<TableBody>
{rows.map((r, ri) => (
<TableRow key={ri}>
{r.map((c, ci) => (
<TableCell key={ci} sx={{ fontSize: 11, color: '#c7ccd1', verticalAlign: 'top' }}>
<Inline text={c} />
</TableCell>
))}
</TableRow>
))}
</TableBody>
</Table>
</Box>
);
continue;
}
const h = line.match(/^(#{1,4})\s+(.*)$/);
if (h) {
const level = h[1].length;
blocks.push(
<Typography
key={key++}
variant={level === 1 ? 'h5' : level === 2 ? 'h6' : 'subtitle1'}
sx={{ fontWeight: 800, mt: level === 1 ? 0.5 : 2, mb: 0.75 }}
>
<Inline text={h[2]} />
</Typography>
);
i += 1;
continue;
}
if (/^\s*[-*]\s+/.test(line)) {
const items: string[] = [];
while (i < lines.length && /^\s*[-*]\s+/.test(lines[i])) {
items.push(lines[i].replace(/^\s*[-*]\s+/, ''));
i += 1;
}
blocks.push(
<Box key={key++} component="ul" sx={{ my: 0.5, pl: 3 }}>
{items.map((it, j) => (
<Typography key={j} component="li" variant="body2" sx={{ mb: 0.25, color: '#c7ccd1' }}>
<Inline text={it} />
</Typography>
))}
</Box>
);
continue;
}
if (/^\s*\d+\.\s+/.test(line)) {
const items: string[] = [];
while (i < lines.length && /^\s*\d+\.\s+/.test(lines[i])) {
items.push(lines[i].replace(/^\s*\d+\.\s+/, ''));
i += 1;
}
blocks.push(
<Box key={key++} component="ol" sx={{ my: 0.5, pl: 3 }}>
{items.map((it, j) => (
<Typography key={j} component="li" variant="body2" sx={{ mb: 0.25, color: '#c7ccd1' }}>
<Inline text={it} />
</Typography>
))}
</Box>
);
continue;
}
if (line.trim() === '' || /^---+$/.test(line.trim())) {
i += 1;
continue;
}
const para: string[] = [line];
i += 1;
while (
i < lines.length &&
lines[i].trim() !== '' &&
!/^(#{1,4}\s|```|\s*[-*]\s|\s*\d+\.\s|\s*\|)/.test(lines[i])
) {
para.push(lines[i]);
i += 1;
}
blocks.push(
<Typography key={key++} variant="body2" sx={{ mb: 1, color: '#c7ccd1', lineHeight: 1.55 }}>
<Inline text={para.join(' ')} />
</Typography>
);
}
return <Box>{blocks}</Box>;
}
|