init config by window.

This commit is contained in:
qingwei.li 2017-01-04 20:47:55 +08:00
commit 897283418a
3 changed files with 53 additions and 39 deletions

View file

@ -1,8 +1,8 @@
import { load, camel2kebab, isNil, getRoute } from './util' import { load, camel2kebab, isNil, getRoute, merge } from './util'
import { scrollIntoView, activeLink } from './event' import { scrollIntoView, activeLink } from './event'
import * as render from './render' import * as render from './render'
const OPTIONS = { const OPTIONS = merge({
el: '#app', el: '#app',
repo: '', repo: '',
maxLevel: 6, maxLevel: 6,
@ -16,7 +16,7 @@ const OPTIONS = {
coverpage: '', coverpage: '',
basePath: '', basePath: '',
auto2top: false auto2top: false
} }, window.$docsify)
const script = document.currentScript || [].slice.call(document.getElementsByTagName('script')).pop() const script = document.currentScript || [].slice.call(document.getElementsByTagName('script')).pop()
// load configuration for script attribute // load configuration for script attribute
@ -32,7 +32,7 @@ if (script) {
} }
// load options // load options
render.config(OPTIONS) render.init(OPTIONS)
let cacheRoute = null let cacheRoute = null
let cacheXhr = null let cacheXhr = null

View file

@ -2,9 +2,10 @@ import marked from 'marked'
import Prism from 'prismjs' import Prism from 'prismjs'
import * as tpl from './tpl' import * as tpl from './tpl'
import { activeLink, scrollActiveSidebar, bindToggle, scroll2Top, sticky } from './event' import { activeLink, scrollActiveSidebar, bindToggle, scroll2Top, sticky } from './event'
import { genTree, getRoute, isMobile, slugify } from './util' import { genTree, getRoute, isMobile, slugify, merge } from './util'
let OPTIONS = {} let OPTIONS = {}
let toc = []
const CACHE = {} const CACHE = {}
const renderTo = function (dom, content) { const renderTo = function (dom, content) {
@ -14,9 +15,15 @@ const renderTo = function (dom, content) {
return dom return dom
} }
let toc = []
const renderer = new marked.Renderer()
/**
* init render
* @param {Object} options
*/
export function init (options) {
OPTIONS = options
const renderer = new marked.Renderer()
/** /**
* render anchor tag * render anchor tag
* @link https://github.com/chjj/marked#overriding-renderer-methods * @link https://github.com/chjj/marked#overriding-renderer-methods
@ -47,7 +54,8 @@ renderer.link = function (href, title, text) {
return `<a href="${href}" title="${title || ''}">${text}</a>` return `<a href="${href}" title="${title || ''}">${text}</a>`
} }
marked.setOptions({ renderer }) marked.setOptions(merge({ renderer }, OPTIONS.marked))
}
/** /**
* App * App
@ -191,12 +199,3 @@ export function renderLoading ({ loaded, total, step }) {
}, 200) }, 200)
} }
} }
/**
* Load Config
* @param {Object} options
*/
export function config (options) {
OPTIONS = options
}

View file

@ -139,3 +139,18 @@ export function slugify (string) {
slugify.clear = function () { slugify.clear = function () {
slugify.occurrences = {} slugify.occurrences = {}
} }
const hasOwnProperty = Object.prototype.hasOwnProperty
export const merge = Object.assign || function (to) {
for (let i = 1; i < arguments.length; i++) {
const from = Object(arguments[i])
for (const key in from) {
if (hasOwnProperty.call(from, key)) {
to[key] = from[key]
}
}
}
return to
}