From 7d97904d3fdafe2b2a156463932f2ff497c6f392 Mon Sep 17 00:00:00 2001 From: Zyao89 Date: Fri, 16 Jul 2021 14:24:05 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=20img=20=E6=A0=87?= =?UTF-8?q?=E7=AD=BE=20alt=20=E5=B1=9E=E6=80=A7=E6=B3=A8=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- theme/index.js | 10 ++++++++-- theme/plugins/markdown/mdAltImg.js | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 theme/plugins/markdown/mdAltImg.js diff --git a/theme/index.js b/theme/index.js index bcb5f14..15808c9 100644 --- a/theme/index.js +++ b/theme/index.js @@ -96,10 +96,16 @@ module.exports = (options, ctx) => { chainMarkdown(config) { config .plugin('custom-style') - .use(require('./plugins/markdown/mdCustomStyle')); + .use(require('./plugins/markdown/mdCustomStyle')) + .end(); config .plugin('code-result') - .use(require('./plugins/markdown/mdCodeResult')); + .use(require('./plugins/markdown/mdCodeResult')) + .end(); + config + .plugin('img-alt') + .use(require('./plugins/markdown/mdAltImg')) + .end(); // plugin config.plugin('sup').use(require('markdown-it-sup')); diff --git a/theme/plugins/markdown/mdAltImg.js b/theme/plugins/markdown/mdAltImg.js new file mode 100644 index 0000000..23ce85d --- /dev/null +++ b/theme/plugins/markdown/mdAltImg.js @@ -0,0 +1,15 @@ +'use strict'; + +module.exports = (md) => { + + md.renderer.rules.image = (tokens, idx, options, env, self) => { + const token = tokens[idx] + const hrefIndex = token.attrIndex('src') + if (hrefIndex >= 0) { + const alt = token.content || (token.children && token.children[0] && token.children[0].content) || ''; + token.attrSet('alt', alt); + } + return self.renderToken(tokens, idx, options) + } + +} \ No newline at end of file