Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: new option figcaption #264

Merged
merged 3 commits into from
Jul 22, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ marked:
dompurify: false
headerIds: true
lazyload: false
figcaption: false
prependRoot: true
postAsset: false
external_link:
Expand Down Expand Up @@ -79,6 +80,7 @@ marked:
* Example: `## [foo](#bar)`, id will be set as "bar".
* Requires **headerIds** to be enabled.
- **lazyload** - Lazy loading images via `loading="lazy"` attribute.
- **figcaption** - Append `figcaption` element after each image.
- **prependRoot** - Prepend root value to (internal) image path.
* Example `_config.yml`:
``` yml
Expand Down
5 changes: 4 additions & 1 deletion lib/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class Renderer extends MarkedRenderer {
image(href, title, text) {
const { hexo, options } = this;
const { relative_link } = hexo.config;
const { lazyload, prependRoot, postPath } = options;
const { lazyload, figcaption, prependRoot, postPath } = options;

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

out += '>';
if (figcaption) {
if (text) out += `<figcaption aria-hidden="true">${text}</figcaption>`;
}
return out;
}
}
Expand Down
20 changes: 20 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,26 @@ describe('Marked renderer', () => {
].join('\n'));
});

it('figcaption', () => {
const body = [
'![](/bar/baz.jpg)',
'![](/bar/baz.jpg "foo")',
'![foo](/aaa/bbb.jpg)'
].join('\n');

hexo.config.marked.figcaption = true;

const r = require('../lib/renderer').bind(hexo);

const result = r({ text: body });

result.should.eql([
'<p><img src="/bar/baz.jpg">',
'<img src="/bar/baz.jpg" title="foo"><figcaption aria-hidden="true">foo</figcaption>',
'<img src="/aaa/bbb.jpg" alt="foo"><figcaption aria-hidden="true">foo</figcaption></p>\n'
].join('\n'));
});

describe('postAsset', () => {
const Post = hexo.model('Post');
const PostAsset = hexo.model('PostAsset');
Expand Down