Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/3.2 #108

Merged
merged 7 commits into from
Feb 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@

3.2.0 / 2017-02-28
==================

* fix(render): Toc rendering error, fixed #106
* feat(search): Localization for no data tip, close #103
* fix(fetch): load sidebar and navbar for parent path, fixed #100
* feat(render) nameLink for each route, fixed #99

3.1.2 / 2017-02-27
==================

Expand Down
2 changes: 1 addition & 1 deletion docs/_coverpage.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
![logo](_media/icon.svg)

# docsify <small>3.1</small>
# docsify <small>3.2</small>

> A magical documentation site generator.

Expand Down
2 changes: 1 addition & 1 deletion docs/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
- [Helpers](/helpers)
- [Vue compatibility](/vue)
- [CDN](/cdn)
- [Offline Mode(PWA)<sup style="color: #F44336;">new</sup>](/pwa)
- [Offline Mode(PWA)](/pwa)

- [Changelog](/changelog)
8 changes: 7 additions & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,13 @@ The name of the link.

```js
window.$docsify = {
nameLink: '/'
nameLink: '/',

// For each route
nameLink: {
'/zh-cn/': '/zh-cn/',
'/': '/'
}
}
```

Expand Down
8 changes: 8 additions & 0 deletions docs/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ By default, the hyperlink on the current page is recognized and the content is s
placeholder: {
'/zh-cn/': '搜索',
'/': 'Type to search'
},

noData: 'No Results!',

// Localization
noData: {
'/zh-cn/': '找不到结果',
'/': 'No Results'
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion docs/zh-cn/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
- [文档助手](zh-cn/helpers)
- [兼容 Vue](zh-cn/vue)
- [CDN](zh-cn/cdn)
- [离线模式(PWA)<sup style="color: #F44336;">new</sup>](zh-cn/pwa)
- [离线模式(PWA)](zh-cn/pwa)

- [Changelog](zh-cn/changelog)
8 changes: 7 additions & 1 deletion docs/zh-cn/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,13 @@ window.$docsify = {

```js
window.$docsify = {
nameLink: '/'
nameLink: '/',

// 按照路由切换
nameLink: {
'/zh-cn/': '/zh-cn/',
'/': '/'
}
}
```

Expand Down
8 changes: 8 additions & 0 deletions docs/zh-cn/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@
placeholder: {
'/zh-cn/': '搜索',
'/': 'Type to search'
},

noData: 'No Results!',

// 支持本地化
noData: {
'/zh-cn/': '找不到结果',
'/': 'No Results'
}
}
}
Expand Down
26 changes: 14 additions & 12 deletions src/core/fetch/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import { get } from './ajax'
import { callHook } from '../init/lifecycle'
import { getRoot } from '../route/util'
import { getParentPath } from '../route/util'
import { noop } from '../util/core'

function loadNested (path, file, next, vm, first) {
path = first ? path : path.replace(/\/$/, '')
path = getParentPath(path)

if (!path) return

get(vm.$getFile(path + file))
.then(next, _ => loadNested(path, file, next, vm))
}

export function fetchMixin (proto) {
let last
proto._fetch = function (cb = noop) {
const { path } = this.route
const { loadNavbar, loadSidebar } = this.config
const root = getRoot(path)

// Abort last request
last && last.abort && last.abort()
Expand All @@ -26,25 +35,18 @@ export function fetchMixin (proto) {
const fn = result => { this._renderSidebar(result); cb() }

// Load sidebar
get(this.$getFile(root + loadSidebar))
// fallback root navbar when fail
.then(fn, _ => get(loadSidebar).then(fn))
loadNested(path, loadSidebar, fn, this, true)
},
_ => this._renderMain(null))

// Load nav
loadNavbar &&
get(this.$getFile(root + loadNavbar))
.then(
text => this._renderNav(text),
// fallback root navbar when fail
_ => get(loadNavbar).then(text => this._renderNav(text))
)
loadNested(path, loadNavbar, text => this._renderNav(text), this, true)
}

proto._fetchCover = function () {
const { coverpage } = this.config
const root = getRoot(this.route.path)
const root = getParentPath(this.route.path)
const path = this.$getFile(root + coverpage)

if (this.route.path !== '/' || !coverpage) {
Expand Down
4 changes: 2 additions & 2 deletions src/core/render/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ markdown.renderer = renderer

markdown.init = function (config = {}, base = window.location.pathname) {
contentBase = getBasePath(base)
currentPath = parse().path

if (isFn(config)) {
markdownCompiler = config(marked, renderer)
Expand Down Expand Up @@ -107,8 +106,9 @@ export function sidebar (text, level) {
html = markdown(text)
html = html.match(/<ul[^>]*>([\s\S]+)<\/ul>/g)[0]
} else {
const tree = genTree(toc, level)
const tree = cacheTree[currentPath] || genTree(toc, level)
html = treeTpl(tree, '<ul>')
cacheTree[currentPath] = tree
}

return html
Expand Down
21 changes: 20 additions & 1 deletion src/core/render/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as tpl from './tpl'
import { markdown, sidebar, subSidebar, cover } from './compiler'
import { callHook } from '../init/lifecycle'
import { getBasePath, getPath, isAbsolutePath } from '../route/util'
import { isPrimitive } from '../util/core'

function executeScript () {
const script = dom.findAll('.markdown-section>script')
Expand Down Expand Up @@ -47,6 +48,22 @@ function renderMain (html) {
}
}

function renderNameLink (vm) {
const el = dom.getNode('.app-name-link')
const nameLink = vm.config.nameLink
const path = vm.route.path

if (!el) return

if (isPrimitive(vm.config.nameLink)) {
el.setAttribute('href', nameLink)
} else if (typeof nameLink === 'object') {
const match = Object.keys(nameLink).find(key => path.indexOf(key) > -1)

el.setAttribute('href', nameLink[match])
}
}

export function renderMixin (proto) {
proto._renderTo = function (el, content, replace) {
const node = dom.getNode(el)
Expand All @@ -58,7 +75,7 @@ export function renderMixin (proto) {

this._renderTo('.sidebar-nav', sidebar(text, maxLevel))
const active = getAndActive('.sidebar-nav', true, true)
loadSidebar && subSidebar(active, subMaxLevel)
subSidebar(loadSidebar ? active : '', subMaxLevel)
// bind event
this.activeLink = active
scrollActiveSidebar()
Expand Down Expand Up @@ -120,6 +137,8 @@ export function renderMixin (proto) {

proto._updateRender = function () {
markdown.update()
// render name link
renderNameLink(this)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/render/tpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function main (config) {
'</button>' +
'<aside class="sidebar">' +
(config.name
? `<h1><a data-nosearch href="${config.nameLink}">${config.name}</a></h1>`
? `<h1><a class="app-name-link" data-nosearch>${config.name}</a></h1>`
: '') +
'<div class="sidebar-nav"></div>' +
'</aside>')
Expand Down
1 change: 1 addition & 0 deletions src/core/route/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ let lastRoute = {}
export function initRoute (vm) {
normalize()
lastRoute = vm.route = parse()
vm._updateRender()

on('hashchange', _ => {
normalize()
Expand Down
8 changes: 6 additions & 2 deletions src/core/route/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,12 @@ export const isAbsolutePath = cached(path => {
return /(:|(\/{2}))/.test(path)
})

export const getRoot = cached(path => {
return /\/$/g.test(path) ? path : path.match(/(\S*\/)[^\/]+$/)[1]
export const getParentPath = cached(path => {
return /\/$/g.test(path)
? path
: (path = path.match(/(\S*\/)[^\/]+$/))
? path[1]
: ''
})

export const cleanPath = cached(path => {
Expand Down
13 changes: 12 additions & 1 deletion src/plugins/search/component.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { search } from './search'

let dom
let NO_DATA_TEXT = ''

function style () {
const code = `
Expand Down Expand Up @@ -98,7 +99,7 @@ function bindEvents () {
})

$panel.classList.add('show')
$panel.innerHTML = html || '<p class="empty">No Results!</p>'
$panel.innerHTML = html || `<p class="empty">${NO_DATA_TEXT}</p>`
}

let timeId
Expand All @@ -122,6 +123,15 @@ function updatePlaceholder (text, path) {
}
}

function updateNoData (text, path) {
if (typeof text === 'string') {
NO_DATA_TEXT = text
} else {
const match = Object.keys(text).find(key => path.indexOf(key) > -1)
NO_DATA_TEXT = text[match]
}
}

export function init (opts) {
dom = Docsify.dom
style()
Expand All @@ -131,5 +141,6 @@ export function init (opts) {

export function update (opts, vm) {
updatePlaceholder(opts.placeholder, vm.route.path)
updateNoData(opts.noData, vm.route.path)
}

2 changes: 2 additions & 0 deletions src/plugins/search/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { init as initSearch } from './search'

const CONFIG = {
placeholder: 'Type to search',
noData: 'No Results!',
paths: 'auto',
maxAge: 86400000 // 1 day
}
Expand All @@ -17,6 +18,7 @@ const install = function (hook, vm) {
CONFIG.paths = Array.isArray(opts.paths) ? opts.paths : 'auto'
CONFIG.maxAge = util.isPrimitive(opts.maxAge) ? opts.maxAge : CONFIG.maxAge
CONFIG.placeholder = opts.placeholder || CONFIG.placeholder
CONFIG.noData = opts.noData || CONFIG.noData
}

const isAuto = CONFIG.paths === 'auto'
Expand Down