From 082c68ecee7c4055006ff1c77cd5163812ae36ae Mon Sep 17 00:00:00 2001 From: "cinwell.li" Date: Sun, 31 Mar 2019 14:28:42 +0800 Subject: [PATCH] fix(static): file path --- lib/docsify.js | 144 +++++++++++----------- lib/plugins/search.js | 26 ---- packages/docsify-server-renderer/index.js | 36 +++--- src/core/render/compiler.js | 53 ++++++-- src/core/render/index.js | 27 +--- src/core/render/tpl.js | 21 ++-- src/core/router/util.js | 4 +- 7 files changed, 151 insertions(+), 160 deletions(-) diff --git a/lib/docsify.js b/lib/docsify.js index 4df2c2e..018990d 100644 --- a/lib/docsify.js +++ b/lib/docsify.js @@ -362,16 +362,21 @@ function main(config) { /** * Cover Page */ -function cover() { +function cover(ref) { + var style = ref.style; + var text = ref.text; + var hasMask = ref.hasMask; + var SL = ', 100%, 85%'; - var bgc = - 'linear-gradient(to left bottom, ' + - "hsl(" + (Math.floor(Math.random() * 255) + SL) + ") 0%," + - "hsl(" + (Math.floor(Math.random() * 255) + SL) + ") 100%)"; + style = + style || + 'background: linear-gradient(to left bottom, ' + + "hsl(" + (Math.floor(Math.random() * 255) + SL) + ") 0%," + + "hsl(" + (Math.floor(Math.random() * 255) + SL) + ") 100%)"; return ( - "
" + - '
' + + "
" + + "
" + text + "
" + '
' + '
' ) @@ -3335,11 +3340,7 @@ function getAndRemoveConfig(str) { str = str .replace(/^'/, '') .replace(/'$/, '') -<<<<<<< HEAD .replace(/(?:^|\s):([\w-]+)=?([\w-]+)?/g, function (m, key, value) { -======= - .replace(/:([\w-]+)=?([\w-]+)?/g, function (m, key, value) { ->>>>>>> [build] 4.8.6 config[key] = (value && value.replace(/"/g, '')) || true; return '' }) @@ -3487,10 +3488,7 @@ Compiler.prototype.compileEmbed = function compileEmbed (href, title) { embed = compileMedia[type].call(this, href, title); embed.type = type; } -<<<<<<< HEAD embed.fragment = config.fragment; -======= ->>>>>>> [build] 4.8.6 return embed } @@ -3641,26 +3639,29 @@ Compiler.prototype._initRenderer = function _initRenderer () { return ("\""") }; -<<<<<<< HEAD origin.list = renderer.list = function (body, ordered, start) { - var isTaskList = /
  • /.test(body.split('class="task-list"')[0]); + var isTaskList = /
  • /.test( + body.split('class="task-list"')[0] + ); var isStartReq = start && start > 1; var tag = ordered ? 'ol' : 'ul'; var tagAttrs = [ - (isTaskList ? 'class="task-list"' : ''), - (isStartReq ? ("start=\"" + start + "\"") : '') - ].join(' ').trim(); + isTaskList ? 'class="task-list"' : '', + isStartReq ? ("start=\"" + start + "\"") : '' + ] + .join(' ') + .trim(); return ("<" + tag + " " + tagAttrs + ">" + body + "") }; origin.listitem = renderer.listitem = function (text) { var isTaskItem = /^(]*>)/.test(text); - var html = isTaskItem ? ("
  • ") : ("
  • " + text + "
  • "); + var html = isTaskItem ? + ("
  • ") : + ("
  • " + text + "
  • "); return html }; -======= ->>>>>>> [build] 4.8.6 renderer.origin = origin; @@ -3719,13 +3720,40 @@ Compiler.prototype.article = function article (text) { /** * Compile cover page */ -Compiler.prototype.cover = function cover$$1 (text) { - var cacheToc = this.toc.slice(); - var html = this.compile(text); +Compiler.prototype.cover = function cover$$1 (text, isHTML) { + if ( isHTML === void 0 ) isHTML = false; - this.toc = cacheToc.slice(); + var html = text; + if (!isHTML) { + var cacheToc = this.toc.slice(); + html = this.compile(text); + this.toc = cacheToc.slice(); + } - return html + var m = html + .trim() + .match('

    ([^<]*?)

    $'); + var style = ''; + var hasMask = false; + if (m) { + if (m[2] === 'color') { + style = "background: " + (m[1] + (m[3] || '')); + } else { + var path = m[1]; + hasMask = true; + if (!isAbsolutePath(m[1])) { + path = getPath(this.router.getBasePath(), m[1]); + } + style = "background-image: url(" + path + "); background-size: cover; background-position: center center;"; + } + html = html.replace(m[0], ''); + } + + return cover({ + style: style, + hasMask: hasMask, + text: html + }) }; var title = $.title; @@ -3736,12 +3764,9 @@ function btn(el) { var toggle = function (_) { return body.classList.toggle('close'); }; el = getNode(el); -<<<<<<< HEAD if (el == null) { return } -======= ->>>>>>> [build] 4.8.6 on(el, 'click', function (e) { e.stopPropagation(); toggle(); @@ -3757,13 +3782,9 @@ function btn(el) { function collapse(el) { el = getNode(el); -<<<<<<< HEAD if (el == null) { return } -======= - ->>>>>>> [build] 4.8.6 on(el, 'click', function (ref) { var target = ref.target; @@ -3801,15 +3822,10 @@ function sticky() { */ function getAndActive(router, el, isParent, autoTitle) { el = getNode(el); -<<<<<<< HEAD var links = []; if (el != null) { links = findAll(el, 'a'); } -======= - - var links = findAll(el, 'a'); ->>>>>>> [build] 4.8.6 var hash = decodeURI(router.toURL(router.getCurrentPath())); var target; @@ -4012,14 +4028,10 @@ function scrollActiveSidebar(router) { coverHeight = cover ? cover.offsetHeight : 0; var sidebar = getNode('.sidebar'); -<<<<<<< HEAD var lis = []; if (sidebar != null) { lis = findAll(sidebar, 'li'); } -======= - var lis = findAll(sidebar, 'li'); ->>>>>>> [build] 4.8.6 for (var i = 0, len = lis.length; i < len; i += 1) { var li = lis[i]; @@ -4103,14 +4115,13 @@ function walkFetchEmbed(ref, cb) { if (token.embed.type === 'markdown') { embedToken = compile.lexer(text); } else if (token.embed.type === 'code') { -<<<<<<< HEAD if (token.embed.fragment) { var fragment = token.embed.fragment; - var pattern = new RegExp(("(?:###|\\/\\/\\/)\\s*\\[" + fragment + "\\]([\\s\\S]*)(?:###|\\/\\/\\/)\\s*\\[" + fragment + "\\]")); - text = ((text.match(pattern) || [])[1] || '').trim(); + var pattern = new RegExp( + ("(?:###|\\/\\/\\/)\\s*\\[" + fragment + "\\]([\\s\\S]*)(?:###|\\/\\/\\/)\\s*\\[" + fragment + "\\]") + ); + text = ((text.match(pattern) || [])[1] || '').trim(); } -======= ->>>>>>> [build] 4.8.6 embedToken = compile.lexer( '```' + token.embed.lang + @@ -4376,30 +4387,10 @@ function renderMixin(proto) { } toggleClass(el, 'add', 'show'); - var html = this.coverIsHTML ? text : this.compiler.cover(text); + var html = this.compiler.cover(text, this.coverIsHTML); - var m = html - .trim() - .match('

    ([^<]*?)

    $'); + this._renderTo('.cover-placeholder', html, true); - if (m) { - if (m[2] === 'color') { - el.style.background = m[1] + (m[3] || ''); - } else { - var path = m[1]; - - toggleClass(el, 'add', 'has-mask'); - if (!isAbsolutePath(m[1])) { - path = getPath(this.router.getBasePath(), m[1]); - } - el.style.backgroundImage = "url(" + path + ")"; - el.style.backgroundSize = 'cover'; - el.style.backgroundPosition = 'center center'; - } - html = html.replace(m[0], ''); - } - - this._renderTo('.cover-main', html); sticky(); }; @@ -4429,8 +4420,9 @@ function initRender(vm) { if (config.repo) { html += corner(config.repo); } + if (config.coverpage) { - html += cover(); + html += '
    '; } if (config.logo) { @@ -4490,9 +4482,11 @@ function getAlias(path, alias, last) { } function getFileName(path, ext) { - return new RegExp(("\\.(" + (ext.replace(/^\./, '')) + "|html)$"), 'g').test(path) ? + return /\.\w+$/.test(path) ? path : - /\/$/g.test(path) ? (path + "README" + ext) : ("" + path + ext) + /\/$/g.test(path) ? + (path + "README" + ext) : + ("" + path + ext) } var History = function History(config) { @@ -5075,6 +5069,7 @@ function initGlobalAPI () { dom: dom, get: get, slugify: slugify, +<<<<<<< HEAD <<<<<<< HEAD version: '4.9.1' ======= @@ -5084,6 +5079,9 @@ function initGlobalAPI () { version: '4.8.6' >>>>>>> [build] 4.8.6 >>>>>>> [build] 4.8.6 +======= + version: '4.9.0' +>>>>>>> fix(static): file path }; window.DocsifyCompiler = Compiler; window.marked = marked; diff --git a/lib/plugins/search.js b/lib/plugins/search.js index bfd46ba..3d820f6 100644 --- a/lib/plugins/search.js +++ b/lib/plugins/search.js @@ -1,7 +1,6 @@ (function () { var INDEXS = {}; -<<<<<<< HEAD var LOCAL_STORAGE = { EXPIRE_KEY: 'docsify.search.expires', INDEX_KEY: 'docsify.search.index' @@ -14,8 +13,6 @@ function resolveIndexKey(namespace) { return namespace ? ((LOCAL_STORAGE.INDEX_KEY) + "/" + namespace) : LOCAL_STORAGE.INDEX_KEY } -======= ->>>>>>> [build] 4.8.6 function escapeHtml(string) { var entityMap = { '&': '&', @@ -49,15 +46,9 @@ function getAllPaths(router) { return paths } -<<<<<<< HEAD function saveData(maxAge, expireKey, indexKey) { localStorage.setItem(expireKey, Date.now() + maxAge); localStorage.setItem(indexKey, JSON.stringify(INDEXS)); -======= -function saveData(maxAge) { - localStorage.setItem('docsify.search.expires', Date.now() + maxAge); - localStorage.setItem('docsify.search.index', JSON.stringify(INDEXS)); ->>>>>>> [build] 4.8.6 } function genIndex(path, content, router, depth) { @@ -175,7 +166,6 @@ function search(query) { function init$1(config, vm) { var isAuto = config.paths === 'auto'; -<<<<<<< HEAD var expireKey = resolveExpireKey(config.namespace); var indexKey = resolveIndexKey(config.namespace); @@ -183,11 +173,6 @@ function init$1(config, vm) { var isExpired = localStorage.getItem(expireKey) < Date.now(); INDEXS = JSON.parse(localStorage.getItem(indexKey)); -======= - var isExpired = localStorage.getItem('docsify.search.expires') < Date.now(); - - INDEXS = JSON.parse(localStorage.getItem('docsify.search.index')); ->>>>>>> [build] 4.8.6 if (isExpired) { INDEXS = {}; @@ -208,11 +193,7 @@ function init$1(config, vm) { .get(vm.router.getFile(path), false, vm.config.requestHeaders) .then(function (result) { INDEXS[path] = genIndex(path, result, vm.router, config.depth); -<<<<<<< HEAD len === ++count && saveData(config.maxAge, expireKey, indexKey); -======= - len === ++count && saveData(config.maxAge); ->>>>>>> [build] 4.8.6 }); }); } @@ -346,12 +327,8 @@ var CONFIG = { paths: 'auto', depth: 2, maxAge: 86400000, // 1 day -<<<<<<< HEAD hideOtherSidebarContent: false, namespace: undefined -======= - hideOtherSidebarContent: false ->>>>>>> [build] 4.8.6 }; var install = function (hook, vm) { @@ -367,10 +344,7 @@ var install = function (hook, vm) { CONFIG.noData = opts.noData || CONFIG.noData; CONFIG.depth = opts.depth || CONFIG.depth; CONFIG.hideOtherSidebarContent = opts.hideOtherSidebarContent || CONFIG.hideOtherSidebarContent; -<<<<<<< HEAD CONFIG.namespace = opts.namespace || CONFIG.namespace; -======= ->>>>>>> [build] 4.8.6 } var isAuto = CONFIG.paths === 'auto'; diff --git a/packages/docsify-server-renderer/index.js b/packages/docsify-server-renderer/index.js index 9261d9f..e65018e 100644 --- a/packages/docsify-server-renderer/index.js +++ b/packages/docsify-server-renderer/index.js @@ -22,7 +22,7 @@ function mainTpl(config) { html += tpl.corner(config.repo) } if (config.coverpage) { - html += tpl.cover() + html += '' } html += tpl.main(config) @@ -53,9 +53,9 @@ export default class Renderer { } _getPath(url) { - const file = this.router.getFile(url) - - return isAbsolutePath(file) ? file : cwd(this.path, `./${file}`) + const path = resolve(this.path, url) + const file = this.router.getFile(path) + return file } async render(url) { @@ -77,36 +77,34 @@ export default class Renderer { if (loadSidebar) { const name = loadSidebar === true ? '_sidebar.md' : loadSidebar - const sidebarFile = this._getPath(resolve(this.path, url, `./${name}`)) + const sidebarFile = this._getPath(resolvePathname(name, url)) this._renderHtml('sidebar', await this._render(sidebarFile, 'sidebar')) } if (loadNavbar) { const name = loadNavbar === true ? '_navbar.md' : loadNavbar - const navbarFile = this._getPath(resolve(this.path, url, `./${name}`)) + const navbarFile = this._getPath(resolvePathname(name, url)) this._renderHtml('navbar', await this._render(navbarFile, 'navbar')) } - if (coverpage === true) { - coverpage = '_coverpage.md' - } if (coverpage) { - let path = null - if (typeof coverpage === 'string') { + let name = null + + if (typeof coverpage === 'string' || coverpage === true) { if (url === 'README.md') { - path = coverpage + name = coverpage === true ? '_coverpage.md' : coverpage } } else if (Array.isArray(coverpage)) { - path = coverpage.indexOf(url) > -1 && '_coverpage.md' + name = coverpage.indexOf(url) > -1 && '_coverpage.md' } else { const cover = coverpage[url] - path = cover === true ? '_coverpage.md' : cover + name = cover === true ? '_coverpage.md' : cover } - if (path) { - const coverFile = this._getPath(resolve(url, `./${path}`)) + if (name) { + const coverFile = this._getPath(resolvePathname(name, url)) - this._renderHtml('cover', await this._render(coverFile), 'cover') + this._renderHtml('cover', await this._render(coverFile, 'cover')) } } @@ -124,6 +122,7 @@ export default class Renderer { async _render(path, type) { let html = await this._loadFile(path) + const {subMaxLevel, maxLevel} = this.config let tokens @@ -142,7 +141,8 @@ export default class Renderer { tokens = await new Promise(r => { prerenderEmbed( { - fetch: url => this._loadFile(this._getPath(url)), + // Url is absolute path + fetch: url => this._loadFile(this._getPath('.' + url)), compiler: this.compiler, raw: html }, diff --git a/src/core/render/compiler.js b/src/core/render/compiler.js index 91b954c..e1b406b 100644 --- a/src/core/render/compiler.js +++ b/src/core/render/compiler.js @@ -1,6 +1,6 @@ import marked from 'marked' import Prism from 'prismjs' -import {helper as helperTpl, tree as treeTpl} from './tpl' +import {helper as helperTpl, tree as treeTpl, cover as coverTpl} from './tpl' import {genTree} from './gen-tree' import {slugify} from './slugify' import {emojify} from './emojify' @@ -303,19 +303,25 @@ export class Compiler { return `${text}` } origin.list = renderer.list = function (body, ordered, start) { - const isTaskList = /
  • /.test(body.split('class="task-list"')[0]) + const isTaskList = /
  • /.test( + body.split('class="task-list"')[0] + ) const isStartReq = start && start > 1 const tag = ordered ? 'ol' : 'ul' const tagAttrs = [ - (isTaskList ? 'class="task-list"' : ''), - (isStartReq ? `start="${start}"` : '') - ].join(' ').trim() + isTaskList ? 'class="task-list"' : '', + isStartReq ? `start="${start}"` : '' + ] + .join(' ') + .trim() return `<${tag} ${tagAttrs}>${body}` } origin.listitem = renderer.listitem = function (text) { const isTaskItem = /^(]*>)/.test(text) - const html = isTaskItem ? `
  • ` : `
  • ${text}
  • ` + const html = isTaskItem ? + `
  • ` : + `
  • ${text}
  • ` return html } @@ -375,12 +381,37 @@ export class Compiler { /** * Compile cover page */ - cover(text) { - const cacheToc = this.toc.slice() - const html = this.compile(text) + cover(text, isHTML = false) { + let html = text + if (!isHTML) { + const cacheToc = this.toc.slice() + html = this.compile(text) || '' + this.toc = cacheToc.slice() + } - this.toc = cacheToc.slice() + const m = html + .trim() + .match('

    ([^<]*?)

    $') + let style = '' + let hasMask = false + if (m) { + if (m[2] === 'color') { + style = `background: ${m[1] + (m[3] || '')}` + } else { + let path = m[1] + hasMask = true + if (!isAbsolutePath(m[1])) { + path = getPath(this.router.getBasePath(), m[1]) + } + style = `background-image: url(${path}); background-size: cover; background-position: center center;` + } + html = html.replace(m[0], '') + } - return html + return coverTpl({ + style, + hasMask, + text: html + }) } } diff --git a/src/core/render/index.js b/src/core/render/index.js index fae1bc5..936d507 100644 --- a/src/core/render/index.js +++ b/src/core/render/index.js @@ -175,30 +175,10 @@ export function renderMixin(proto) { } dom.toggleClass(el, 'add', 'show') - let html = this.coverIsHTML ? text : this.compiler.cover(text) + let html = this.compiler.cover(text, this.coverIsHTML) - const m = html - .trim() - .match('

    ([^<]*?)

    $') + this._renderTo('.cover-placeholder', html, true) - if (m) { - if (m[2] === 'color') { - el.style.background = m[1] + (m[3] || '') - } else { - let path = m[1] - - dom.toggleClass(el, 'add', 'has-mask') - if (!isAbsolutePath(m[1])) { - path = getPath(this.router.getBasePath(), m[1]) - } - el.style.backgroundImage = `url(${path})` - el.style.backgroundSize = 'cover' - el.style.backgroundPosition = 'center center' - } - html = html.replace(m[0], '') - } - - this._renderTo('.cover-main', html) sticky() } @@ -228,8 +208,9 @@ export function initRender(vm) { if (config.repo) { html += tpl.corner(config.repo) } + if (config.coverpage) { - html += tpl.cover() + html += '
    ' } if (config.logo) { diff --git a/src/core/render/tpl.js b/src/core/render/tpl.js index 71f5652..7cac6d2 100644 --- a/src/core/render/tpl.js +++ b/src/core/render/tpl.js @@ -57,16 +57,19 @@ export function main(config) { /** * Cover Page */ -export function cover() { +export function cover({style, text, hasMask}) { const SL = ', 100%, 85%' - const bgc = - 'linear-gradient(to left bottom, ' + - `hsl(${Math.floor(Math.random() * 255) + SL}) 0%,` + - `hsl(${Math.floor(Math.random() * 255) + SL}) 100%)` + style = + style || + 'background: linear-gradient(to left bottom, ' + + `hsl(${Math.floor(Math.random() * 255) + SL}) 0%,` + + `hsl(${Math.floor(Math.random() * 255) + SL}) 100%)` return ( - `
    ` + - '
    ' + + `
    ` + + `
    ${text}
    ` + '
    ' + '
    ' ) @@ -84,7 +87,9 @@ export function tree(toc, tpl = '
      {inner}
    ') { } let innerHTML = '' toc.forEach(node => { - innerHTML += `
  • ${node.title}
  • ` + innerHTML += `
  • ${ + node.title + }
  • ` if (node.children) { innerHTML += tree(node.children, tpl) } diff --git a/src/core/router/util.js b/src/core/router/util.js index 217461f..13c8109 100644 --- a/src/core/router/util.js +++ b/src/core/router/util.js @@ -46,7 +46,9 @@ export const isAbsolutePath = cached(path => { export const getParentPath = cached(path => { return /\/$/g.test(path) ? path : - (path = path.match(/(\S*\/)[^/]+$/)) ? path[1] : '' + (path = path.match(/(\S*\/)[^/]+$/)) ? + path[1] : + '' }) export const cleanPath = cached(path => {