feat: add docsify-updated, close #158

This commit is contained in:
qingwei.li 2017-05-16 23:03:22 +08:00
commit d2be5aecf8
No known key found for this signature in database
GPG key ID: B6DDC2F7AE80B2F4
11 changed files with 95 additions and 13 deletions

View file

@ -18,7 +18,8 @@ const config = merge({
executeScript: null,
noEmoji: false,
ga: '',
mergeNavbar: false
mergeNavbar: false,
formatUpdated: ''
}, window.$docsify)
const script = document.currentScript ||

View file

@ -14,9 +14,10 @@ export function get (url, hasBar = false) {
const on = function () {
xhr.addEventListener.apply(xhr, arguments)
}
const cached = cache[url]
if (cache[url]) {
return { then: cb => cb(cache[url]), abort: noop }
if (cached) {
return { then: cb => cb(cached.content, cached.opt), abort: noop }
}
xhr.open('GET', url)
@ -41,8 +42,14 @@ export function get (url, hasBar = false) {
if (target.status >= 400) {
error(target)
} else {
cache[url] = target.response
success(target.response)
const result = cache[url] = {
content: target.response,
opt: {
updatedAt: xhr.getResponseHeader('last-modified')
}
}
success(result.content, result.opt)
}
})
},

View file

@ -28,8 +28,8 @@ export function fetchMixin (proto) {
this.isHTML = /\.html$/g.test(path)
// Load main content
last.then(text => {
this._renderMain(text)
last.then((text, opt) => {
this._renderMain(text, opt)
if (!loadSidebar) return cb()
const fn = result => { this._renderSidebar(result); cb() }

View file

@ -8,6 +8,7 @@ import { callHook } from '../init/lifecycle'
import { getBasePath, getPath, isAbsolutePath } from '../route/util'
import { isPrimitive } from '../util/core'
import { isMobile } from '../util/env'
import tinydate from 'tinydate'
function executeScript () {
const script = dom.findAll('.markdown-section>script')
@ -21,6 +22,16 @@ function executeScript () {
}, 0)
}
function formatUpdated (html, updated, fn) {
updated = typeof fn === 'function'
? fn(updated)
: typeof fn === 'string'
? tinydate(fn)(new Date(updated))
: updated
return html.replace(/{docsify-updated}/g, updated)
}
function renderMain (html) {
if (!html) {
// TODO: Custom 404 page
@ -97,9 +108,11 @@ export function renderMixin (proto) {
getAndActive('nav')
}
proto._renderMain = function (text) {
proto._renderMain = function (text, opt) {
callHook(this, 'beforeEach', text, result => {
const html = this.isHTML ? result : markdown(result)
let html = this.isHTML ? result : markdown(result)
html = formatUpdated(html, opt.updatedAt, this.config.formatUpdated)
callHook(this, 'afterEach', html, text => renderMain.call(this, text))
})
}