-
Notifications
You must be signed in to change notification settings - Fork 105
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
Image Size #611
Comments
What do you mean? That MDsveX would automatically scale the image files when building the project? Or do you simply want to add |
Yes, right now I use the |
MDsveX is built on Unified, which uses remark for parsing Markdown, which in turn supports CommonMark. CommonMark does not support setting the image width/height in Markdown, so using HTML directly is the easiest way to go. However, adding support for what you want is not very hard using the remark-directive plugin to support general Markdown text directives, and then you also add your own plugin to instruct Markdown how these text directives should be mapped to HTML. To turn this: Hi there!
How are you? Here's an image: :img[the-urlx100x200].
Nice, right? Into this: <p>Hi there!</p>
<p>How are you? Here's an image: <img src="the-url" width="100" height="200">.</p>
<p>Nice, right?</p> You can use the plugin (and also import { unified } from 'unified'
import remarkParse from 'remark-parse'
import rehypeStringify from 'rehype-stringify'
import { visit } from 'unist-util-visit'
import remarkDirective from 'remark-directive'
import remarkRehype from 'remark-rehype'
const doc = `
Hi there!
How are you? Here's an image: :img[the-urlx100x200].
Nice, right?
`
// This plugin turns :img[SRCxWIDTHxHEIGHT] into <img src="SRC" width="WIDTH" height="HEIGHT">
function myImgPlugin() {
/**
* @param {import('mdast').Root} tree
* Tree.
* @param {import('vfile').VFile} file
* File.
* @returns {undefined}
* Nothing.
*/
return (tree, file) => {
visit(tree, function (node){
if(node.type === 'textDirective'){
if(node.name !== 'img'){
return
}
// Create the data property if no
// previous plugin has created it.
node.data = node.data ?? {}
// Read out the data.
const text = node.children[0].value // "SRCxWIDTHxHEIGHT"
const [src, width, height] = text.split('x')
// Tell markdown how to map this text directive to HTML.
node.data.hName = 'img'
node.data.hProperties = {
src,
width,
height,
}
// <img> is a void element, so it should not have any children.
node.children = []
}
})
}
}
const file = await unified()
.use(remarkParse)
.use(remarkDirective)
.use(myImgPlugin)
.use(remarkRehype)
.use(rehypeStringify)
.process(doc)
console.log(String(file)) Just remember:
|
Hi,
Is it possible to customize the image size width/height?
Thanks,
🙏🏻
The text was updated successfully, but these errors were encountered: