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

Table width adjustment by dash character #816

Closed
wants to merge 3 commits into from
Closed
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
37 changes: 32 additions & 5 deletions lib/marked.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,16 @@ Lexer.prototype.token = function(src, top, bq) {
type: 'table',
header: cap[1].replace(/^ *| *\| *$/g, '').split(/ *\| */),
align: cap[2].replace(/^ *|\| *$/g, '').split(/ *\| */),
cells: cap[3].replace(/\n$/, '').split('\n')
cells: cap[3].replace(/\n$/, '').split('\n'),
width: []
};

var widthSum = 0;
var width;
for (i = 0; i < item.align.length; i++) {
width = item.align[i].split('-').length - 1;
widthSum += width;
item.width[i] = width;
if (/^ *-+: *$/.test(item.align[i])) {
item.align[i] = 'right';
} else if (/^ *:-+: *$/.test(item.align[i])) {
Expand All @@ -228,6 +234,10 @@ Lexer.prototype.token = function(src, top, bq) {
}
}

for (i = 0; i < item.width.length; i++) {
item.width[i] = item.width[i] / widthSum * 100;
}

for (i = 0; i < item.cells.length; i++) {
item.cells[i] = item.cells[i].split(/ *\| */);
}
Expand Down Expand Up @@ -385,10 +395,16 @@ Lexer.prototype.token = function(src, top, bq) {
type: 'table',
header: cap[1].replace(/^ *| *\| *$/g, '').split(/ *\| */),
align: cap[2].replace(/^ *|\| *$/g, '').split(/ *\| */),
cells: cap[3].replace(/(?: *\| *)?\n$/, '').split('\n')
cells: cap[3].replace(/(?: *\| *)?\n$/, '').split('\n'),
width: []
};

var widthSum = 0;
var width;
for (i = 0; i < item.align.length; i++) {
width = item.align[i].split('-').length - 1;
widthSum += width;
item.width[i] = width;
if (/^ *-+: *$/.test(item.align[i])) {
item.align[i] = 'right';
} else if (/^ *:-+: *$/.test(item.align[i])) {
Expand All @@ -400,6 +416,10 @@ Lexer.prototype.token = function(src, top, bq) {
}
}

for (i = 0; i < item.width.length; i++) {
item.width[i] = item.width[i] / widthSum * 100;
}

for (i = 0; i < item.cells.length; i++) {
item.cells[i] = item.cells[i]
.replace(/^ *\| *| *\| *$/g, '')
Expand Down Expand Up @@ -839,8 +859,15 @@ Renderer.prototype.tablerow = function(content) {

Renderer.prototype.tablecell = function(content, flags) {
var type = flags.header ? 'th' : 'td';
var tag = flags.align
? '<' + type + ' style="text-align:' + flags.align + '">'
var style = [];
if (flags.align) {
style.push('text-align:' + flags.align);
}
if (this.options.tableWidth && flags.width) {
style.push('width:' + flags.width + '%');
}
var tag = style.length
? '<' + type + ' style="' + style.join(';') + '">'
: '<' + type + '>';
return tag + content + '</' + type + '>\n';
};
Expand Down Expand Up @@ -1006,7 +1033,7 @@ Parser.prototype.tok = function() {
flags = { header: true, align: this.token.align[i] };
cell += this.renderer.tablecell(
this.inline.output(this.token.header[i]),
{ header: true, align: this.token.align[i] }
{ header: true, align: this.token.align[i], width: this.token.width[i] }
);
}
header += this.renderer.tablerow(cell);
Expand Down
39 changes: 39 additions & 0 deletions test/width/width.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

const tableMd = `
Header 1 | Header 2
-- | ---
Cell 1 | Cell 2
Cell 3 | Cell 4

Header 1|Header 2|Header 3|Header 4
:--- |:--: |--: |---
Cell 1 |Cell 2 |Cell 3 |Cell 4
*Cell 5*|Cell 6 |Cell 7 |Cell 8

`;

const marked = require('../../lib/marked');
const Renderer = marked.Renderer;

class MyRenderer extends Renderer {

table(header, body) {
return super.table(header, body);
}

tablerow(content) {
return super.tablerow(content);
}

tablecell(content, flags) {
return super.tablecell(content, flags);
}

}

marked.setOptions({
tableWidth: true,
renderer: new MyRenderer
});

console.log(marked(tableMd));
43 changes: 43 additions & 0 deletions test/width/width.result.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<table>
<thead>
<tr>
<th style="width:40%">Header 1</th>
<th style="width:60%">Header 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
</tr>
<tr>
<td>Cell 3</td>
<td>Cell 4</td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th style="text-align:left;width:30%">Header 1</th>
<th style="text-align:center;width:20%">Header 2</th>
<th style="text-align:right;width:20%">Header 3</th>
<th style="width:30%">Header 4</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">Cell 1</td>
<td style="text-align:center">Cell 2</td>
<td style="text-align:right">Cell 3</td>
<td>Cell 4</td>
</tr>
<tr>
<td style="text-align:left"><em>Cell 5</em></td>
<td style="text-align:center">Cell 6</td>
<td style="text-align:right">Cell 7</td>
<td>Cell 8</td>
</tr>
</tbody>
</table>