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

throw error since "prettify" doesn't check null #1

Closed
o-w-o opened this issue Feb 11, 2019 · 1 comment

Comments

Projects
None yet
2 participants
@o-w-o
Copy link

commented Feb 11, 2019

解析一篇无内容文档时报的错误

TypeError: Cannot read property 'replace' of null
    at prune (/node_modules/vuepress-plugin-yuque/lib/html.js:50:6)
    at prettify (/node_modules/vuepress-plugin-yuque/lib/html.js:39:10)
    at ready (/node_modules/vuepress-plugin-yuque/lib/index.js:129:27)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)

/lib/index.js:129

        let postContent
        if (status && status === 404) {
          postContent = `# ${title}\n > 此文档尚未创建`
        } else {
          if (useMarkdown || data.format === 'markdown') {
            postContent = data.body
          } else {
            postContent = prettify(data.body_html)  // 这里 data.body_html 可能为 null
          }
        }
function prettify(html) {
  html = prune(html)
  const $ = cheerio.load(html, {
    // ref: https://github.com/cheeriojs/cheerio/issues/565
    decodeEntities: false
  })
  transformHeaders($)
  return $('body').html()
}

function prune(html) {  // 这里 html 可能传入 null
  return html
    .replace(/<p><br\s\/><\/p>/g, '')
    .replace(/<p><span><br\s\/><\/span><\/p>/g, '')
}

@o-w-o o-w-o changed the title html prettify 未作 空值校验 html prettify 未作 空值校验 label:bug Feb 11, 2019

@ulivz ulivz closed this in 1110274 Feb 11, 2019

@ulivz ulivz changed the title html prettify 未作 空值校验 label:bug throw error since "prettify" doesn't check null Feb 11, 2019

@ulivz

This comment has been minimized.

Copy link
Owner

commented Feb 11, 2019

Thanks, fixed at 1110274 and release at vuepress-plugin-yuque@0.2.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.