ttcMD: improves table with full width

This commit is contained in:
Emmaline Autumn 2024-03-16 03:19:18 -06:00
parent 2e7eaccde8
commit a72741e4d1

View File

@ -502,7 +502,6 @@ export const buildOnlyDefaultElements = () => {
return ( return (
<div className="p"> <div className="p">
{children?.map((e) => { {children?.map((e) => {
console.log(e);
return <Fragment key={e.uuid}>{e.render(e)}</Fragment>; return <Fragment key={e.uuid}>{e.render(e)}</Fragment>;
})} })}
</div> </div>
@ -564,9 +563,24 @@ export const buildOnlyDefaultElements = () => {
registerIdentifier( registerIdentifier(
"table", "table",
/^\|\s[\s\S]*?\|(?=(\n\n)|$)/g, /(?<=\n|^)\| [\s\S]*? \|(?=(\n|$)(?!\|))/g,
(s) => { (s) => {
const rowSections = s.split(/^\|[|-\s]+\|$/gm).map((s) => const splitMarker = "{{^^}}";
const original = s;
let columnPattern: string[] = [];
let fullWidth = false;
s = s.replace(/^\| (<-)?[-|\s^]+(->)? \|$/gm, (e) => {
if (!columnPattern.length) {
columnPattern = e.split("|").filter((e) => e);
}
if (e.match(/^\| <-.*?-> \|/gm)) {
fullWidth = true;
}
return splitMarker;
});
const rowSections = s.split(splitMarker).map((s) =>
s s
.split("\n") .split("\n")
.filter((r) => !!r) .filter((r) => !!r)
@ -602,22 +616,34 @@ export const buildOnlyDefaultElements = () => {
); );
return { return {
content: s, content: original,
raw: s, raw: original,
metadata: { metadata: {
headerRows: headerRows, headerRows: headerRows,
bodyRows: bodyRows, bodyRows: bodyRows,
footerRows: footerRows, footerRows: footerRows,
columns: maxColumns, columns: maxColumns,
columnPattern,
fullWidth,
}, },
uuid: crypto.randomUUID(), uuid: crypto.randomUUID(),
}; };
}, },
(t) => { (t) => {
const { headerRows, bodyRows, footerRows, columns } = t.metadata; const {
headerRows,
bodyRows,
footerRows,
columns,
columnPattern,
fullWidth,
} = t.metadata;
return ( return (
<table className="md-table"> <table
data-full-width={fullWidth}
className="md-table data-[full-width=true]:w-full"
>
{!!headerRows && ( {!!headerRows && (
<thead> <thead>
{headerRows.map((r, i) => ( {headerRows.map((r, i) => (
@ -638,10 +664,15 @@ export const buildOnlyDefaultElements = () => {
<tbody> <tbody>
{bodyRows.map((r, i) => ( {bodyRows.map((r, i) => (
<tr key={r.join() + i}> <tr key={r.join() + i}>
{r.concat(Array(columns - r.length).fill("")).map((c) => { {r.concat(Array(columns - r.length).fill("")).map((c, i) => {
const child = t.children?.find((child) => child.raw === c); const child = t.children?.find((child) => child.raw === c);
return ( return (
<td key={r.join() + i + c}> <td
key={r.join() + i + c}
className="data-[center=true]:text-center"
data-center={!!(columnPattern?.at(i) &&
columnPattern.at(i)?.includes("^"))}
>
{child?.render(child) || c} {child?.render(child) || c}
</td> </td>
); );