From 776671e64dca98e96649cb0c1796c3658a73fd22 Mon Sep 17 00:00:00 2001 From: ikeq <9206414+ikeq@users.noreply.github.com> Date: Sat, 25 Jan 2020 16:36:12 +0800 Subject: [PATCH] fix: zero size instead of remove when it's empty (#188) --- .travis.yml | 20 ++++++++++++++------ lib/renderer/md.js | 22 +++++++++++----------- lib/utils.js | 10 +++++++--- test/scripts/renderers/md.js | 2 +- 4 files changed, 33 insertions(+), 21 deletions(-) diff --git a/.travis.yml b/.travis.yml index 27e5419..92c763c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/lib/renderer/md.js b/lib/renderer/md.js index 44b9728..747e755 100644 --- a/lib/renderer/md.js +++ b/lib/renderer/md.js @@ -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) { @@ -28,18 +21,25 @@ class Renderer extends marked.Renderer { } table(header, body) { const { styles } = this - const hasHeader = DomUtils.getText(parseHtml(header)) - .replace(/ /g, '').trim() - return [ `
`, '', - hasHeader ? `${header}` : '', + `${header}`, `${body}`, '
', '
' ].join('') } + tablecell(content, flags) { + if (flags.header) { + if (content === ' ') 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); } diff --git a/lib/utils.js b/lib/utils.js index 6442872..b7f8147 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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() { @@ -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; } } } diff --git a/test/scripts/renderers/md.js b/test/scripts/renderers/md.js index b512d34..6c49401 100644 --- a/test/scripts/renderers/md.js +++ b/test/scripts/renderers/md.js @@ -62,6 +62,6 @@ describe('md', function () { text: ` | |  -|-|- a|b|c` })) - .toBe('
abc
'); + .toBe('
abc
'); }); });