fix: zoom image plugin issue, fixed #187 (#300)

This commit is contained in:
cinwell.li 2017-10-31 08:20:08 -05:00 committed by GitHub
parent cc98f56cdd
commit fa772cfa94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 53 additions and 37 deletions

View File

@ -97,6 +97,13 @@ Medium's Image Zoom. Based on [zoom-image](https://github.com/egoist/zoom-image)
<script src="//unpkg.com/docsify/lib/plugins/zoom-image.min.js"></script> <script src="//unpkg.com/docsify/lib/plugins/zoom-image.min.js"></script>
``` ```
Exclude the special image
```markdown
![](image.png ':no-zoom')
```
## Edit on github ## Edit on github
Add `Edit on github` button on every pages. provided by 3rd party, check [document](https://github.com/njleonzhang/docsify-edit-on-github) Add `Edit on github` button on every pages. provided by 3rd party, check [document](https://github.com/njleonzhang/docsify-edit-on-github)

10
package-lock.json generated
View File

@ -4239,6 +4239,11 @@
"integrity": "sha1-3oGf282E3M2PrlnGrreWFbnSZqw=", "integrity": "sha1-3oGf282E3M2PrlnGrreWFbnSZqw=",
"dev": true "dev": true
}, },
"medium-zoom": {
"version": "0.2.0",
"resolved": "https://registry.npmjs.org/medium-zoom/-/medium-zoom-0.2.0.tgz",
"integrity": "sha512-2595M4GDwjoyZVDtkw6JWY8JxkhZVumqPjvM8coyRcULPZpoij2bQJ1/syqtx4XiflaqTva0cAch1kzqk0ir9Q=="
},
"mem": { "mem": {
"version": "1.1.0", "version": "1.1.0",
"resolved": "https://registry.npmjs.org/mem/-/mem-1.1.0.tgz", "resolved": "https://registry.npmjs.org/mem/-/mem-1.1.0.tgz",
@ -7192,11 +7197,6 @@
"dev": true "dev": true
} }
} }
},
"zoom-image": {
"version": "0.1.4",
"resolved": "https://registry.npmjs.org/zoom-image/-/zoom-image-0.1.4.tgz",
"integrity": "sha1-+q8KgFG7Zya+YM891qM17HPx6Wk="
} }
} }
} }

View File

@ -31,10 +31,10 @@
}, },
"dependencies": { "dependencies": {
"marked": "^0.3.6", "marked": "^0.3.6",
"medium-zoom": "^0.2.0",
"prismjs": "^1.6.0", "prismjs": "^1.6.0",
"tinydate": "^1.0.0", "tinydate": "^1.0.0",
"tweezer.js": "^1.4.0", "tweezer.js": "^1.4.0"
"zoom-image": "^0.1.4"
}, },
"devDependencies": { "devDependencies": {
"conventional-changelog-cli": "^1.3.2", "conventional-changelog-cli": "^1.3.2",

View File

@ -8,6 +8,20 @@ import { isAbsolutePath, getPath } from '../router/util'
import { isFn, merge, cached } from '../util/core' import { isFn, merge, cached } from '../util/core'
const cachedLinks = {} const cachedLinks = {}
function getAndRemoveConfig (str = '') {
const config = {}
if (str) {
str = str
.replace(/:([\w-]+)=?([\w-]+)?/g, (m, key, value) => {
config[key] = value || true
return ''
})
.trim()
}
return { str, config }
}
export class Compiler { export class Compiler {
constructor (config, router) { constructor (config, router) {
@ -102,16 +116,9 @@ export class Compiler {
} }
origin.link = renderer.link = function (href, title = '', text) { origin.link = renderer.link = function (href, title = '', text) {
let attrs = '' let attrs = ''
const config = {}
if (title) { const { str, config } = getAndRemoveConfig(title)
title = title title = str
.replace(/:(\w+)=?(\w+)?/g, (m, key, value) => {
config[key] = value || true
return ''
})
.trim()
}
if ( if (
!/:|(\/{2})/.test(href) && !/:|(\/{2})/.test(href) &&
@ -133,9 +140,10 @@ export class Compiler {
} }
if (title) { if (title) {
title = ` title="${title}"` attrs += ` title="${title}"`
} }
return `<a href="${href}"${title || ''}${attrs}>${text}</a>`
return `<a href="${href}"${attrs}>${text}</a>`
} }
origin.paragraph = renderer.paragraph = function (text) { origin.paragraph = renderer.paragraph = function (text) {
if (/^!&gt;/.test(text)) { if (/^!&gt;/.test(text)) {
@ -147,13 +155,24 @@ export class Compiler {
} }
origin.image = renderer.image = function (href, title, text) { origin.image = renderer.image = function (href, title, text) {
let url = href let url = href
const titleHTML = title ? ` title="${title}"` : '' let attrs = ''
const { str, config } = getAndRemoveConfig(title)
title = str
if (config['no-zoom']) {
attrs += ' data-no-zoom'
}
if (title) {
attrs += ` title="${title}"`
}
if (!isAbsolutePath(href)) { if (!isAbsolutePath(href)) {
url = getPath(contentBase, href) url = getPath(contentBase, href)
} }
return `<img src="${url}" data-origin="${href}" alt="${text}"${titleHTML}>` return `<img src="${url}"data-origin="${href}" alt="${text}"${attrs}>`
} }
renderer.origin = origin renderer.origin = origin

View File

@ -1,22 +1,14 @@
import zoom from 'zoom-image' import mediumZoom from 'medium-zoom'
import style from 'zoom-image/css/zoom-image.css'
function install (hook) { function install (hook) {
const dom = Docsify.dom let zoom
let destroys
// add style
dom.appendTo(dom.head, dom.create('style', style))
hook.doneEach(_ => { hook.doneEach(_ => {
const images = dom.findAll('img:not(.emoji)') if (zoom) {
zoom.detach()
if (Array.isArray(destroys) && destroys.length) {
destroys.forEach(o => o())
destroys = []
} }
destroys = images.map(zoom) zoom = mediumZoom('img:not(.emoji):not([data-no-zoom])')
}) })
} }

View File

@ -212,6 +212,7 @@ main {
display: block; display: block;
position: relative; position: relative;
size: 100vw 100%; size: 100vw 100%;
z-index: 0;
} }
.anchor { .anchor {
@ -450,10 +451,7 @@ body.close {
@media print { @media print {
.github-corner, .github-corner,
.sidebar-toggle, .sidebar-toggle,
.sidebar { .sidebar,
display: none;
}
.app-nav { .app-nav {
display: none; display: none;
} }