Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Flo0806 authored Aug 20, 2021
1 parent 0c70220 commit d9917cc
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
28 changes: 28 additions & 0 deletions index.js
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
15 changes: 15 additions & 0 deletions package.json
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"
}
22 changes: 22 additions & 0 deletions readme.md
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%**.

0 comments on commit d9917cc

Please sign in to comment.