1.0 features (#21)

* feat: hash routing, close #2

* Fix router bug

* Remove console

* Add hash router docs

* Improved scrolling on mobile

* Add change log

* Use hash router
This commit is contained in:
cinwell.li 2016-12-08 21:11:18 +08:00 committed by GitHub
commit 864935bfc1
15 changed files with 174 additions and 60 deletions

View file

@ -1,4 +1,4 @@
import { load, camel2kebab, isNil } from './util'
import { load, camel2kebab, isNil, getRoute } from './util'
import * as render from './render'
const OPTIONS = {
@ -8,7 +8,8 @@ const OPTIONS = {
sidebar: '',
sidebarToggle: false,
loadSidebar: null,
loadNavbar: null
loadNavbar: null,
router: false
}
const script = document.currentScript || [].slice.call(document.getElementsByTagName('script')).pop()
@ -23,31 +24,48 @@ if (script) {
if (OPTIONS.sidebar) OPTIONS.sidebar = window[OPTIONS.sidebar]
}
const Docsify = function () {
const dom = document.querySelector(OPTIONS.el) || document.body
const replace = dom !== document.body
let loc = document.location.pathname
// load options
render.config(OPTIONS)
if (/\/$/.test(loc)) loc += 'README'
let cacheRoute = null
// Render app
render.renderApp(dom, replace, OPTIONS)
const mainRender = function () {
const route = getRoute()
if (cacheRoute === route) return
let basePath = cacheRoute = route
if (!/\//.test(basePath)) {
basePath = ''
} else if (basePath && !/\/$/.test(basePath)) {
basePath = basePath.match(/(\S+\/)[^\/]+$/)[1]
}
// Render markdown file
load(`${loc}.md`)
.then(content => render.renderArticle(content, OPTIONS),
_ => render.renderArticle(null, OPTIONS))
load((!route || /\/$/.test(route)) ? `${route}README.md` : `${route}.md`)
.then(render.renderArticle, _ => render.renderArticle(null))
// Render sidebar
if (OPTIONS.loadSidebar) {
load(OPTIONS.loadSidebar)
.then(content => render.renderSidebar(content, OPTIONS))
load(basePath + OPTIONS.loadSidebar).then(render.renderSidebar)
}
// Render navbar
if (OPTIONS.loadNavbar) {
load(OPTIONS.loadNavbar)
.then(content => render.renderNavbar(content, OPTIONS))
load(basePath + OPTIONS.loadNavbar).then(render.renderNavbar)
}
}
const Docsify = function () {
const dom = document.querySelector(OPTIONS.el) || document.body
const replace = dom !== document.body
// Render app
render.renderApp(dom, replace)
mainRender()
if (OPTIONS.router) {
if (!/^#\//.test(window.location.hash)) window.location.hash = '#/'
window.addEventListener('hashchange', mainRender)
}
}