Skip to content

Commit

Permalink
wip: Fixes summary generation for other article blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickbrown-io committed Oct 6, 2023
1 parent bc31e0f commit 1152119
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 78 deletions.
77 changes: 36 additions & 41 deletions js/ucb-article-feature-block.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ArticleFeatureBlockElement extends HTMLElement {
}

// This is the article list filtering function that assembles the article javascript object array from the JSON response. Handles exclusions of categories and tags.
build(data, display, count, imgSize, excludeCatArray, excludeTagArray, finalArticles = []){
async build(data, display, count, imgSize, excludeCatArray, excludeTagArray, finalArticles = []){
// More than 10 articles? This sets up the next call if there's more articles to be retrieved but not enough post-filters
let NEXTJSONURL = "";
if(data.links.next) {
Expand Down Expand Up @@ -70,7 +70,7 @@ class ArticleFeatureBlockElement extends HTMLElement {
})
}
// Iterate over each Article
data.data.map(item=>{
await Promise.all (data.data.map(async(item)=>{
// Article must have a thumbnail
if(item.relationships.field_ucb_article_thumbnail.data){
let thisArticleCats = [];
Expand Down Expand Up @@ -118,33 +118,7 @@ class ArticleFeatureBlockElement extends HTMLElement {
let imageSrcWide = "";

if (!body.length && bodyAndImageId != "") {
this.getArticleParagraph(bodyAndImageId)
.then((response) => response.json())
.then((data) => {
// Remove any html tags within the article
let htmlStrip = data.data.attributes.field_article_text.processed.replace(
/<\/?[^>]+(>|$)/g,
""
)
// Remove any line breaks if media is embedded in the body
let lineBreakStrip = htmlStrip.replace(/(\r\n|\n|\r)/gm, "");
// take only the first 100 words ~ 500 chars
let trimmedString = lineBreakStrip.substr(0, 250);
// if in the middle of the string, take the whole word
if(trimmedString.length > 100){
trimmedString = trimmedString.substr(
0,
Math.min(
trimmedString.length,
trimmedString.lastIndexOf(" ")
)
)
body = `${trimmedString}...`;
}
// set the contentBody of Article Summary card to the minified body instead
body = `${trimmedString}`;
document.getElementById(`body-${bodyAndImageId}`).innerText = body;
})
body = await this.getArticleParagraph(bodyAndImageId);
}
//Use the idObj as a memo to add the corresponding image url
let thumbId = item.relationships.field_ucb_article_thumbnail.data.id;
Expand All @@ -170,7 +144,7 @@ class ArticleFeatureBlockElement extends HTMLElement {
finalArticles.push(article)
}
}
})
}));
// Case for not enough articles selected and extra articles available
if(finalArticles.length < count && NEXTJSONURL){
fetch(NEXTJSONURL).then(this.handleError)
Expand Down Expand Up @@ -201,17 +175,38 @@ class ArticleFeatureBlockElement extends HTMLElement {
}
}
}
// Gets the body if no summary
async getArticleParagraph(id) {
if(id) {
const response = await fetch(
`/jsonapi/paragraph/article_content/${id}`
);
return response;
} else {
return "";
}
}
// Responsible for fetching & processing the body of the Article if no summary provided
async getArticleParagraph(id) {
if (!id) {
return "";
}

const response = await fetch(`/jsonapi/paragraph/article_content/${id}`);
if (!response.ok) {
throw new Error('Failed to fetch article paragraph');
}

const data = await response.json();

let htmlStrip = data.data.attributes.field_article_text.processed.replace(
/<\/?[^>]+(>|$)/g,
""
);
let lineBreakStrip = htmlStrip.replace(/(\r\n|\n|\r)/gm, "");
let trimmedString = lineBreakStrip.substr(0, 250);

if (trimmedString.length > 100) {
trimmedString = trimmedString.substr(
0,
Math.min(
trimmedString.length,
trimmedString.lastIndexOf(" ")
)
);
}

return trimmedString + "...";
}

// Responsible for calling the render function of the appropriate display
renderDisplay(articleArray, display, imgSize){
Expand Down
70 changes: 33 additions & 37 deletions js/ucb-article-grid-block.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ArticleGridBlockElement extends HTMLElement {
});
}

build(data, count, includeSummary, excludeCatArray, excludeTagArray, finalArticles = []){
async build(data, count, includeSummary, excludeCatArray, excludeTagArray, finalArticles = []){
// More than 10 articles? This sets up the next call if there's more articles to be retrieved but not enough post-filters
let NEXTJSONURL = "";
if(data.links.next) {
Expand Down Expand Up @@ -65,7 +65,7 @@ class ArticleGridBlockElement extends HTMLElement {
})
}
// Iterate over each Article
data.data.map(item=>{
await Promise.all(data.data.map(async (item)=>{
// Article must have a thumbnail
if(item.relationships.field_ucb_article_thumbnail.data){
let thisArticleCats = [];
Expand Down Expand Up @@ -112,33 +112,7 @@ class ArticleGridBlockElement extends HTMLElement {
let imageSrc = "";

if (!body.length && bodyAndImageId != "") {
this.getArticleParagraph(bodyAndImageId)
.then((response) => response.json())
.then((data) => {
// Remove any html tags within the article
let htmlStrip = data.data.attributes.field_article_text.processed.replace(
/<\/?[^>]+(>|$)/g,
""
)
// Remove any line breaks if media is embedded in the body
let lineBreakStrip = htmlStrip.replace(/(\r\n|\n|\r)/gm, "");
// take only the first 100 words ~ 500 chars
let trimmedString = lineBreakStrip.substr(0, 250);
// if in the middle of the string, take the whole word
if(trimmedString.length > 100){
trimmedString = trimmedString.substr(
0,
Math.min(
trimmedString.length,
trimmedString.lastIndexOf(" ")
)
)
body = `${trimmedString}...`;
}
// set the contentBody of Article Summary card to the minified body instead
body = `${trimmedString}`;
document.getElementById(`body-${bodyAndImageId}`).innerText = body;
})
body = await this.getArticleParagraph(bodyAndImageId);
}

// if no thumbnail, show no image
Expand Down Expand Up @@ -168,7 +142,7 @@ class ArticleGridBlockElement extends HTMLElement {
finalArticles.push(article)
}
}
})
}));
// Case for not enough articles selected and extra articles available
if(finalArticles.length < count && NEXTJSONURL){
fetch(NEXTJSONURL).then(this.handleError)
Expand Down Expand Up @@ -200,15 +174,37 @@ class ArticleGridBlockElement extends HTMLElement {
}
}

// Responsible for fetching & processing the body of the Article if no summary provided
async getArticleParagraph(id) {
if(id) {
const response = await fetch(
`/jsonapi/paragraph/article_content/${id}`
if (!id) {
return "";
}

const response = await fetch(`/jsonapi/paragraph/article_content/${id}`);
if (!response.ok) {
throw new Error('Failed to fetch article paragraph');
}

const data = await response.json();

let htmlStrip = data.data.attributes.field_article_text.processed.replace(
/<\/?[^>]+(>|$)/g,
""
);
return response;
} else {
return "";
}
let lineBreakStrip = htmlStrip.replace(/(\r\n|\n|\r)/gm, "");
let trimmedString = lineBreakStrip.substr(0, 250);

if (trimmedString.length > 100) {
trimmedString = trimmedString.substr(
0,
Math.min(
trimmedString.length,
trimmedString.lastIndexOf(" ")
)
);
}

return trimmedString + "...";
}

// Responsible for calling the render function of the appropriate display
Expand Down

0 comments on commit 1152119

Please sign in to comment.