Skip to content

Commit

Permalink
Support multiple images. (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
yilei authored Jun 18, 2024
1 parent 01db521 commit 3d3562b
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 3 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ Render result:
</figure>
```

Multiple images are supported:

```md
![Picture of Oscar.](/path/to/cat1.jpg)
![Picture of Luna.](/path/to/cat2.jpg)
Awesome captions about the **kitties**.
```

```html
<figure>
<img src="/path/to/cat1.jpg" alt="Picture of Oscar.">
<img src="/path/to/cat2.jpg" alt="Picture of Luna.">
<figcaption><p>Awesome captions about the <strong>kitties</strong>.</p></figcaption>
</figure>
```

# Why?

Using dedicated `<figure>` and `<figcaption>` elements makes styling images
Expand Down Expand Up @@ -97,6 +113,11 @@ See [`figure_test.go`](/figure_test.go) for more examples.

# Changelog

## v1.2.0 (2024-06-19)

* Support multiple images (see
[#5](https://github.com/MangoUmbrella/goldmark-figure/issues/5)).

## v1.1.0 (2024-06-18)

* New option to add a link to the image when rendering the figure (see
Expand Down
57 changes: 57 additions & 0 deletions figure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,42 @@ Last paragraph.
This is the continued line with <strong>bold</strong>.</p></figcaption>
</figure>
<p>Last paragraph.</p>
`,
}, t)

count++
testutil.DoTestCase(markdown, testutil.MarkdownTestCase{
No: count,
Description: "Two images",
Markdown: `
![Picture of Oscar.](/path/to/cat1.jpg)
![Picture of Luna.](/path/to/cat2.jpg)
Awesome captions about the **kitties**.
`,
Expected: `<figure>
<img src="/path/to/cat1.jpg" alt="Picture of Oscar.">
<img src="/path/to/cat2.jpg" alt="Picture of Luna.">
<figcaption><p>Awesome captions about the <strong>kitties</strong>.</p></figcaption>
</figure>
`,
}, t)

count++
testutil.DoTestCase(markdown, testutil.MarkdownTestCase{
No: count,
Description: "Three images",
Markdown: `
![Picture of Oscar.](/path/to/cat1.jpg)
![Picture of Luna.](/path/to/cat2.jpg)
![Picture of Oreo.](/path/to/cat3.jpg)
Awesome captions about the **kitties**.
`,
Expected: `<figure>
<img src="/path/to/cat1.jpg" alt="Picture of Oscar.">
<img src="/path/to/cat2.jpg" alt="Picture of Luna.">
<img src="/path/to/cat3.jpg" alt="Picture of Oreo.">
<figcaption><p>Awesome captions about the <strong>kitties</strong>.</p></figcaption>
</figure>
`,
}, t)

Expand Down Expand Up @@ -133,6 +169,27 @@ This is caption.
</a>
<figcaption><p>This is caption.</p></figcaption>
</figure>
`,
}, t)

count++
testutil.DoTestCase(markdown, testutil.MarkdownTestCase{
No: count,
Description: "Multi images",
Markdown: `
![Picture of Oscar.](/path/to/cat1.jpg)
![Picture of Luna.](/path/to/cat2.jpg)
Awesome captions about the **kitties**.
`,
Expected: `<figure>
<a href="/path/to/cat1.jpg">
<img src="/path/to/cat1.jpg" alt="Picture of Oscar.">
</a>
<a href="/path/to/cat2.jpg">
<img src="/path/to/cat2.jpg" alt="Picture of Luna.">
</a>
<figcaption><p>Awesome captions about the <strong>kitties</strong>.</p></figcaption>
</figure>
`,
}, t)
}
22 changes: 19 additions & 3 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ func (b *figureParagraphTransformer) Transform(node *gast.Paragraph, reader text
if lines.Len() < 1 {
return
}
var source = reader.Source()
var firstSeg = lines.At(0)
var firstLineStr = firstSeg.Value(reader.Source())
var firstLineStr = firstSeg.Value(source)
// Here we simply match by regex.
// But this simple regex ignores image descriptions that contain other links.
// E.g. ![foo ![bar](/url)](/url2).
Expand All @@ -48,9 +49,24 @@ func (b *figureParagraphTransformer) Transform(node *gast.Paragraph, reader text
figureImage.Lines().Append(lines.At(0))
figure.AppendChild(figure, figureImage)

if lines.Len() >= 2 {
var currentLine = 1
for currentLine < lines.Len() {
var currentSeg = lines.At(currentLine)
var currentLineStr = currentSeg.Value(source)
if imageRegexp.Match(currentLineStr) {
// Continued images.
figureImage := fast.NewFigureImage()
figureImage.Lines().Append(lines.At(currentLine))
figure.AppendChild(figure, figureImage)
currentLine += 1
} else {
break
}
}

if currentLine < lines.Len() {
figureCaption := fast.NewFigureCaption()
for i := 1; i < lines.Len(); i++ {
for i := currentLine; i < lines.Len(); i++ {
seg := lines.At(i)
if i == lines.Len()-1 {
// trim last newline(\n)
Expand Down

0 comments on commit 3d3562b

Please sign in to comment.