resolve #10
parent
d140ffe6a0
commit
d569e67642
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -115,10 +115,15 @@ $(document).on('ready pjax:success', function() {
|
||||||
encode: true,
|
encode: true,
|
||||||
}).done(function(data) {
|
}).done(function(data) {
|
||||||
notification({
|
notification({
|
||||||
text: "File created successfully. You will be redirected.",
|
text: "File created successfully.",
|
||||||
type: 'success',
|
type: 'success',
|
||||||
timeout: 5000
|
timeout: 5000
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$.pjax({
|
||||||
|
url: "/admin/edit/" + filename,
|
||||||
|
container: '#content'
|
||||||
|
})
|
||||||
}).fail(function(data) {
|
}).fail(function(data) {
|
||||||
// error types
|
// error types
|
||||||
notification({
|
notification({
|
||||||
|
@ -224,6 +229,7 @@ $(document).on('ready pjax:success', function() {
|
||||||
data: data,
|
data: data,
|
||||||
headers: {
|
headers: {
|
||||||
'X-Regenerate': button.data("regenerate"),
|
'X-Regenerate': button.data("regenerate"),
|
||||||
|
'X-Schedule': button.data("schedule"),
|
||||||
'X-Content-Type': button.data("type")
|
'X-Content-Type': button.data("type")
|
||||||
},
|
},
|
||||||
dataType: 'json',
|
dataType: 'json',
|
||||||
|
|
|
@ -61,8 +61,8 @@
|
||||||
margin-right: auto;
|
margin-right: auto;
|
||||||
}
|
}
|
||||||
|
|
||||||
.toolbar *:last-child {
|
.toolbar input {
|
||||||
margin-left: 1em;
|
margin-left: 1em !important;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* EDITOR FRONTMATTER ONLY STYLES */
|
/* EDITOR FRONTMATTER ONLY STYLES */
|
||||||
|
|
|
@ -5,21 +5,25 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/hacdias/caddy-hugo/config"
|
"github.com/hacdias/caddy-hugo/config"
|
||||||
"github.com/hacdias/caddy-hugo/frontmatter"
|
"github.com/hacdias/caddy-hugo/frontmatter"
|
||||||
"github.com/hacdias/caddy-hugo/utils"
|
"github.com/hacdias/caddy-hugo/utils"
|
||||||
|
"github.com/robfig/cron"
|
||||||
"github.com/spf13/hugo/parser"
|
"github.com/spf13/hugo/parser"
|
||||||
)
|
)
|
||||||
|
|
||||||
type editor struct {
|
type editor struct {
|
||||||
Name string
|
Name string
|
||||||
Class string
|
Class string
|
||||||
|
IsPost bool
|
||||||
Mode string
|
Mode string
|
||||||
Content string
|
Content string
|
||||||
FrontMatter interface{}
|
FrontMatter interface{}
|
||||||
|
@ -31,13 +35,13 @@ func ServeHTTP(w http.ResponseWriter, r *http.Request, c *config.Config) (int, e
|
||||||
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" {
|
||||||
return servePost(w, r, filename)
|
return servePost(w, r, c, filename)
|
||||||
}
|
}
|
||||||
|
|
||||||
return serveGet(w, r, c, filename)
|
return serveGet(w, r, c, filename)
|
||||||
}
|
}
|
||||||
|
|
||||||
func servePost(w http.ResponseWriter, r *http.Request, filename string) (int, error) {
|
func servePost(w http.ResponseWriter, r *http.Request, c *config.Config, filename string) (int, error) {
|
||||||
// Get the JSON information sent using a buffer
|
// Get the JSON information sent using a buffer
|
||||||
rawBuffer := new(bytes.Buffer)
|
rawBuffer := new(bytes.Buffer)
|
||||||
rawBuffer.ReadFrom(r.Body)
|
rawBuffer.ReadFrom(r.Body)
|
||||||
|
@ -101,6 +105,49 @@ func servePost(w http.ResponseWriter, r *http.Request, filename string) (int, er
|
||||||
// Removes the main content from the rest of the frontmatter
|
// Removes the main content from the rest of the frontmatter
|
||||||
delete(rawFile, "content")
|
delete(rawFile, "content")
|
||||||
|
|
||||||
|
// Schedule the post
|
||||||
|
if r.Header.Get("X-Schedule") == "true" {
|
||||||
|
t, err := time.Parse("2006-01-02 15:04:05-07:00", rawFile["date"].(string))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Print(err)
|
||||||
|
return 500, err
|
||||||
|
}
|
||||||
|
|
||||||
|
scheduler := cron.New()
|
||||||
|
scheduler.AddFunc(t.Format("05 04 15 02 01 *"), func() {
|
||||||
|
// Set draft to false
|
||||||
|
rawFile["draft"] = false
|
||||||
|
|
||||||
|
// Converts the frontmatter in JSON
|
||||||
|
jsonFrontmatter, err := json.Marshal(rawFile)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Indents the json
|
||||||
|
frontMatterBuffer := new(bytes.Buffer)
|
||||||
|
json.Indent(frontMatterBuffer, jsonFrontmatter, "", " ")
|
||||||
|
|
||||||
|
// Generates the final file
|
||||||
|
f := new(bytes.Buffer)
|
||||||
|
f.Write(frontMatterBuffer.Bytes())
|
||||||
|
f.Write([]byte(mainContent))
|
||||||
|
file = f.Bytes()
|
||||||
|
|
||||||
|
// Write the file
|
||||||
|
err = ioutil.WriteFile(filename, file, 0666)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
utils.RunHugo(c)
|
||||||
|
})
|
||||||
|
scheduler.Start()
|
||||||
|
}
|
||||||
|
|
||||||
// Converts the frontmatter in JSON
|
// Converts the frontmatter in JSON
|
||||||
jsonFrontmatter, err := json.Marshal(rawFile)
|
jsonFrontmatter, err := json.Marshal(rawFile)
|
||||||
|
|
||||||
|
@ -160,6 +207,7 @@ func serveGet(w http.ResponseWriter, r *http.Request, c *config.Config, filename
|
||||||
page.Mode = strings.TrimPrefix(filepath.Ext(filename), ".")
|
page.Mode = strings.TrimPrefix(filepath.Ext(filename), ".")
|
||||||
page.Name = filename
|
page.Name = filename
|
||||||
page.Config = c
|
page.Config = c
|
||||||
|
page.IsPost = false
|
||||||
|
|
||||||
// Sanitize the extension
|
// Sanitize the extension
|
||||||
page.Mode = sanitizeMode(page.Mode)
|
page.Mode = sanitizeMode(page.Mode)
|
||||||
|
@ -176,6 +224,10 @@ func serveGet(w http.ResponseWriter, r *http.Request, c *config.Config, filename
|
||||||
return 500, err
|
return 500, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if strings.Contains(string(file.FrontMatter()), "date") {
|
||||||
|
page.IsPost = true
|
||||||
|
}
|
||||||
|
|
||||||
// Parses the page content and the frontmatter
|
// Parses the page content and the frontmatter
|
||||||
page.Content = strings.TrimSpace(string(file.Content()))
|
page.Content = strings.TrimSpace(string(file.Content()))
|
||||||
page.FrontMatter, err = frontmatter.Pretty(file.FrontMatter())
|
page.FrontMatter, err = frontmatter.Pretty(file.FrontMatter())
|
||||||
|
|
|
@ -43,13 +43,16 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="toolbar">
|
<div class="toolbar">
|
||||||
{{ if or (eq .Mode "markdown") (eq .Class "full") }}
|
{{ if or (eq .Mode "markdown") (eq .Class "complete") }}
|
||||||
<button id="preview" class="left">Preview</button>
|
<button id="preview" class="left">Preview</button>
|
||||||
{{ else }}
|
{{ else }}
|
||||||
<span class="left"></span>
|
<span class="left"></span>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
<input type="submit" data-type="{{ .Class }}" data-message="{{ if eq .Class " frontmatter-only " }}The fields were put on their way.{{ else if eq .Class "content-only " }}Every byte was saved.{{ else }}Post saved with pomp and circumstance.{{ end }}"
|
<input type="submit" data-type="{{ .Class }}" data-message="{{ if eq .Class " frontmatter-only " }}The fields were put on their way.{{ else if eq .Class "content-only " }}Every byte was saved.{{ else }}Post saved with pomp and circumstance.{{ end }}"
|
||||||
data-regenerate="false" value="Save">
|
data-regenerate="false" value="Save">
|
||||||
|
{{ if and (eq .Class "complete") ( .IsPost ) }}
|
||||||
|
<input type="submit" data-type="{{ .Class }}" data-message="Post Schedule" data-schedule="true" value="Schedule">
|
||||||
|
{{ end }}
|
||||||
<input type="submit" data-type="{{ .Class }}" data-message="{{ if eq .Class " frontmatter-only " }}Saved and regenerated.{{ else if eq .Class "content-only " }}Done. What do you want more?{{ else }}Post published. Go and share it!{{ end }}" data-regenerate="true"
|
<input type="submit" data-type="{{ .Class }}" data-message="{{ if eq .Class " frontmatter-only " }}Saved and regenerated.{{ else if eq .Class "content-only " }}Done. What do you want more?{{ else }}Post published. Go and share it!{{ end }}" data-regenerate="true"
|
||||||
class="default" value="Publish">
|
class="default" value="Publish">
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Reference in New Issue