Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: docsifyjs/docsify
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.0.1
Choose a base ref
...
head repository: docsifyjs/docsify
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v1.0.2
Choose a head ref
  • 3 commits
  • 9 files changed
  • 1 contributor

Commits on Dec 13, 2016

  1. Fix/1.0.2 (#25)

    * Fix regular expression, fixed #23
    
    * Fix repeat binding events
    
    * Add content cache
    
    * Add changelog
    QingWei-Li authored Dec 13, 2016

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    44a2551 View commit details
  2. bump 1.0.2

    QingWei-Li committed Dec 13, 2016
    Copy the full SHA
    2574778 View commit details
  3. -> v1.0.2

    QingWei-Li committed Dec 13, 2016
    Copy the full SHA
    d175ba6 View commit details
Showing with 176 additions and 126 deletions.
  1. +5 −0 CHANGELOG.md
  2. +124 −102 lib/docsify.js
  3. +2 −2 lib/docsify.min.js
  4. +1 −1 lib/themes/vue.css
  5. +1 −1 package.json
  6. +9 −10 src/event.js
  7. +27 −8 src/index.js
  8. +6 −1 src/render.js
  9. +1 −1 src/themes/vue.css
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.0.2
### Bug fixes
- Fix binding events bug, fixed #24
- Fix regular expression, fixed #23

## 1.0.1
### Bug fixes
- `img` style
226 changes: 124 additions & 102 deletions lib/docsify.js
Original file line number Diff line number Diff line change
@@ -99,6 +99,98 @@ function getRoute () {
return route
}

/**
* Active sidebar when scroll
* @link https://buble.surge.sh/
*/
function scrollActiveSidebar () {
if (/mobile/i.test(navigator.userAgent)) { return }

var anchors = document.querySelectorAll('.anchor');
var nav = {};
var lis = document.querySelectorAll('.sidebar li');
var active = null;

for (var i = 0, len = lis.length; i < len; i += 1) {
var li = lis[i];
var href = li.querySelector('a').getAttribute('href');

if (href !== '/') { href = href.match(/#([^#]+)$/g)[0].slice(1); }

nav[href] = li;
}

function highlight () {
for (var i = 0, len = anchors.length; i < len; i += 1) {
var node = anchors[i].parentNode;
var bcr = node.getBoundingClientRect();

if (bcr.top < 10 && bcr.bottom > 10) {
var li = nav[node.id];

if (!li || li === active) { return }
if (active) { active.setAttribute('class', ''); }

li.setAttribute('class', 'active');
active = li;

return
}
}
}

var dom = document.querySelector('main .content');
dom.removeEventListener('scroll', highlight);
dom.addEventListener('scroll', highlight);
highlight();
}

function scrollIntoView () {
var id = window.location.hash.match(/#[^#\/]+$/g);
if (!id || !id.length) { return }
var section = document.querySelector(id[0]);

if (section) { section.scrollIntoView(); }
}

/**
* Acitve link
*/
function activeLink (dom, activeParent) {
var host = window.location.href;

dom = typeof dom === 'object' ? dom : document.querySelector(dom);
if (!dom) { return

; }[].slice.call(dom.querySelectorAll('a')).forEach(function (node) {
if (node.href === host) {
activeParent
? node.parentNode.setAttribute('class', 'active')
: node.setAttribute('class', 'active');
} else {
activeParent
? node.parentNode.removeAttribute('class')
: node.removeAttribute('class');
}
});
}

/**
* sidebar toggle
*/
function bindToggle (dom) {
dom = typeof dom === 'object' ? dom : document.querySelector(dom);
if (!dom) { return }
var body = document.body;

dom.addEventListener('click', function () { return body.classList.toggle('close'); });

if (!/mobile/i.test(navigator.userAgent)) { return }
document.querySelector('aside').addEventListener('click', function (event) {
body.classList.toggle('close');
});
}

var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};


@@ -2240,100 +2332,8 @@ function tree (toc, tpl) {
return tpl
}

/**
* Active sidebar when scroll
* @link https://buble.surge.sh/
*/
function scrollActiveSidebar () {
if (/mobile/i.test(navigator.userAgent)) { return }

var anchors = document.querySelectorAll('.anchor');
var nav = {};
var lis = document.querySelectorAll('.sidebar li');
var active = null;

for (var i = 0, len = lis.length; i < len; i += 1) {
var li = lis[i];
var href = li.querySelector('a').getAttribute('href');

if (href !== '/') { href = href.match(/#([^#]+)$/g)[0].slice(1); }

nav[href] = li;
}

function highlight () {
for (var i = 0, len = anchors.length; i < len; i += 1) {
var node = anchors[i].parentNode;
var bcr = node.getBoundingClientRect();

if (bcr.top < 10 && bcr.bottom > 10) {
var li = nav[node.id];

if (!li || li === active) { return }
if (active) { active.setAttribute('class', ''); }

li.setAttribute('class', 'active');
active = li;

return
}
}
}

document.querySelector('main .content').addEventListener('scroll', highlight);
highlight();

function scrollIntoView () {
var id = window.location.hash.match(/#[^#\/]+$/g);
if (!id || !id.length) { return }
var section = document.querySelector(id[0]);

if (section) { section.scrollIntoView(); }
}

window.addEventListener('hashchange', scrollIntoView);
scrollIntoView();
}

/**
* Acitve link
*/
function activeLink (dom, activeParent) {
var host = window.location.href;

dom = typeof dom === 'object' ? dom : document.querySelector(dom);
if (!dom) { return

; }[].slice.call(dom.querySelectorAll('a')).forEach(function (node) {
if (node.href === host) {
activeParent
? node.parentNode.setAttribute('class', 'active')
: node.setAttribute('class', 'active');
} else {
activeParent
? node.parentNode.removeAttribute('class')
: node.removeAttribute('class');
}
});
}

/**
* sidebar toggle
*/
function bindToggle (dom) {
dom = typeof dom === 'object' ? dom : document.querySelector(dom);
if (!dom) { return }
var body = document.body;

dom.addEventListener('click', function () { return body.classList.toggle('close'); });

if (!/mobile/i.test(navigator.userAgent)) { return }
document.querySelector('aside').addEventListener('click', function (event) {
body.classList.toggle('close');
});
}

var OPTIONS$1 = {};
var CACHE = {};

var renderTo = function (dom, content) {
dom = typeof dom === 'object' ? dom : document.querySelector(dom);
@@ -2405,6 +2405,8 @@ function renderArticle (content) {
* navbar
*/
function renderNavbar (content) {
if (CACHE['navbar'] === content) { return }
CACHE['navbar'] = content;
renderNavbar.rendered = true;

if (content) { renderTo('nav', marked(content)); }
@@ -2415,6 +2417,8 @@ function renderNavbar (content) {
* sidebar
*/
function renderSidebar (content) {
if (CACHE['sidebar'] === content) { return }
CACHE['sidebar'] = content;
renderSidebar.rendered = true;

var isToc = false;
@@ -2429,7 +2433,7 @@ function renderSidebar (content) {
}

renderTo('aside.sidebar', content);
isToc ? scrollActiveSidebar() : activeLink('aside.sidebar', true);
if (isToc) { scrollActiveSidebar(); }
toc = [];
}

@@ -2465,25 +2469,37 @@ config(OPTIONS);

var cacheRoute = null;

var mainRender = function () {
var mainRender = function (cb) {
var route = getRoute();
if (cacheRoute === route) { return }

if (cacheRoute === route) { return cb() }
var wait;
var basePath = cacheRoute = route;

if (!/\//.test(basePath)) {
basePath = '';
} else if (basePath && !/\/$/.test(basePath)) {
basePath = basePath.match(/(\S+\/)[^\/]+$/)[1];
basePath = basePath.match(/(\S*\/)[^\/]+$/)[1];
}

// Render markdown file
load((!route || /\/$/.test(route)) ? (route + "README.md") : (route + ".md"))
.then(renderArticle, function (_) { return renderArticle(null); });
.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) {
load(basePath + OPTIONS.loadSidebar).then(renderSidebar);
load(basePath + OPTIONS.loadSidebar).then(function (result) {
renderSidebar(result);
if (wait === false) { cb(); }
wait = false;
});
}

// Render navbar
@@ -2495,13 +2511,19 @@ var mainRender = function () {
var Docsify = function () {
var dom = document.querySelector(OPTIONS.el) || document.body;
var replace = dom !== document.body;
var main = function () {
mainRender(function (_) {
activeLink('aside.sidebar', true);
scrollIntoView();
});
};

// Render app
renderApp(dom, replace);
mainRender();
main();
if (OPTIONS.router) {
if (!/^#\//.test(window.location.hash)) { window.location.hash = '#/'; }
window.addEventListener('hashchange', mainRender);
window.addEventListener('hashchange', main);
}
};

Loading