Change urlparentignoreindexhtml into urlparentignorepathregexp

Make user be able to customize the path to remove before
go to the parent path.
This commit is contained in:
gholk 2022-08-16 23:54:25 +08:00 committed by Oliver Blanthorn
parent dbcc21c425
commit e375417cd4
No known key found for this signature in database
GPG key ID: 2BB8C36BB504BFF3
3 changed files with 25 additions and 11 deletions

View file

@ -2066,12 +2066,19 @@ export function urlroot() {
//#content //#content
export function urlparent(count = 1) { export function urlparent(count = 1) {
const option = {} const option = {}
for (const key of "trailingSlash ignoreFragment ignoreSearch ignoreIndexHtml".split(" ")) { for (const key of "trailingSlash ignoreFragment ignoreSearch".split(" ")) {
const configKey = ("urlparent" + key.toLowerCase()) as ( const configKey = ("urlparent" + key.toLowerCase()) as (
keyof config.default_config keyof config.default_config
) )
option[key] = config.get(configKey) === "true" option[key] = config.get(configKey) === "true"
} }
const regexpString = config.get("urlparentignorepathregexp")
const regexpScan = regexpString.match(/^\/(.+)\/([a-z]*?)$/)
if (regexpString && regexpScan) {
option["ignorePathRegExp"] = new RegExp(
regexpScan[1], regexpScan[2]
)
}
const parentUrl = UrlUtil.getUrlParent(window.location, option, count) const parentUrl = UrlUtil.getUrlParent(window.location, option, count)

View file

@ -1281,10 +1281,16 @@ export class default_config {
urlparentignoresearch: "true" | "false" = "false" urlparentignoresearch: "true" | "false" = "false"
/** /**
* Whether remove the /\/index\.(html?|php|aspx?|jsp|cgi|pl|js)$/i * RegExp to remove from the url pathname before go to any parent path.
* before remove any path. * To ignore "index.html" in "parent/index.html", set it to
* "//index\.html/". The regexp flag is supported, and the escape of
* the slashes inside the regexp is not required.
*
* An empty string will disable this feature.
*
* Suggested value: //index\.(html?|php|aspx?|jsp|cgi|pl|js)$/i
*/ */
urlparentignoreindexhtml: "true" | "false" = "false" urlparentignorepathregexp = ""
/** /**
* Whether to enter visual mode when text is selected. Visual mode can always be entered with `:mode visual`. * Whether to enter visual mode when text is selected. Visual mode can always be entered with `:mode visual`.

View file

@ -54,13 +54,14 @@ export function getUrlRoot(url) {
/** Get the parent of the current URL. Parent is determined as: /** Get the parent of the current URL. Parent is determined as:
* *
* * if there is a hash fragment, strip that, or * * if there is a hash fragment and option.ignoreFragment is falsy, strip that, or
* * If there is a query string, strip that, or * * If there is a query string and option.ignoreSearch is falsy, strip that, or
* * If option.ignorePathRegExp match the path, strip that and
* * Remove one level from the path if there is one, or * * Remove one level from the path if there is one, or
* * Remove one subdomain from the front if there is one * * Remove one subdomain from the front if there is one
* *
* @param url the URL to get the parent of * @param url the URL to get the parent of
* @param option search option, boolean dict of trailingSlash, ignoreSearch, ignoreFragment and ignoreIndexHtml * @param option removal option. Boolean properties: trailingSlash, ignoreFragment and ignoreSearch. Regular Expression properties: ignorePathRegExp. All properties are optional.
* @param count how many "generations" you wish to go back (1 = parent, 2 = grandparent, etc.) * @param count how many "generations" you wish to go back (1 = parent, 2 = grandparent, etc.)
* @return the parent of the URL, or null if there is no parent * @return the parent of the URL, or null if there is no parent
*/ */
@ -87,10 +88,10 @@ export function getUrlParent(url, option, count = 1) {
return gup(parent, option, count) return gup(parent, option, count)
} }
if (option.ignoreIndexHtml) { if (option.ignorePathRegExp) {
const indexHtml = /\/index\.(html?|php|aspx?|jsp|cgi|pl|js)$/i const re = option.ignorePathRegExp
if (parent.pathname.match(indexHtml)) { if (parent.pathname.match(re)) {
parent.pathname = parent.pathname.replace(indexHtml, "/") parent.pathname = parent.pathname.replace(re, "/")
return gup(parent, option, count) return gup(parent, option, count)
} }
} }