Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged

1.3 #38

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
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
## 1.3.0
### Features

- Add cover page

### Bug fixes
- sidebar highlight
- headling can be cliked
- add `<kbd>` style

## 1.2.0
### Features
- custom basePath
Expand Down
46 changes: 33 additions & 13 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
<p align="center">
<img alt="docsify" src="https://cloud.githubusercontent.com/assets/7565692/21292094/5c206de4-c533-11e6-9493-29ea67b01dde.png">
</p>

<p align="center">
A magical documentation site generator.
</p>

## Features
- Simple and lightweight (~12kb gzipped)
- Multiple themes
- Not build static html files

## Quick Start

### Create a project
Expand Down Expand Up @@ -107,6 +94,28 @@ Code in `404.html`
- UNPKG [https://unpkg.com/docsify/](https://unpkg.com/docsify/)
- jsDelivr [http://www.jsdelivr.com/projects/docsify](http://www.jsdelivr.com/projects/docsify)


### Cover Page

Through the markdown can generate a cover page. create a `_coverpage.md` and set `data-coverpage` in script tag.

```markdown
![logo](_media/icon.svg)

# docsify <small>1.2.0</small>

> A magical documentation site generator.

- Simple and lightweight (~12kb gzipped)
- Multiple themes
- Not build static html files


[GitHub](https://github.com/QingWei-Li/docsify/)
[Get Started](#quick-start)
```


### Options

#### repo
Expand Down Expand Up @@ -261,3 +270,14 @@ If your HTML entry file and the markdown files are in different directories, we
<!-- Even if the docs is on another site 😄 -->
<script src="/lib/docsify.js" data-base-path="https://docsify.js.org/"></script>
```


#### coverpage

Generate cover page.

```html
<script src="/lib/docsify.js" data-coverpage></script>
<!-- or -->
<script src="/lib/docsify.js" data-coverpage="other.md"></script>
```
13 changes: 13 additions & 0 deletions docs/_coverpage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
![logo](_media/icon.svg)

# docsify <small>1.2.0</small>

> A magical documentation site generator.

- Simple and lightweight (~12kb gzipped)
- Multiple themes
- Not build static html files


[GitHub](https://github.com/QingWei-Li/docsify/)
[Get Started](#quick-start)
30 changes: 30 additions & 0 deletions docs/_media/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
</body>
<script
src="//cdn.jsdelivr.net/docsify/latest/lib/docsify.min.js"
data-repo="qingwei-li/docsify"
data-max-level="3"
data-max-level="4"
data-sidebar-toggle
data-coverpage
data-router></script>
</html>
40 changes: 33 additions & 7 deletions docs/zh-cn.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
<p align="center">
<img alt="docsify" src="https://cloud.githubusercontent.com/assets/7565692/21292094/5c206de4-c533-11e6-9493-29ea67b01dde.png">
</p>

<p align="center">
无需构建快速生成文档页
</p>

## 特性
- 无需构建,写完 markdown 直接发布
Expand Down Expand Up @@ -106,6 +99,28 @@ docsify serve docs
- UNPKG [https://unpkg.com/docsify/](https://unpkg.com/docsify/)
- jsDelivr [http://www.jsdelivr.com/projects/docsify](http://www.jsdelivr.com/projects/docsify)

### 封面

只需要写几行简单的 markdown 就可以拥有一页精致的封面,通过添加 `data-coverpage` 属性,并创建 `_coverpage.md`,按照下面的格式书写即可。

```markdown
![logo](_media/icon.svg)

# docsify <small>1.2.0</small>

> A magical documentation site generator.

- Simple and lightweight (~12kb gzipped)
- Multiple themes
- Not build static html files


[GitHub](https://github.com/QingWei-Li/docsify/)
[Get Started](#quick-start)
```



### 配置参数

#### repo
Expand Down Expand Up @@ -258,3 +273,14 @@ Sidebar 开关按钮
<!-- 甚至文档是在其他站点下 😄 -->
<script src="/lib/docsify.js" data-base-path="https://docsify.js.org/"></script>
```

#### coverpage

生成封面,参考 [#封面](#封面).

```html
<script src="/lib/docsify.js" data-coverpage></script>
<!-- or -->
<script src="/lib/docsify.js" data-coverpage="other.md"></script>

```
22 changes: 17 additions & 5 deletions src/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ export function scrollActiveSidebar () {

function highlight () {
for (let i = 0, len = anchors.length; i < len; i += 1) {
const node = anchors[i].parentNode
const node = anchors[i]
const bcr = node.getBoundingClientRect()

if (bcr.top < 10 && bcr.bottom > 10) {
const li = nav[node.id]
const li = nav[node.getAttribute('data-id')]

if (!li || li === active) return
if (active) active.setAttribute('class', '')
Expand All @@ -40,9 +40,8 @@ export function scrollActiveSidebar () {
}
}

const dom = document.querySelector('main .content')
dom.removeEventListener('scroll', highlight)
dom.addEventListener('scroll', highlight)
window.removeEventListener('scroll', highlight)
window.addEventListener('scroll', highlight)
highlight()
}

Expand Down Expand Up @@ -100,3 +99,16 @@ export function scroll2Top () {
}
cacheContentDOM.scrollTop = 0
}

export function sticky () {
const dom = document.querySelector('section.cover')
const coverHeight = dom.getBoundingClientRect().height

return (function () {
if (window.pageYOffset >= coverHeight || dom.classList.contains('hidden')) {
document.body.classList.add('sticky')
} else {
document.body.classList.remove('sticky')
}
})()
}
14 changes: 13 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { load, camel2kebab, isNil, getRoute } from './util'
import { activeLink, scrollIntoView } from './event'
import { activeLink, scrollIntoView, sticky } from './event'
import * as render from './render'

const OPTIONS = {
Expand All @@ -12,6 +12,7 @@ const OPTIONS = {
loadNavbar: null,
router: false,
homepage: 'README.md',
coverpage: '',
basePath: '',
auto2top: false
}
Expand All @@ -25,6 +26,7 @@ if (script) {
}
if (OPTIONS.loadSidebar === true) OPTIONS.loadSidebar = '_sidebar.md'
if (OPTIONS.loadNavbar === true) OPTIONS.loadNavbar = '_navbar.md'
if (OPTIONS.coverpage === true) OPTIONS.coverpage = '_coverpage.md'
if (OPTIONS.sidebar) OPTIONS.sidebar = window[OPTIONS.sidebar]
}

Expand Down Expand Up @@ -56,6 +58,15 @@ const mainRender = function (cb) {
page = `${route}.md`
}

// Render Cover page
if (OPTIONS.coverpage) {
if (page === OPTIONS.homepage) {
load(OPTIONS.coverpage).then(render.renderCover)
} else {
render.renderCover()
}
}

cacheXhr && cacheXhr.abort && cacheXhr.abort()
// Render markdown file
cacheXhr = load(page, 'GET', render.renderLoading)
Expand Down Expand Up @@ -91,6 +102,7 @@ const Docsify = function () {
mainRender(_ => {
activeLink('aside.sidebar', true)
scrollIntoView()
OPTIONS.coverpage && sticky()
})
}

Expand Down
32 changes: 28 additions & 4 deletions src/render.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import marked from 'marked'
import Prism from 'prismjs'
import * as tpl from './tpl'
import { activeLink, scrollActiveSidebar, bindToggle, scroll2Top } from './event'
import { genTree, getRoute } from './util'
import { activeLink, scrollActiveSidebar, bindToggle, scroll2Top, sticky } from './event'
import { genTree, getRoute, isMobile } from './util'

let OPTIONS = {}
const CACHE = {}
Expand Down Expand Up @@ -32,7 +32,7 @@ renderer.heading = function (text, level) {

toc.push({ level, slug: `${route}#${encodeURIComponent(slug)}`, title: text })

return `<h${level} id="${slug}"><a href="${route}#${slug}" class="anchor"></a>${text}</h${level}>`
return `<a href="${route}#${slug}" data-id="${slug}" class="anchor"><h${level} id="${slug}">${text}</h${level}></a>`
}
// highlight code
renderer.code = function (code, lang = '') {
Expand All @@ -55,11 +55,21 @@ marked.setOptions({ renderer })
export function renderApp (dom, replace) {
const nav = document.querySelector('nav') || document.createElement('nav')

dom[replace ? 'outerHTML' : 'innerHTML'] = tpl.toggle(OPTIONS.sidebarToggle) + tpl.corner(OPTIONS.repo) + tpl.main()
if (!OPTIONS.repo) nav.classList.add('no-badge')

dom[replace ? 'outerHTML' : 'innerHTML'] = tpl.corner(OPTIONS.repo) +
(OPTIONS.coverpage ? tpl.cover() : '') +
tpl.main(OPTIONS.sidebarToggle ? tpl.toggle() : '')
document.body.insertBefore(nav, document.body.children[0])

// bind toggle
bindToggle('button.sidebar-toggle')
// bind sticky effect
if (OPTIONS.coverpage) {
!isMobile() && window.addEventListener('scroll', sticky)
} else {
document.body.classList.add('sticky')
}
}

/**
Expand Down Expand Up @@ -110,6 +120,20 @@ export function renderSidebar (content) {
toc = []
}

/**
* Cover Page
*/
export function renderCover (content) {
renderCover.dom = renderCover.dom || document.querySelector('section.cover')
if (!content) {
renderCover.dom.classList.add('hidden')
} else {
renderCover.dom.classList.remove('hidden')
!renderCover.rendered && renderTo('.cover-main', marked(content))
renderCover.rendered = true
}
}

/**
* render loading bar
* @return {[type]} [description]
Expand Down
Loading