-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Enhance Markdown table rendering with styled extension #3802
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* Markdown Table Styles */ | ||
| .markdown-table { | ||
| border-collapse: collapse; | ||
| width: 100%; | ||
| margin: 1em 0; | ||
| font-size: 14px; | ||
| box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1); | ||
| border-radius: 8px; | ||
| overflow: hidden; | ||
| } | ||
|
|
||
| .markdown-table th { | ||
| background: linear-gradient(135deg, #4a90e2 0%, #357abd 100%); | ||
| color: white; | ||
| font-weight: bold; | ||
| text-align: center; | ||
| padding: 12px 8px; | ||
| border: none; | ||
| } | ||
|
|
||
| .markdown-table td { | ||
| padding: 12px 8px; | ||
| text-align: center; | ||
| border: 1px solid #e0e0e0; | ||
| } | ||
|
|
||
| .markdown-table tr:nth-child(even) { | ||
| background-color: #f8f9fa; | ||
| } | ||
|
|
||
| .markdown-table tr:nth-child(odd) { | ||
| background-color: #ffffff; | ||
| } | ||
|
|
||
| .markdown-table tr:hover { | ||
| background-color: #e3f2fd; | ||
| transition: background-color 0.2s ease; | ||
| } | ||
|
|
||
| .markdown-table tbody tr:last-child td { | ||
| border-bottom: none; | ||
| } | ||
|
|
||
| /* Responsive Design */ | ||
| @media (max-width: 768px) { | ||
| .markdown-table { | ||
| font-size: 12px; | ||
| } | ||
|
|
||
| .markdown-table th, | ||
| .markdown-table td { | ||
| padding: 8px 4px; | ||
| } | ||
| } | ||
|
|
||
| @media (max-width: 480px) { | ||
| .markdown-table { | ||
| font-size: 11px; | ||
| } | ||
|
|
||
| .markdown-table th, | ||
| .markdown-table td { | ||
| padding: 6px 2px; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,49 @@ | ||
| const tableExtension = { | ||
| renderer: { | ||
| table(token) { | ||
| let header = ''; | ||
|
|
||
| // header | ||
| let cell = ''; | ||
| for (let j = 0; j < token.header.length; j++) { | ||
| cell += this.tablecell(token.header[j]); | ||
| } | ||
| header += this.tablerow({ text: cell }); | ||
|
|
||
| let body = ''; | ||
| for (let j = 0; j < token.rows.length; j++) { | ||
| const row = token.rows[j]; | ||
|
|
||
| cell = ''; | ||
| for (let k = 0; k < row.length; k++) { | ||
| cell += this.tablecell(row[k]); | ||
| } | ||
|
|
||
| body += this.tablerow({ text: cell }); | ||
| } | ||
| if (body) body = `<tbody>${body}</tbody>`; | ||
|
|
||
| return '<table class="markdown-table">\n' | ||
| + '<thead>\n' | ||
| + header | ||
| + '</thead>\n' | ||
| + body | ||
| + '</table>\n'; | ||
| }, | ||
|
Comment on lines
+3
to
+32
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current implementation of the table(token) {
const headerCells = token.header
.map(headerCell => this.tablecell(headerCell))
.join('');
const header = this.tablerow({ text: headerCells });
const bodyRows = token.rows
.map(row => {
const rowCells = row.map(cell => this.tablecell(cell)).join('');
return this.tablerow({ text: rowCells });
})
.join('');
const body = bodyRows ? `<tbody>${bodyRows}</tbody>` : '';
return '<table class="markdown-table">\n'
+ '<thead>\n'
+ header
+ '</thead>\n'
+ body
+ '</table>\n';
} |
||
| tablerow({ text }) { | ||
| return `<tr class="markdown-table-row">\n${text}</tr>\n`; | ||
| }, | ||
| tablecell(token) { | ||
| const content = this.parser.parseInline(token.tokens); | ||
| const type = token.header ? 'th' : 'td'; | ||
| const tag = token.align | ||
| ? `<${type} align="${token.align}">` | ||
| : `<${type}>`; | ||
| return tag + content + `</${type}>\n`; | ||
| }, | ||
| }, | ||
| }; | ||
|
Comment on lines
+1
to
+45
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
|
|
||
| const versionCache = {}; | ||
| let currentVersion; | ||
|
|
||
|
|
@@ -62,6 +108,10 @@ function parse(e) { | |
| } | ||
| case 'parse': { | ||
| const marked = versionCache[currentVersion]; | ||
| // Use table extension for local version | ||
| if (currentVersion === '../') { | ||
| marked.use(tableExtension); | ||
| } | ||
| // marked 0.0.1 had tokens array as the second parameter of lexer and no options | ||
| const options = currentVersion.endsWith('@0.0.1') ? [] : mergeOptions(e.data.options); | ||
| const startTime = new Date(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import type { MarkedExtension } from './MarkedOptions.ts'; | ||
| import type { Tokens } from './Tokens.ts'; | ||
|
|
||
| export const tableExtension: MarkedExtension = { | ||
| renderer: { | ||
| table(token: Tokens.Table): string { | ||
| let header = ''; | ||
|
|
||
| // header | ||
| let cell = ''; | ||
| for (let j = 0; j < token.header.length; j++) { | ||
| cell += this.tablecell(token.header[j]); | ||
| } | ||
| header += this.tablerow({ text: cell }); | ||
|
|
||
| let body = ''; | ||
| for (let j = 0; j < token.rows.length; j++) { | ||
| const row = token.rows[j]; | ||
|
|
||
| cell = ''; | ||
| for (let k = 0; k < row.length; k++) { | ||
| cell += this.tablecell(row[k]); | ||
| } | ||
|
|
||
| body += this.tablerow({ text: cell }); | ||
| } | ||
| if (body) body = `<tbody>${body}</tbody>`; | ||
|
|
||
| return '<table class="markdown-table">\n' | ||
| + '<thead>\n' | ||
| + header | ||
| + '</thead>\n' | ||
| + body | ||
| + '</table>\n'; | ||
| }, | ||
|
Comment on lines
+6
to
+35
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current implementation of the table(token: Tokens.Table): string {
const headerCells = token.header
.map(headerCell => this.tablecell(headerCell))
.join('');
const header = this.tablerow({ text: headerCells });
const bodyRows = token.rows
.map(row => {
const rowCells = row.map(cell => this.tablecell(cell)).join('');
return this.tablerow({ text: rowCells });
})
.join('');
const body = bodyRows ? `<tbody>${bodyRows}</tbody>` : '';
return '<table class="markdown-table">\n'
+ '<thead>\n'
+ header
+ '</thead>\n'
+ body
+ '</table>\n';
} |
||
| tablerow({ text }: Tokens.TableRow): string { | ||
| return `<tr class="markdown-table-row">\n${text}</tr>\n`; | ||
| }, | ||
| tablecell(token: Tokens.TableCell): string { | ||
| const content = this.parser.parseInline(token.tokens); | ||
| const type = token.header ? 'th' : 'td'; | ||
| const tag = token.align | ||
| ? `<${type} align="${token.align}">` | ||
| : `<${type}>`; | ||
| return tag + content + `</${type}>\n`; | ||
| }, | ||
| }, | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The zebra striping and hover effects are currently applied to all table rows (
tr), including those in the table header (thead). This is likely unintentional and can be avoided by making the CSS selectors more specific. To ensure these styles only apply to the data rows in the table body, you should scope the selectors totbody.