Skip to content

Commit 8d2171d

Browse files
nimonianfarnabazatinux
authored
fix: preserve image attributes when a title is present (#290)
Co-authored-by: Farnabaz <farnabaz@gmail.com> Co-authored-by: Sébastien Chopin <atinux@gmail.com>
1 parent ca54d7d commit 8d2171d

3 files changed

Lines changed: 102 additions & 1 deletion

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
## Input
2+
3+
```md
4+
![alt](https://example.com/image.jpg "title"){attr="value"}
5+
6+
![alt](img.png "A title"){.rounded-asymmetric width="200"}
7+
```
8+
9+
## AST
10+
11+
```json
12+
{
13+
"frontmatter": {},
14+
"meta": {},
15+
"nodes": [
16+
[
17+
"p",
18+
{},
19+
[
20+
"img",
21+
{
22+
"src": "https://example.com/image.jpg",
23+
"alt": "alt",
24+
"title": "title",
25+
"attr": "value"
26+
}
27+
]
28+
],
29+
[
30+
"p",
31+
{},
32+
[
33+
"img",
34+
{
35+
"src": "img.png",
36+
"alt": "alt",
37+
"title": "A title",
38+
"class": "rounded-asymmetric",
39+
"width":"200"
40+
}
41+
]
42+
]
43+
]
44+
}
45+
```
46+
47+
## HTML
48+
49+
```html
50+
<p><img src="https://example.com/image.jpg" title="title" alt="alt" attr="value" /></p>
51+
<p><img src="img.png" title="A title" alt="alt" class="rounded-asymmetric" width="200" /></p>
52+
```
53+
54+
## Markdown
55+
56+
```md
57+
![alt](https://example.com/image.jpg "title"){attr="value"}
58+
59+
![alt](img.png "A title"){.rounded-asymmetric width="200"}
60+
```
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
## Input
2+
3+
```md
4+
![alt](https://example.com/image.jpg "title")
5+
```
6+
7+
## AST
8+
9+
```json
10+
{
11+
"frontmatter": {},
12+
"meta": {},
13+
"nodes": [
14+
[
15+
"p",
16+
{},
17+
[
18+
"img",
19+
{
20+
"src": "https://example.com/image.jpg",
21+
"alt": "alt",
22+
"title": "title"
23+
}
24+
]
25+
]
26+
]
27+
}
28+
```
29+
30+
## HTML
31+
32+
```html
33+
<p><img src="https://example.com/image.jpg" title="title" alt="alt" /></p>
34+
```
35+
36+
## Markdown
37+
38+
```md
39+
![alt](https://example.com/image.jpg "title")
40+
```

packages/comark/src/internal/stringify/handlers/img.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ export function img(node: ComarkElement, _state: State) {
88

99
const attrsString = Object.keys(rest).length > 0 ? comarkAttributes(rest) : ''
1010

11-
return title ? `![${alt}](${src} "${title}")` : `![${alt}](${src})${attrsString}`
11+
const link = title ? `![${alt}](${src} "${title}")` : `![${alt}](${src})`
12+
return `${link}${attrsString}`
1213
}

0 commit comments

Comments
 (0)