Skip to content

Commit

Permalink
feat: 增加 img 标签 alt 属性注入
Browse files Browse the repository at this point in the history
  • Loading branch information
zyao89 committed Jul 16, 2021
1 parent 7556f38 commit 7d97904
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
10 changes: 8 additions & 2 deletions theme/index.js
Expand Up @@ -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'));
Expand Down
15 changes: 15 additions & 0 deletions 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)
}

}

0 comments on commit 7d97904

Please sign in to comment.