mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-26 07:33:21 +03:00
feat(new tool): Markdown Table Generator
With paste as markdown capabilities Fix #542 Co-authored-by: ssdwgg <1982549567@qq.com>
This commit is contained in:
1 parent
d041556f9f
commit
81149949b7
4 files changed
+306
No files matched your search
@@ -0,0 +1,12 @@
|
|||||||
|
import { defineTool } from '../tool';
|
||||||
|
|
||||||
|
export const tool = defineTool({
|
||||||
|
name: 'Markdown Table Generator',
|
||||||
|
path: '/markdown-table-generator',
|
||||||
|
description: 'Create GitHub-flavored Markdown tables with a visual editor.',
|
||||||
|
keywords: ['markdown', 'table', 'generator', 'gfm', 'github'],
|
||||||
|
component: () => import('./markdown-table-generator.vue'),
|
||||||
|
icon: defineAsyncComponent(() => import('@vicons/tabler/es/Table')),
|
||||||
|
createdAt: new Date('2026-07-11'),
|
||||||
|
category: 'Markdown',
|
||||||
|
});
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
import { describe, expect, it } from 'vitest';
|
||||||
|
import { createMarkdownTable, escapeMarkdownTableCell, generateMarkdownTable } from './markdown-table-generator.service';
|
||||||
|
|
||||||
|
describe('markdown-table-generator service', () => {
|
||||||
|
describe('createMarkdownTable', () => {
|
||||||
|
it('creates a table with default headers and empty rows', () => {
|
||||||
|
expect(createMarkdownTable({ rows: 1, columns: 2 })).toEqual({
|
||||||
|
headers: ['Column 1', 'Column 2'],
|
||||||
|
alignments: ['left', 'left'],
|
||||||
|
rows: [['', '']],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('escapeMarkdownTableCell', () => {
|
||||||
|
it('escapes pipes and converts line breaks to br tags', () => {
|
||||||
|
expect(escapeMarkdownTableCell(' foo | bar\nbaz ')).toBe('foo \\| bar<br>baz');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('generateMarkdownTable', () => {
|
||||||
|
it('generates a markdown table with column alignment', () => {
|
||||||
|
expect(generateMarkdownTable({
|
||||||
|
headers: ['Name', 'Count', 'Notes'],
|
||||||
|
alignments: ['left', 'right', 'center'],
|
||||||
|
rows: [
|
||||||
|
['Alpha', '10', 'Ready'],
|
||||||
|
['Beta', '3', 'Needs review'],
|
||||||
|
],
|
||||||
|
})).toMatchInlineSnapshot(`
|
||||||
|
"| Name | Count | Notes |
|
||||||
|
| :--- | ---: | :---: |
|
||||||
|
| Alpha | 10 | Ready |
|
||||||
|
| Beta | 3 | Needs review |"
|
||||||
|
`);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('pads uneven rows to keep the table shape valid', () => {
|
||||||
|
expect(generateMarkdownTable({
|
||||||
|
headers: ['Name'],
|
||||||
|
alignments: ['left'],
|
||||||
|
rows: [['Alpha', 'Extra']],
|
||||||
|
})).toMatchInlineSnapshot(`
|
||||||
|
"| Name | |
|
||||||
|
| :--- | :--- |
|
||||||
|
| Alpha | Extra |"
|
||||||
|
`);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,82 @@
|
|||||||
|
import { JsonLayout, MarkdownTable2Json } from 'mdt2json';
|
||||||
|
import { translate as t } from '@/plugins/i18n.plugin';
|
||||||
|
|
||||||
|
export type MarkdownTableAlignment = 'left' | 'center' | 'right';
|
||||||
|
|
||||||
|
export interface MarkdownTable {
|
||||||
|
headers: string[];
|
||||||
|
alignments: MarkdownTableAlignment[];
|
||||||
|
rows: string[][];
|
||||||
|
}
|
||||||
|
|
||||||
|
const alignmentSeparators: Record<MarkdownTableAlignment, string> = {
|
||||||
|
left: ':---',
|
||||||
|
center: ':---:',
|
||||||
|
right: '---:',
|
||||||
|
};
|
||||||
|
|
||||||
|
export function pasteMarkdownTable(markdownContent: string): MarkdownTable | null {
|
||||||
|
const transpiler = new MarkdownTable2Json({
|
||||||
|
markdownString: markdownContent.replace(/<!--[\s\S]*?-->/g, ' '), // Remove HTML comments
|
||||||
|
layout: JsonLayout.AoS,
|
||||||
|
minify: true,
|
||||||
|
});
|
||||||
|
const output_markdown = transpiler.transform();
|
||||||
|
if ([...(output_markdown?.match(/```json/g) || [])].length > 1) {
|
||||||
|
throw new Error(t('tools.html-to-data.texts.error-multiple-json-blocks'));
|
||||||
|
}
|
||||||
|
const output_first_codeblock = output_markdown?.match(/```json\n(.*?)\n```/s)?.[1] || '[]';
|
||||||
|
const data = JSON.parse(output_first_codeblock);
|
||||||
|
return {
|
||||||
|
headers: Object.keys(data[0] || {}),
|
||||||
|
alignments: Object.keys(data[0] || {}).map(() => 'left'),
|
||||||
|
rows: data.map((row: Record<string, string>) => Object.values(row)),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createMarkdownTable({
|
||||||
|
rows = 2,
|
||||||
|
columns = 3,
|
||||||
|
}: { rows?: number; columns?: number } = {}): MarkdownTable {
|
||||||
|
return {
|
||||||
|
headers: Array.from({ length: columns }, (_, index) => `Column ${index + 1}`),
|
||||||
|
alignments: Array.from({ length: columns }, () => 'left'),
|
||||||
|
rows: Array.from({ length: rows }, () => Array.from({ length: columns }, () => '')),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function escapeMarkdownTableCell(value: string): string {
|
||||||
|
return value
|
||||||
|
.replace(/\|/g, '\\|')
|
||||||
|
.replace(/\r\n|\r|\n/g, '<br>')
|
||||||
|
.trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
function getColumnCount(table: MarkdownTable): number {
|
||||||
|
return Math.max(table.headers.length, table.alignments.length, ...table.rows.map((row) => row.length));
|
||||||
|
}
|
||||||
|
|
||||||
|
function getCells(cells: string[], columns: number): string[] {
|
||||||
|
return Array.from({ length: columns }, (_, index) => escapeMarkdownTableCell(cells[index] ?? ''));
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatRow(cells: string[]): string {
|
||||||
|
return `| ${cells.join(' | ')} |`;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function generateMarkdownTable(table: MarkdownTable): string {
|
||||||
|
const columns = getColumnCount(table);
|
||||||
|
|
||||||
|
if (columns === 0) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
const headers = getCells(table.headers, columns);
|
||||||
|
const separators = Array.from(
|
||||||
|
{ length: columns },
|
||||||
|
(_, index) => alignmentSeparators[table.alignments[index] ?? 'left'],
|
||||||
|
);
|
||||||
|
const rows = table.rows.map((row) => formatRow(getCells(row, columns)));
|
||||||
|
|
||||||
|
return [formatRow(headers), formatRow(separators), ...rows].join('\n');
|
||||||
|
}
|
||||||
@@ -0,0 +1,162 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
import {
|
||||||
|
type MarkdownTableAlignment,
|
||||||
|
createMarkdownTable,
|
||||||
|
generateMarkdownTable,
|
||||||
|
pasteMarkdownTable,
|
||||||
|
} from './markdown-table-generator.service';
|
||||||
|
import { subscribe } from '@github/paste-markdown';
|
||||||
|
import CInputText from '@/ui/c-input-text/c-input-text.vue';
|
||||||
|
|
||||||
|
const table = ref(createMarkdownTable());
|
||||||
|
const alignmentOptions: { label: string; value: MarkdownTableAlignment }[] = [
|
||||||
|
{ label: 'Left', value: 'left' },
|
||||||
|
{ label: 'Center', value: 'center' },
|
||||||
|
{ label: 'Right', value: 'right' },
|
||||||
|
];
|
||||||
|
const inputMarkdown = ref('');
|
||||||
|
const error = ref('');
|
||||||
|
const inputElement = ref<typeof CInputText>();
|
||||||
|
// Subscribe the behavior to the textarea.
|
||||||
|
onMounted(() => {
|
||||||
|
subscribe(inputElement.value?.textareaRef as never);
|
||||||
|
});
|
||||||
|
const output = computed(() => generateMarkdownTable(table.value));
|
||||||
|
|
||||||
|
function addColumn() {
|
||||||
|
const columnNumber = table.value.headers.length + 1;
|
||||||
|
table.value.headers.push(`Column ${columnNumber}`);
|
||||||
|
table.value.alignments.push('left');
|
||||||
|
table.value.rows.forEach((row) => row.push(''));
|
||||||
|
}
|
||||||
|
|
||||||
|
function removeColumn(index: number) {
|
||||||
|
if (table.value.headers.length <= 1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
table.value.headers.splice(index, 1);
|
||||||
|
table.value.alignments.splice(index, 1);
|
||||||
|
table.value.rows.forEach((row) => row.splice(index, 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
function addRow() {
|
||||||
|
table.value.rows.push(Array.from({ length: table.value.headers.length }, () => ''));
|
||||||
|
}
|
||||||
|
|
||||||
|
function removeRow(index: number) {
|
||||||
|
if (table.value.rows.length <= 1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
table.value.rows.splice(index, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
function onPasteMarkdownTable(markdownContent: string) {
|
||||||
|
error.value = '';
|
||||||
|
try {
|
||||||
|
return pasteMarkdownTable(markdownContent);
|
||||||
|
} catch (e: any) {
|
||||||
|
error.value = e.toString();
|
||||||
|
return createMarkdownTable();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<div>
|
||||||
|
<c-card>
|
||||||
|
<c-input-text
|
||||||
|
v-model:value="inputMarkdown"
|
||||||
|
label="Paste clipboard table data here:"
|
||||||
|
placeholder="Paste your table to edit..."
|
||||||
|
ref="inputElement"
|
||||||
|
multiline
|
||||||
|
rows="5"
|
||||||
|
mb-1
|
||||||
|
/>
|
||||||
|
<n-space justify="center" mb-2>
|
||||||
|
<c-button @click="table = onPasteMarkdownTable(inputMarkdown) || createMarkdownTable()"
|
||||||
|
>Import Table Data
|
||||||
|
</c-button>
|
||||||
|
</n-space>
|
||||||
|
|
||||||
|
<c-alert v-if="error" type="error" mb-2>
|
||||||
|
{{ error }}
|
||||||
|
</c-alert>
|
||||||
|
|
||||||
|
<div mb-4 flex flex-wrap items-center gap-2>
|
||||||
|
<c-button @click="addRow"> Add row </c-button>
|
||||||
|
<c-button @click="addColumn"> Add column </c-button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<n-scrollbar x-scrollable>
|
||||||
|
<n-table :bordered="false" :single-line="false" min-w-700px>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th v-for="(_, columnIndex) of table.headers" :key="columnIndex" scope="col" min-w-170px>
|
||||||
|
<div flex flex-col gap-2>
|
||||||
|
<c-input-text
|
||||||
|
v-model:value="table.headers[columnIndex]"
|
||||||
|
raw-text
|
||||||
|
:placeholder="`Column ${columnIndex + 1}`"
|
||||||
|
:test-id="`header-${columnIndex}`"
|
||||||
|
/>
|
||||||
|
<div flex items-center gap-2>
|
||||||
|
<c-select
|
||||||
|
v-model:value="table.alignments[columnIndex]"
|
||||||
|
:options="alignmentOptions"
|
||||||
|
size="small"
|
||||||
|
flex-1
|
||||||
|
/>
|
||||||
|
<c-button
|
||||||
|
circle
|
||||||
|
size="small"
|
||||||
|
variant="text"
|
||||||
|
:disabled="table.headers.length <= 1"
|
||||||
|
@click="removeColumn(columnIndex)"
|
||||||
|
>
|
||||||
|
<icon-mdi-delete-outline />
|
||||||
|
</c-button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</th>
|
||||||
|
<th scope="col" w-45px />
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<tr v-for="(row, rowIndex) of table.rows" :key="rowIndex">
|
||||||
|
<td v-for="(_, columnIndex) of table.headers" :key="columnIndex">
|
||||||
|
<c-input-text
|
||||||
|
v-model:value="row[columnIndex]"
|
||||||
|
raw-text
|
||||||
|
multiline
|
||||||
|
rows="2"
|
||||||
|
:placeholder="`Row ${rowIndex + 1}, column ${columnIndex + 1}`"
|
||||||
|
:test-id="`cell-${rowIndex}-${columnIndex}`"
|
||||||
|
/>
|
||||||
|
</td>
|
||||||
|
<td text-center>
|
||||||
|
<c-button
|
||||||
|
circle
|
||||||
|
size="small"
|
||||||
|
variant="text"
|
||||||
|
:disabled="table.rows.length <= 1"
|
||||||
|
@click="removeRow(rowIndex)"
|
||||||
|
>
|
||||||
|
<icon-mdi-delete-outline />
|
||||||
|
</c-button>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</n-table>
|
||||||
|
</n-scrollbar>
|
||||||
|
</c-card>
|
||||||
|
|
||||||
|
<n-divider />
|
||||||
|
|
||||||
|
<n-form-item label="Generated Markdown table:">
|
||||||
|
<TextareaCopyable :value="output" language="markdown" copy-placement="outside" />
|
||||||
|
</n-form-item>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
Reference in new issue
Block a user