Skip to content

Commit

Permalink
refactor(router): clean global api
Browse files Browse the repository at this point in the history
  • Loading branch information
QingWei-Li committed May 29, 2017
1 parent 2c7041c commit 8741c74
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 124 deletions.
4 changes: 2 additions & 2 deletions src/core/event/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import { scrollIntoView } from './scroll'
export function eventMixin (proto) {
proto.$resetEvents = function () {
scrollIntoView(this.route.query.id)
sidebar.getAndActive('nav')
sidebar.getAndActive(this.router, 'nav')
}
}

export function initEvent (vm) {
// Bind toggle button
sidebar.btn('button.sidebar-toggle')
sidebar.btn('button.sidebar-toggle', vm.router)
// Bind sticky effect
if (vm.config.coverpage) {
!isMobile && on('scroll', sidebar.sticky)
Expand Down
5 changes: 2 additions & 3 deletions src/core/event/scroll.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { isMobile } from '../util/env'
import * as dom from '../util/dom'
import { parse } from '../router/hash'

const nav = {}
let hoverOver = false
Expand Down Expand Up @@ -53,7 +52,7 @@ function highlight () {
}
}

export function scrollActiveSidebar () {
export function scrollActiveSidebar (router) {
if (isMobile) return

const sidebar = dom.getNode('.sidebar')
Expand All @@ -66,7 +65,7 @@ export function scrollActiveSidebar () {
let href = a.getAttribute('href')

if (href !== '/') {
href = parse(href).query.id
href = router.parse(href).query.id
}

nav[decodeURIComponent(href)] = li
Expand Down
10 changes: 5 additions & 5 deletions src/core/event/sidebar.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { isMobile } from '../util/env'
import * as dom from '../util/dom'
import { getHash } from '../router/hash'

const title = dom.$.title
/**
* Toggle button
*/
export function btn (el) {
export function btn (el, router) {
const toggle = _ => dom.body.classList.toggle('close')

el = dom.getNode(el)
Expand All @@ -21,7 +20,7 @@ export function btn (el) {
dom.body.classList.contains('close') && toggle()
)
dom.on(sidebar, 'click', _ =>
setTimeout((_ => getAndActive(sidebar, true, true), 0))
setTimeout((_ => getAndActive(router, sidebar, true, true), 0))
)
}

Expand All @@ -39,16 +38,17 @@ export function sticky () {

/**
* Get and active link
* @param {object} router
* @param {string|element} el
* @param {Boolean} isParent acitve parent
* @param {Boolean} autoTitle auto set title
* @return {element}
*/
export function getAndActive (el, isParent, autoTitle) {
export function getAndActive (router, el, isParent, autoTitle) {
el = dom.getNode(el)

const links = dom.findAll(el, 'a')
const hash = '#' + getHash()
const hash = router.toURL(router.getCurrentPath())
let target

links
Expand Down
6 changes: 3 additions & 3 deletions src/core/global-api.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import * as util from './util'
import * as dom from './util/dom'
import * as render from './render/compiler'
import * as route from './router/hash'
import { Compiler } from './render/compiler'
import { slugify } from './render/slugify'
import { get } from './fetch/ajax'
import marked from 'marked'
import prism from 'prismjs'

export default function () {
window.Docsify = { util, dom, render, route, get, slugify }
window.Docsify = { util, dom, get, slugify }
window.DocsifyCompiler = Compiler
window.marked = marked
window.Prism = prism
}
16 changes: 8 additions & 8 deletions src/core/render/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,24 @@ export class Compiler {
this.contentBase = getBasePath(config.base)

const renderer = this._initRenderer()
let runner
let compile
const mdConf = config.markdown || {}

if (isFn(mdConf)) {
runner = mdConf(marked, renderer)
compile = mdConf(marked, renderer)
} else {
marked.setOptions(merge(mdConf, {
renderer: merge(renderer, mdConf.renderer)
}))
runner = marked
compile = marked
}

this.runner = cached(text => {
this.compile = cached(text => {
let html = ''

if (!text) return text

html = runner(text)
html = compile(text)
html = emojify(html)
slugify.clear()

Expand Down Expand Up @@ -119,7 +119,7 @@ export class Compiler {
let html = ''

if (text) {
html = this.runner(text)
html = this.compile(text)
html = html.match(/<ul[^>]*>([\s\S]+)<\/ul>/g)[0]
} else {
const tree = this.cacheTree[currentPath] || genTree(this.toc, level)
Expand Down Expand Up @@ -151,15 +151,15 @@ export class Compiler {
}

article (text) {
return this.runner(text)
return this.compile(text)
}

/**
* Compile cover page
*/
cover (text) {
const cacheToc = this.toc.slice()
const html = this.runner(text)
const html = this.compile(text)

this.toc = cacheToc.slice()

Expand Down
22 changes: 11 additions & 11 deletions src/core/render/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import * as dom from '../util/dom'
import { getAndActive, sticky } from '../event/sidebar'
import { scrollActiveSidebar, scroll2Top } from '../event/scroll'
import cssVars from '../util/polyfill/css-vars'
import * as tpl from './tpl'
import { Compiler } from './compiler'
import cssVars from '../util/polyfill/css-vars'
import tinydate from 'tinydate'
import { callHook } from '../init/lifecycle'
import { Compiler } from './compiler'
import { getAndActive, sticky } from '../event/sidebar'
import { getBasePath, getPath, isAbsolutePath } from '../router/util'
import { isPrimitive } from '../util/core'
import { isMobile } from '../util/env'
import tinydate from 'tinydate'
import { isPrimitive } from '../util/core'
import { scrollActiveSidebar, scroll2Top } from '../event/scroll'

function executeScript () {
const script = dom.findAll('.markdown-section>script')
Expand Down Expand Up @@ -86,13 +86,13 @@ export function renderMixin (proto) {
const { maxLevel, subMaxLevel, autoHeader, loadSidebar } = this.config

this._renderTo('.sidebar-nav', this.compiler.sidebar(text, maxLevel))
const activeEl = getAndActive('.sidebar-nav', true, true)
const activeEl = getAndActive(this.router, '.sidebar-nav', true, true)
if (loadSidebar && activeEl) {
activeEl.parentNode.innerHTML += this.compiler.subSidebar(subMaxLevel)
}
// bind event
this.activeLink = activeEl
scrollActiveSidebar()
scrollActiveSidebar(this.router)

if (autoHeader && activeEl) {
const main = dom.getNode('#main')
Expand All @@ -106,13 +106,13 @@ export function renderMixin (proto) {
}

proto._renderNav = function (text) {
text && this._renderTo('nav', this.compiler.runner(text))
getAndActive('nav')
text && this._renderTo('nav', this.compiler.compile(text))
getAndActive(this.router, 'nav')
}

proto._renderMain = function (text, opt = {}) {
callHook(this, 'beforeEach', text, result => {
let html = this.isHTML ? result : this.compiler.runner(result)
let html = this.isHTML ? result : this.compiler.compile(result)
if (opt.updatedAt) {
html = formatUpdated(html, opt.updatedAt, this.config.formatUpdated)
}
Expand Down
80 changes: 0 additions & 80 deletions src/core/router/hash.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/core/router/history/hash.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { History } from './base'
import { merge, cached, noop } from '../../util/core'
import { parseQuery, stringifyQuery, cleanPath } from '../util'
import { on } from '../../util/dom'
import { parseQuery, stringifyQuery, cleanPath } from '../util'

function replaceHash (path) {
const i = location.href.indexOf('#')
Expand Down
1 change: 1 addition & 0 deletions src/core/util/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './core'
export * from './env'
export * from '../router/util'
4 changes: 2 additions & 2 deletions src/plugins/search/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ function updateNoData (text, path) {
}
}

export function init (opts) {
export function init (opts, vm) {
dom = Docsify.dom
const keywords = Docsify.route.parse().query.s
const keywords = vm.router.parse().query.s

style()
tpl(opts, keywords)
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/search/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const install = function (hook, vm) {
const isAuto = CONFIG.paths === 'auto'

hook.mounted(_ => {
initComponet(CONFIG)
initComponet(CONFIG, vm)
!isAuto && initSearch(CONFIG, vm)
})
hook.doneEach(_ => {
Expand All @@ -33,4 +33,4 @@ const install = function (hook, vm) {
})
}

window.$docsify.plugins = [].concat(install, window.$docsify.plugins)
$docsify.plugins = [].concat(install, $docsify.plugins)
14 changes: 7 additions & 7 deletions src/plugins/search/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ function escapeHtml (string) {
return String(string).replace(/[&<>"'\/]/g, s => entityMap[s])
}

function getAllPaths () {
function getAllPaths (router) {
const paths = []

helper.dom.findAll('a:not([data-nosearch])')
.map(node => {
const href = node.href
const originHref = node.getAttribute('href')
const path = helper.route.parse(href).path
const path = router.parse(href).path

if (path &&
paths.indexOf(path) === -1 &&
!helper.route.isAbsolutePath(originHref)) {
!Docsify.util.isAbsolutePath(originHref)) {
paths.push(path)
}
})
Expand All @@ -38,10 +38,10 @@ function saveData (maxAge) {
localStorage.setItem('docsify.search.index', JSON.stringify(INDEXS))
}

export function genIndex (path, content = '') {
export function genIndex (path, content = '', router) {
const tokens = window.marked.lexer(content)
const slugify = window.Docsify.slugify
const toURL = Docsify.route.toURL
const toURL = router.toURL
const index = {}
let slug

Expand Down Expand Up @@ -145,7 +145,7 @@ export function init (config, vm) {
return
}

const paths = isAuto ? getAllPaths() : config.paths
const paths = isAuto ? getAllPaths(vm.router) : config.paths
const len = paths.length
let count = 0

Expand All @@ -155,7 +155,7 @@ export function init (config, vm) {
helper
.get(vm.router.getFile(path))
.then(result => {
INDEXS[path] = genIndex(path, result)
INDEXS[path] = genIndex(path, result, vm.router)
len === ++count && saveData(config.maxAge)
})
})
Expand Down

0 comments on commit 8741c74

Please sign in to comment.