Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tolking committed Sep 21, 2019
0 parents commit 2abd2d9
Show file tree
Hide file tree
Showing 13 changed files with 418 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.DS_Store
node_modules/
dist/
npm-debug.log
package-lock.json
yarn.lock
yarn-error.log
19 changes: 19 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.DS_Store
.npmignore
.gitignore
.editorconfig
.eslintrc.js
.prettierrc

node_modules/
test/
dist/
docs/
examples/
npm-debug.log
yarn-error.log
yarn.lock
package-lock.json

*.test.js
.travis.yml
19 changes: 19 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
language: node_js
node_js:
- lts/*
install:
- yarn
cache:
directories:
- node_modules
script:
- yarn build
deploy:
committer_from_gh: true
provider: pages
skip-cleanup: true
local_dir: dist
github-token: $GITHUB_TOKEN
keep-history: true
on:
branch: master
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright 2019 tolking <qw13131wang@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# vuepress-plugin-img-lazy

> a vuepress plugin to better supporting image lazy loading
base on [markdown-it-img-lazy](https://github.com/tolking/markdown-it-img-lazy) and [markdown-it-imsize](https://github.com/tatsy/markdown-it-imsize) and [lozad](https://github.com/ApoorvSaxena/lozad.js)

[Live Demo and Documentation](https://tolking.github.io/vuepress-plugin-img-lazy/preview.html)

---

## Installation

``` sh
yarn add vuepress-plugin-img-lazy
# or
npm i vuepress-plugin-img-lazy
```

## Usage

``` js
module.exports = {
plugins: [
'img-lazy'
]
}
```

``` md
![img](img.jpg)
<!-- or -->
![img](img.jpg =500x300) <!-- better -->
<!-- or -->
<img loading="lazy" data-src="img.jpg" class="lazy">
```

## Options

### useLoading
- Type: `Boolben`
- Default: `true`

Use the native image [lazy-loading](https://caniuse.com/#feat=loading-lazy-attr) for the web

### selector
- Type: `string`
- Default: `lazy`

Default class name for image
66 changes: 66 additions & 0 deletions docs/.vuepress/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
module.exports = {
base: '/vuepress-plugin-img-lazy/',
title: 'vuepress-plugin-img-lazy',
description: 'a vuepress plugin to better supporting image lazy loading',
dest: 'dist/',
evergreen: true,
cache: false,
theme: 'default-prefers-color-scheme',
locales: {
'/': {
lang: 'en-US',
title: 'vuepress-plugin-img-lazy',
description: 'a vuepress plugin to better supporting image lazy loading'
},
'/zh/': {
lang: 'zh-CN',
title: 'vuepress-plugin-img-lazy',
description: '为 vuepress 的默认主题增加 prefers-color-scheme'
}
},
themeConfig: {
repo: 'tolking/vuepress-plugin-img-lazy',
docsDir: 'examples',
editLinks: true,
locales: {
'/': {
selectText: 'Languages',
label: 'English',
editLinkText: 'Edit this page on GitHub',
lastUpdated: 'Last Updated',
serviceWorker: {
updatePopup: {
message: "New content is available.",
buttonText: "Refresh"
}
},
sidebar: [
'/',
'/preview'
]
},
'/zh/': {
selectText: '选择语言',
label: '简体中文',
editLinkText: '在 GitHub 上编辑此页',
lastUpdated: '最后更新时间',
serviceWorker: {
updatePopup: {
message: "发现新内容可用.",
buttonText: "刷新"
}
},
sidebar: [
'/zh/',
'/preview'
]
}
}
},
plugins: [
[require.resolve('../../index.js'), {
useLoading: true,
selector: 'lazy'
}]
]
}
Binary file added docs/.vuepress/public/favicon.ico
Binary file not shown.
47 changes: 47 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
## Introduction

> a vuepress plugin to better supporting image lazy loading
base on [markdown-it-img-lazy](https://github.com/tolking/markdown-it-img-lazy) and [markdown-it-imsize](https://github.com/tatsy/markdown-it-imsize) and [lozad](https://github.com/ApoorvSaxena/lozad.js)

---

## Installation

``` sh
yarn add vuepress-plugin-img-lazy
# or
npm i vuepress-plugin-img-lazy
```

## Usage

``` js
module.exports = {
plugins: [
'img-lazy'
]
}
```

``` md
![img](img.jpg)
<!-- or -->
![img](img.jpg =500x300) <!-- better -->
<!-- or -->
<img loading="lazy" data-src="img.jpg" class="lazy">
```

## Options

### useLoading
- Type: `Boolben`
- Default: `true`

Use the native image [lazy-loading](https://caniuse.com/#feat=loading-lazy-attr) for the web

### selector
- Type: `string`
- Default: `lazy`

Default class name for image
52 changes: 52 additions & 0 deletions docs/preview.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
## Preview

![img](https://picsum.photos/536/354?random=1 =536x354)
![img](https://picsum.photos/536/354?random=2 =536x354)
![img](https://picsum.photos/536/354?random=3 =536x354)
![img](https://picsum.photos/536/354?random=4 =536x354)
![img](https://picsum.photos/536/354?random=5 =536x354)
![img](https://picsum.photos/536/354?random=6 =536x354)
![img](https://picsum.photos/536/354?random=7 =536x354)
![img](https://picsum.photos/536/354?random=8 =536x354)
![img](https://picsum.photos/536/354?random=9 =536x354)
![img](https://picsum.photos/536/354?random=10 =536x354)
![img](https://picsum.photos/536/354?random=11 =536x354)
![img](https://picsum.photos/536/354?random=12 =536x354)
![img](https://picsum.photos/536/354?random=13 =536x354)
![img](https://picsum.photos/536/354?random=14 =536x354)
![img](https://picsum.photos/536/354?random=15 =536x354)
![img](https://picsum.photos/536/354?random=16 =536x354)
![img](https://picsum.photos/536/354?random=17 =536x354)
![img](https://picsum.photos/536/354?random=18 =536x354)
![img](https://picsum.photos/536/354?random=19 =536x354)
![img](https://picsum.photos/536/354?random=20 =536x354)
![img](https://picsum.photos/536/354?random=21 =536x354)
![img](https://picsum.photos/536/354?random=22 =536x354)
![img](https://picsum.photos/536/354?random=23 =536x354)
![img](https://picsum.photos/536/354?random=24 =536x354)
![img](https://picsum.photos/536/354?random=25 =536x354)
![img](https://picsum.photos/536/354?random=26 =536x354)
![img](https://picsum.photos/536/354?random=27 =536x354)
![img](https://picsum.photos/536/354?random=28 =536x354)
![img](https://picsum.photos/536/354?random=29 =536x354)
![img](https://picsum.photos/536/354?random=30 =536x354)
![img](https://picsum.photos/536/354?random=31 =536x354)
![img](https://picsum.photos/536/354?random=32 =536x354)
![img](https://picsum.photos/536/354?random=33 =536x354)
![img](https://picsum.photos/536/354?random=34 =536x354)
![img](https://picsum.photos/536/354?random=35 =536x354)
![img](https://picsum.photos/536/354?random=36 =536x354)
![img](https://picsum.photos/536/354?random=37 =536x354)
![img](https://picsum.photos/536/354?random=38 =536x354)
![img](https://picsum.photos/536/354?random=39 =536x354)
![img](https://picsum.photos/536/354?random=40 =536x354)
![img](https://picsum.photos/536/354?random=41 =536x354)
![img](https://picsum.photos/536/354?random=42 =536x354)
![img](https://picsum.photos/536/354?random=43 =536x354)
![img](https://picsum.photos/536/354?random=44 =536x354)
![img](https://picsum.photos/536/354?random=45 =536x354)
![img](https://picsum.photos/536/354?random=46 =536x354)
![img](https://picsum.photos/536/354?random=47 =536x354)
![img](https://picsum.photos/536/354?random=48 =536x354)
![img](https://picsum.photos/536/354?random=49 =536x354)
![img](https://picsum.photos/536/354?random=50 =536x354)
47 changes: 47 additions & 0 deletions docs/zh/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
## 介绍

> a vuepress plugin to better supporting image lazy loading
基于 [markdown-it-img-lazy](https://github.com/tolking/markdown-it-img-lazy)[markdown-it-imsize](https://github.com/tatsy/markdown-it-imsize)[lozad](https://github.com/ApoorvSaxena/lozad.js)

---

## 安装

``` sh
yarn add vuepress-plugin-img-lazy
# or
npm i vuepress-plugin-img-lazy
```

## 使用

``` js
module.exports = {
plugins: [
'img-lazy'
]
}
```

``` md
![img](img.jpg)
<!-- or -->
![img](img.jpg =500x300) <!-- 最佳 -->
<!-- or -->
<img loading="lazy" data-src="img.jpg" class="lazy">
```

## 配置

### useLoading
- Type: `Boolben`
- Default: `true`

是否使用基于 [原生 web](https://caniuse.com/#feat=loading-lazy-attr) 的懒加载

### selector
- Type: `string`
- Default: `lazy`

默认的懒加载类名
28 changes: 28 additions & 0 deletions enhanceAppFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import config from '@dynamic/imgLazy'

export default ({ Vue }) => {
Vue.mixin({
data() {
return {
$imgLazyObserver: {}
}
},

computed: {
$supLoading() {
return config.useLoading && "loading" in HTMLImageElement.prototype
}
},

mounted() {
if (this.$supLoading) {
const lazyEls = document.querySelectorAll('.' + config.selector)
lazyEls.forEach(lazyEl => lazyEl.setAttribute("src", lazyEl.getAttribute("data-src")))
} else {
const lozad = require('lozad')
this.$imgLazyObserver = lozad('.' + config.selector)
this.$imgLazyObserver.observe()
}
}
})
}
28 changes: 28 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const { path } = require('@vuepress/shared-utils')

module.exports = (options, ctx) => {
const {
useLoading = true,
selector = 'lazy'
} = options

return {
name: 'vuepress-plugin-img-lazy',

extendMarkdown: md => {
md.use(require('markdown-it-img-lazy'), { useLoading, selector })
md.use(require('markdown-it-imsize'))
},

async clientDynamicModules () {
return [
{
name: 'imgLazy.js',
content: `export default ${JSON.stringify({ useLoading, selector })}`
}
]
},

enhanceAppFiles: path.resolve(__dirname, 'enhanceAppFile.js')
}
}
Loading

0 comments on commit 2abd2d9

Please sign in to comment.