feat(src): dynamic title and fix sidebar style

This commit is contained in:
qingwei.li 2017-02-11 19:04:12 +08:00
commit 6b30eb65eb
6 changed files with 25 additions and 3 deletions

View file

@ -19,7 +19,9 @@ export function scrollActiveSidebar () {
for (let i = 0, len = lis.length; i < len; i += 1) { for (let i = 0, len = lis.length; i < len; i += 1) {
const li = lis[i] 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 !== '/') { if (href !== '/') {
const match = href.match('#([^#]+)$') const match = href.match('#([^#]+)$')

View file

@ -25,6 +25,9 @@ function collect () {
} }
const install = function () { const install = function () {
if (install.installed) return
install.installed = true
if (!window.Docsify || !window.Docsify.installed) { if (!window.Docsify || !window.Docsify.installed) {
console.error('[Docsify] Please load docsify.js first.') console.error('[Docsify] Please load docsify.js first.')
return return

View file

@ -321,6 +321,9 @@ const searchPlugin = function () {
} }
const install = function () { const install = function () {
if (install.installed) return
install.installed = true
if (!window.Docsify || !window.Docsify.installed) { if (!window.Docsify || !window.Docsify.installed) {
console.error('[Docsify] Please load docsify.js first.') console.error('[Docsify] Please load docsify.js first.')
return return

View file

@ -8,6 +8,7 @@ import { genTree, getRoute, isMobile, slugify, merge, emojify } from './util'
let markdown = marked let markdown = marked
let toc = [] let toc = []
const CACHE = {} const CACHE = {}
const originTitle = document.title
const renderTo = function (dom, content) { const renderTo = function (dom, content) {
dom = typeof dom === 'object' ? dom : document.querySelector(dom) dom = typeof dom === 'object' ? dom : document.querySelector(dom)
@ -163,15 +164,21 @@ export function renderSidebar (content) {
} }
renderTo('.sidebar-nav', html) 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) const target = event.activeLink('.sidebar-nav', true)
if (target) renderSubSidebar(target) if (target) renderSubSidebar(target)
toc = []
toc = []
event.scrollActiveSidebar() event.scrollActiveSidebar()
} }
export function renderSubSidebar (target) { export function renderSubSidebar (target) {
if (!$docsify.subMaxLevel) return if (!$docsify.subMaxLevel) return
toc[0] && toc[0].level === 1 && toc.shift()
target.parentNode.innerHTML += tpl.tree(genTree(toc, $docsify.subMaxLevel), '<ul>') target.parentNode.innerHTML += tpl.tree(genTree(toc, $docsify.subMaxLevel), '<ul>')
} }

View file

@ -223,6 +223,11 @@ main {
padding: 0; padding: 0;
} }
li>p {
font-weight: 700;
margin: 0;
}
ul, ul li { ul, ul li {
list-style: none; list-style: none;
} }

View file

@ -18,7 +18,7 @@ body {
background-color: $color-bg; background-color: $color-bg;
li { li {
margin: 6px 15px; margin: 6px 0 6px 15px;
} }
ul li a { ul li a {
@ -28,6 +28,7 @@ body {
white-space: nowrap; white-space: nowrap;
text-decoration: none; text-decoration: none;
font-size: 14px; font-size: 14px;
font-weight: normal;
&:hover { &:hover {
text-decoration: underline; text-decoration: underline;
@ -41,6 +42,7 @@ body {
ul li.active>a { ul li.active>a {
color: var(--theme-color, $color-primary); color: var(--theme-color, $color-primary);
font-weight: 600; font-weight: 600;
border-right: 2px solid;
} }
} }