From cdca03b3cc72ced1b6dd8ad023f1a6f61c2de3b9 Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Sat, 22 Jul 2023 22:21:18 +0800 Subject: [PATCH] feat: new option figcaption (#264) --- README.md | 2 ++ lib/renderer.js | 5 ++++- test/index.js | 20 ++++++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 65892c8..bd6cbcd 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,7 @@ marked: dompurify: false headerIds: true lazyload: false + figcaption: false prependRoot: true postAsset: false external_link: @@ -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 diff --git a/lib/renderer.js b/lib/renderer.js index 7b8f769..8a206c4 100644 --- a/lib/renderer.js +++ b/lib/renderer.js @@ -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) { @@ -137,6 +137,9 @@ class Renderer extends MarkedRenderer { if (lazyload) out += ' loading="lazy"'; out += '>'; + if (figcaption) { + if (text) out += ``; + } return out; } } diff --git a/test/index.js b/test/index.js index 84994c3..89610a0 100644 --- a/test/index.js +++ b/test/index.js @@ -791,6 +791,26 @@ describe('Marked renderer', () => { ].join('\n')); }); + it('figcaption', () => { + const body = [ + '![](/bar/baz.jpg "bar")', + '![foo](/bar/baz.jpg "bar")', + '![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([ + '

', + 'foo

', + 'foo

\n' + ].join('\n')); + }); + describe('postAsset', () => { const Post = hexo.model('Post'); const PostAsset = hexo.model('PostAsset');