From 0165fcb2cdc32fc546448e15e72cf637b0dafa82 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Sun, 7 Oct 2018 17:21:30 +0800 Subject: [PATCH] refactor: use es6 template string --- src/disqus.js | 43 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/src/disqus.js b/src/disqus.js index 06f6fdd..3b07712 100644 --- a/src/disqus.js +++ b/src/disqus.js @@ -141,8 +141,8 @@ function checkDisqus() { }; img.src = 'https://' + domain + '/favicon.ico?' + +(new Date); }; - for (var i = 0; i < domain.length; i++) { - check(domain[i]); + for (var i of domain) { + check(i); }; } @@ -224,7 +224,7 @@ function getCommentList(data) { var commentLists = topLevelComments.map(function (comment) { return { - comment: comment, + comment, author: comment.author.name, isPrimary: comment.author.username === disqusjs.config.admin, children: getChildren(Number(comment.id)) @@ -237,11 +237,10 @@ function getCommentList(data) { } var list = []; - for (var i = 0; i < childComments.length; i++) { - var comment = childComments[i]; + for (var comment of childComments) { if (comment.parent === id) { list.unshift({ - comment: comment, + comment, author: comment.author.name, isPrimary: comment.author.username === disqusjs.config.admin, children: getChildren(Number(comment.id)) @@ -263,10 +262,36 @@ function renderComment(data) { var disqusjsBaseTpl = '
    '; document.getElementById('disqus_thread').innerHTML = disqusjsBaseTpl; - var commentItemTpl = '
  • <% if (comment.author.profileUrl) { %><% } %><% if (comment.author.profileUrl) { %><% } %>
    <% if (comment.author.profileUrl) { %><% } %><%= comment.author.name %><% if (comment.author.profileUrl) { %><% } %>
    <%- comment.message %>
  • '; - data.map(function (s) { - var html = baidu.template(commentItemTpl, s); + var comment = s.comment; + if (typeof comment.author.profileUrl === null) { + comment.author.profileUrl = "#" + } + var html = ` +
  • +
    +
    + + + +
    +
    +
    + + ${comment.author.name} + + + + +
    +
    + ${comment.message} +
    +
    +
    +
  • + `; document.getElementById('dsqjs-list').insertAdjacentHTML('beforeend', html); }) }