From ec53f1e307f8d0480cb2936bf98fd262bfd587c3 Mon Sep 17 00:00:00 2001 From: "cinwell.li" Date: Thu, 15 Dec 2016 19:11:15 +0800 Subject: [PATCH 001/983] Add Beerpay's badge --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 12c85e5..055c0f4 100644 --- a/README.md +++ b/README.md @@ -60,3 +60,8 @@ These open-source projects are using docsify to generate their sites. Pull reque ## License MIT + +## Support on Beerpay +Hey dude! Help me out for a couple of :beers:! + +[![Beerpay](https://beerpay.io/QingWei-Li/docsify/badge.svg?style=beer-square)](https://beerpay.io/QingWei-Li/docsify) [![Beerpay](https://beerpay.io/QingWei-Li/docsify/make-wish.svg?style=flat-square)](https://beerpay.io/QingWei-Li/docsify?focus=wish) \ No newline at end of file From 2610e25040665c129c9665a37197ec1096e5cfc5 Mon Sep 17 00:00:00 2001 From: "cinwell.li" Date: Fri, 16 Dec 2016 22:37:21 +0800 Subject: [PATCH 002/983] Add progress bar and auto2top (#27) * Add progress bar * Add abort ajax * Remove console * Add cache folder * Add auto2top --- .travis.yml | 3 +++ 404.dev.html | 2 +- CHANGELOG.md | 5 +++++ docs/README.md | 10 ++++++++++ docs/zh-cn.md | 10 ++++++++++ src/event.js | 6 ++++++ src/index.js | 29 ++++++++++++++++++----------- src/render.js | 26 +++++++++++++++++++++++++- src/themes/basic/_layout.css | 12 ++++++++++++ src/tpl.js | 1 + src/util.js | 12 +++++++++--- 11 files changed, 100 insertions(+), 16 deletions(-) diff --git a/.travis.yml b/.travis.yml index 87576c5..fb0b83e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,6 @@ sudo: false +cache: + directories: + - $HOME/.yarn-cache language: node_js node_js: stable diff --git a/404.dev.html b/404.dev.html index b8c6e05..dba5d41 100644 --- a/404.dev.html +++ b/404.dev.html @@ -8,5 +8,5 @@
- + diff --git a/CHANGELOG.md b/CHANGELOG.md index 440325a..5131f25 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 1.1.0 +## Features +- Add progress bar +- Add `auto2top` option for hash router + ## 1.0.3 ### Bug fixes - Fix cache diff --git a/docs/README.md b/docs/README.md index 7aea75c..44ebf25 100644 --- a/docs/README.md +++ b/docs/README.md @@ -220,3 +220,13 @@ Hash router. You can replace `404.html` with `index.html`. ```html ``` + +### auto2top + +Scroll to the top on changing hash. + + +```html + +``` + diff --git a/docs/zh-cn.md b/docs/zh-cn.md index 28e6fda..2ac3106 100644 --- a/docs/zh-cn.md +++ b/docs/zh-cn.md @@ -221,3 +221,13 @@ Sidebar 开关按钮 ```html ``` + +### auto2top + +切换路由时自动跳转到页面顶部 + + +```html + +``` + diff --git a/src/event.js b/src/event.js index fda0205..48922fb 100644 --- a/src/event.js +++ b/src/event.js @@ -90,3 +90,9 @@ export function bindToggle (dom) { body.classList.toggle('close') }) } + +let cacheContentDOM +export function scroll2Top (dom) { + cacheContentDOM = cacheContentDOM || document.querySelector(dom) + cacheContentDOM.scrollTop = 0 +} diff --git a/src/index.js b/src/index.js index f7063e4..9d35660 100644 --- a/src/index.js +++ b/src/index.js @@ -10,7 +10,8 @@ const OPTIONS = { sidebarToggle: false, loadSidebar: null, loadNavbar: null, - router: false + router: false, + auto2top: false } const script = document.currentScript || [].slice.call(document.getElementsByTagName('script')).pop() @@ -29,6 +30,7 @@ if (script) { render.config(OPTIONS) let cacheRoute = null +let cacheXhr = null const mainRender = function (cb) { const route = getRoute() @@ -42,17 +44,22 @@ const mainRender = function (cb) { basePath = basePath.match(/(\S*\/)[^\/]+$/)[1] } + cacheXhr && cacheXhr.abort && cacheXhr.abort() // Render markdown file - load((!route || /\/$/.test(route)) ? `${route}README.md` : `${route}.md`) - .then(result => { - render.renderArticle(result) - if (OPTIONS.loadSidebar) { - if (wait === false) cb() - wait = false - } else { - cb() - } - }, _ => render.renderArticle(null)) + cacheXhr = load( + (!route || /\/$/.test(route)) ? `${route}README.md` : `${route}.md`, + 'GET', + render.renderLoading) + + cacheXhr.then(result => { + render.renderArticle(result) + if (OPTIONS.loadSidebar) { + if (wait === false) cb() + wait = false + } else { + cb() + } + }, _ => render.renderArticle(null)) // Render sidebar if (OPTIONS.loadSidebar) { diff --git a/src/render.js b/src/render.js index 3688db3..e94cd03 100644 --- a/src/render.js +++ b/src/render.js @@ -1,7 +1,7 @@ import marked from 'marked' import Prism from 'prismjs' import * as tpl from './tpl' -import { activeLink, scrollActiveSidebar, bindToggle } from './event' +import { activeLink, scrollActiveSidebar, bindToggle, scroll2Top } from './event' import { genTree, getRoute } from './util' let OPTIONS = {} @@ -69,6 +69,8 @@ export function renderArticle (content) { if (!renderNavbar.rendered) renderNavbar(null, OPTIONS) renderSidebar.rendered = false renderNavbar.rendered = false + + if (OPTIONS.auto2top) scroll2Top('section.content') } /** @@ -106,6 +108,28 @@ export function renderSidebar (content) { toc = [] } +/** + * render loading bar + * @return {[type]} [description] + */ +export function renderLoading ({ loaded, total }) { + const num = Math.floor(loaded / total * 100) + + if (!CACHE['loading']) { + const div = document.createElement('div') + + div.classList.add('progress') + document.body.appendChild(div) + CACHE['loading'] = div + } + CACHE['loading'].style.width = num >= 95 ? '0%' : num + '%' +} + +/** + * Load Config + * @param {Object} options + */ export function config (options) { OPTIONS = options } + diff --git a/src/themes/basic/_layout.css b/src/themes/basic/_layout.css index 67f6db9..ddcead4 100644 --- a/src/themes/basic/_layout.css +++ b/src/themes/basic/_layout.css @@ -8,6 +8,18 @@ -webkit-font-smoothing: antialiased; } +.progress { + background-color: $color-primary; + height: 2px; + left: 0px; + position: fixed; + right: 0px; + top: 0px; + transition: width 0.2s, opacity 0.4s; + width: 0%; + z-index: 999999; +} + html, body { height: 100%; } diff --git a/src/tpl.js b/src/tpl.js index 5c74a8b..d8912d1 100644 --- a/src/tpl.js +++ b/src/tpl.js @@ -57,3 +57,4 @@ export function tree (toc, tpl = '') { return tpl } + diff --git a/src/util.js b/src/util.js index 26208e6..4db5623 100644 --- a/src/util.js +++ b/src/util.js @@ -1,10 +1,11 @@ /** * Simple ajax * @param {String} url - * @param {String} [method=get] + * @param {String} [method=GET] + * @param {Function} [loading] handle loading * @return {Promise} */ -export function load (url, method = 'get') { +export function load (url, method = 'GET', loading) { const xhr = new XMLHttpRequest() xhr.open(method, url) @@ -12,11 +13,16 @@ export function load (url, method = 'get') { return { then: function (success, error = function () {}) { + if (loading) { + xhr.addEventListener('progress', loading) + xhr.addEventListener('loaded', loading) + } xhr.addEventListener('error', error) xhr.addEventListener('load', ({ target }) => { target.status >= 400 ? error(target) : success(target.response) }) - } + }, + abort: () => xhr.readyState !== 4 && xhr.abort() } } From 7f0b1089c049cd8013ec8b27ed471ee52b08e322 Mon Sep 17 00:00:00 2001 From: "qingwei.li" Date: Fri, 16 Dec 2016 22:38:44 +0800 Subject: [PATCH 003/983] bump 1.1.0 --- lib/docsify.js | 75 +++++++++++++++++++++++++++++++++++--------- lib/docsify.min.js | 4 +-- lib/themes/buble.css | 2 +- lib/themes/pure.css | 2 +- lib/themes/vue.css | 2 +- 5 files changed, 65 insertions(+), 20 deletions(-) diff --git a/lib/docsify.js b/lib/docsify.js index 415fb8b..487c5f2 100644 --- a/lib/docsify.js +++ b/lib/docsify.js @@ -4,11 +4,12 @@ var Docsify = (function () { /** * Simple ajax * @param {String} url - * @param {String} [method=get] + * @param {String} [method=GET] + * @param {Function} [loading] handle loading * @return {Promise} */ -function load (url, method) { - if ( method === void 0 ) method = 'get'; +function load (url, method, loading) { + if ( method === void 0 ) method = 'GET'; var xhr = new XMLHttpRequest(); @@ -19,13 +20,18 @@ function load (url, method) { then: function (success, error) { if ( error === void 0 ) error = function () {}; + if (loading) { + xhr.addEventListener('progress', loading); + xhr.addEventListener('loaded', loading); + } xhr.addEventListener('error', error); xhr.addEventListener('load', function (ref) { var target = ref.target; target.status >= 400 ? error(target) : success(target.response); }); - } + }, + abort: function () { return xhr.readyState !== 4 && xhr.abort(); } } } @@ -191,6 +197,12 @@ function bindToggle (dom) { }); } +var cacheContentDOM; +function scroll2Top (dom) { + cacheContentDOM = cacheContentDOM || document.querySelector(dom); + cacheContentDOM.scrollTop = 0; +} + var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; @@ -2399,6 +2411,8 @@ function renderArticle (content) { if (!renderNavbar.rendered) { renderNavbar(null, OPTIONS$1); } renderSidebar.rendered = false; renderNavbar.rendered = false; + + if (OPTIONS$1.auto2top) { scroll2Top('section.content'); } } /** @@ -2436,6 +2450,30 @@ function renderSidebar (content) { toc = []; } +/** + * render loading bar + * @return {[type]} [description] + */ +function renderLoading (ref) { + var loaded = ref.loaded; + var total = ref.total; + + var num = Math.floor(loaded / total * 100); + + if (!CACHE['loading']) { + var div = document.createElement('div'); + + div.classList.add('progress'); + document.body.appendChild(div); + CACHE['loading'] = div; + } + CACHE['loading'].style.width = num >= 95 ? '0%' : num + '%'; +} + +/** + * Load Config + * @param {Object} options + */ function config (options) { OPTIONS$1 = options; } @@ -2448,7 +2486,8 @@ var OPTIONS = { sidebarToggle: false, loadSidebar: null, loadNavbar: null, - router: false + router: false, + auto2top: false }; var script = document.currentScript || [].slice.call(document.getElementsByTagName('script')).pop(); @@ -2467,6 +2506,7 @@ if (script) { config(OPTIONS); var cacheRoute = null; +var cacheXhr = null; var mainRender = function (cb) { var route = getRoute(); @@ -2480,17 +2520,22 @@ var mainRender = function (cb) { basePath = basePath.match(/(\S*\/)[^\/]+$/)[1]; } + cacheXhr && cacheXhr.abort && cacheXhr.abort(); // Render markdown file - load((!route || /\/$/.test(route)) ? (route + "README.md") : (route + ".md")) - .then(function (result) { - renderArticle(result); - if (OPTIONS.loadSidebar) { - if (wait === false) { cb(); } - wait = false; - } else { - cb(); - } - }, function (_) { return renderArticle(null); }); + cacheXhr = load( + (!route || /\/$/.test(route)) ? (route + "README.md") : (route + ".md"), + 'GET', + renderLoading); + + cacheXhr.then(function (result) { + renderArticle(result); + if (OPTIONS.loadSidebar) { + if (wait === false) { cb(); } + wait = false; + } else { + cb(); + } + }, function (_) { return renderArticle(null); }); // Render sidebar if (OPTIONS.loadSidebar) { diff --git a/lib/docsify.min.js b/lib/docsify.min.js index 441baac..dddb056 100644 --- a/lib/docsify.min.js +++ b/lib/docsify.min.js @@ -1,2 +1,2 @@ -var Docsify=function(){"use strict";function e(e,t){void 0===t&&(t="get");var n=new XMLHttpRequest;return n.open(t,e),n.send(),{then:function(e,t){void 0===t&&(t=function(){}),n.addEventListener("error",t),n.addEventListener("load",function(n){var r=n.target;r.status>=400?t(r):e(r.response)})}}}function t(e,t){var n=[],r={};return e.forEach(function(e){var i=e.level||1,a=i-1;i>t||(r[a]?(r[a].children=r[a].children||[],r[a].children.push(e)):n.push(e),r[i]=e)}),n}function n(e){return e.replace(/([A-Z])/g,function(e){return"-"+e.toLowerCase()})}function r(e){return null===e||void 0===e}function i(){var e=window.location;if(v===e.hash&&!r(k))return k;var t=e.hash.match(/^#\/([^#]+)/);return t=t&&2===t.length?t[1]:/^#\//.test(e.hash)?"":e.pathname,k=t,v=e.hash,t}function a(){function e(){for(var e=0,r=t.length;e10){var o=n[a.id];if(!o||o===i)return;return i&&i.setAttribute("class",""),o.setAttribute("class","active"),void(i=o)}}}if(!/mobile/i.test(navigator.userAgent)){for(var t=document.querySelectorAll(".anchor"),n={},r=document.querySelectorAll(".sidebar li"),i=null,a=0,s=r.length;a\n \n '):""}function p(){return'
\n \n
\n
\n
\n
'}function g(e){return e?'':""}function h(e,t){return void 0===t&&(t=""),e&&e.length?(e.forEach(function(e){t+='
  • '+e.title+"
  • ",e.children&&(t+='
    • '+h(e.children)+"
    ")}),t):""}function d(e,t){var n=document.querySelector("nav")||document.createElement("nav");e[t?"outerHTML":"innerHTML"]=g(_.sidebarToggle)+c(_.repo)+p(),document.body.insertBefore(n,document.body.children[0]),l("button.sidebar-toggle")}function f(e){C("article",e?w(e):"not found"),b.rendered||b(null,_),m.rendered||m(null,_),b.rendered=!1,m.rendered=!1}function m(e){L.navbar&&L.navbar===e||(L.navbar=e,m.rendered=!0,e&&C("nav",w(e)),o("nav"))}function b(e){var n=!1;e?e=w(e):_.sidebar?e=h(_.sidebar,"