changes
This commit is contained in:
parent
234e2a1ec4
commit
e3ac3a6a4b
40
edit/edit.go
40
edit/edit.go
|
@ -1,6 +1,8 @@
|
||||||
package edit
|
package edit
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -24,15 +26,47 @@ func Execute(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||||
filename := strings.Replace(r.URL.Path, "/admin/edit/", "", 1)
|
filename := strings.Replace(r.URL.Path, "/admin/edit/", "", 1)
|
||||||
|
|
||||||
if r.Method == "POST" {
|
if r.Method == "POST" {
|
||||||
r.ParseForm()
|
// Get the JSON information sent using a buffer
|
||||||
err := ioutil.WriteFile(filename, []byte(r.Form["content"][0]), 0666)
|
rawBuffer := new(bytes.Buffer)
|
||||||
|
rawBuffer.ReadFrom(r.Body)
|
||||||
|
|
||||||
|
// Creates the raw file "map" using the JSON
|
||||||
|
var rawFile map[string]interface{}
|
||||||
|
json.Unmarshal(rawBuffer.Bytes(), &rawFile)
|
||||||
|
|
||||||
|
// The main content of the file
|
||||||
|
mainContent := rawFile["content"].(string)
|
||||||
|
|
||||||
|
// Removes the main content from the rest of the frontmatter
|
||||||
|
delete(rawFile, "content")
|
||||||
|
|
||||||
|
// Converts the frontmatter in JSON
|
||||||
|
jsonFrontmatter, err := json.Marshal(rawFile)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
return 500, err
|
return 500, err
|
||||||
}
|
}
|
||||||
|
|
||||||
commands.Execute()
|
// Indents the json
|
||||||
|
frontMatterBuffer := new(bytes.Buffer)
|
||||||
|
json.Indent(frontMatterBuffer, jsonFrontmatter, "", " ")
|
||||||
|
|
||||||
|
// Generates the final file
|
||||||
|
file := new(bytes.Buffer)
|
||||||
|
file.Write(frontMatterBuffer.Bytes())
|
||||||
|
file.Write([]byte(mainContent))
|
||||||
|
|
||||||
|
err = ioutil.WriteFile(filename, file.Bytes(), 0666)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Print(err)
|
||||||
|
return 500, err
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.Write([]byte("{}"))
|
||||||
|
go commands.Execute()
|
||||||
} else {
|
} else {
|
||||||
if _, err := os.Stat(filename); os.IsNotExist(err) {
|
if _, err := os.Stat(filename); os.IsNotExist(err) {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
|
|
|
@ -50,7 +50,10 @@ func Execute(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.Write([]byte("{}"))
|
w.Write([]byte("{}"))
|
||||||
go commands.Execute()
|
|
||||||
|
if r.Header.Get("X-Save-Mode") == "publish" {
|
||||||
|
go commands.Execute()
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
content, err := ioutil.ReadFile("config." + language)
|
content, err := ioutil.ReadFile("config." + language)
|
||||||
|
|
||||||
|
|
|
@ -3,10 +3,16 @@ $(document).ready(function() {
|
||||||
var data = JSON.stringify($(this).serializeField())
|
var data = JSON.stringify($(this).serializeField())
|
||||||
var url = $(this).attr('action')
|
var url = $(this).attr('action')
|
||||||
|
|
||||||
|
console.log(data)
|
||||||
|
|
||||||
$.ajax({
|
$.ajax({
|
||||||
type: 'POST',
|
type: 'POST',
|
||||||
url: url,
|
url: url,
|
||||||
data: data,
|
data: data,
|
||||||
|
beforeSend: function(xhr) {
|
||||||
|
// add publish and save
|
||||||
|
xhr.setRequestHeader('X-Save-Mode', 'publish');
|
||||||
|
}
|
||||||
dataType: 'json',
|
dataType: 'json',
|
||||||
encode: true,
|
encode: true,
|
||||||
}).done(function(data) {
|
}).done(function(data) {
|
||||||
|
|
Loading…
Reference in New Issue