Pre-fill suggested New File 'name' and 'content' with Query Params (#16556)

* feature: add (GitHub-style) querystrings for pre-filling new file content

* docs: add query parameters for new files
This commit is contained in:
AJ ONeal 2021-07-28 21:39:46 -06:00 committed by GitHub
parent 4e68d6f41d
commit b9a0e33238
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 3 deletions

View file

@ -81,7 +81,11 @@ func editFile(ctx *context.Context, isNewFile bool) {
return
}
treeNames, treePaths := getParentTreeFields(ctx.Repo.TreePath)
// Check if the filename (and additional path) is specified in the querystring
// (filename is a misnomer, but kept for compatibility with Github)
filePath, fileName := path.Split(ctx.Req.URL.Query().Get("filename"))
filePath = strings.Trim(filePath, "/")
treeNames, treePaths := getParentTreeFields(path.Join(ctx.Repo.TreePath, filePath))
if !isNewFile {
entry, err := ctx.Repo.Commit.GetTreeEntryByPath(ctx.Repo.TreePath)
@ -136,7 +140,8 @@ func editFile(ctx *context.Context, isNewFile bool) {
ctx.Data["FileContent"] = content
}
} else {
treeNames = append(treeNames, "") // Append empty string to allow user name the new file.
// Append filename from query, or empty string to allow user name the new file.
treeNames = append(treeNames, fileName)
}
ctx.Data["TreeNames"] = treeNames