From 8ad835ca26d4d0b2da1d2d18f7981f91747cff13 Mon Sep 17 00:00:00 2001 From: "qingwei.li" Date: Tue, 28 Feb 2017 20:56:11 +0800 Subject: [PATCH 1/9] feat(render) nameLink for each route, fixed #99 --- src/core/render/compiler.js | 1 - src/core/render/index.js | 19 +++++++++++++++++++ src/core/render/tpl.js | 2 +- src/core/route/index.js | 1 + 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/core/render/compiler.js b/src/core/render/compiler.js index 785f70c35..fbc3f083c 100644 --- a/src/core/render/compiler.js +++ b/src/core/render/compiler.js @@ -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) diff --git a/src/core/render/index.js b/src/core/render/index.js index 4144adad9..d6b689336 100644 --- a/src/core/render/index.js +++ b/src/core/render/index.js @@ -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') @@ -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) @@ -120,6 +137,8 @@ export function renderMixin (proto) { proto._updateRender = function () { markdown.update() + // render name link + renderNameLink(this) } } diff --git a/src/core/render/tpl.js b/src/core/render/tpl.js index 6f7915945..e2f1ec759 100644 --- a/src/core/render/tpl.js +++ b/src/core/render/tpl.js @@ -31,7 +31,7 @@ export function main (config) { '' + '') diff --git a/src/core/route/index.js b/src/core/route/index.js index 7c4061ea2..09a14f929 100644 --- a/src/core/route/index.js +++ b/src/core/route/index.js @@ -34,6 +34,7 @@ let lastRoute = {} export function initRoute (vm) { normalize() lastRoute = vm.route = parse() + vm._updateRender() on('hashchange', _ => { normalize() From 07a2a91296291810a3e4ab30478284dfffcc4566 Mon Sep 17 00:00:00 2001 From: "qingwei.li" Date: Tue, 28 Feb 2017 20:57:32 +0800 Subject: [PATCH 2/9] docs(configuration): nameLink --- docs/configuration.md | 8 +++++++- docs/zh-cn/configuration.md | 8 +++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 26e3f0df8..5c997af2f 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -206,7 +206,13 @@ The name of the link. ```js window.$docsify = { - nameLink: '/' + nameLink: '/', + + // For each route + nameLink: { + '/zh-cn/': '/zh-cn/', + '/': '/' + } } ``` diff --git a/docs/zh-cn/configuration.md b/docs/zh-cn/configuration.md index 28c0af9b3..a474f7d38 100644 --- a/docs/zh-cn/configuration.md +++ b/docs/zh-cn/configuration.md @@ -206,7 +206,13 @@ window.$docsify = { ```js window.$docsify = { - nameLink: '/' + nameLink: '/', + + // 按照路由切换 + nameLink: { + '/zh-cn/': '/zh-cn/', + '/': '/' + } } ``` From f3fc5969512941416fb5082654f146153943715b Mon Sep 17 00:00:00 2001 From: "qingwei.li" Date: Tue, 28 Feb 2017 21:16:10 +0800 Subject: [PATCH 3/9] fix(fetch): load sidebar and navbar for parent path, fixed #100 --- src/core/fetch/index.js | 26 ++++++++++++++------------ src/core/route/util.js | 8 ++++++-- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/core/fetch/index.js b/src/core/fetch/index.js index 61577ac28..0f2380af2 100644 --- a/src/core/fetch/index.js +++ b/src/core/fetch/index.js @@ -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() @@ -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) { diff --git a/src/core/route/util.js b/src/core/route/util.js index 3f742c86b..cfdd2a9ee 100644 --- a/src/core/route/util.js +++ b/src/core/route/util.js @@ -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 => { From d3c9fbd124b4cec16293e93a6c2f000d767e163a Mon Sep 17 00:00:00 2001 From: "qingwei.li" Date: Tue, 28 Feb 2017 21:27:07 +0800 Subject: [PATCH 4/9] feat(search): Localization for no data tip, close #103 --- docs/plugins.md | 8 ++++++++ docs/zh-cn/plugins.md | 8 ++++++++ src/plugins/search/component.js | 13 ++++++++++++- src/plugins/search/index.js | 2 ++ 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/docs/plugins.md b/docs/plugins.md index 29f434874..3142694cd 100644 --- a/docs/plugins.md +++ b/docs/plugins.md @@ -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' } } } diff --git a/docs/zh-cn/plugins.md b/docs/zh-cn/plugins.md index aa29eab90..5fa1530a4 100644 --- a/docs/zh-cn/plugins.md +++ b/docs/zh-cn/plugins.md @@ -27,6 +27,14 @@ placeholder: { '/zh-cn/': '搜索', '/': 'Type to search' + }, + + noData: 'No Results!', + + // 支持本地化 + noData: { + '/zh-cn/': '找不到结果', + '/': 'No Results' } } } diff --git a/src/plugins/search/component.js b/src/plugins/search/component.js index 524448c8e..219fc8dcd 100644 --- a/src/plugins/search/component.js +++ b/src/plugins/search/component.js @@ -1,6 +1,7 @@ import { search } from './search' let dom +let NO_DATA_TEXT = '' function style () { const code = ` @@ -98,7 +99,7 @@ function bindEvents () { }) $panel.classList.add('show') - $panel.innerHTML = html || '

No Results!

' + $panel.innerHTML = html || `

${NO_DATA_TEXT}

` } let timeId @@ -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() @@ -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) } diff --git a/src/plugins/search/index.js b/src/plugins/search/index.js index e1962793f..6b9d3e167 100644 --- a/src/plugins/search/index.js +++ b/src/plugins/search/index.js @@ -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 } @@ -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' From 0aead898eb4f0ea7313fc114c832f2cbad4f019e Mon Sep 17 00:00:00 2001 From: "qingwei.li" Date: Tue, 28 Feb 2017 21:28:25 +0800 Subject: [PATCH 5/9] docs: updates --- docs/_sidebar.md | 2 +- docs/zh-cn/_sidebar.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/_sidebar.md b/docs/_sidebar.md index e747a918d..cf254be95 100644 --- a/docs/_sidebar.md +++ b/docs/_sidebar.md @@ -17,6 +17,6 @@ - [Helpers](/helpers) - [Vue compatibility](/vue) - [CDN](/cdn) - - [Offline Mode(PWA)new](/pwa) + - [Offline Mode(PWA)](/pwa) - [Changelog](/changelog) \ No newline at end of file diff --git a/docs/zh-cn/_sidebar.md b/docs/zh-cn/_sidebar.md index 47d970e50..d9376b4a1 100644 --- a/docs/zh-cn/_sidebar.md +++ b/docs/zh-cn/_sidebar.md @@ -17,6 +17,6 @@ - [文档助手](zh-cn/helpers) - [兼容 Vue](zh-cn/vue) - [CDN](zh-cn/cdn) - - [离线模式(PWA)new](zh-cn/pwa) + - [离线模式(PWA)](zh-cn/pwa) - [Changelog](zh-cn/changelog) \ No newline at end of file From 0d59ee939fac6b0a8980c0e24d2da5f654227052 Mon Sep 17 00:00:00 2001 From: "qingwei.li" Date: Tue, 28 Feb 2017 22:00:53 +0800 Subject: [PATCH 6/9] fix(render): Toc rendering error, fixed #106 --- src/core/render/compiler.js | 3 ++- src/core/render/index.js | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/core/render/compiler.js b/src/core/render/compiler.js index fbc3f083c..78250f257 100644 --- a/src/core/render/compiler.js +++ b/src/core/render/compiler.js @@ -106,8 +106,9 @@ export function sidebar (text, level) { html = markdown(text) html = html.match(/]*>([\s\S]+)<\/ul>/g)[0] } else { - const tree = genTree(toc, level) + const tree = cacheTree[currentPath] || genTree(toc, level) html = treeTpl(tree, '