Skip to content

Commit

Permalink
render image captions
Browse files Browse the repository at this point in the history
  • Loading branch information
AprilSylph committed Nov 24, 2021
1 parent 4e17d56 commit 1d8c21e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/lib/npf.css
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@
width: 100%;
}

[data-block="image"] figcaption {
text-align: center;
}

[data-row] > [data-block="image"] img {
width: 100%;
height: 100%;
Expand Down
7 changes: 6 additions & 1 deletion src/lib/npf.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const blockRenderers = {
return element;
},

image ({ attribution, alt_text, media }) {
image ({ attribution, alt_text, caption, media }) {
return document.createElement('figure').tap(figure => {
figure.append(document.createElement('img').tap(img => {
alt_text && (img.alt = img.title = alt_text);
Expand All @@ -94,6 +94,7 @@ const blockRenderers = {
.map(({ url, width }) => `${url} ${width}w`)
.join(',\n');
}));

if (attribution?.url) {
figure.append(document.createElement('a').tap(a => {
a.href = attribution.url;
Expand All @@ -110,6 +111,10 @@ const blockRenderers = {
}
}));
}

if (caption) {
figure.append(Object.assign(document.createElement('figcaption'), { textContent: caption }));
}
});
},

Expand Down
5 changes: 5 additions & 0 deletions src/outbox.css
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,11 @@ article footer button {
margin-right: 0;
}

[data-block="image"] figcaption {
padding: 0 var(--post-padding);
margin: calc(var(--post-vertical-spacing) / 2) 0;
}

[data-block="image"] a {
display: block;
overflow: hidden;
Expand Down

0 comments on commit 1d8c21e

Please sign in to comment.