From e2b7b976cf5f36933bd946461d8f100f22962dba Mon Sep 17 00:00:00 2001 From: "qingwei.li" Date: Sat, 18 Feb 2017 19:35:14 +0800 Subject: [PATCH] refactor(core): fix route path --- src/core/event/index.js | 8 ++ src/core/event/scroll.js | 4 +- src/core/fetch/ajax.js | 4 +- src/core/fetch/index.js | 24 ++-- src/core/index.js | 12 +- src/core/init/index.js | 4 +- src/core/render/compiler.js | 34 +++-- src/core/render/index.js | 39 +++++- src/core/render/progressbar.js | 6 +- src/core/render/tpl.js | 2 +- src/core/route/hash.js | 13 +- src/core/route/index.js | 16 +-- src/core/route/util.js | 12 +- src/index.js | 145 -------------------- src/render.js | 237 --------------------------------- src/themes/vue.css | 9 ++ 16 files changed, 125 insertions(+), 444 deletions(-) delete mode 100644 src/index.js delete mode 100644 src/render.js diff --git a/src/core/event/index.js b/src/core/event/index.js index 1181c9ba5..6c469a592 100644 --- a/src/core/event/index.js +++ b/src/core/event/index.js @@ -1,6 +1,14 @@ import { isMobile } from '../util/env' import { body, on } from '../util/dom' import * as sidebar from './sidebar' +import { scrollIntoView } from './scroll' + +export function eventMixin (proto) { + proto.$resetEvents = function () { + scrollIntoView(this.route.query.id) + sidebar.getAndActive('nav') + } +} export function initEvent (vm) { // Bind toggle button diff --git a/src/core/event/scroll.js b/src/core/event/scroll.js index 89b9b82c8..5cfdb1171 100644 --- a/src/core/event/scroll.js +++ b/src/core/event/scroll.js @@ -1,5 +1,6 @@ import { isMobile } from '../util/env' import * as dom from '../util/dom' +import { parse } from '../route/hash' export function scrollActiveSidebar () { if (isMobile) return @@ -21,8 +22,7 @@ export function scrollActiveSidebar () { let href = a.getAttribute('href') if (href !== '/') { - const match = href.match('#([^#]+)$') - if (match && match.length) href = match[0].slice(1) + href = parse(href).query.id } nav[decodeURIComponent(href)] = li diff --git a/src/core/fetch/ajax.js b/src/core/fetch/ajax.js index 09d43905a..503889742 100644 --- a/src/core/fetch/ajax.js +++ b/src/core/fetch/ajax.js @@ -28,7 +28,9 @@ export function get (url, hasBar = false) { return { then: function (success, error = noop) { if (hasBar) { - const id = setInterval(_ => progressbar({}), 500) + const id = setInterval(_ => progressbar({ + step: Math.floor(Math.random() * 5 + 1) + }), 500) on('progress', progressbar) on('loadend', evt => { diff --git a/src/core/fetch/index.js b/src/core/fetch/index.js index 25b74387f..e3f4c0d77 100644 --- a/src/core/fetch/index.js +++ b/src/core/fetch/index.js @@ -2,13 +2,10 @@ import { get } from './ajax' import { callHook } from '../init/lifecycle' import { getRoot } from '../route/util' import { noop } from '../util/core' -import { scrollIntoView } from '../event/scroll' -import { getAndActive } from '../event/sidebar' -export function fetchMixin (Docsify) { +export function fetchMixin (proto) { let last - - Docsify.prototype._fetch = function (cb = noop) { + proto._fetch = function (cb = noop) { const { path } = this.route const { loadNavbar, loadSidebar } = this.config const root = getRoot(path) @@ -42,7 +39,7 @@ export function fetchMixin (Docsify) { ) } - Docsify.prototype._fetchCover = function () { + proto._fetchCover = function () { const { coverpage } = this.config const root = getRoot(this.route.path) @@ -54,13 +51,16 @@ export function fetchMixin (Docsify) { get(this.$getFile(root + coverpage)) .then(text => this._renderCover(text)) } + + proto.$fetch = function () { + this._fetchCover() + this._fetch(result => { + this.$resetEvents() + callHook(this, 'doneEach') + }) + } } export function initFetch (vm) { - vm._fetchCover(vm) - vm._fetch(result => { - scrollIntoView(vm.route.query.id) - getAndActive('nav') - callHook(vm, 'doneEach') - }) + vm.$fetch() } diff --git a/src/core/index.js b/src/core/index.js index f5b8b4b69..3a8b6be99 100644 --- a/src/core/index.js +++ b/src/core/index.js @@ -2,16 +2,20 @@ import { initMixin } from './init' import { routeMixin } from './route' import { renderMixin } from './render' import { fetchMixin } from './fetch' +import { eventMixin } from './event' import initGlobalAPI from './global-api' function Docsify () { this._init() } -initMixin(Docsify) -routeMixin(Docsify) -renderMixin(Docsify) -fetchMixin(Docsify) +const proto = Docsify.prototype + +initMixin(proto) +routeMixin(proto) +renderMixin(proto) +fetchMixin(proto) +eventMixin(proto) /** * Global API diff --git a/src/core/init/index.js b/src/core/init/index.js index 826e6efb3..064704fa7 100644 --- a/src/core/init/index.js +++ b/src/core/init/index.js @@ -6,8 +6,8 @@ import { initEvent } from '../event' import { initFetch } from '../fetch' import { isFn } from '../util/core' -export function initMixin (Docsify) { - Docsify.prototype._init = function () { +export function initMixin (proto) { + proto._init = function () { const vm = this vm.config = config || {} diff --git a/src/core/render/compiler.js b/src/core/render/compiler.js index 06c046c49..c4abe6a12 100644 --- a/src/core/render/compiler.js +++ b/src/core/render/compiler.js @@ -4,12 +4,15 @@ import { helper as helperTpl, tree as treeTpl } from './tpl' import { genTree } from './gen-tree' import { slugify, clearSlugCache } from './slugify' import { emojify } from './emojify' -import { toURL } from '../route/hash' -import { isFn, merge, cached, noop } from '../util/core' +import { toURL, parse } from '../route/hash' +import { getBasePath, getPath } from '../route/util' +import { isFn, merge, cached } from '../util/core' let markdownCompiler = marked let contentBase = '' +let currentPath = '' let renderer = new marked.Renderer() +const TOC = {} let toc = [] /** @@ -30,7 +33,8 @@ export const markdown = cached(text => { markdown.renderer = renderer markdown.init = function (config = {}, base = window.location.pathname) { - contentBase = base + contentBase = getBasePath(base) + currentPath = parse().path if (isFn(config)) { markdownCompiler = config(marked, renderer) @@ -40,13 +44,17 @@ markdown.init = function (config = {}, base = window.location.pathname) { } } +markdown.update = function () { + currentPath = parse().path +} + /** * render anchor tag * @link https://github.com/chjj/marked#overriding-renderer-methods */ renderer.heading = function (text, level) { const slug = slugify(text) - const url = toURL(contentBase, { id: slug }) + const url = toURL(currentPath, { id: slug }) toc.push({ level, slug: url, title: text }) @@ -60,8 +68,7 @@ renderer.code = function (code, lang = '') { } renderer.link = function (href, title, text) { if (!/:|(\/{2})/.test(href)) { - // TODO - href = `#/${href}`.replace(/\/+/g, '/') + href = toURL(href) } return `${text}` } @@ -74,12 +81,10 @@ renderer.paragraph = function (text) { return `

${text}

` } renderer.image = function (href, title, text) { - // TODO - // get base path - // const url = /:|(\/{2})/.test(href) ? href : ($docsify.basePath + href).replace(/\/+/g, '/') - // const titleHTML = title ? ` title="${title}"` : '' + const url = getPath(contentBase, href) + const titleHTML = title ? ` title="${title}"` : '' - // return `${text}` + return `${text}` } /** @@ -105,8 +110,11 @@ export function sidebar (text, level) { export function subSidebar (el, level) { if (el) { toc[0] && toc[0].level === 1 && toc.shift() - const tree = genTree(toc, level) - el.parentNode.innerHTML += treeTpl(tree, '