Skip to content

Commit 7d97904

Browse files
committed
feat: 增加 img 标签 alt 属性注入
1 parent 7556f38 commit 7d97904

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

theme/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,16 @@ module.exports = (options, ctx) => {
9696
chainMarkdown(config) {
9797
config
9898
.plugin('custom-style')
99-
.use(require('./plugins/markdown/mdCustomStyle'));
99+
.use(require('./plugins/markdown/mdCustomStyle'))
100+
.end();
100101
config
101102
.plugin('code-result')
102-
.use(require('./plugins/markdown/mdCodeResult'));
103+
.use(require('./plugins/markdown/mdCodeResult'))
104+
.end();
105+
config
106+
.plugin('img-alt')
107+
.use(require('./plugins/markdown/mdAltImg'))
108+
.end();
103109

104110
// plugin
105111
config.plugin('sup').use(require('markdown-it-sup'));

theme/plugins/markdown/mdAltImg.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
module.exports = (md) => {
4+
5+
md.renderer.rules.image = (tokens, idx, options, env, self) => {
6+
const token = tokens[idx]
7+
const hrefIndex = token.attrIndex('src')
8+
if (hrefIndex >= 0) {
9+
const alt = token.content || (token.children && token.children[0] && token.children[0].content) || '';
10+
token.attrSet('alt', alt);
11+
}
12+
return self.renderToken(tokens, idx, options)
13+
}
14+
15+
}

0 commit comments

Comments
 (0)