-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Support sidebarAbsolutePath to fix relative link and nested sidebar when I just provide one sidebar.md #1076
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
024d257
85bec56
6db386b
14a71d2
8aa6a10
6516b76
33862c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
import { readFileSync } from 'fs'; | ||
import { resolve, basename } from 'path'; | ||
import resolvePathname from 'resolve-pathname'; | ||
import fetch from 'node-fetch'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should be reverted to the previous state. There is no point in rearranging imports if this is the only changes in the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a result of the automated husky lint process, which modifies the files. The |
||
import debug from 'debug'; | ||
import { AbstractHistory } from '../../src/core/router/history/abstract'; | ||
import { Compiler } from '../../src/core/render/compiler'; | ||
import { isAbsolutePath } from '../../src/core/router/util'; | ||
import * as tpl from '../../src/core/render/tpl'; | ||
import { prerenderEmbed } from '../../src/core/render/embed'; | ||
import fetch from 'node-fetch'; | ||
import debug from 'debug'; | ||
|
||
function cwd(...args) { | ||
return resolve(process.cwd(), ...args); | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,23 +1,37 @@ | ||||||||||||||||
import { getAndRemoveConfig } from '../utils'; | ||||||||||||||||
import { isAbsolutePath } from '../../router/util'; | ||||||||||||||||
|
||||||||||||||||
export const linkCompiler = ({ renderer, router, linkTarget, compilerClass }) => | ||||||||||||||||
(renderer.link = (href, title = '', text) => { | ||||||||||||||||
export const linkCompiler = ({ | ||||||||||||||||
renderer, | ||||||||||||||||
router, | ||||||||||||||||
linkTarget, | ||||||||||||||||
compiler, | ||||||||||||||||
isSidebar = false, | ||||||||||||||||
}) => { | ||||||||||||||||
const link = (href, title = '', text) => { | ||||||||||||||||
let attrs = []; | ||||||||||||||||
const { str, config } = getAndRemoveConfig(title); | ||||||||||||||||
|
||||||||||||||||
title = str; | ||||||||||||||||
|
||||||||||||||||
if ( | ||||||||||||||||
!isAbsolutePath(href) && | ||||||||||||||||
!compilerClass._matchNotCompileLink(href) && | ||||||||||||||||
!compiler._matchNotCompileLink(href) && | ||||||||||||||||
!config.ignore | ||||||||||||||||
) { | ||||||||||||||||
if (href === compilerClass.config.homepage) { | ||||||||||||||||
const sidebarAbsolutePath = compiler.config.sidebarAbsolutePath; | ||||||||||||||||
const basePath = | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To me the following lines has a weird formatting. I would suggest doing this instead:
Suggested change
|
||||||||||||||||
typeof sidebarAbsolutePath === 'string' | ||||||||||||||||
? sidebarAbsolutePath | ||||||||||||||||
: router.getBasePath(); | ||||||||||||||||
const relativeTo = | ||||||||||||||||
isSidebar && sidebarAbsolutePath ? basePath : router.getCurrentPath(); | ||||||||||||||||
|
||||||||||||||||
if (href === compiler.config.homepage) { | ||||||||||||||||
href = 'README'; | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
href = router.toURL(href, null, router.getCurrentPath()); | ||||||||||||||||
href = router.toURL(href, null, relativeTo); | ||||||||||||||||
} else { | ||||||||||||||||
if (!isAbsolutePath(href) && href.startsWith('./')) { | ||||||||||||||||
href = | ||||||||||||||||
|
@@ -48,4 +62,7 @@ export const linkCompiler = ({ renderer, router, linkTarget, compilerClass }) => | |||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
return `<a href="${href}" ${attrs.join(' ')}>${text}</a>`; | ||||||||||||||||
}); | ||||||||||||||||
}; | ||||||||||||||||
renderer.link = link; | ||||||||||||||||
return link; | ||||||||||||||||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,8 +42,20 @@ export class History { | |
const { config } = this; | ||
const base = this.getBasePath(); | ||
const ext = typeof config.ext === 'string' ? config.ext : '.md'; | ||
const alias = config.alias || {}; | ||
const rules = Object.assign({}, alias); | ||
|
||
// auto alias sidebar which is set to be absolute path | ||
if (config.sidebarAbsolutePath) { | ||
const sidebar = config.loadSidebar || '_sidebar.md'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Think about this: what happend if they set Here's an idea on how to detect absolute paths with a regex: https://stackoverflow.com/a/19709846/454780 (but we should allow paths starting with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, we should consider detecting too. |
||
const base = | ||
typeof config.sidebarAbsolutePath === 'string' | ||
? config.sidebarAbsolutePath | ||
: this.getBasePath(); | ||
rules['/.*/' + sidebar] = base + sidebar; | ||
} | ||
|
||
path = config.alias ? getAlias(path, config.alias) : path; | ||
path = getAlias(path, rules); | ||
path = getFileName(path, ext); | ||
path = path === `/README${ext}` ? config.homepage || path : path; | ||
path = isAbsolutePath(path) ? path : getPath(base, path); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These docs don't describe what the option actually does. It doesn't mention that it can be a string. Can you please update?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with @trusktr. There also seems to be some gramma thats not correct