Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(table): support align option #37

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,15 @@ md.strikethrough("Hello, World!");
// => "~~Hello, World!~~"
```

### `table(table: { rows[][], columns[] })`
### `table()`

Render a markdown table.

**Example:**

```js
md.table({
align: "center",
columns: ["Breed", "Origin", "Size", "Temperament"],
rows: [
["Abyssinian", "Egypt", "Medium", "Active"],
Expand Down
16 changes: 14 additions & 2 deletions src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export function codeBlock(
*
* ```js
* md.table({
* align: "center",
* columns: ["Breed", "Origin", "Size", "Temperament"],
* rows: [
* ["Abyssinian", "Egypt", "Medium", "Active"],
Expand All @@ -135,13 +136,24 @@ export function codeBlock(
* @param table Table object
* @param table.rows Table rows
* @param table.columns Table columns
* @param table.align Table alignment
* @returns Rendered markdown string
*
* @group render_utils
*/
export function table(table: { rows: string[][]; columns: string[] }): string {
export function table(table: {
rows: string[][];
columns: string[];
align?: "left" | "center" | "right";
}): string {
const header = `| ${table.columns.join(" | ")} |`;
const separator = `| ${table.columns.map(() => "---").join(" | ")} |`;
const separator = `| ${table.columns
.map(() => {
if (table.align === "center") return ":-:";
if (table.align === "right") return "--:";
return "---";
})
.join(" | ")} |`;
const body = table.rows.map((row) => `| ${row.join(" | ")} |`).join("\n");
return `${header}\n${separator}\n${body}`;
}
Expand Down
57 changes: 57 additions & 0 deletions test/render.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,62 @@ describe("mdbox", () => {
| American Bobtail | United States | Medium | Active |
| Applehead Siamese | Thailand | Medium | Active |"
`);
expect(
md.table({
align: "left",
columns: ["Breed", "Origin", "Size", "Temperament"],
rows: [
["Abyssinian", "Egypt", "Medium", "Active"],
["Aegean", "Greece", "Medium", "Active"],
["American Bobtail", "United States", "Medium", "Active"],
["Applehead Siamese", "Thailand", "Medium", "Active"],
],
}),
).toMatchInlineSnapshot(`
"| Breed | Origin | Size | Temperament |
| --- | --- | --- | --- |
| Abyssinian | Egypt | Medium | Active |
| Aegean | Greece | Medium | Active |
| American Bobtail | United States | Medium | Active |
| Applehead Siamese | Thailand | Medium | Active |"
`);
expect(
md.table({
align: "center",
columns: ["Breed", "Origin", "Size", "Temperament"],
rows: [
["Abyssinian", "Egypt", "Medium", "Active"],
["Aegean", "Greece", "Medium", "Active"],
["American Bobtail", "United States", "Medium", "Active"],
["Applehead Siamese", "Thailand", "Medium", "Active"],
],
}),
).toMatchInlineSnapshot(`
"| Breed | Origin | Size | Temperament |
| :-: | :-: | :-: | :-: |
| Abyssinian | Egypt | Medium | Active |
| Aegean | Greece | Medium | Active |
| American Bobtail | United States | Medium | Active |
| Applehead Siamese | Thailand | Medium | Active |"
`);
expect(
md.table({
align: "right",
columns: ["Breed", "Origin", "Size", "Temperament"],
rows: [
["Abyssinian", "Egypt", "Medium", "Active"],
["Aegean", "Greece", "Medium", "Active"],
["American Bobtail", "United States", "Medium", "Active"],
["Applehead Siamese", "Thailand", "Medium"],
],
}),
).toMatchInlineSnapshot(`
"| Breed | Origin | Size | Temperament |
| --: | --: | --: | --: |
| Abyssinian | Egypt | Medium | Active |
| Aegean | Greece | Medium | Active |
| American Bobtail | United States | Medium | Active |
| Applehead Siamese | Thailand | Medium |"
`);
});
});