Skip to content

Commit fa772cf

Browse files
authored
fix: zoom image plugin issue, fixed #187 (#300)
1 parent cc98f56 commit fa772cf

File tree

6 files changed

+53
-37
lines changed

6 files changed

+53
-37
lines changed

docs/plugins.md

+7
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ Medium's Image Zoom. Based on [zoom-image](https://github.com/egoist/zoom-image)
9797
<script src="//unpkg.com/docsify/lib/plugins/zoom-image.min.js"></script>
9898
```
9999

100+
Exclude the special image
101+
102+
```markdown
103+
![](image.png ':no-zoom')
104+
```
105+
106+
100107
## Edit on github
101108

102109
Add `Edit on github` button on every pages. provided by 3rd party, check [document](https://github.com/njleonzhang/docsify-edit-on-github)

package-lock.json

+5-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131
},
3232
"dependencies": {
3333
"marked": "^0.3.6",
34+
"medium-zoom": "^0.2.0",
3435
"prismjs": "^1.6.0",
3536
"tinydate": "^1.0.0",
36-
"tweezer.js": "^1.4.0",
37-
"zoom-image": "^0.1.4"
37+
"tweezer.js": "^1.4.0"
3838
},
3939
"devDependencies": {
4040
"conventional-changelog-cli": "^1.3.2",

src/core/render/compiler.js

+32-13
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,20 @@ import { isAbsolutePath, getPath } from '../router/util'
88
import { isFn, merge, cached } from '../util/core'
99

1010
const cachedLinks = {}
11+
function getAndRemoveConfig (str = '') {
12+
const config = {}
13+
14+
if (str) {
15+
str = str
16+
.replace(/:([\w-]+)=?([\w-]+)?/g, (m, key, value) => {
17+
config[key] = value || true
18+
return ''
19+
})
20+
.trim()
21+
}
22+
23+
return { str, config }
24+
}
1125

1226
export class Compiler {
1327
constructor (config, router) {
@@ -102,16 +116,9 @@ export class Compiler {
102116
}
103117
origin.link = renderer.link = function (href, title = '', text) {
104118
let attrs = ''
105-
const config = {}
106119

107-
if (title) {
108-
title = title
109-
.replace(/:(\w+)=?(\w+)?/g, (m, key, value) => {
110-
config[key] = value || true
111-
return ''
112-
})
113-
.trim()
114-
}
120+
const { str, config } = getAndRemoveConfig(title)
121+
title = str
115122

116123
if (
117124
!/:|(\/{2})/.test(href) &&
@@ -133,9 +140,10 @@ export class Compiler {
133140
}
134141

135142
if (title) {
136-
title = ` title="${title}"`
143+
attrs += ` title="${title}"`
137144
}
138-
return `<a href="${href}"${title || ''}${attrs}>${text}</a>`
145+
146+
return `<a href="${href}"${attrs}>${text}</a>`
139147
}
140148
origin.paragraph = renderer.paragraph = function (text) {
141149
if (/^!&gt;/.test(text)) {
@@ -147,13 +155,24 @@ export class Compiler {
147155
}
148156
origin.image = renderer.image = function (href, title, text) {
149157
let url = href
150-
const titleHTML = title ? ` title="${title}"` : ''
158+
let attrs = ''
159+
160+
const { str, config } = getAndRemoveConfig(title)
161+
title = str
162+
163+
if (config['no-zoom']) {
164+
attrs += ' data-no-zoom'
165+
}
166+
167+
if (title) {
168+
attrs += ` title="${title}"`
169+
}
151170

152171
if (!isAbsolutePath(href)) {
153172
url = getPath(contentBase, href)
154173
}
155174

156-
return `<img src="${url}" data-origin="${href}" alt="${text}"${titleHTML}>`
175+
return `<img src="${url}"data-origin="${href}" alt="${text}"${attrs}>`
157176
}
158177

159178
renderer.origin = origin

src/plugins/zoom-image.js

+5-13
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
1-
import zoom from 'zoom-image'
2-
import style from 'zoom-image/css/zoom-image.css'
1+
import mediumZoom from 'medium-zoom'
32

43
function install (hook) {
5-
const dom = Docsify.dom
6-
let destroys
7-
8-
// add style
9-
dom.appendTo(dom.head, dom.create('style', style))
4+
let zoom
105

116
hook.doneEach(_ => {
12-
const images = dom.findAll('img:not(.emoji)')
13-
14-
if (Array.isArray(destroys) && destroys.length) {
15-
destroys.forEach(o => o())
16-
destroys = []
7+
if (zoom) {
8+
zoom.detach()
179
}
1810

19-
destroys = images.map(zoom)
11+
zoom = mediumZoom('img:not(.emoji):not([data-no-zoom])')
2012
})
2113
}
2214

src/themes/basic/_layout.css

+2-4
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ main {
212212
display: block;
213213
position: relative;
214214
size: 100vw 100%;
215+
z-index: 0;
215216
}
216217

217218
.anchor {
@@ -450,10 +451,7 @@ body.close {
450451
@media print {
451452
.github-corner,
452453
.sidebar-toggle,
453-
.sidebar {
454-
display: none;
455-
}
456-
454+
.sidebar,
457455
.app-nav {
458456
display: none;
459457
}

0 commit comments

Comments
 (0)