Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Mar 7, 2022. It is now read-only.

Commit 8741c74

Browse filesBrowse files
committed
refactor(router): clean global api
1 parent 2c7041c commit 8741c74
Copy full SHA for 8741c74

12 files changed

+44-124Lines changed: 44 additions & 124 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/core/event/index.js‎

Copy file name to clipboardExpand all lines: src/core/event/index.js
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import { scrollIntoView } from './scroll'
66
export function eventMixin (proto) {
77
proto.$resetEvents = function () {
88
scrollIntoView(this.route.query.id)
9-
sidebar.getAndActive('nav')
9+
sidebar.getAndActive(this.router, 'nav')
1010
}
1111
}
1212

1313
export function initEvent (vm) {
1414
// Bind toggle button
15-
sidebar.btn('button.sidebar-toggle')
15+
sidebar.btn('button.sidebar-toggle', vm.router)
1616
// Bind sticky effect
1717
if (vm.config.coverpage) {
1818
!isMobile && on('scroll', sidebar.sticky)
Collapse file

‎src/core/event/scroll.js‎

Copy file name to clipboardExpand all lines: src/core/event/scroll.js
+2-3Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { isMobile } from '../util/env'
22
import * as dom from '../util/dom'
3-
import { parse } from '../router/hash'
43

54
const nav = {}
65
let hoverOver = false
@@ -53,7 +52,7 @@ function highlight () {
5352
}
5453
}
5554

56-
export function scrollActiveSidebar () {
55+
export function scrollActiveSidebar (router) {
5756
if (isMobile) return
5857

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

6867
if (href !== '/') {
69-
href = parse(href).query.id
68+
href = router.parse(href).query.id
7069
}
7170

7271
nav[decodeURIComponent(href)] = li
Collapse file

‎src/core/event/sidebar.js‎

Copy file name to clipboardExpand all lines: src/core/event/sidebar.js
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { isMobile } from '../util/env'
22
import * as dom from '../util/dom'
3-
import { getHash } from '../router/hash'
43

54
const title = dom.$.title
65
/**
76
* Toggle button
87
*/
9-
export function btn (el) {
8+
export function btn (el, router) {
109
const toggle = _ => dom.body.classList.toggle('close')
1110

1211
el = dom.getNode(el)
@@ -21,7 +20,7 @@ export function btn (el) {
2120
dom.body.classList.contains('close') && toggle()
2221
)
2322
dom.on(sidebar, 'click', _ =>
24-
setTimeout((_ => getAndActive(sidebar, true, true), 0))
23+
setTimeout((_ => getAndActive(router, sidebar, true, true), 0))
2524
)
2625
}
2726

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

4039
/**
4140
* Get and active link
41+
* @param {object} router
4242
* @param {string|element} el
4343
* @param {Boolean} isParent acitve parent
4444
* @param {Boolean} autoTitle auto set title
4545
* @return {element}
4646
*/
47-
export function getAndActive (el, isParent, autoTitle) {
47+
export function getAndActive (router, el, isParent, autoTitle) {
4848
el = dom.getNode(el)
4949

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

5454
links
Collapse file

‎src/core/global-api.js‎

Copy file name to clipboard
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import * as util from './util'
22
import * as dom from './util/dom'
3-
import * as render from './render/compiler'
4-
import * as route from './router/hash'
3+
import { Compiler } from './render/compiler'
54
import { slugify } from './render/slugify'
65
import { get } from './fetch/ajax'
76
import marked from 'marked'
87
import prism from 'prismjs'
98

109
export default function () {
11-
window.Docsify = { util, dom, render, route, get, slugify }
10+
window.Docsify = { util, dom, get, slugify }
11+
window.DocsifyCompiler = Compiler
1212
window.marked = marked
1313
window.Prism = prism
1414
}
Collapse file

‎src/core/render/compiler.js‎

Copy file name to clipboardExpand all lines: src/core/render/compiler.js
+8-8Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,24 @@ export class Compiler {
1717
this.contentBase = getBasePath(config.base)
1818

1919
const renderer = this._initRenderer()
20-
let runner
20+
let compile
2121
const mdConf = config.markdown || {}
2222

2323
if (isFn(mdConf)) {
24-
runner = mdConf(marked, renderer)
24+
compile = mdConf(marked, renderer)
2525
} else {
2626
marked.setOptions(merge(mdConf, {
2727
renderer: merge(renderer, mdConf.renderer)
2828
}))
29-
runner = marked
29+
compile = marked
3030
}
3131

32-
this.runner = cached(text => {
32+
this.compile = cached(text => {
3333
let html = ''
3434

3535
if (!text) return text
3636

37-
html = runner(text)
37+
html = compile(text)
3838
html = emojify(html)
3939
slugify.clear()
4040

@@ -119,7 +119,7 @@ export class Compiler {
119119
let html = ''
120120

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

153153
article (text) {
154-
return this.runner(text)
154+
return this.compile(text)
155155
}
156156

157157
/**
158158
* Compile cover page
159159
*/
160160
cover (text) {
161161
const cacheToc = this.toc.slice()
162-
const html = this.runner(text)
162+
const html = this.compile(text)
163163

164164
this.toc = cacheToc.slice()
165165

Collapse file

‎src/core/render/index.js‎

Copy file name to clipboardExpand all lines: src/core/render/index.js
+11-11Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import * as dom from '../util/dom'
2-
import { getAndActive, sticky } from '../event/sidebar'
3-
import { scrollActiveSidebar, scroll2Top } from '../event/scroll'
4-
import cssVars from '../util/polyfill/css-vars'
52
import * as tpl from './tpl'
6-
import { Compiler } from './compiler'
3+
import cssVars from '../util/polyfill/css-vars'
4+
import tinydate from 'tinydate'
75
import { callHook } from '../init/lifecycle'
6+
import { Compiler } from './compiler'
7+
import { getAndActive, sticky } from '../event/sidebar'
88
import { getBasePath, getPath, isAbsolutePath } from '../router/util'
9-
import { isPrimitive } from '../util/core'
109
import { isMobile } from '../util/env'
11-
import tinydate from 'tinydate'
10+
import { isPrimitive } from '../util/core'
11+
import { scrollActiveSidebar, scroll2Top } from '../event/scroll'
1212

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

8888
this._renderTo('.sidebar-nav', this.compiler.sidebar(text, maxLevel))
89-
const activeEl = getAndActive('.sidebar-nav', true, true)
89+
const activeEl = getAndActive(this.router, '.sidebar-nav', true, true)
9090
if (loadSidebar && activeEl) {
9191
activeEl.parentNode.innerHTML += this.compiler.subSidebar(subMaxLevel)
9292
}
9393
// bind event
9494
this.activeLink = activeEl
95-
scrollActiveSidebar()
95+
scrollActiveSidebar(this.router)
9696

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

108108
proto._renderNav = function (text) {
109-
text && this._renderTo('nav', this.compiler.runner(text))
110-
getAndActive('nav')
109+
text && this._renderTo('nav', this.compiler.compile(text))
110+
getAndActive(this.router, 'nav')
111111
}
112112

113113
proto._renderMain = function (text, opt = {}) {
114114
callHook(this, 'beforeEach', text, result => {
115-
let html = this.isHTML ? result : this.compiler.runner(result)
115+
let html = this.isHTML ? result : this.compiler.compile(result)
116116
if (opt.updatedAt) {
117117
html = formatUpdated(html, opt.updatedAt, this.config.formatUpdated)
118118
}
Collapse file

‎src/core/router/hash.js‎

Copy file name to clipboardExpand all lines: src/core/router/hash.js
-80Lines changed: 0 additions & 80 deletions
This file was deleted.
Collapse file

‎src/core/router/history/hash.js‎

Copy file name to clipboardExpand all lines: src/core/router/history/hash.js
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { History } from './base'
22
import { merge, cached, noop } from '../../util/core'
3-
import { parseQuery, stringifyQuery, cleanPath } from '../util'
43
import { on } from '../../util/dom'
4+
import { parseQuery, stringifyQuery, cleanPath } from '../util'
55

66
function replaceHash (path) {
77
const i = location.href.indexOf('#')
Collapse file

‎src/core/util/index.js‎

Copy file name to clipboard
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './core'
22
export * from './env'
3+
export * from '../router/util'
Collapse file

‎src/plugins/search/component.js‎

Copy file name to clipboardExpand all lines: src/plugins/search/component.js
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ function updateNoData (text, path) {
138138
}
139139
}
140140

141-
export function init (opts) {
141+
export function init (opts, vm) {
142142
dom = Docsify.dom
143-
const keywords = Docsify.route.parse().query.s
143+
const keywords = vm.router.parse().query.s
144144

145145
style()
146146
tpl(opts, keywords)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.