Skip to content
This repository was archived by the owner on Feb 6, 2024. It is now read-only.

Commit 64a7ac4

Browse files
feat: markdown either height or width also ok and more units
1 parent b82df4a commit 64a7ac4

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed
Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import marked from 'marked';
22

3-
// Source: https://github.com/markedjs/marked/issues/339
3+
// Original source: https://github.com/markedjs/marked/issues/339
4+
// our version:
45
// ![](https://www.nmattia.com/images/autoupdate-notifications.jpg "width:100px,height:20px")
56
export function changeImgCreation(renderer: marked.Renderer) {
67
renderer.image = (src: string | null, title: string | null, alt: string) => {
7-
const getWidthAndHeightStyle = checkForWidthAndHeight(title);
8-
if (getWidthAndHeightStyle.length) {
9-
return `<deckgo-lazy-img img-src="${sanitize(src)}" img-alt="${sanitize(alt)}" style="${getWidthAndHeightStyle}"></deckgo-lazy-img>`;
10-
}
11-
return `<deckgo-lazy-img img-src="${sanitize(src)}" img-alt="${sanitize(alt)}"></deckgo-lazy-img>`;
8+
const style: string = checkForWidthAndHeight(title);
9+
return `<deckgo-lazy-img img-src="${sanitize(src)}" img-alt="${sanitize(alt)}" style="${style}"></deckgo-lazy-img>`;
1210
};
1311
}
1412

@@ -20,15 +18,21 @@ function sanitize(str: string) {
2018
});
2119
}
2220

23-
function checkForWidthAndHeight(title) {
24-
const isWidthSet = /width:\d+px/.exec(title);
25-
const isHeightSet = /height:\d+px/.exec(title);
21+
function checkForWidthAndHeight(title): string {
22+
const isWidthSet: RegExpExecArray | null = /width:\d+(?:px|em|ex|ch|rem|vw|vh|vmin|vmax|%)/.exec(title);
23+
const isHeightSet: RegExpExecArray | null = /height:\d+(?:px|em|ex|ch|rem|vw|vh|vmin|vmax|%)/.exec(title);
24+
2625
let style: string = '';
27-
if(!!(isWidthSet && isHeightSet)){
28-
const width = /\d+px/.exec(isWidthSet[0])[0];
29-
const height = /\d+px/.exec(isHeightSet[0])[0];
26+
27+
if (isWidthSet) {
28+
const width = /\d+(?:px|em|ex|ch|rem|vw|vh|vmin|vmax|%)/.exec(isWidthSet[0])[0];
3029
style += `--deckgo-lazy-img-width: ${width};`;
30+
}
31+
32+
if (isHeightSet) {
33+
const height = /\d+(?:px|em|ex|ch|rem|vw|vh|vmin|vmax|%)/.exec(isHeightSet[0])[0];
3134
style += `--deckgo-lazy-img-height: ${height};`;
3235
}
36+
3337
return style;
34-
};
38+
}

webcomponents/markdown/src/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ <h1>Editable:</h1>
3434
[Link](https://deckdeckgo.com/)
3535
<br/>
3636
![](https://www.nmattia.com/images/autoupdate-notifications.jpg "width:100px,height:20px")
37-
![](https://www.nmattia.com/images/autoupdate-notifications.jpg "width:300px,height:200px")
37+
![](https://www.nmattia.com/images/autoupdate-notifications.jpg "width:30px")
3838
![](https://www.nmattia.com/images/autoupdate-notifications.jpg)
3939
<br/>
4040
This is basic paragraph with some **bold text** and also some *italic text*.

0 commit comments

Comments
 (0)