@novastera-oss/react-native-markdown-display 8.0.2 → 8.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -287,6 +287,8 @@ And some additional, less used options:
287
287
  <details><summary>Images</summary>
288
288
  <p>
289
289
 
290
+ You can pass image attributes (e.g. `width`, `height`, `contentFit`, `contentPosition`) via markdown-it attributes or HTML to map onto Expo Image props.
291
+
290
292
  ```
291
293
  ![Minion](https://octodex.github.com/images/minion.png)
292
294
  ![Stormtroopocat](https://octodex.github.com/images/stormtroopocat.jpg "The Stormtroopocat")
@@ -109,30 +109,46 @@ const renderRules = {
109
109
  blocklink: linkRule((node, children, parent, styles, onLinkPress) => ((0, jsx_runtime_1.jsx)(react_native_1.TouchableWithoutFeedback, { onPress: () => (0, openUrl_1.default)(node.attributes.href, onLinkPress), style: styles.blocklink, children: (0, jsx_runtime_1.jsx)(react_native_1.View, { style: styles.image, children: children }) }, node.key))),
110
110
  // Images
111
111
  image: imageRule((node, children, parent, styles, allowedImageHandlers, defaultImageHandler) => {
112
- const { src, alt } = node.attributes;
112
+ const { src, alt, width, height, contentFit, contentPosition, } = node.attributes;
113
113
  if (!src) {
114
114
  return null;
115
115
  }
116
+ const srcValue = String(src);
116
117
  // we check that the source starts with at least one of the elements in allowedImageHandlers
117
118
  const show = allowedImageHandlers.filter((value) => {
118
- return src.toLowerCase().startsWith(value.toLowerCase());
119
+ return srcValue.toLowerCase().startsWith(value.toLowerCase());
119
120
  }).length > 0;
120
121
  if (show === false && defaultImageHandler === null) {
121
122
  return null;
122
123
  }
123
- const imageUri = show === true ? src : `${defaultImageHandler !== null && defaultImageHandler !== void 0 ? defaultImageHandler : ''}${src}`;
124
+ const imageUri = show === true ? srcValue : `${defaultImageHandler !== null && defaultImageHandler !== void 0 ? defaultImageHandler : ''}${srcValue}`;
125
+ const widthValue = typeof width === 'number' ? width : width ? Number(width) : undefined;
126
+ const heightValue = typeof height === 'number' ? height : height ? Number(height) : undefined;
127
+ const sizeStyle = Number.isFinite(widthValue) || Number.isFinite(heightValue)
128
+ ? {
129
+ ...(Number.isFinite(widthValue) ? { width: widthValue } : {}),
130
+ ...(Number.isFinite(heightValue) ? { height: heightValue } : {}),
131
+ }
132
+ : undefined;
124
133
  const imageProps = {
125
- key: node.key,
126
- style: styles._VIEW_SAFE_image,
134
+ style: sizeStyle
135
+ ? [styles._VIEW_SAFE_image, sizeStyle]
136
+ : styles._VIEW_SAFE_image,
127
137
  source: {
128
138
  uri: imageUri,
129
139
  },
130
140
  };
131
- if (alt) {
141
+ if (typeof contentFit === 'string') {
142
+ imageProps.contentFit = contentFit;
143
+ }
144
+ if (typeof contentPosition === 'string') {
145
+ imageProps.contentPosition = contentPosition;
146
+ }
147
+ if (typeof alt === 'string' && alt.length > 0) {
132
148
  imageProps.accessible = true;
133
149
  imageProps.accessibilityLabel = alt;
134
150
  }
135
- return (0, jsx_runtime_1.jsx)(expo_image_1.Image, { ...imageProps });
151
+ return (0, jsx_runtime_1.jsx)(expo_image_1.Image, { ...imageProps }, node.key);
136
152
  }),
137
153
  // Text Output
138
154
  text: baseRule((node, children, parent, styles, inheritedStyles = {}) => ((0, jsx_runtime_1.jsx)(react_native_1.Text, { style: [inheritedStyles, styles.text], children: node.content }, node.key))),
@@ -104,30 +104,46 @@ const renderRules = {
104
104
  blocklink: linkRule((node, children, parent, styles, onLinkPress) => (_jsx(TouchableWithoutFeedback, { onPress: () => openUrl(node.attributes.href, onLinkPress), style: styles.blocklink, children: _jsx(View, { style: styles.image, children: children }) }, node.key))),
105
105
  // Images
106
106
  image: imageRule((node, children, parent, styles, allowedImageHandlers, defaultImageHandler) => {
107
- const { src, alt } = node.attributes;
107
+ const { src, alt, width, height, contentFit, contentPosition, } = node.attributes;
108
108
  if (!src) {
109
109
  return null;
110
110
  }
111
+ const srcValue = String(src);
111
112
  // we check that the source starts with at least one of the elements in allowedImageHandlers
112
113
  const show = allowedImageHandlers.filter((value) => {
113
- return src.toLowerCase().startsWith(value.toLowerCase());
114
+ return srcValue.toLowerCase().startsWith(value.toLowerCase());
114
115
  }).length > 0;
115
116
  if (show === false && defaultImageHandler === null) {
116
117
  return null;
117
118
  }
118
- const imageUri = show === true ? src : `${defaultImageHandler !== null && defaultImageHandler !== void 0 ? defaultImageHandler : ''}${src}`;
119
+ const imageUri = show === true ? srcValue : `${defaultImageHandler !== null && defaultImageHandler !== void 0 ? defaultImageHandler : ''}${srcValue}`;
120
+ const widthValue = typeof width === 'number' ? width : width ? Number(width) : undefined;
121
+ const heightValue = typeof height === 'number' ? height : height ? Number(height) : undefined;
122
+ const sizeStyle = Number.isFinite(widthValue) || Number.isFinite(heightValue)
123
+ ? {
124
+ ...(Number.isFinite(widthValue) ? { width: widthValue } : {}),
125
+ ...(Number.isFinite(heightValue) ? { height: heightValue } : {}),
126
+ }
127
+ : undefined;
119
128
  const imageProps = {
120
- key: node.key,
121
- style: styles._VIEW_SAFE_image,
129
+ style: sizeStyle
130
+ ? [styles._VIEW_SAFE_image, sizeStyle]
131
+ : styles._VIEW_SAFE_image,
122
132
  source: {
123
133
  uri: imageUri,
124
134
  },
125
135
  };
126
- if (alt) {
136
+ if (typeof contentFit === 'string') {
137
+ imageProps.contentFit = contentFit;
138
+ }
139
+ if (typeof contentPosition === 'string') {
140
+ imageProps.contentPosition = contentPosition;
141
+ }
142
+ if (typeof alt === 'string' && alt.length > 0) {
127
143
  imageProps.accessible = true;
128
144
  imageProps.accessibilityLabel = alt;
129
145
  }
130
- return _jsx(Image, { ...imageProps });
146
+ return _jsx(Image, { ...imageProps }, node.key);
131
147
  }),
132
148
  // Text Output
133
149
  text: baseRule((node, children, parent, styles, inheritedStyles = {}) => (_jsx(Text, { style: [inheritedStyles, styles.text], children: node.content }, node.key))),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@novastera-oss/react-native-markdown-display",
3
- "version": "8.0.2",
3
+ "version": "8.1.0",
4
4
  "description": "Markdown renderer for react-native, with CommonMark spec support. Maintained by Novastera.",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",