Skip to content

Commit cdca03b

Browse files
feat: new option figcaption (#264)
1 parent 906eae3 commit cdca03b

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ marked:
4848
dompurify: false
4949
headerIds: true
5050
lazyload: false
51+
figcaption: false
5152
prependRoot: true
5253
postAsset: false
5354
external_link:
@@ -79,6 +80,7 @@ marked:
7980
* Example: `## [foo](#bar)`, id will be set as "bar".
8081
* Requires **headerIds** to be enabled.
8182
- **lazyload** - Lazy loading images via `loading="lazy"` attribute.
83+
- **figcaption** - Append `figcaption` element after each image.
8284
- **prependRoot** - Prepend root value to (internal) image path.
8385
* Example `_config.yml`:
8486
``` yml

lib/renderer.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class Renderer extends MarkedRenderer {
118118
image(href, title, text) {
119119
const { hexo, options } = this;
120120
const { relative_link } = hexo.config;
121-
const { lazyload, prependRoot, postPath } = options;
121+
const { lazyload, figcaption, prependRoot, postPath } = options;
122122

123123
if (!/^(#|\/\/|http(s)?:)/.test(href) && !relative_link && prependRoot) {
124124
if (!href.startsWith('/') && !href.startsWith('\\') && postPath) {
@@ -137,6 +137,9 @@ class Renderer extends MarkedRenderer {
137137
if (lazyload) out += ' loading="lazy"';
138138

139139
out += '>';
140+
if (figcaption) {
141+
if (text) out += `<figcaption aria-hidden="true">${text}</figcaption>`;
142+
}
140143
return out;
141144
}
142145
}

test/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,26 @@ describe('Marked renderer', () => {
791791
].join('\n'));
792792
});
793793

794+
it('figcaption', () => {
795+
const body = [
796+
'![](/bar/baz.jpg "bar")',
797+
'![foo](/bar/baz.jpg "bar")',
798+
'![foo](/aaa/bbb.jpg)'
799+
].join('\n');
800+
801+
hexo.config.marked.figcaption = true;
802+
803+
const r = require('../lib/renderer').bind(hexo);
804+
805+
const result = r({ text: body });
806+
807+
result.should.eql([
808+
'<p><img src="/bar/baz.jpg" title="bar">',
809+
'<img src="/bar/baz.jpg" alt="foo" title="bar"><figcaption aria-hidden="true">foo</figcaption>',
810+
'<img src="/aaa/bbb.jpg" alt="foo"><figcaption aria-hidden="true">foo</figcaption></p>\n'
811+
].join('\n'));
812+
});
813+
794814
describe('postAsset', () => {
795815
const Post = hexo.model('Post');
796816
const PostAsset = hexo.model('PostAsset');

0 commit comments

Comments
 (0)