From 6b30eb65eb40a9636455ec078e644382e1d7e7aa Mon Sep 17 00:00:00 2001 From: "qingwei.li" Date: Sat, 11 Feb 2017 19:04:12 +0800 Subject: [PATCH] feat(src): dynamic title and fix sidebar style --- src/event.js | 4 +++- src/plugins/ga.js | 3 +++ src/plugins/search.js | 3 +++ src/render.js | 9 ++++++++- src/themes/basic/_layout.css | 5 +++++ src/themes/vue.css | 4 +++- 6 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/event.js b/src/event.js index b4df38714..536899c05 100644 --- a/src/event.js +++ b/src/event.js @@ -19,7 +19,9 @@ export function scrollActiveSidebar () { for (let i = 0, len = lis.length; i < len; i += 1) { const li = lis[i] - let href = li.querySelector('a').getAttribute('href') + const a = li.querySelector('a') + if (!a) continue + let href = a.getAttribute('href') if (href !== '/') { const match = href.match('#([^#]+)$') diff --git a/src/plugins/ga.js b/src/plugins/ga.js index 5200240c0..d45fac22f 100644 --- a/src/plugins/ga.js +++ b/src/plugins/ga.js @@ -25,6 +25,9 @@ function collect () { } const install = function () { + if (install.installed) return + install.installed = true + if (!window.Docsify || !window.Docsify.installed) { console.error('[Docsify] Please load docsify.js first.') return diff --git a/src/plugins/search.js b/src/plugins/search.js index 966fea887..a34307db8 100644 --- a/src/plugins/search.js +++ b/src/plugins/search.js @@ -321,6 +321,9 @@ const searchPlugin = function () { } const install = function () { + if (install.installed) return + install.installed = true + if (!window.Docsify || !window.Docsify.installed) { console.error('[Docsify] Please load docsify.js first.') return diff --git a/src/render.js b/src/render.js index d8df50084..8f8f4ce2d 100644 --- a/src/render.js +++ b/src/render.js @@ -8,6 +8,7 @@ import { genTree, getRoute, isMobile, slugify, merge, emojify } from './util' let markdown = marked let toc = [] const CACHE = {} +const originTitle = document.title const renderTo = function (dom, content) { dom = typeof dom === 'object' ? dom : document.querySelector(dom) @@ -163,15 +164,21 @@ export function renderSidebar (content) { } renderTo('.sidebar-nav', html) + + if (toc[0] && toc[0].level === 1) { + document.title = `${toc[0].title}${originTitle ? ' - ' + originTitle : ''}` + } + const target = event.activeLink('.sidebar-nav', true) if (target) renderSubSidebar(target) - toc = [] + toc = [] event.scrollActiveSidebar() } export function renderSubSidebar (target) { if (!$docsify.subMaxLevel) return + toc[0] && toc[0].level === 1 && toc.shift() target.parentNode.innerHTML += tpl.tree(genTree(toc, $docsify.subMaxLevel), '