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

Enable generate content from file #327

Open
wants to merge 9 commits into
base: pr-327
Choose a base branch
from
Next Next commit
make slug editable
  • Loading branch information
junnotantra committed Jun 10, 2019
commit aecca64e255c2efb3150517bce91bc1a86d21b44
3 changes: 1 addition & 2 deletions management/editor/editor.go
Original file line number Diff line number Diff line change
@@ -238,8 +238,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 for new item",
}),
},
{
63 changes: 53 additions & 10 deletions system/db/content.go
Original file line number Diff line number Diff line change
@@ -108,6 +108,37 @@ func update(ns, id string, data url.Values, existingContent *[]byte) (int, error
return err
}

// update the slug,type:id in contentIndex if public content
if specifier == "" {
if len(data["slug"]) > 1 {
slug := data["slug"][0]
customSlug := data["slug"][1]

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

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

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

return nil
})
if err != nil {
@@ -773,18 +804,30 @@ func postToJSON(ns string, data url.Values) ([]byte, error) {
// 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
}
// check custom slug
slugsData := data["slug"]
if len(slugsData) > 1 && data["slug"][1] != "" {
customSlug := data["slug"][1]

if customSlug != "" {
post.(item.Sluggable).SetSlug(customSlug)
data.Set("slug", customSlug)
}
} else {
// generate default slug
slug, err := item.Slug(post.(item.Identifiable))
if err != nil {
return nil, err
}

slug, err = checkSlugForDuplicate(slug)
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)
post.(item.Sluggable).SetSlug(slug)
data.Set("slug", slug)
}
}

// marshall content struct to json for db storage