Skip to content

Commit d2be5ae

Browse files
committedMay 16, 2017
feat: add docsify-updated, close #158
1 parent ca0d618 commit d2be5ae

File tree

11 files changed

+95
-13
lines changed

11 files changed

+95
-13
lines changed
 

‎app.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var fs = require('fs')
44

55
http.createServer(function (req, res) {
66
serveStatic('.')(req, res, function () {
7-
res.writeHead(404, { 'Content-Type': 'text/html' })
7+
res.writeHead(200, { 'Content-Type': 'text/html' })
88
res.end(fs.readFileSync('dev.html'))
99
})
1010
}).listen(3000, '0.0.0.0')

‎dev.html

+9-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,15 @@
3030
loadSidebar: true,
3131
name: 'docsify',
3232
subMaxLevel: 2,
33-
mergeNavbar: true
33+
mergeNavbar: true,
34+
formatUpdated: '{MM}/{DD} {HH}:{mm}',
35+
plugins: [
36+
function(hook) {
37+
hook.beforeEach(function (html) {
38+
return html += '> Last modified {docsify-updated}'
39+
})
40+
}
41+
]
3442
}
3543
</script>
3644
<script src="/lib/docsify.js"></script>

‎docs/configuration.md

+15
Original file line numberDiff line numberDiff line change
@@ -315,3 +315,18 @@ window.$docsify = {
315315
mergeNavbar: true
316316
}
317317
```
318+
319+
## format-updated
320+
We can display the file update date through **{docsify-updated<span>}</span>** variable. And format it by `formatUpdated`.
321+
See https://github.com/lukeed/tinydate#patterns
322+
```js
323+
window.$docsify = {
324+
formatUpdated: '{MM}/{DD} {HH}:{mm}',
325+
326+
formatUpdated: function (time) {
327+
// ...
328+
329+
return time
330+
}
331+
}
332+
```

‎docs/de-de/configuration.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -314,4 +314,19 @@ Navbar will be merged with the sidebar on smaller screens.
314314
window.$docsify = {
315315
mergeNavbar: true
316316
}
317-
```
317+
```
318+
319+
## format-updated
320+
We can display the file update date through **{docsify-updated<span>}</span>** variable. And format it by `formatUpdated`.
321+
See https://github.com/lukeed/tinydate#patterns
322+
```js
323+
window.$docsify = {
324+
formatUpdated: '{MM}/{DD} {HH}:{mm}',
325+
326+
formatUpdated: function (time) {
327+
// ...
328+
329+
return time
330+
}
331+
}
332+
```

‎docs/index.html

+9-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
loadNavbar: true,
3535
mergeNavbar: true,
3636
maxLevel: 4,
37+
subMaxLevel: 2,
3738
name: 'docsify',
3839
search: {
3940
noData: {
@@ -48,7 +49,14 @@
4849
'/': 'Search'
4950
}
5051
},
51-
subMaxLevel: 2
52+
formatUpdated: '{MM}/{DD} {HH}:{mm}',
53+
plugins: [
54+
function(hook) {
55+
hook.beforeEach(function (html) {
56+
return html += '> Last modified {docsify-updated}'
57+
})
58+
}
59+
]
5260
}
5361
</script>
5462
<script src="//unpkg.com/docsify/lib/docsify.min.js"></script>

‎docs/zh-cn/configuration.md

+14
Original file line numberDiff line numberDiff line change
@@ -326,3 +326,17 @@ window.$docsify = {
326326
}
327327
```
328328

329+
## format-updated
330+
我们可以显示文档更新日期通过 **{docsify-updated<span>}</span>** 变量. 并且格式化日期通过 `formatUpdated`.
331+
参考 https://github.com/lukeed/tinydate#patterns
332+
```js
333+
window.$docsify = {
334+
formatUpdated: '{MM}/{DD} {HH}:{mm}',
335+
336+
formatUpdated: function (time) {
337+
// ...
338+
339+
return time
340+
}
341+
}
342+
```

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"dependencies": {
2929
"marked": "^0.3.6",
3030
"prismjs": "^1.6.0",
31+
"tinydate": "^1.0.0",
3132
"zoom-image": "^0.1.4"
3233
},
3334
"devDependencies": {

‎src/core/config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ const config = merge({
1818
executeScript: null,
1919
noEmoji: false,
2020
ga: '',
21-
mergeNavbar: false
21+
mergeNavbar: false,
22+
formatUpdated: ''
2223
}, window.$docsify)
2324

2425
const script = document.currentScript ||

‎src/core/fetch/ajax.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ export function get (url, hasBar = false) {
1414
const on = function () {
1515
xhr.addEventListener.apply(xhr, arguments)
1616
}
17+
const cached = cache[url]
1718

18-
if (cache[url]) {
19-
return { then: cb => cb(cache[url]), abort: noop }
19+
if (cached) {
20+
return { then: cb => cb(cached.content, cached.opt), abort: noop }
2021
}
2122

2223
xhr.open('GET', url)
@@ -41,8 +42,14 @@ export function get (url, hasBar = false) {
4142
if (target.status >= 400) {
4243
error(target)
4344
} else {
44-
cache[url] = target.response
45-
success(target.response)
45+
const result = cache[url] = {
46+
content: target.response,
47+
opt: {
48+
updatedAt: xhr.getResponseHeader('last-modified')
49+
}
50+
}
51+
52+
success(result.content, result.opt)
4653
}
4754
})
4855
},

‎src/core/fetch/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ export function fetchMixin (proto) {
2828
this.isHTML = /\.html$/g.test(path)
2929

3030
// Load main content
31-
last.then(text => {
32-
this._renderMain(text)
31+
last.then((text, opt) => {
32+
this._renderMain(text, opt)
3333
if (!loadSidebar) return cb()
3434

3535
const fn = result => { this._renderSidebar(result); cb() }

‎src/core/render/index.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { callHook } from '../init/lifecycle'
88
import { getBasePath, getPath, isAbsolutePath } from '../route/util'
99
import { isPrimitive } from '../util/core'
1010
import { isMobile } from '../util/env'
11+
import tinydate from 'tinydate'
1112

1213
function executeScript () {
1314
const script = dom.findAll('.markdown-section>script')
@@ -21,6 +22,16 @@ function executeScript () {
2122
}, 0)
2223
}
2324

25+
function formatUpdated (html, updated, fn) {
26+
updated = typeof fn === 'function'
27+
? fn(updated)
28+
: typeof fn === 'string'
29+
? tinydate(fn)(new Date(updated))
30+
: updated
31+
32+
return html.replace(/{docsify-updated}/g, updated)
33+
}
34+
2435
function renderMain (html) {
2536
if (!html) {
2637
// TODO: Custom 404 page
@@ -97,9 +108,11 @@ export function renderMixin (proto) {
97108
getAndActive('nav')
98109
}
99110

100-
proto._renderMain = function (text) {
111+
proto._renderMain = function (text, opt) {
101112
callHook(this, 'beforeEach', text, result => {
102-
const html = this.isHTML ? result : markdown(result)
113+
let html = this.isHTML ? result : markdown(result)
114+
html = formatUpdated(html, opt.updatedAt, this.config.formatUpdated)
115+
103116
callHook(this, 'afterEach', html, text => renderMain.call(this, text))
104117
})
105118
}

0 commit comments

Comments
 (0)
Please sign in to comment.