feat: optional kroki/plantuml svg caching (#2047)

* feat: Caching kroki svgs #2020
This commit is contained in:
Regev Brody 2020-06-19 01:39:25 +03:00 committed by GitHub
parent e03a80dccc
commit 77086a6e0a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 41 additions and 3 deletions

View File

@ -24,7 +24,7 @@ module.exports = {
for (let child of _.reject(this.children, ['step', 'post'])) { for (let child of _.reject(this.children, ['step', 'post'])) {
const renderer = require(`../${_.kebabCase(child.key)}/renderer.js`) const renderer = require(`../${_.kebabCase(child.key)}/renderer.js`)
renderer.init($, child.config) await renderer.init($, child.config)
} }
// -------------------------------- // --------------------------------

View File

@ -0,0 +1,8 @@
key: htmlImagePrefetch
title: Image Prefetch
description: Prefetch remotely rendered images (korki/plantuml)
author: requarks.io
icon: mdi-cloud-download-outline
enabledDefault: false
dependsOn: htmlCore
props: {}

View File

@ -0,0 +1,30 @@
const request = require('request-promise')
const prefetch = async (element) => {
const url = element.attr(`src`)
let response
try {
response = await request({
method: `GET`,
url,
resolveWithFullResponse: true
})
} catch (err) {
WIKI.logger.warn(`Failed to prefetch ${url}`)
WIKI.logger.warn(err)
return
}
const contentType = response.headers[`content-type`]
const image = Buffer.from(response.body).toString('base64')
element.attr('src', `data:${contentType};base64,${image}`)
element.removeClass('prefetch-candidate');
}
module.exports = {
async init($) {
const promises = $('img.prefetch-candidate').map((index, element) => {
return prefetch($(element))
}).toArray()
await Promise.all(promises)
}
}

View File

@ -120,7 +120,7 @@ module.exports = {
token = state.push('kroki', 'img', 0) token = state.push('kroki', 'img', 0)
// alt is constructed from children. No point in populating it here. // alt is constructed from children. No point in populating it here.
token.attrs = [ [ 'src', `${server}/${diagramType}/svg/${result}` ], [ 'alt', '' ], ['class', 'uml-diagram'] ] token.attrs = [ [ 'src', `${server}/${diagramType}/svg/${result}` ], [ 'alt', '' ], ['class', 'uml-diagram prefetch-candidate'] ]
token.block = true token.block = true
token.children = altToken token.children = altToken
token.info = params token.info = params

View File

@ -116,7 +116,7 @@ module.exports = {
token = state.push('uml_diagram', 'img', 0) token = state.push('uml_diagram', 'img', 0)
// alt is constructed from children. No point in populating it here. // alt is constructed from children. No point in populating it here.
token.attrs = [ [ 'src', `${server}/${imageFormat}/${zippedCode}` ], [ 'alt', '' ], ['class', 'uml-diagram'] ] token.attrs = [ [ 'src', `${server}/${imageFormat}/${zippedCode}` ], [ 'alt', '' ], ['class', 'uml-diagram prefetch-candidate'] ]
token.block = true token.block = true
token.children = altToken token.children = altToken
token.info = params token.info = params