diff --git a/src/disqus.js b/src/disqus.js index a1dd666..d9d74b3 100644 --- a/src/disqus.js +++ b/src/disqus.js @@ -296,84 +296,113 @@ function DisqusJS(config) { } /* - * processData(data) - 处理评论列表 + * renderCommentData(data) - 渲染评论列表 * - * @param {Object} data - 解析后的评论列表 JSON + * @param {Object} data - 从 getComment() 获取到的 JSON */ - let processData = (data) => { - // 处理 Disqus Profile - if (data.comment.author.profileUrl) { - /* - Avatar Element - - - - - Author Element - - ${data.comment.author.name} - - */ - data.comment.avatarEl = `` - data.comment.authorEl = `${data.comment.author.name}` - } else { - data.comment.avatarEl = `` - data.comment.authorEl = `${data.comment.author.name}` - } + let renderComment = (data) => { + /* + * processData(data) - 处理评论列表 + * + * @param {Object} data - 解析后的评论列表 JSON + */ + let processData = (data) => { + // 处理 Disqus Profile + if (data.comment.author.profileUrl) { + /* + Avatar Element + + + + + Author Element + + ${data.comment.author.name} + + */ + data.comment.avatarEl = `` + data.comment.authorEl = `${data.comment.author.name}` + } else { + data.comment.avatarEl = `` + data.comment.authorEl = `${data.comment.author.name}` + } - // 处理 Admin Label - if (data.isPrimary) { - data.comment.authorEl += `${disqusjs.config.adminLabel}`; - } + // 处理 Admin Label + if (data.isPrimary) { + data.comment.authorEl += `${disqusjs.config.adminLabel}`; + } - // 处理评论嵌套级数 - if (data.children) { - // 如果有子评论,设置当前评论前套数为 1 - data.nesting = 1; - } + // 处理评论嵌套级数 + if (data.children) { + // 如果有子评论,设置当前评论前套数为 1 + data.nesting = 1; + } - return data; - } + return data; + } - let renderPostItem = (s) => { - /* -
-
- ${s.avatarEl} -
-
-
- ${s.authorEl} - - + let renderPostItem = (s) => { + /* +
+
+ ${s.avatarEl}
-
- ${s.message} +
+
+ ${s.authorEl} + + +
+
+ ${s.message} +
-
- */ - let html = `
${s.avatarEl}
${s.authorEl}
${s.message}
` + */ + let html = `
${s.avatarEl}
${s.authorEl}
${s.message}
` - return html; - } + return html; + } + let childrenComments = (data) => { + var nesting = data.nesting, + children = (data.children || []); + if (!children) { + return; + } - /* - * renderCommentData(data) - 渲染评论列表 - * - * @param {Object} data - 从 getComment() 获取到的 JSON - */ - let renderComment = (data) => { - var html = ''; + let html = (() => { + // 如果当前评论嵌套数大于 4 则不再右移 + if (nesting < 4) { + return '
    '; + } else { + return '
      '; + } + })(); + + html += children.map((comment) => { + comment = processData(comment); + comment.nesting = nesting + 1; + return `
    • ${renderPostItem(comment.comment)}${childrenComments(comment)}
    • `; + }); + + html += '
    '; + + if (html.length !== 0) { + return html; + } else { + return; + } + } + + let html = '' data = parseCommentData(data); - data.map((comment) => { + html += data.map((comment) => { comment = processData(comment); - console.log(comment) - html += `
  • ${renderPostItem(comment.comment)}${childrenComments(s)}
  • `; - }) + return `
    • ${renderPostItem(comment.comment)}${childrenComments(comment)}
    `; + }); console.log(html) }