-
Notifications
You must be signed in to change notification settings - Fork 0
LIF 612 - Add support for Flourish graphics #549
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -88,18 +88,31 @@ module.exports = exports = class DocumentBuilder { | |
| removeNonSyndicatableImages() { | ||
|
|
||
| const embedsMap = arrayToMap(this.content.embeds); | ||
|
|
||
| Array.from(this.contentDocument.getElementsByTagName('img')).forEach( | ||
| (el) => { | ||
| const imageType = el.getAttribute('data-image-type'); | ||
|
|
||
| let isFlourishElement = false; | ||
| // identify flourish element | ||
| const elementSrc = el.getAttribute('src'); | ||
| if(elementSrc?.includes('public.flourish.studio/')) { | ||
| isFlourishElement = true; | ||
| } | ||
|
|
||
| let imageType = el.getAttribute('data-image-type'); | ||
| if(isFlourishElement) { | ||
| imageType = 'graphic'; | ||
| } | ||
|
|
||
| let imageId = | ||
| el.getAttribute('data-id') || | ||
| el.getAttribute('data-content-id'); | ||
|
|
||
| // to handle ids in this format (https://api.ft.com/content/{content_id}}) | ||
| imageId = imageId.split('/').pop(); | ||
|
|
||
|
|
||
| if(isFlourishElement) { | ||
| const match = elementSrc.match(/\/visualisation\/(\d+)\//); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comment: What is the value of Instead of Regex, we can use the built-in URL parser and then apply a focused regex only to the pathname. Regexes are fragile in case of matching with URL paths, as they will not cover the URL variations: |
||
| imageId = match ? match[1] : null; | ||
| } | ||
|
|
||
| const imageDetails = embedsMap[imageId]; | ||
|
|
||
| if (imageType !== 'graphic' || !imageDetails || imageDetails.canBeSyndicated !== 'yes') { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,34 @@ const { | |
| const RE_BAD_CHARS = /[^A-Za-z0-9_]/gm; | ||
| const RE_SPACE = /\s/gm; | ||
|
|
||
| function extractFourishEmbeds(contentHTMLBody) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo: |
||
| try { | ||
| // Regex to extract Flourish IDs | ||
| const flourishIdRegex = /data-flourish-id="(\d+)"/g; | ||
|
|
||
| const flourishEmbeds = []; | ||
| let match; | ||
|
|
||
| while ((match = flourishIdRegex.exec(contentHTMLBody)) !== null) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comment: Regex.exec has a state inside it, explained here. Maybe we can use a simpler approach which doesn't involve any states. Example: |
||
| const id = match[1]; | ||
| const flourishContentUrl = encodeURIComponent(`https://public.flourish.studio/visualisation/${id}/thumbnail?cacheBuster=`); | ||
| const proxyUrl = `https://www.ft.com/__origami/service/image/v2/images/raw/${flourishContentUrl}?source=cp-content-pipeline&fit=scale-down&quality=highest&width=1020&dpr=1`; | ||
|
|
||
| flourishEmbeds.push({ | ||
| apiUrl: proxyUrl, | ||
| binaryUrl: proxyUrl, | ||
| canBeSyndicated: 'yes', | ||
| id: id, | ||
| type: 'http://www.ft.com/ontology/content/Graphic' | ||
| }); | ||
| } | ||
| return flourishEmbeds; | ||
|
|
||
| } catch(error){ | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| module.exports = exports = function article(content, contract, graphicSyndicationFlag) { | ||
| if (!content.content_id) { | ||
| content.content_id = path.basename(content.id); | ||
|
|
@@ -28,6 +56,15 @@ module.exports = exports = function article(content, contract, graphicSyndicatio | |
| content.bodyHTML = content.body; | ||
| } | ||
|
|
||
| const flourishEmbeds = extractFourishEmbeds(content.bodyHTML); | ||
| if(flourishEmbeds) { | ||
| content.embeds = content.embeds ?? []; | ||
| content.embeds.push(...flourishEmbeds); | ||
| if(content.contentStats){ | ||
| content.contentStats.graphics += flourishEmbeds.length; | ||
| } | ||
| } | ||
|
|
||
| content.wordCount = getWordCount(content); | ||
| content.hasGraphics = Boolean(content.contentStats && content.contentStats.graphics); | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment/suggestion: I'll consider a more functional approach instead of imperative style when making changes in the DOM. We could simplify this to something like:
This reduces multiple declarations and less to maintain.