Skip to content

Commit

Permalink
Remove ascii-table3, move Table to toolkit
Browse files Browse the repository at this point in the history
  • Loading branch information
gc committed Mar 2, 2025
1 parent 5f5827f commit 8576c5d
Show file tree
Hide file tree
Showing 8 changed files with 1,257 additions and 1,165 deletions.
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
"@sapphire/time-utilities": "^1.6.0",
"@sapphire/timer-manager": "^1.0.2",
"@sentry/node": "^8.49.0",
"ascii-table3": "^0.9.0",
"bufferutil": "^4.0.8",
"discord.js": "^14.16.3",
"dotenv": "^16.4.5",
Expand Down Expand Up @@ -80,7 +79,7 @@
"@types/node-fetch": "^2.6.1",
"@vitest/coverage-v8": "^2.1.2",
"concurrently": "^8.2.2",
"esbuild": "0.24.0",
"esbuild": "0.25.0",
"node-gyp": "^10.2.0",
"node-gyp-build": "^4.8.4",
"nodemon": "^3.1.7",
Expand All @@ -92,7 +91,7 @@
"vitest": "^2.1.9"
},
"resolutions": {
"esbuild": "0.24.0"
"esbuild": "0.25.0"
},
"packageManager": "pnpm@9.14.2+sha512.6e2baf77d06b9362294152c851c4f278ede37ab1eba3a55fda317a4a17b209f4dbb973fb250a77abc463a341fcb1f17f17cfa24091c4eb319cda0d9b84278387"
}
1 change: 1 addition & 0 deletions packages/toolkit/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export * from './util/purerand';
export * from './util/runescape';
export * from './util/string';
export * from './lib/Store';
export * from './util/markdown.js';

// External
export { default as deepMerge } from 'deepmerge';
Expand Down
101 changes: 101 additions & 0 deletions packages/toolkit/src/util/markdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { isFunction } from 'e';

export class Tab {
private title = '';
public content = '';

setTitle(title: string): this {
this.title = title;
return this;
}

setContent(content: string | (() => string)): this {
this.content = isFunction(content) ? content() : content;
return this;
}

toString(): string {
return `<TabItem label="${this.title}">
${this.content}
</TabItem>`;
}
}

export class Tabs {
private tabs: Tab[] = [];

constructor(tabs: Tab[] = []) {
this.tabs = tabs;
}

addTab(tab: Tab): this {
this.tabs.push(tab);
return this;
}

toString(): string {
const inner = this.tabs
.filter(tab => tab.content?.length > 0)
.map(tab => tab.toString())
.join('\n');
return `<Tabs>
${inner}
</Tabs>`;
}
}

export class Table {
private headers: string[] = [];
private rows: string[][] = [];

addHeader(...headers: string[]): this {
this.headers = headers;
return this;
}

addRow(...row: string[]): this {
this.rows.push(row);
return this;
}

toString(): string {
const headerLine = `| ${this.headers.join(' | ')} |`;
const separatorLine = `| ${this.headers.map(() => '---').join(' | ')} |`;
const rows = this.rows.map(row => `| ${row.join(' | ')} |`).join('\n');
return `${headerLine}\n${separatorLine}\n${rows}\n`;
}
}

export class Markdown {
private elements: (Tabs | Table | string | Markdown)[] = [];
private accordionTitle: string | null = null;

setAccordion(title: string): this {
this.accordionTitle = title;
return this;
}

addLine(str: string) {
this.elements.push(`${str}\n`);
return this;
}

add(element: Tabs | Table | string | Markdown): this {
this.elements.push(element);
return this;
}

toString(): string {
const inner = this.elements
.map(el => el.toString())
.join('\n')
.replace(/(\[\[[^\]]*?)(?<!\\):(.*?\]\])/g, '$1\\:$2');
if (this.accordionTitle) {
return `<details>
<summary>${this.accordionTitle}</summary>
${inner}
</details>`;
}
return inner;
}
}
Loading

0 comments on commit 8576c5d

Please sign in to comment.