Skip to content

Commit

Permalink
fix(format): add cache-manager for image cache in blocks formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
sargreal committed Oct 2, 2024
1 parent c87f982 commit 083804e
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 17 deletions.
1 change: 1 addition & 0 deletions plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"axios": "^1.6.7",
"blocks-html-renderer": "^1.0.5",
"bottleneck": "^2.19.5",
"cache-manager": "^6.1.0",
"jsdom": "^24.0.0",
"showdown": "^2.1.0"
},
Expand Down
9 changes: 4 additions & 5 deletions plugin/server/services/__tests__/format-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,11 @@ describe('format', () => {
formatService.htmlToMarkdown(formatService.markdownToHtml(markdown))
).toEqual(markdown)
})
test('block to html and back', () => {
test('block to html and back', async () => {
const formatService = strapi.service('plugin::translate.format')
const html = formatService.blockToHtml(block)
console.log('html', html)
expect(
const html = await formatService.blockToHtml(block)
await expect(
formatService.htmlToBlock(html)
).toEqual(block)
).resolves.toEqual(block)
})
})
23 changes: 14 additions & 9 deletions plugin/server/services/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

const showdown = require('showdown')
const jsdom = require('jsdom')
const cacheManager = require('cache-manager')
const { TRANSLATE_BLOCKS_IMAGE_CACHE_TTL } = require('../utils/constants')
const renderBlock = require('blocks-html-renderer').renderBlock


Expand All @@ -11,7 +13,9 @@ const showdownConverter = new showdown.Converter({
strikethrough: true,
})

const blocksImageCache = new Map()
const blocksImageCache = cacheManager.createCache({
ttl: TRANSLATE_BLOCKS_IMAGE_CACHE_TTL
})

function markdownToHtml(singleText) {
return showdownConverter.makeHtml(singleText)
Expand All @@ -28,10 +32,10 @@ function htmlToMarkdown(singleText) {
*
* @param {Array} blocks
*/
function cacheImages(blocks) {
async function cacheImages(blocks) {
for (const block of blocks.flat(2)) {
if (block.type === 'image') {
blocksImageCache.set(block.image.url, block.image)
await blocksImageCache.set(block.image.url, block.image)
}
}
}
Expand Down Expand Up @@ -111,7 +115,7 @@ function convertInlineElementToBlocks(element) {
}


function convertHtmlToBlock(html) {
async function convertHtmlToBlock(html) {
const root = dom.window.document.createElement('div')
root.innerHTML = html

Expand Down Expand Up @@ -163,7 +167,8 @@ function convertHtmlToBlock(html) {
})
}
if (child.tagName === "IMG") {
const image = blocksImageCache.has(child.src) ? blocksImageCache.get(child.src) : {
const cachedImage = await blocksImageCache.get(child.src)
const image = cachedImage != null ? cachedImage : {
url: child.src,
alt: child.alt,
}
Expand Down Expand Up @@ -197,21 +202,21 @@ module.exports = () => ({
}
return htmlToMarkdown(text)
},
blockToHtml(block) {
async blockToHtml(block) {
if (!Array.isArray(block)) {
throw new Error('blockToHtml expects an array of blocks or a single block. Got ' + typeof block)
}
cacheImages(block)
await cacheImages(block)
if (block.length > 0 ) {
if (!block[0].type) {
return block.map(renderBlock)
}
return renderBlock(block)
}
},
htmlToBlock(html) {
async htmlToBlock(html) {
if (Array.isArray(html)) {
return html.map(convertHtmlToBlock)
return Promise.all(html.map(convertHtmlToBlock))
}
return convertHtmlToBlock(html)
},
Expand Down
1 change: 1 addition & 0 deletions plugin/server/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ module.exports = {
TRANSLATE_PRIORITY_BATCH_TRANSLATION: 6,
TRANSLATE_PRIORITY_DIRECT_TRANSLATION: 3,
TRANSLATE_PRIORITY_DEFAULT: 5,
TRANSLATE_BLOCKS_IMAGE_CACHE_TTL: 60000,
}
2 changes: 1 addition & 1 deletion providers/deepl/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ module.exports = {

let input = text
if (format === 'jsonb') {
input = formatService.blockToHtml(input)
input = await formatService.blockToHtml(input)
} else if (format === 'markdown') {
input = formatService.markdownToHtml(input)
}
Expand Down
2 changes: 1 addition & 1 deletion providers/libretranslate/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ module.exports = {

let input = text
if (format === 'jsonb') {
input = formatService.blockToHtml(input)
input = await formatService.blockToHtml(input)
} else if (format === 'markdown') {
input = formatService.markdownToHtml(input)
}
Expand Down
31 changes: 30 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2165,6 +2165,13 @@
resolved "https://registry.yarnpkg.com/@juggle/resize-observer/-/resize-observer-3.4.0.tgz#08d6c5e20cf7e4cc02fd181c4b0c225cd31dbb60"
integrity sha512-dfLbk+PwWvFzSxwk3n5ySL0hfBog779o8h68wK/7/APo/7cgyWp5jcXockbxdk5kFRkbeXWm4Fbi9FrdN381sA==

"@keyv/serialize@*":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@keyv/serialize/-/serialize-1.0.1.tgz#8dae240d5fe11c589e38b73a2db238dcf26a33cf"
integrity sha512-kKXeynfORDGPUEEl2PvTExM2zs+IldC6ZD8jPcfvI351MDNtfMlw9V9s4XZXuJNDK2qR5gbEKxRyoYx3quHUVQ==
dependencies:
buffer "^6.0.3"

"@koa/[email protected]":
version "3.4.3"
resolved "https://registry.yarnpkg.com/@koa/cors/-/cors-3.4.3.tgz#d669ee6e8d6e4f0ec4a7a7b0a17e7a3ed3752ebb"
Expand Down Expand Up @@ -5585,6 +5592,14 @@ buffer@^5.1.0, buffer@^5.5.0, buffer@^5.6.0:
base64-js "^1.3.1"
ieee754 "^1.1.13"

buffer@^6.0.3:
version "6.0.3"
resolved "https://registry.yarnpkg.com/buffer/-/buffer-6.0.3.tgz#2ace578459cc8fbe2a70aaa8f52ee63b6a74c6c6"
integrity sha512-FTiCpNxtwiZZHEZbcbTIcZjERVICn9yq/pDFkTl95/AxzD1naBctN7YO68riM/gLSDY7sdrMby8hofADYuuqOA==
dependencies:
base64-js "^1.3.1"
ieee754 "^1.2.1"

[email protected]:
version "3.10.0"
resolved "https://registry.yarnpkg.com/buildmail/-/buildmail-3.10.0.tgz#c6826d716e7945bb6f6b1434b53985e029a03159"
Expand Down Expand Up @@ -5686,6 +5701,13 @@ cache-content-type@^1.0.0:
mime-types "^2.1.18"
ylru "^1.2.0"

cache-manager@^6.1.0:
version "6.1.0"
resolved "https://registry.yarnpkg.com/cache-manager/-/cache-manager-6.1.0.tgz#e1d60430b358decc8b6df2ce6cdc3013303740e4"
integrity sha512-Z0gN4aTrCPNxWnXJqdTM+fAFbjYV1al9PNb3bShtw8CeNuaDloYm184f4wPNodPzTBznT3F2sqzTpwlGcL2Hjg==
dependencies:
keyv "^5.0.3"

cacheable-lookup@^5.0.3:
version "5.0.4"
resolved "https://registry.yarnpkg.com/cacheable-lookup/-/cacheable-lookup-5.0.4.tgz#5a6b865b2c44357be3d5ebc2a467b032719a7005"
Expand Down Expand Up @@ -9686,7 +9708,7 @@ icss-utils@^5.0.0, icss-utils@^5.1.0:
resolved "https://registry.yarnpkg.com/icss-utils/-/icss-utils-5.1.0.tgz#c6be6858abd013d768e98366ae47e25d5887b1ae"
integrity sha512-soFhflCVWLfRNOPU3iv5Z9VUdT44xFRbzjLsEzSr5AQmgqPMTHdU3PMT1Cf1ssx8fLNJDA1juftYl+PUcv3MqA==

ieee754@^1.1.13:
ieee754@^1.1.13, ieee754@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/ieee754/-/ieee754-1.2.1.tgz#8eb7a10a63fff25d15a57b001586d177d1b0d352"
integrity sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==
Expand Down Expand Up @@ -11283,6 +11305,13 @@ keyv@*, keyv@^4.0.0:
dependencies:
json-buffer "3.0.1"

keyv@^5.0.3:
version "5.0.3"
resolved "https://registry.yarnpkg.com/keyv/-/keyv-5.0.3.tgz#f0a5a3b3bf41ee4a15a1c481140c0f1e26e6af3f"
integrity sha512-WmefGWaWkWiWDkIasfHxpWmM1lych/LPtRmNj8jnIQVGLsAgFw73Vg9utZ7ss97/JwRlERABb/fSejTPY4hlZQ==
dependencies:
"@keyv/serialize" "*"

kind-of@^3.0.2, kind-of@^3.0.3, kind-of@^3.2.0:
version "3.2.2"
resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-3.2.2.tgz#31ea21a734bab9bbb0f32466d893aea51e4a3c64"
Expand Down

0 comments on commit 083804e

Please sign in to comment.