-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
function getImageChilds(parent, options) { | ||
console.log(options) | ||
for (let child in parent.children) { | ||
if ( | ||
parent.children[child].children && | ||
parent.children[child].children.length > 0 | ||
) { | ||
getImageChilds(parent.children[child], options); | ||
} | ||
|
||
if (parent.children[child].type === "image") { | ||
const data = (parent.children[child].data = {}); | ||
|
||
const props = (data.hProperties = {}); | ||
props.width = options.width ? options.width : "100%"; | ||
} | ||
} | ||
} | ||
|
||
function imageSize(options={}) { | ||
return (node) => { | ||
for (let child in node.children) { | ||
getImageChilds(node.children[child], options); | ||
} | ||
}; | ||
} | ||
|
||
module.exports = imageSize |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "fs-imagesize", | ||
"version": "0.1.0", | ||
"description": "Set images size with react-markdown", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [ | ||
"react-markdown", | ||
"remark" | ||
], | ||
"author": "Florian Heuberger", | ||
"license": "MIT" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# react-markdown plugin to set images size. | ||
### A simple to use plugin to set `images` width property. | ||
Basically, `images` are displayed in their full size when you use [react-markdown](https://github.com/remarkjs/react-markdown). This can cause them to overlap their parent element or to be displayed too small. | ||
With this plugin the `width` property can be set very easily. | ||
## Install | ||
``` | ||
npm install fs-imagesize | ||
``` | ||
## Simple to use | ||
```js | ||
import React from "react"; | ||
import ReactMarkdown from "react-markdown"; | ||
import imageSize from "fs-imagesize"; | ||
|
||
function MyComponent(props) { | ||
return ( | ||
<React.Fragment> | ||
<ReactMarkdown plugins={[[imageSize, {width: "230px"}]]}>![Image](https://anyImageUrl.png)</ReactMarkdown> | ||
</React.Fragment>) | ||
} | ||
``` | ||
The `width` property can be set to any pixel/percent size. If it not set the default value is **100%**. |