Skip to content
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

make slug editable #309

Merged
merged 2 commits into from
Aug 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions management/editor/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,14 @@ func Form(post Editable, fields ...Field) ([]byte, error) {
external = form.find('.post-controls.external'),
id = form.find('input[name=id]'),
timestamp = $('.__ponzu.content-only'),
slug = $('input[name=slug]');
slug = $('input[name=slug]'),
allowEmptySlug = false;

// hide if this is a new post, or a non-post editor page
if (id.val() === '-1' || form.attr('action') !== '/admin/edit') {
del.hide();
external.hide();
allowEmptySlug = true;
}

// hide approval if not on a pending content item
Expand All @@ -167,11 +169,19 @@ func Form(post Editable, fields ...Field) ([]byte, error) {
if (form.attr('action') === '/admin/addon') {
timestamp.hide();
slug.parent().hide();
allowEmptySlug = true;
}

save.on('click', function(e) {
e.preventDefault();

if (!allowEmptySlug) {
if (slug.length > 1 && slug.eq(1).val() === "") {
alert("Slug cannot be empty");
return;
}
}

if (getParam('status') === 'pending') {
var action = form.attr('action');
form.attr('action', action + '?status=pending')
Expand Down Expand Up @@ -238,8 +248,7 @@ func addPostDefaultFieldsToEditorView(p Editable, e *Editor) error {
View: Input("Slug", p, map[string]string{
"label": "URL Slug",
"type": "text",
"disabled": "true",
"placeholder": "Will be set automatically",
"placeholder": "Can be left empty, only for new item",
}),
},
{
Expand Down
80 changes: 70 additions & 10 deletions system/db/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,42 @@ func update(ns, id string, data url.Values, existingContent *[]byte) (int, error
return err
}

// get slugs data
var (
existingSlug, newSlug string
)
existingSlug = data.Get("slug")
if len(data["slug"]) > 1 {
newSlug = data["slug"][1]
}

// update the slug (type:id) in contentIndex if public content
if specifier == "" {

target := fmt.Sprintf("%s:%d", ns, cid)
// if slug changed and valid
if existingSlug != newSlug && newSlug != "" {
ci := tx.Bucket([]byte("__contentIndex"))
if ci == nil {
return bolt.ErrBucketNotFound
}

// remove existing slug from __contentIndex
err = ci.Delete([]byte(fmt.Sprintf("%s", existingSlug)))
if err != nil {
return err
}

// insert new slug to __contentIndex
k := []byte(newSlug)
v := []byte(target)
err := ci.Put(k, v)
if err != nil {
return err
}
}
}

return nil
})
if err != nil {
Expand Down Expand Up @@ -770,21 +806,45 @@ func postToJSON(ns string, data url.Values) ([]byte, error) {
return nil, err
}

// get slugs data
var (
existingSlug, newSlug string
)
existingSlug = data.Get("slug")
if len(data["slug"]) > 1 {
newSlug = data["slug"][1]
}

// for new item
// if the content has no slug, and has no specifier, create a slug, check it
// for duplicates, and add it to our values
if data.Get("slug") == "" && data.Get("__specifier") == "" {
slug, err := item.Slug(post.(item.Identifiable))
if err != nil {
return nil, err
}
if existingSlug == "" && data.Get("__specifier") == "" {

slug, err = checkSlugForDuplicate(slug)
if err != nil {
return nil, err
// if no new slug specified, generate slug
if newSlug == "" {
slug, err := item.Slug(post.(item.Identifiable))
if err != nil {
return nil, err
}

slug, err = checkSlugForDuplicate(slug)
if err != nil {
return nil, err
}

post.(item.Sluggable).SetSlug(slug)
data.Set("slug", slug)
} else {
post.(item.Sluggable).SetSlug(newSlug)
data.Set("slug", newSlug)
}
}

post.(item.Sluggable).SetSlug(slug)
data.Set("slug", slug)
// for editing item
// prevent edit to empty slug
if existingSlug != "" && newSlug == "" {
post.(item.Sluggable).SetSlug(existingSlug)
data.Set("slug", existingSlug)
}

// marshall content struct to json for db storage
Expand Down