diff --git a/src/index.js b/src/index.js index c849bade4..a3604c291 100644 --- a/src/index.js +++ b/src/index.js @@ -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 * as render from './render' -const OPTIONS = { +const OPTIONS = merge({ el: '#app', repo: '', maxLevel: 6, @@ -16,7 +16,7 @@ const OPTIONS = { coverpage: '', basePath: '', auto2top: false -} +}, window.$docsify) const script = document.currentScript || [].slice.call(document.getElementsByTagName('script')).pop() // load configuration for script attribute @@ -32,7 +32,7 @@ if (script) { } // load options -render.config(OPTIONS) +render.init(OPTIONS) let cacheRoute = null let cacheXhr = null diff --git a/src/render.js b/src/render.js index c2e144c65..13312127d 100644 --- a/src/render.js +++ b/src/render.js @@ -2,9 +2,10 @@ import marked from 'marked' import Prism from 'prismjs' import * as tpl from './tpl' 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 toc = [] const CACHE = {} const renderTo = function (dom, content) { @@ -14,40 +15,47 @@ const renderTo = function (dom, content) { return dom } -let toc = [] -const renderer = new marked.Renderer() /** - * render anchor tag - * @link https://github.com/chjj/marked#overriding-renderer-methods + * init render + * @param {Object} options */ -renderer.heading = function (text, level) { - const slug = slugify(text) - let route = '' +export function init (options) { + OPTIONS = options - if (OPTIONS.router) { - route = `#/${getRoute()}` - } + const renderer = new marked.Renderer() + /** + * render anchor tag + * @link https://github.com/chjj/marked#overriding-renderer-methods + */ + renderer.heading = function (text, level) { + const slug = slugify(text) + let route = '' + + if (OPTIONS.router) { + route = `#/${getRoute()}` + } - toc.push({ level, slug: `${route}#${encodeURIComponent(slug)}`, title: text }) + toc.push({ level, slug: `${route}#${encodeURIComponent(slug)}`, title: text }) - return `${text}` -} -// highlight code -renderer.code = function (code, lang = '') { - const hl = Prism.highlight(code, Prism.languages[lang] || Prism.languages.markup) - .replace(/{{/g, '{{') + return `${text}` + } + // highlight code + renderer.code = function (code, lang = '') { + const hl = Prism.highlight(code, Prism.languages[lang] || Prism.languages.markup) + .replace(/{{/g, '{{') - return `
${hl}
` -} -renderer.link = function (href, title, text) { - if (OPTIONS.router && !/:/.test(href)) { - href = `#/${href}`.replace(/\/\//g, '/') + return `
${hl}
` } + renderer.link = function (href, title, text) { + if (OPTIONS.router && !/:/.test(href)) { + href = `#/${href}`.replace(/\/\//g, '/') + } - return `${text}` + return `${text}` + } + marked.setOptions(merge({ renderer }, OPTIONS.marked)) } -marked.setOptions({ renderer }) /** * App @@ -191,12 +199,3 @@ export function renderLoading ({ loaded, total, step }) { }, 200) } } - -/** - * Load Config - * @param {Object} options - */ -export function config (options) { - OPTIONS = options -} - diff --git a/src/util.js b/src/util.js index 8d531768a..daf48672b 100644 --- a/src/util.js +++ b/src/util.js @@ -139,3 +139,18 @@ export function slugify (string) { slugify.clear = function () { 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 +}