frontmatter conversion into toml is done in native host
This commit is contained in:
parent
b6247c1fab
commit
901eb12374
5 changed files with 54 additions and 36 deletions
56
app/c.js
56
app/c.js
|
@ -1,37 +1,39 @@
|
||||||
function captureFrontMatterAndMarkdown() {
|
function captureFrontMatterAndMarkdown() {
|
||||||
console.log(location.pathname.split('/public')[0])
|
|
||||||
const formData = new FormData(document.querySelector("form"))
|
const formData = new FormData(document.querySelector("form"))
|
||||||
let frontmatter = "---\n"
|
|
||||||
for (const k of formData.keys()) {
|
var formDataJSON = {};
|
||||||
// draft, iscjklanguage are default implicit (false) params in hugo
|
formData.forEach((value, key) => {
|
||||||
if (["draft", "iscjklanguage"].includes(k)){
|
if(key.endsWith('[]')){
|
||||||
if (formData.get(k) === "true") {
|
let kie = key.slice(0, -2);
|
||||||
frontmatter += `${k}: true\n`
|
(kie in formDataJSON) || (formDataJSON[kie] = [])
|
||||||
}
|
formDataJSON[kie].push(value);
|
||||||
} else if (k.endsWith('[]')) {
|
} else if (['draft', 'iscjklanguage'].includes(key) && value!="true") {
|
||||||
formData.set(k, formData.getAll(k))
|
return
|
||||||
var s = JSON.stringify(formData.getAll(k)[0].split(","))
|
} else if (key == "relpath"){
|
||||||
if (s != "") {
|
filepath = `${location.pathname.split('/public')[0]}/content/${value}`;
|
||||||
frontmatter += `${k.slice(0, -2)}: ${s}\n`
|
gitpath = `${location.pathname.split('/public')[0]}`;
|
||||||
}
|
} else if (key == "relpermalink" ){
|
||||||
} else if (k == "relpath") {
|
publishpath = `${location.pathname.split('/public')[0]}/public${value}index.html`;
|
||||||
filepath = `${location.pathname.split('/public')[0]}/content/${formData.get(k)}`
|
} else if (["protocol", "offline", "ignore"].includes(key)) {
|
||||||
gitpath = `${location.pathname.split('/public')[0]}`
|
return
|
||||||
} else if (k == "relpermalink") {
|
|
||||||
publishpath = `${location.pathname.split('/public')[0]}/public${formData.get(k)}index.html`
|
|
||||||
} else if (["protocol", "offline", "ignore"].includes(k)) {
|
|
||||||
continue
|
|
||||||
} else {
|
} else {
|
||||||
var s = JSON.stringify(formData.get(k))
|
formDataJSON[key] = value
|
||||||
frontmatter += `${k}: ${s}\n`
|
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
frontmatter += "---\n"
|
|
||||||
const content = document.querySelector('textarea').value
|
const content = document.querySelector('textarea').value
|
||||||
let doc = frontmatter + "\n" + content
|
|
||||||
let publish = formData.get("publish") == "on" ? true : false
|
let publish = formData.get("publish") == "on" ? true : false
|
||||||
let offline = formData.get("offline") == "on" ? true : false
|
let offline = formData.get("offline") == "on" ? true : false
|
||||||
return {"hugopage": doc, "filepath": filepath, "gitpath": gitpath, "publishpath": publishpath, "publish": publish, "offline": offline, "protocol": document.location.protocol.substr(0,4)}
|
return {
|
||||||
|
"hugofrontmatter": JSON.stringify(formDataJSON),
|
||||||
|
"hugocontent": content,
|
||||||
|
"filepath": filepath,
|
||||||
|
"gitpath": gitpath,
|
||||||
|
"publishpath": publishpath,
|
||||||
|
"publish": publish,
|
||||||
|
"offline": offline,
|
||||||
|
"protocol": document.location.protocol.substr(0,4)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let sb = document.getElementById('sandpointsButton')
|
let sb = document.getElementById('sandpointsButton')
|
||||||
|
|
|
@ -2,4 +2,7 @@ module main
|
||||||
|
|
||||||
go 1.15
|
go 1.15
|
||||||
|
|
||||||
require github.com/go-git/go-git/v5 v5.2.0
|
require (
|
||||||
|
github.com/go-git/go-git/v5 v5.2.0
|
||||||
|
github.com/pelletier/go-toml v1.9.2 // indirect
|
||||||
|
)
|
||||||
|
|
|
@ -32,6 +32,8 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||||
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
|
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
|
||||||
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
|
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
|
||||||
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
|
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
|
||||||
|
github.com/pelletier/go-toml v1.9.2 h1:7NiByeVF4jKSG1lDF3X8LTIkq2/bu+1uYbIm1eS5tzk=
|
||||||
|
github.com/pelletier/go-toml v1.9.2/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
|
||||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
|
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
|
||||||
|
|
|
@ -17,6 +17,7 @@ import (
|
||||||
|
|
||||||
"github.com/go-git/go-git/v5"
|
"github.com/go-git/go-git/v5"
|
||||||
"github.com/go-git/go-git/v5/plumbing/object"
|
"github.com/go-git/go-git/v5/plumbing/object"
|
||||||
|
"github.com/pelletier/go-toml"
|
||||||
)
|
)
|
||||||
|
|
||||||
// this project started by copying:
|
// this project started by copying:
|
||||||
|
@ -38,13 +39,14 @@ var bufferSize = 8192 * 8
|
||||||
|
|
||||||
// IncomingMessage represents a message sent to the native host.
|
// IncomingMessage represents a message sent to the native host.
|
||||||
type IncomingMessage struct {
|
type IncomingMessage struct {
|
||||||
Hugopage string `json:"hugopage"`
|
Hugocontent string `json:"hugocontent"`
|
||||||
Filepath string `json:"filepath"`
|
Hugofrontmatter string `json:"hugofrontmatter"`
|
||||||
Gitpath string `json:"gitpath"`
|
Filepath string `json:"filepath"`
|
||||||
Publishpath string `json:"publishpath"`
|
Gitpath string `json:"gitpath"`
|
||||||
Publish bool `json:"publish"`
|
Publishpath string `json:"publishpath"`
|
||||||
Offline bool `json:"offline"`
|
Publish bool `json:"publish"`
|
||||||
Protocol string `json:"protocol"`
|
Offline bool `json:"offline"`
|
||||||
|
Protocol string `json:"protocol"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// OutgoingMessage respresents a response to an incoming message query.
|
// OutgoingMessage respresents a response to an incoming message query.
|
||||||
|
@ -157,7 +159,16 @@ func commitChangeToGit(iMsg IncomingMessage) {
|
||||||
relPathFile := filepath.Join(strings.ReplaceAll(iMsg.Filepath, iMsg.Gitpath+"/", ""))
|
relPathFile := filepath.Join(strings.ReplaceAll(iMsg.Filepath, iMsg.Gitpath+"/", ""))
|
||||||
// err = ioutil.WriteFile(iMsg.Filepath, []byte(iMsg.Hugopage), 0644)
|
// err = ioutil.WriteFile(iMsg.Filepath, []byte(iMsg.Hugopage), 0644)
|
||||||
// check(err)
|
// check(err)
|
||||||
writeMessageToFile(iMsg.Filepath, iMsg.Hugopage)
|
var frontmatter map[string]interface{}
|
||||||
|
json.Unmarshal([]byte(iMsg.Hugofrontmatter), &frontmatter)
|
||||||
|
Trace.Printf("Frontmatter JSON: %s", &frontmatter)
|
||||||
|
tml, err := toml.TreeFromMap(frontmatter)
|
||||||
|
if err != nil {
|
||||||
|
Error.Printf("ERROR: Frontmatter JSON to TML:", err)
|
||||||
|
}
|
||||||
|
tomlFrontmatter := fmt.Sprintf("+++\n%s+++\n", tml)
|
||||||
|
Trace.Printf("TOML Frontmatter:", tml)
|
||||||
|
writeMessageToFile(iMsg.Filepath, fmt.Sprintf("%s%s", tomlFrontmatter, iMsg.Hugocontent))
|
||||||
_, err = w.Add(relPathFile)
|
_, err = w.Add(relPathFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Error.Printf("ERROR: Go-git Add:", err)
|
Error.Printf("ERROR: Go-git Add:", err)
|
||||||
|
|
Binary file not shown.
Loading…
Add table
Reference in a new issue