Skip to content

Commit

Permalink
fix: zero size <th> instead of remove when it's empty (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
ikeq authored Jan 25, 2020
1 parent 34edfff commit 776671e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 21 deletions.
20 changes: 14 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@ cache:
node_js:
- "10"

before_script:
- rm -rf node_modules
- rm -rf .git
- git clone https://github.com/hexojs/hexo-theme-unit-test temp
- mkdir temp/themes
- mkdir temp/themes/landscape
- rsync -av ./ ./temp/themes/landscape --exclude ./temp/
- cd temp
- npm install
- cd themes/landscape
- npm install

script:
- git clone https://github.com/hexojs/hexo-theme-unit-test generate_test
- git clone https://github.com/ikeq/hexo-theme-inside.git generate_test/themes/landscape
- cd generate_test
- npm install
- hexo g & hexo g
- cd ..
- npm test
- cd ../..
- hexo g && hexo g
22 changes: 11 additions & 11 deletions lib/renderer/md.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
const marked = require('marked');
const { stripHTML } = require('hexo-util');
const { isExternal, htmlTag } = require('../utils');
const { DomHandler, DomUtils, Parser } = require('htmlparser2');

const parseHtml = html => {
const handler = new DomHandler();
new Parser(handler).end(html);
return handler.dom;
};

class Renderer extends marked.Renderer {
constructor(ctx) {
Expand All @@ -28,18 +21,25 @@ class Renderer extends marked.Renderer {
}
table(header, body) {
const { styles } = this
const hasHeader = DomUtils.getText(parseHtml(header))
.replace(/&nbsp;/g, '').trim()

return [
`<div class="${styles.bounded}"><div class="${styles.table}">`,
'<table>',
hasHeader ? `<thead>${header}</thead>` : '',
`<thead>${header}</thead>`,
`<tbody>${body}</tbody>`,
'</table>',
'</div></div>'
].join('')
}
tablecell(content, flags) {
if (flags.header) {
if (content === '&nbsp;') content = '';
const attrs = !content // is empty
? { style: 'padding:0' }
: { align: flags.align };
return htmlTag('th', attrs, content);
}
return htmlTag('td', { align: flags.align }, content);
}
link(href, title, text) {
return htmlTag('a', { href, title, target: isExternal(href) ? '_blank' : '' }, text);
}
Expand Down
10 changes: 7 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,10 @@ exports.parseToc = function (html, depth) {
current.push({
id: item.id,
title: item.text,
index: [parents[parents.length - 1].index, current.length + 1]
.filter(i => i).join('.')
index: [
parents.length ? parents[parents.length - 1].index : '',
current.length + 1
].filter(i => i).join('.')
})
},
open() {
Expand All @@ -195,7 +197,9 @@ exports.parseToc = function (html, depth) {
},
close() {
parents.pop()
current = parents[parents.length - 1].children
current = parents.length
? parents[parents.length - 1].children
: parents;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/scripts/renderers/md.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ describe('md', function () {
text: `&nbsp;|&nbsp;|&nbsp;
-|-|-
a|b|c` }))
.toBe('<div class="bd"><div class="tb"><table><tbody><tr><td>a</td><td>b</td><td>c</td></tr></tbody></table></div></div>');
.toBe('<div class="bd"><div class="tb"><table><thead><tr><th style="padding:0"></th><th style="padding:0"></th><th style="padding:0"></th></tr></thead><tbody><tr><td>a</td><td>b</td><td>c</td></tr></tbody></table></div></div>');
});
});

0 comments on commit 776671e

Please sign in to comment.