From fa772cfa94056e7f25b00d3f30a0c3604ae2d55e Mon Sep 17 00:00:00 2001 From: "cinwell.li" Date: Tue, 31 Oct 2017 08:20:08 -0500 Subject: [PATCH] fix: zoom image plugin issue, fixed #187 (#300) --- docs/plugins.md | 7 ++++++ package-lock.json | 10 ++++---- package.json | 4 ++-- src/core/render/compiler.js | 45 +++++++++++++++++++++++++----------- src/plugins/zoom-image.js | 18 ++++----------- src/themes/basic/_layout.css | 6 ++--- 6 files changed, 53 insertions(+), 37 deletions(-) diff --git a/docs/plugins.md b/docs/plugins.md index a2118b533..0bc605471 100644 --- a/docs/plugins.md +++ b/docs/plugins.md @@ -97,6 +97,13 @@ Medium's Image Zoom. Based on [zoom-image](https://github.com/egoist/zoom-image) ``` +Exclude the special image + +```markdown +![](image.png ':no-zoom') +``` + + ## 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) diff --git a/package-lock.json b/package-lock.json index fb6e399b6..fd681267f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4239,6 +4239,11 @@ "integrity": "sha1-3oGf282E3M2PrlnGrreWFbnSZqw=", "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": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/mem/-/mem-1.1.0.tgz", @@ -7192,11 +7197,6 @@ "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=" } } } diff --git a/package.json b/package.json index 1a5c35de4..c5d802c97 100644 --- a/package.json +++ b/package.json @@ -31,10 +31,10 @@ }, "dependencies": { "marked": "^0.3.6", + "medium-zoom": "^0.2.0", "prismjs": "^1.6.0", "tinydate": "^1.0.0", - "tweezer.js": "^1.4.0", - "zoom-image": "^0.1.4" + "tweezer.js": "^1.4.0" }, "devDependencies": { "conventional-changelog-cli": "^1.3.2", diff --git a/src/core/render/compiler.js b/src/core/render/compiler.js index fc140f193..6bc68b2ea 100644 --- a/src/core/render/compiler.js +++ b/src/core/render/compiler.js @@ -8,6 +8,20 @@ import { isAbsolutePath, getPath } from '../router/util' import { isFn, merge, cached } from '../util/core' 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 { constructor (config, router) { @@ -102,16 +116,9 @@ export class Compiler { } origin.link = renderer.link = function (href, title = '', text) { let attrs = '' - const config = {} - if (title) { - title = title - .replace(/:(\w+)=?(\w+)?/g, (m, key, value) => { - config[key] = value || true - return '' - }) - .trim() - } + const { str, config } = getAndRemoveConfig(title) + title = str if ( !/:|(\/{2})/.test(href) && @@ -133,9 +140,10 @@ export class Compiler { } if (title) { - title = ` title="${title}"` + attrs += ` title="${title}"` } - return `${text}` + + return `${text}` } origin.paragraph = renderer.paragraph = function (text) { if (/^!>/.test(text)) { @@ -147,13 +155,24 @@ export class Compiler { } origin.image = renderer.image = function (href, title, text) { 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)) { url = getPath(contentBase, href) } - return `${text}` + return `${text}` } renderer.origin = origin diff --git a/src/plugins/zoom-image.js b/src/plugins/zoom-image.js index 15e643502..6f3b94c38 100644 --- a/src/plugins/zoom-image.js +++ b/src/plugins/zoom-image.js @@ -1,22 +1,14 @@ -import zoom from 'zoom-image' -import style from 'zoom-image/css/zoom-image.css' +import mediumZoom from 'medium-zoom' function install (hook) { - const dom = Docsify.dom - let destroys - - // add style - dom.appendTo(dom.head, dom.create('style', style)) + let zoom hook.doneEach(_ => { - const images = dom.findAll('img:not(.emoji)') - - if (Array.isArray(destroys) && destroys.length) { - destroys.forEach(o => o()) - destroys = [] + if (zoom) { + zoom.detach() } - destroys = images.map(zoom) + zoom = mediumZoom('img:not(.emoji):not([data-no-zoom])') }) } diff --git a/src/themes/basic/_layout.css b/src/themes/basic/_layout.css index 36ba79919..6663e1b90 100644 --- a/src/themes/basic/_layout.css +++ b/src/themes/basic/_layout.css @@ -212,6 +212,7 @@ main { display: block; position: relative; size: 100vw 100%; + z-index: 0; } .anchor { @@ -450,10 +451,7 @@ body.close { @media print { .github-corner, .sidebar-toggle, - .sidebar { - display: none; - } - + .sidebar, .app-nav { display: none; }