mirror of https://github.com/lukechilds/docs.git
Thomas Osmonson
4 years ago
committed by
Thomas Osmonson
6 changed files with 95 additions and 9 deletions
@ -0,0 +1,36 @@ |
|||||
|
const memoize = require('micro-memoize'); |
||||
|
const visit = require('unist-util-visit'); |
||||
|
const pAll = require('p-all'); |
||||
|
const sizeOf = require('image-size'); |
||||
|
|
||||
|
/** |
||||
|
* Simple plugin to get the size of local images so we can use it in react |
||||
|
*/ |
||||
|
const rehypeImageSize = () => { |
||||
|
async function transformer(tree) { |
||||
|
const nodes = []; |
||||
|
visit(tree, 'element', node => { |
||||
|
if (node.tagName !== 'img') { |
||||
|
return; |
||||
|
} else { |
||||
|
nodes.push(node); |
||||
|
} |
||||
|
}); |
||||
|
await pAll( |
||||
|
nodes.map(node => () => visitor(node)), |
||||
|
{ concurrency: 25 } |
||||
|
); |
||||
|
return tree; |
||||
|
} |
||||
|
async function visitor(node) { |
||||
|
const isRelative = |
||||
|
node && node.properties && node.properties.src && node.properties.src.startsWith('/'); |
||||
|
if (isRelative) { |
||||
|
const dimensions = sizeOf(`public/${node.properties.src}`); |
||||
|
node.properties['dimensions'] = dimensions; |
||||
|
} |
||||
|
} |
||||
|
return transformer; |
||||
|
}; |
||||
|
|
||||
|
module.exports = memoize(rehypeImageSize); |
@ -1,6 +1,7 @@ |
|||||
const memoize = require('micro-memoize'); |
const memoize = require('micro-memoize'); |
||||
const { rehypeVscode } = require('unified-vscode'); |
const { rehypeVscode } = require('unified-vscode'); |
||||
|
const rehypeImgs = require('./rehype-image-size'); |
||||
|
|
||||
const rehypePlugins = [memoize(rehypeVscode)]; |
const rehypePlugins = [memoize(rehypeVscode), rehypeImgs]; |
||||
|
|
||||
module.exports = { rehypePlugins }; |
module.exports = { rehypePlugins }; |
||||
|
Loading…
Reference in new issue