/* global timeAgo, filterXSS */ /* eslint-disable no-multi-str */ function buildArticleHTML(article, currentUserId = null) { var tagIcon = ``; if (article && article.class_name === 'Tag') { return `
${tagIcon}

${article.name}

${ article.short_summary ? `
${article.short_summary}
` : '' }
`; } if (article && article.class_name === 'PodcastEpisode') { return `
${article.podcast.title}

${article.podcast.title}

${article.podcast.title}

`; } if (article && article.class_name === 'Organization') { const html = `

${article.name}

@${article.slug}

${ article.summary ? `
${article.summary}
` : '' }
`; const parser = new DOMParser(); const parsedDocument = parser.parseFromString(html, 'text/html'); parsedDocument.querySelector('img').alt = article.name; parsedDocument.querySelector('button').dataset.info = JSON.stringify({ id: article.id, name: article.name, className: 'Organization', style: 'full', }); return parsedDocument.body.innerHTML; } if (article && article.class_name === 'User' && article.user === undefined) { // Represents different return values for how users are fetched. const html = `

${article.name}

@${article.username}

${ article.summary ? `
${article.summary}
` : '' }
`; const parser = new DOMParser(); const parsedDocument = parser.parseFromString(html, 'text/html'); parsedDocument.querySelector('img').alt = article.name; parsedDocument.querySelector('button').dataset.info = JSON.stringify({ id: article.id, name: article.name, className: 'User', style: 'full', }); return parsedDocument.body.innerHTML; } if (article) { var container = document.getElementById('index-container'); var flareTag = ''; var currentTag = ''; if (container) { currentTag = JSON.parse(container.dataset.params).tag; } if (article.flare_tag && currentTag !== article.flare_tag.name) { flareTag = ` # ${article.flare_tag.name} `; } var tagString = ''; var tagList = article.tag_list || article.cached_tag_list_array || []; if (flareTag) { tagList = tagList.filter(function (tag) { return tag !== article.flare_tag.name; }); tagString += flareTag; } if (tagList) { tagList.forEach(function buildTagString(t) { tagString = tagString + `#${t}\n`; }); } var commentsDisplay = ''; var commentsCount = '0'; if ((article.comments_count || '0') > 0) { commentsCount = article.comments_count || '0'; } var commentsAriaLabelText = `aria-label="Add a comment to post - ${article.title}"`; if (article.class_name !== 'User') { commentsDisplay = ''; if (commentsCount > 0) { commentsDisplay += commentsCount + ''; } else { commentsDisplay += ''; } } var reactionsCount = article.public_reactions_count; var reactionsDisplay = ''; var reactionsText = reactionsCount === 1 ? 'reaction' : 'reactions'; var reactionIcons = document.getElementById('reaction-category-resources'); if (article.class_name !== 'User' && reactionsCount > 0) { var icons = []; for (var category of article.public_reaction_categories) { var icon = reactionIcons.querySelector( `img[data-slug=${category.slug}]`, ).outerHTML; icons = icons.concat( `${icon}`, ); } icons.reverse(); reactionsDisplay = `
${icons.join('')}
`; } var picUrl; var profileUsername; var userName; if (article.class_name === 'PodcastEpisode') { picUrl = article.main_image; profileUsername = article.slug; userName = article.title; } else { picUrl = article.user.profile_image_90; profileUsername = article.user.username; userName = filterXSS(article.user.name); } var orgHeadline = ''; var forOrganization = ''; var organizationLogo = ''; var organizationClasses = 'crayons-avatar--l'; if ( article.organization && !document.getElementById('organization-article-index') ) { organizationLogo = ''; forOrganization = ' for ' + article.organization.name + ''; organizationClasses = 'crayons-avatar--s absolute -right-2 -bottom-2 border-solid border-2 border-base-inverted'; } var timeAgoInWords = ''; if (article.published_at_int) { timeAgoInWords = timeAgo({ oldTimeInSeconds: article.published_at_int }); } var publishDate = ''; if (article.readable_publish_date) { if (article.published_timestamp) { publishDate = ''; } else { publishDate = ''; } } // We only show profile preview cards for Posts var isArticle = article.class_name === 'Article'; // We need to be able to set the data-info hash attribute with escaped characters. // NB: Escaping apostrophes with a "/" does not have the desired effect, as we eventually render the name inside a double quoted string "" // To avoid complications with single quotes inside double quotes inside single quotes, we instead replace any apostrophe with its encoded value var name = userName.replace(/'/g, ''').replace(/[\\"]/g, '\\$&'); var previewCardContent = `
`; var meta = `
${organizationLogo} ${profileUsername} profile
${userName} ${ isArticle ? `` : '' } ${forOrganization}
${publishDate}
`; var bodyTextSnippet = ''; var searchSnippetHTML = ''; if (article.highlight && article.highlight.body_text.length > 0) { var firstSnippetChar = article.highlight.body_text[0]; var startingEllipsis = ''; if (firstSnippetChar.toLowerCase() !== firstSnippetChar.toUpperCase()) { startingEllipsis = '…'; } bodyTextSnippet = startingEllipsis + article.highlight.body_text.join('...') + '…'; if (bodyTextSnippet.length > 0) { searchSnippetHTML = '
' + bodyTextSnippet + '
'; } } var readingTimeHTML = ''; if (article.class_name === 'Article') { // we have ` ... || null` for the case article.reading_time is undefined readingTimeHTML = '' + ((article.reading_time || null) < 1 ? '1 min' : article.reading_time + ' min') + ' read'; } var saveButton = ''; var saveSVG = ''; var saveFilledSVG = ''; // "!=" instead of "!==" used to compare user_id and currentUserId because // currentUserId is a String while user_id is an Integer if (article.class_name === 'Article' && article.user_id != currentUserId) { saveButton = ` `; } else if (article.class_name === 'User') { saveButton = ` `; } var videoHTML = ''; if (article.cloudinary_video_url) { videoHTML = '
' + (article.video_duration_string || article.video_duration_in_minutes) + '
'; } var navigationLink = ` ${filterXSS(article.title)} `; let feedContentAttribute = ''; if (article.class_name === 'Article') { feedContentAttribute = `data-feed-content-id="${article.id}"`; } return `
\ ${navigationLink}\
\ ${videoHTML}\
\
\ ${meta}
\

${filterXSS(article.title)}

\ \ ${searchSnippetHTML}\
\
${reactionsDisplay} ${commentsDisplay}
\
\ ${readingTimeHTML}\ ${saveButton}
\
\
\
\
\
`; } return ''; } /* eslint-enable no-multi-str */